Update contrib.
3 * Portions Copyright (c) 1990-2006 Nokia Corporation and/or its subsidiary(-ies).
8 * Copyright (c) 1990 The Regents of the University of California.
11 * Redistribution and use in source and binary forms are permitted
12 * provided that the above copyright notice and this paragraph are
13 * duplicated in all such forms and that any documentation,
14 * advertising materials, and other materials related to such
15 * distribution and use acknowledge that the software was developed
16 * by the University of California, Berkeley. The name of the
17 * University may not be used to endorse or promote products derived
18 * from this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
27 <<printf>>, <<fprintf>>, <<sprintf>>---format output
38 int printf(const char *<[format]> [, <[arg]>, ...]);
39 int fprintf(FILE *<[fd]>, const char *<[format]> [, <[arg]>, ...]);
40 int sprintf(char *<[str]>, const char *<[format]> [, <[arg]>, ...]);
45 int printf(<[format]> [, <[arg]>, ...])
48 int fprintf(<[fd]>, <[format]> [, <[arg]>, ...]);
52 int sprintf(<[str]>, <[format]> [, <[arg]>, ...]);
57 <<printf>> accepts a series of arguments, applies to each a
58 format specifier from <<*<[format]>>>, and writes the
59 formatted data to <<stdout>>, terminated with a null character.
60 The behavior of <<printf>> is undefined if there are not enough
61 arguments for the format.
62 <<printf>> returns when it reaches the end of the format string.
63 If there are more arguments than the format requires, excess
64 arguments are ignored.
66 <<fprintf>> and <<sprintf>> are identical to <<printf>>, other than the
67 destination of the formatted output: <<fprintf>> sends the
68 output to a specified file <[fd]>, while <<sprintf>> stores the
69 output in the specified char array <[str]>. For <<sprintf>>,
70 the behavior is also undefined if the output <<*<[str]>>>
71 overlaps with one of the arguments.
72 <[format]> is a pointer to a charater string containing two types of
73 objects: ordinary characters (other than <<%>>), which are
74 copied unchanged to the output, and conversion
75 specifications, each of which is introduced by <<%>>.
76 (To include <<%>> in the output, use <<%%>> in the format string.)
77 A conversion specification has the following form:
79 . %[<[flags]>][<[width]>][.<[prec]>][<[size]>][<[type]>]
81 The fields of the conversion specification have the following meanings:
86 an optional sequence of characters which control
87 output justification, numeric signs, decimal points,
88 trailing zeroes, and octal and hex prefixes.
89 The flag characters are minus (<<->>), plus (<<+>>),
90 space ( ), zero (<<0>>), and sharp (<<#>>). They can
91 appear in any combination.
95 The result of the conversion is left justified, and the right is
96 padded with blanks. If you do not use this flag, the result is right
97 justified, and padded on the left.
100 The result of a signed conversion (as determined by <[type]>)
101 will always begin with a plus or minus sign. (If you do not use
102 this flag, positive values do not begin with a plus sign.)
105 If the first character of a signed conversion specification
106 is not a sign, or if a signed conversion results in no
107 characters, the result will begin with a space. If the
108 space ( ) flag and the plus (<<+>>) flag both appear,
109 the space flag is ignored.
112 If the <[type]> character is <<d>>, <<i>>, <<o>>, <<u>>,
113 <<x>>, <<X>>, <<e>>, <<E>>, <<f>>, <<g>>, or <<G>>: leading zeroes,
114 are used to pad the field width (following any indication of sign or
115 base); no spaces are used for padding. If the zero (<<0>>) and
116 minus (<<->>) flags both appear, the zero (<<0>>) flag will
117 be ignored. For <<d>>, <<i>>, <<o>>, <<u>>, <<x>>, and <<X>>
118 conversions, if a precision <[prec]> is specified, the zero (<<0>>)
121 Note that <<0>> is interpreted as a flag, not as the beginning
125 The result is to be converted to an alternative form, according
126 to the next character:
130 increases precision to force the first digit
131 of the result to be a zero.
134 a non-zero result will have a <<0x>> prefix.
137 a non-zero result will have a <<0X>> prefix.
140 The result will always contain a decimal point
141 even if no digits follow the point.
142 (Normally, a decimal point appears only if a
143 digit follows it.) Trailing zeroes are removed.
146 same as <<e>> or <<E>>, but trailing zeroes
157 <[width]> is an optional minimum field width. You can either
158 specify it directly as a decimal integer, or indirectly by
159 using instead an asterisk (<<*>>), in which case an <<int>>
160 argument is used as the field width. Negative field widths
161 are not supported; if you attempt to specify a negative field
162 width, it is interpreted as a minus (<<->>) flag followed by a
163 positive field width.
167 an optional field; if present, it is introduced with `<<.>>'
168 (a period). This field gives the maximum number of
169 characters to print in a conversion; the minimum number of
170 digits of an integer to print, for conversions with <[type]>
171 <<d>>, <<i>>, <<o>>, <<u>>, <<x>>, and <<X>>; the maximum number of
172 significant digits, for the <<g>> and <<G>> conversions;
173 or the number of digits to print after the decimal
174 point, for <<e>>, <<E>>, and <<f>> conversions. You can specify
175 the precision either directly as a decimal integer or
176 indirectly by using an asterisk (<<*>>), in which case
177 an <<int>> argument is used as the precision. Supplying a negative
178 precision is equivalent to omitting the precision.
179 If only a period is specified the precision is zero.
180 If a precision appears with any other conversion <[type]>
181 than those listed here, the behavior is undefined.
185 <<h>>, <<l>>, and <<L>> are optional size characters which
186 override the default way that <<printf>> interprets the
187 data type of the corresponding argument. <<h>> forces
188 the following <<d>>, <<i>>, <<o>>, <<u>>, <<x>> or <<X>> conversion
189 <[type]> to apply to a <<short>> or <<unsigned short>>. <<h>> also
190 forces a following <<n>> <[type]> to apply to
191 a pointer to a <<short>>. Similarily, an
192 <<l>> forces the following <<d>>, <<i>>, <<o>>, <<u>>,
193 <<x>> or <<X>> conversion <[type]> to apply to a <<long>> or
194 <<unsigned long>>. <<l>> also forces a following <<n>> <[type]> to
195 apply to a pointer to a <<long>>. If an <<h>>
196 or an <<l>> appears with another conversion
197 specifier, the behavior is undefined. <<L>> forces a
198 following <<e>>, <<E>>, <<f>>, <<g>> or <<G>> conversion <[type]> to
199 apply to a <<long double>> argument. If <<L>> appears with
200 any other conversion <[type]>, the behavior is undefined.
204 <[type]> specifies what kind of conversion <<printf>> performs.
205 Here is a table of these:
209 prints the percent character (<<%>>)
212 prints <[arg]> as single character
215 prints characters until precision is reached or a null terminator
216 is encountered; takes a string pointer
219 prints a signed decimal integer; takes an <<int>> (same as <<i>>)
222 prints a signed decimal integer; takes an <<int>> (same as <<d>>)
225 prints a signed octal integer; takes an <<int>>
228 prints an unsigned decimal integer; takes an <<int>>
231 prints an unsigned hexadecimal integer (using <<abcdef>> as
232 digits beyond <<9>>); takes an <<int>>
235 prints an unsigned hexadecimal integer (using <<ABCDEF>> as
236 digits beyond <<9>>); takes an <<int>>
239 prints a signed value of the form <<[-]9999.9999>>; takes
240 a floating point number
243 prints a signed value of the form <<[-]9.9999e[+|-]999>>; takes a
244 floating point number
247 prints the same way as <<e>>, but using <<E>> to introduce the
248 exponent; takes a floating point number
251 prints a signed value in either <<f>> or <<e>> form, based on given
252 value and precision---trailing zeros and the decimal point are
253 printed only if necessary; takes a floating point number
256 prints the same way as <<g>>, but using <<E>> for the exponent if an
257 exponent is needed; takes a floating point number
260 stores (in the same object) a count of the characters written;
261 takes a pointer to <<int>>
264 prints a pointer in an implementation-defined format.
265 This implementation treats the pointer as an
266 <<unsigned long>> (same as <<Lu>>).
272 <<sprintf>> returns the number of bytes in the output string,
273 save that the concluding <<NULL>> is not counted.
274 <<printf>> and <<fprintf>> return the number of characters transmitted.
275 If an error occurs, <<printf>> and <<fprintf>> return <<EOF>>.
276 Error returns occur for <<sprintf>>.
279 The ANSI C standard specifies that implementations must
280 support at least formatted output of up to 509 characters.
282 Supporting OS subroutines required: <<close>>, <<fstat>>, <<isatty>>,
283 <<lseek>>, <<read>>, <<sbrk>>, <<write>>.
293 A reentrant version of sprintf().
295 EXPORT_C int _sprintf_r (struct _reent *ptr, char *str, const char *fmt, ...)
301 f._flags = __SWR | __SSTR;
302 f._bf._base = f._p = (unsigned char *) str;
303 f._bf._size = f._w = INT_MAX;
306 ret = vfprintf (&f, fmt, ap);
315 Print formatted data to a string.
316 Writes a sequence of arguments to the given buffer formatted as the format argument specifies.
318 @param str Buffer where to store the resulting formatted string.
319 @param fmt String that contains the text to be printed.
321 @return On Success, the total number of characters printed is returned.
322 On Failure, a negative number is returned.
324 EXPORT_C int sprintf (char *str, const char *fmt, ...)
330 f._flags = __SWR | __SSTR;
331 f._bf._base = f._p = (unsigned char *) str;
332 f._bf._size = f._w = INT_MAX;
335 return EOF; // Memory for library globals is not allocated (errno not set).
337 ret = vfprintf (&f, fmt, ap);
345 @param str Buffer where to store the resulting formatted string.
346 @param fmt String that contains the text to be printed.
349 @return On Success, the total number of characters printed is returned.
350 On Failure, a negative number is returned.
352 EXPORT_C int vsprintf (char *str,char const *fmt, va_list ap)
357 f._flags = __SWR | __SSTR;
358 f._bf._base = f._p = (unsigned char *) str;
359 f._bf._size = f._w = INT_MAX;
362 return EOF; // Memory for library globals is not allocated (errno not set).
363 ret = vfprintf (&f, fmt, ap);