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