print 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. .TH PRINT 2
  2. .SH NAME
  3. print, fprint, sprint, snprint, seprint, smprint, runesprint, runesnprint, runeseprint, runesmprint, vfprint, vsnprint, vseprint, vsmprint, runevsnprint, runevseprint, runevsmprint \- print formatted output
  4. .SH SYNOPSIS
  5. .B #include <u.h>
  6. .br
  7. .B #include <libc.h>
  8. .PP
  9. .ta \w'\fLchar* 'u
  10. .B
  11. int print(char *format, ...)
  12. .PP
  13. .B
  14. int fprint(int fd, char *format, ...)
  15. .PP
  16. .B
  17. int sprint(char *s, char *format, ...)
  18. .PP
  19. .B
  20. int snprint(char *s, int len, char *format, ...)
  21. .PP
  22. .B
  23. char* seprint(char *s, char *e, char *format, ...)
  24. .PP
  25. .B
  26. char* smprint(char *format, ...)
  27. .PP
  28. .B
  29. int runesprint(Rune *s, char *format, ...)
  30. .PP
  31. .B
  32. int runesnprint(Rune *s, int len, char *format, ...)
  33. .PP
  34. .B
  35. Rune* runeseprint(Rune *s, Rune *e, char *format, ...)
  36. .PP
  37. .B
  38. Rune* runesmprint(char *format, ...)
  39. .PP
  40. .B
  41. int vfprint(int fd, char *format, va_list v)
  42. .PP
  43. .B
  44. int vsnprint(char *s, int len, char *format, va_list v)
  45. .PP
  46. .B
  47. char* vseprint(char *s, char *e, char *format, va_list v)
  48. .PP
  49. .B
  50. char* vsmprint(char *format, va_list v)
  51. .PP
  52. .B
  53. int runevsnprint(Rune *s, int len, char *format, va_list v)
  54. .PP
  55. .B
  56. Rune* runevseprint(Rune *s, Rune *e, char *format, va_list v)
  57. .PP
  58. .B
  59. Rune* runevsmprint(Rune *format, va_list v)
  60. .PP
  61. .B
  62. .SH DESCRIPTION
  63. .I Print
  64. writes text to the standard output.
  65. .I Fprint
  66. writes to the named output
  67. file descriptor;
  68. a buffered form
  69. is described in
  70. .IR bio (2).
  71. .I Sprint
  72. places text
  73. followed by the NUL character
  74. .RB ( \e0 )
  75. in consecutive bytes starting at
  76. .IR s ;
  77. it is the user's responsibility to ensure that
  78. enough storage is available.
  79. Each function returns the number of bytes
  80. transmitted (not including the NUL
  81. in the case of
  82. .IR sprint ),
  83. or
  84. a negative value if an output error was encountered.
  85. .PP
  86. .I Snprint
  87. is like
  88. .IR sprint ,
  89. but will not place more than
  90. .I len
  91. bytes in
  92. .IR s .
  93. Its result is always NUL-terminated and holds the maximal
  94. number of complete UTF-8 characters that can fit.
  95. .I Seprint
  96. is like
  97. .IR snprint ,
  98. except that the end is indicated by a pointer
  99. .I e
  100. rather than a count and the return value points to the terminating NUL of the
  101. resulting string.
  102. .I Smprint
  103. is like
  104. .IR sprint ,
  105. except that it prints into and returns a string of the required length, which is
  106. allocated by
  107. .IR malloc (2).
  108. .PP
  109. The routines
  110. .IR runesprint ,
  111. .IR runesnprint ,
  112. .IR runeseprint ,
  113. and
  114. .I runesmprint
  115. are the same as
  116. .IR sprint ,
  117. .IR snprint ,
  118. .IR seprint
  119. and
  120. .I smprint
  121. except that their output is rune strings instead of byte strings.
  122. .PP
  123. Finally, the routines
  124. .IR vfprint ,
  125. .IR vsnprint ,
  126. .IR vseprint ,
  127. .IR vsmprint ,
  128. .IR runevsnprint ,
  129. .IR runevseprint ,
  130. and
  131. .IR runevsmprint
  132. are like their
  133. .BR v-less
  134. relatives except they take as arguments a
  135. .B va_list
  136. parameter, so they can be called within a variadic function.
  137. The Example section shows a representative usage.
  138. .PP
  139. Each of these functions
  140. converts, formats, and prints its
  141. trailing arguments
  142. under control of a
  143. .IR format
  144. string.
  145. The
  146. format
  147. contains two types of objects:
  148. plain characters, which are simply copied to the
  149. output stream,
  150. and conversion specifications,
  151. each of which results in fetching of
  152. zero or more
  153. arguments.
  154. The results are undefined if there are arguments of the
  155. wrong type or too few
  156. arguments for the format.
  157. If the format is exhausted while
  158. arguments remain, the excess
  159. is ignored.
  160. .PP
  161. Each conversion specification has the following format:
  162. .IP
  163. .B "% [flags] verb
  164. .PP
  165. The verb is a single character and each flag is a single character or a
  166. (decimal) numeric string.
  167. Up to two numeric strings may be used;
  168. the first is called
  169. .IR width ,
  170. the second
  171. .IR precision .
  172. A period can be used to separate them, and if the period is
  173. present then
  174. .I width
  175. and
  176. .I precision
  177. are taken to be zero if missing, otherwise they are `omitted'.
  178. Either or both of the numbers may be replaced with the character
  179. .BR * ,
  180. meaning that the actual number will be obtained from the argument list
  181. as an integer.
  182. The flags and numbers are arguments to
  183. the
  184. .I verb
  185. described below.
  186. .PP
  187. The numeric verbs
  188. .BR d ,
  189. .BR u ,
  190. .BR o ,
  191. .BR b ,
  192. .BR x ,
  193. and
  194. .B X
  195. format their arguments in decimal, unsigned decimal,
  196. unsigned octal, unsigned binary, unsigned hexadecimal, and unsigned upper case hexadecimal.
  197. Each interprets the flags
  198. .BR 0 ,
  199. .BR h ,
  200. .BR hh ,
  201. .BR l ,
  202. .BR + ,
  203. .BR - ,
  204. .BR , ,
  205. and
  206. .B #
  207. to mean pad with zeros,
  208. short, byte, long, always print a sign, left justified, commas every three digits,
  209. and alternate format.
  210. Also, a space character in the flag
  211. position is like
  212. .BR + ,
  213. but prints a space instead of a plus sign for non-negative values.
  214. If neither
  215. short nor long is specified,
  216. then the argument is an
  217. .BR int .
  218. If two
  219. .B l
  220. flags are given,
  221. then the argument is interpreted as a
  222. .B vlong
  223. (usually an 8-byte, sometimes a 4-byte integer).
  224. If
  225. .I precision
  226. is not omitted, the number is padded on the left with zeros
  227. until at least
  228. .I precision
  229. digits appear.
  230. Then, if alternate format is specified,
  231. for
  232. .B o
  233. conversion, the number is preceded by a
  234. .B 0
  235. if it doesn't already begin with one;
  236. for
  237. .B x
  238. conversion, the number is preceded by
  239. .BR 0x ;
  240. for
  241. .B X
  242. conversion, the number is preceded by
  243. .BR 0X .
  244. Finally, if
  245. .I width
  246. is not omitted, the number is padded on the left (or right, if
  247. left justification is specified) with enough blanks to
  248. make the field at least
  249. .I width
  250. characters long.
  251. .PP
  252. The floating point verbs
  253. .BR f ,
  254. .BR e ,
  255. .BR E ,
  256. .BR g ,
  257. and
  258. .B G
  259. take a
  260. .B double
  261. argument.
  262. Each interprets the flags
  263. .BR + ,
  264. .BR - ,
  265. and
  266. .B #
  267. to mean
  268. always print a sign,
  269. left justified,
  270. and
  271. alternate format.
  272. .I Width
  273. is the minimum field width and,
  274. if the converted value takes up less than
  275. .I width
  276. characters, it is padded on the left (or right, if `left justified')
  277. with spaces.
  278. .I Precision
  279. is the number of digits that are converted after the decimal place for
  280. .BR e ,
  281. .BR E ,
  282. and
  283. .B f
  284. conversions,
  285. and
  286. .I precision
  287. is the maximum number of significant digits for
  288. .B g
  289. and
  290. .B G
  291. conversions.
  292. The
  293. .B f
  294. verb produces output of the form
  295. .RB [ - ] digits [ .digits\fR].
  296. .B E
  297. conversion appends an exponent
  298. .BR E [ - ] digits ,
  299. and
  300. .B e
  301. conversion appends an exponent
  302. .BR e [ - ] digits .
  303. The
  304. .B g
  305. verb will output the argument in either
  306. .B e
  307. or
  308. .B f
  309. with the goal of producing the smallest output.
  310. Also, trailing zeros are omitted from the fraction part of
  311. the output, and a trailing decimal point appears only if it is followed
  312. by a digit.
  313. The
  314. .B G
  315. verb is similar, but uses
  316. .B E
  317. format instead of
  318. .BR e .
  319. When alternate format is specified, the result will always contain a decimal point,
  320. and for
  321. .B g
  322. and
  323. .B G
  324. conversions, trailing zeros are not removed.
  325. .PP
  326. The
  327. .B s
  328. verb copies a nul-terminated string
  329. (pointer to
  330. .BR char )
  331. to the output.
  332. The number of characters copied
  333. .RI ( n )
  334. is the minimum
  335. of the size of the string and
  336. .IR precision .
  337. These
  338. .I n
  339. characters are justified within a field of
  340. .I width
  341. characters as described above.
  342. If a
  343. .I precision
  344. is given, it is safe for the string not to be nul-terminated
  345. as long as it is at least
  346. .I precision
  347. characters (not bytes!) long.
  348. The
  349. .B S
  350. verb is similar, but it interprets its pointer as an array
  351. of runes (see
  352. .IR utf (6));
  353. the runes are converted to
  354. .SM UTF
  355. before output.
  356. .PP
  357. The
  358. .B c
  359. verb copies a single
  360. .B char
  361. (promoted to
  362. .BR int )
  363. justified within a field of
  364. .I width
  365. characters as described above.
  366. The
  367. .B C
  368. verb is similar, but works on runes.
  369. .PP
  370. The
  371. .B p
  372. verb formats a single pointer or pointer-sized integer
  373. .RB ( uintptr ,
  374. see
  375. .IR intro (2))
  376. in hexadecimal.
  377. .PP
  378. The
  379. .B r
  380. verb takes no arguments; it copies the error string returned by a call to
  381. .IR errstr (2).
  382. .PP
  383. Custom verbs may be installed using
  384. .IR fmtinstall (2).
  385. .SH EXAMPLE
  386. This function prints an error message with a variable
  387. number of arguments and then quits.
  388. .IP
  389. .EX
  390. .ta 6n +6n +6n
  391. void fatal(char *msg, ...)
  392. {
  393. char buf[1024], *out;
  394. va_list arg;
  395. out = seprint(buf, buf+sizeof(buf), "Fatal error: ");
  396. va_start(arg, msg);
  397. out = vseprint(out, buf+sizeof(buf), msg, arg);
  398. va_end(arg);
  399. write(2, buf, out-buf);
  400. exits("fatal error");
  401. }
  402. .EE
  403. .SH SOURCE
  404. .B /sys/src/libc/fmt
  405. .SH SEE ALSO
  406. .IR fmtinstall (2),
  407. .IR fprintf (2),
  408. .IR utf (6),
  409. .IR errstr (2)
  410. .SH DIAGNOSTICS
  411. Routines that write to a file descriptor or call
  412. .IR malloc
  413. set
  414. .IR errstr .
  415. .SH BUGS
  416. The formatting is close to that specified for ANSI
  417. .IR fprintf (2);
  418. the main difference is that
  419. .B b
  420. is not in ANSI and
  421. .B u
  422. is a flag here instead of a verb.
  423. Also, and distinctly not a bug,
  424. .I print
  425. and friends generate
  426. .SM UTF
  427. rather than
  428. .SM ASCII.
  429. .PP
  430. There is no
  431. .BR runeprint ,
  432. .BR runefprint ,
  433. etc. because runes are byte-order dependent and should not be written directly to a file; use the
  434. UTF output of
  435. .I print
  436. or
  437. .I fprint
  438. instead.
  439. Also,
  440. .I sprint
  441. is deprecated for safety reasons; use
  442. .IR snprint ,
  443. .IR seprint ,
  444. or
  445. .I smprint
  446. instead.
  447. Safety also precludes the existence of
  448. .IR runesprint .