Blob


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