]>
Commit | Line | Data |
---|---|---|
5b2abdfb A |
1 | .\" Copyright (c) 1990, 1991, 1993 |
2 | .\" The Regents of the University of California. All rights reserved. | |
3 | .\" | |
4 | .\" This code is derived from software contributed to Berkeley by | |
5 | .\" Chris Torek and the American National Standards Committee X3, | |
6 | .\" on Information Processing Systems. | |
7 | .\" | |
8 | .\" Redistribution and use in source and binary forms, with or without | |
9 | .\" modification, are permitted provided that the following conditions | |
10 | .\" are met: | |
11 | .\" 1. Redistributions of source code must retain the above copyright | |
12 | .\" notice, this list of conditions and the following disclaimer. | |
13 | .\" 2. Redistributions in binary form must reproduce the above copyright | |
14 | .\" notice, this list of conditions and the following disclaimer in the | |
15 | .\" documentation and/or other materials provided with the distribution. | |
16 | .\" 3. All advertising materials mentioning features or use of this software | |
17 | .\" must display the following acknowledgement: | |
18 | .\" This product includes software developed by the University of | |
19 | .\" California, Berkeley and its contributors. | |
20 | .\" 4. Neither the name of the University nor the names of its contributors | |
21 | .\" may be used to endorse or promote products derived from this software | |
22 | .\" without specific prior written permission. | |
23 | .\" | |
24 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
25 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
26 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
27 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
28 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
29 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
30 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
31 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
32 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
33 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
34 | .\" SUCH DAMAGE. | |
35 | .\" | |
36 | .\" @(#)printf.3 8.1 (Berkeley) 6/4/93 | |
9385eb3d | 37 | .\" $FreeBSD: src/lib/libc/stdio/printf.3,v 1.55 2003/01/06 06:19:19 tjr Exp $ |
5b2abdfb | 38 | .\" |
9385eb3d | 39 | .Dd January 4, 2003 |
5b2abdfb A |
40 | .Dt PRINTF 3 |
41 | .Os | |
42 | .Sh NAME | |
43 | .Nm printf , fprintf , sprintf , snprintf , asprintf , | |
44 | .Nm vprintf , vfprintf, vsprintf , vsnprintf , vasprintf | |
45 | .Nd formatted output conversion | |
46 | .Sh LIBRARY | |
47 | .Lb libc | |
48 | .Sh SYNOPSIS | |
49 | .In stdio.h | |
50 | .Ft int | |
9385eb3d | 51 | .Fn printf "const char * restrict format" ... |
5b2abdfb | 52 | .Ft int |
9385eb3d | 53 | .Fn fprintf "FILE * restrict stream" "const char * restrict format" ... |
5b2abdfb | 54 | .Ft int |
9385eb3d | 55 | .Fn sprintf "char * restrict str" "const char * restrict format" ... |
5b2abdfb | 56 | .Ft int |
9385eb3d | 57 | .Fn snprintf "char * restrict str" "size_t size" "const char * restrict format" ... |
5b2abdfb A |
58 | .Ft int |
59 | .Fn asprintf "char **ret" "const char *format" ... | |
60 | .In stdarg.h | |
61 | .Ft int | |
9385eb3d | 62 | .Fn vprintf "const char * restrict format" "va_list ap" |
5b2abdfb | 63 | .Ft int |
9385eb3d | 64 | .Fn vfprintf "FILE * restrict stream" "const char * restrict format" "va_list ap" |
5b2abdfb | 65 | .Ft int |
9385eb3d | 66 | .Fn vsprintf "char * restrict str" "const char * restrict format" "va_list ap" |
5b2abdfb | 67 | .Ft int |
9385eb3d | 68 | .Fn vsnprintf "char * restrict str" "size_t size" "const char * restrict format" "va_list ap" |
5b2abdfb A |
69 | .Ft int |
70 | .Fn vasprintf "char **ret" "const char *format" "va_list ap" | |
71 | .Sh DESCRIPTION | |
72 | The | |
73 | .Fn printf | |
74 | family of functions produces output according to a | |
75 | .Fa format | |
76 | as described below. | |
9385eb3d A |
77 | The |
78 | .Fn printf | |
5b2abdfb A |
79 | and |
80 | .Fn vprintf | |
9385eb3d | 81 | functions |
5b2abdfb | 82 | write output to |
9385eb3d | 83 | .Dv stdout , |
5b2abdfb A |
84 | the standard output stream; |
85 | .Fn fprintf | |
86 | and | |
87 | .Fn vfprintf | |
88 | write output to the given output | |
89 | .Fa stream ; | |
90 | .Fn sprintf , | |
91 | .Fn snprintf , | |
92 | .Fn vsprintf , | |
93 | and | |
94 | .Fn vsnprintf | |
95 | write to the character string | |
96 | .Fa str ; | |
97 | and | |
98 | .Fn asprintf | |
99 | and | |
100 | .Fn vasprintf | |
101 | dynamically allocate a new string with | |
102 | .Xr malloc 3 . | |
103 | .Pp | |
104 | These functions write the output under the control of a | |
105 | .Fa format | |
106 | string that specifies how subsequent arguments | |
107 | (or arguments accessed via the variable-length argument facilities of | |
108 | .Xr stdarg 3 ) | |
109 | are converted for output. | |
110 | .Pp | |
111 | These functions return the number of characters printed | |
112 | (not including the trailing | |
113 | .Ql \e0 | |
9385eb3d | 114 | used to end output to strings) or a negative value if an output error occurs, |
5b2abdfb A |
115 | except for |
116 | .Fn snprintf | |
117 | and | |
118 | .Fn vsnprintf , | |
119 | which return the number of characters that would have been printed if the | |
120 | .Fa size | |
121 | were unlimited | |
122 | (again, not including the final | |
123 | .Ql \e0 ) . | |
124 | .Pp | |
9385eb3d A |
125 | The |
126 | .Fn asprintf | |
5b2abdfb A |
127 | and |
128 | .Fn vasprintf | |
9385eb3d | 129 | functions |
5b2abdfb A |
130 | set |
131 | .Fa *ret | |
132 | to be a pointer to a buffer sufficiently large to hold the formatted string. | |
133 | This pointer should be passed to | |
134 | .Xr free 3 | |
135 | to release the allocated storage when it is no longer needed. | |
136 | If sufficient space cannot be allocated, | |
137 | .Fn asprintf | |
138 | and | |
139 | .Fn vasprintf | |
9385eb3d | 140 | will return \-1 and set |
5b2abdfb A |
141 | .Fa ret |
142 | to be a | |
143 | .Dv NULL | |
144 | pointer. | |
145 | .Pp | |
9385eb3d A |
146 | The |
147 | .Fn snprintf | |
5b2abdfb A |
148 | and |
149 | .Fn vsnprintf | |
9385eb3d | 150 | functions |
5b2abdfb A |
151 | will write at most |
152 | .Fa size Ns \-1 | |
153 | of the characters printed into the output string | |
154 | (the | |
155 | .Fa size Ns 'th | |
156 | character then gets the terminating | |
157 | .Ql \e0 ) ; | |
158 | if the return value is greater than or equal to the | |
159 | .Fa size | |
160 | argument, the string was too short | |
161 | and some of the printed characters were discarded. | |
9385eb3d | 162 | The output is always null-terminated. |
5b2abdfb | 163 | .Pp |
9385eb3d A |
164 | The |
165 | .Fn sprintf | |
5b2abdfb A |
166 | and |
167 | .Fn vsprintf | |
9385eb3d | 168 | functions |
5b2abdfb A |
169 | effectively assume an infinite |
170 | .Fa size . | |
171 | .Pp | |
172 | The format string is composed of zero or more directives: | |
173 | ordinary | |
174 | .\" multibyte | |
175 | characters (not | |
176 | .Cm % ) , | |
177 | which are copied unchanged to the output stream; | |
178 | and conversion specifications, each of which results | |
179 | in fetching zero or more subsequent arguments. | |
180 | Each conversion specification is introduced by | |
181 | the | |
182 | .Cm % | |
183 | character. | |
184 | The arguments must correspond properly (after type promotion) | |
185 | with the conversion specifier. | |
186 | After the | |
187 | .Cm % , | |
188 | the following appear in sequence: | |
189 | .Bl -bullet | |
190 | .It | |
191 | An optional field, consisting of a decimal digit string followed by a | |
192 | .Cm $ , | |
193 | specifying the next argument to access. | |
194 | If this field is not provided, the argument following the last | |
195 | argument accessed will be used. | |
196 | Arguments are numbered starting at | |
197 | .Cm 1 . | |
198 | If unaccessed arguments in the format string are interspersed with ones that | |
199 | are accessed the results will be indeterminate. | |
200 | .It | |
201 | Zero or more of the following flags: | |
9385eb3d A |
202 | .Bl -tag -width ".So \ Sc (space)" |
203 | .It Sq Cm # | |
204 | The value should be converted to an | |
5b2abdfb A |
205 | .Dq alternate form . |
206 | For | |
207 | .Cm c , d , i , n , p , s , | |
208 | and | |
209 | .Cm u | |
210 | conversions, this option has no effect. | |
211 | For | |
212 | .Cm o | |
213 | conversions, the precision of the number is increased to force the first | |
214 | character of the output string to a zero (except if a zero value is printed | |
215 | with an explicit precision of zero). | |
216 | For | |
217 | .Cm x | |
218 | and | |
219 | .Cm X | |
220 | conversions, a non-zero result has the string | |
221 | .Ql 0x | |
222 | (or | |
223 | .Ql 0X | |
224 | for | |
225 | .Cm X | |
226 | conversions) prepended to it. | |
227 | For | |
9385eb3d | 228 | .Cm a , A , e , E , f , F , g , |
5b2abdfb A |
229 | and |
230 | .Cm G | |
231 | conversions, the result will always contain a decimal point, even if no | |
232 | digits follow it (normally, a decimal point appears in the results of | |
233 | those conversions only if a digit follows). | |
234 | For | |
235 | .Cm g | |
236 | and | |
237 | .Cm G | |
238 | conversions, trailing zeros are not removed from the result as they | |
239 | would otherwise be. | |
9385eb3d A |
240 | .It So Cm 0 Sc (zero) |
241 | Zero padding. | |
5b2abdfb A |
242 | For all conversions except |
243 | .Cm n , | |
244 | the converted value is padded on the left with zeros rather than blanks. | |
245 | If a precision is given with a numeric conversion | |
246 | .Cm ( d , i , o , u , i , x , | |
247 | and | |
248 | .Cm X ) , | |
249 | the | |
250 | .Cm 0 | |
251 | flag is ignored. | |
9385eb3d A |
252 | .It Sq Cm \- |
253 | A negative field width flag; | |
254 | the converted value is to be left adjusted on the field boundary. | |
5b2abdfb A |
255 | Except for |
256 | .Cm n | |
257 | conversions, the converted value is padded on the right with blanks, | |
258 | rather than on the left with blanks or zeros. | |
259 | A | |
260 | .Cm \- | |
261 | overrides a | |
262 | .Cm 0 | |
263 | if both are given. | |
9385eb3d A |
264 | .It So "\ " Sc (space) |
265 | A blank should be left before a positive number | |
5b2abdfb | 266 | produced by a signed conversion |
9385eb3d | 267 | .Cm ( a , A , d , e , E , f , F , g , G , |
5b2abdfb A |
268 | or |
269 | .Cm i ) . | |
9385eb3d A |
270 | .It Sq Cm + |
271 | A sign must always be placed before a | |
5b2abdfb A |
272 | number produced by a signed conversion. |
273 | A | |
274 | .Cm + | |
275 | overrides a space if both are used. | |
9385eb3d A |
276 | .It Sq Cm ' |
277 | Decimal conversions | |
278 | .Cm ( d , u , | |
279 | or | |
280 | .Cm i ) | |
281 | or the integral portion of a floating point conversion | |
282 | .Cm ( f | |
283 | or | |
284 | .Cm F ) | |
285 | should be grouped and separated by thousands using | |
286 | the non-monetary separator returned by | |
287 | .Xr localeconv 3 . | |
5b2abdfb A |
288 | .El |
289 | .It | |
290 | An optional decimal digit string specifying a minimum field width. | |
291 | If the converted value has fewer characters than the field width, it will | |
292 | be padded with spaces on the left (or right, if the left-adjustment | |
293 | flag has been given) to fill out | |
294 | the field width. | |
295 | .It | |
296 | An optional precision, in the form of a period | |
297 | .Cm \&. | |
298 | followed by an | |
299 | optional digit string. | |
300 | If the digit string is omitted, the precision is taken as zero. | |
301 | This gives the minimum number of digits to appear for | |
302 | .Cm d , i , o , u , x , | |
303 | and | |
304 | .Cm X | |
305 | conversions, the number of digits to appear after the decimal-point for | |
9385eb3d | 306 | .Cm a , A , e , E , f , |
5b2abdfb | 307 | and |
9385eb3d | 308 | .Cm F |
5b2abdfb A |
309 | conversions, the maximum number of significant digits for |
310 | .Cm g | |
311 | and | |
312 | .Cm G | |
313 | conversions, or the maximum number of characters to be printed from a | |
314 | string for | |
315 | .Cm s | |
316 | conversions. | |
317 | .It | |
9385eb3d A |
318 | An optional length modifier, that specifies the size of the argument. |
319 | The following length modifiers are valid for the | |
320 | .Cm d , i , n , o , u , x , | |
5b2abdfb A |
321 | or |
322 | .Cm X | |
9385eb3d A |
323 | conversion: |
324 | .Bl -column ".Cm q Em (deprecated)" ".Vt signed char" ".Vt unsigned long long" ".Vt long long *" | |
325 | .It Sy Modifier Ta Cm d , i Ta Cm o , u , x , X Ta Cm n | |
326 | .It Cm hh Ta Vt "signed char" Ta Vt "unsigned char" Ta Vt "signed char *" | |
327 | .It Cm h Ta Vt short Ta Vt "unsigned short" Ta Vt "short *" | |
328 | .It Cm l No (ell) Ta Vt long Ta Vt "unsigned long" Ta Vt "long *" | |
329 | .It Cm ll No (ell ell) Ta Vt "long long" Ta Vt "unsigned long long" Ta Vt "long long *" | |
330 | .It Cm j Ta Vt intmax_t Ta Vt uintmax_t Ta Vt "intmax_t *" | |
331 | .It Cm t Ta Vt ptrdiff_t Ta (see note) Ta Vt "ptrdiff_t *" | |
332 | .It Cm z Ta (see note) Ta Vt size_t Ta (see note) | |
333 | .It Cm q Em (deprecated) Ta Vt quad_t Ta Vt u_quad_t Ta Vt "quad_t *" | |
334 | .El | |
335 | .Pp | |
336 | Note: | |
337 | the | |
338 | .Cm t | |
339 | modifier, when applied to a | |
340 | .Cm o , u , x , | |
5b2abdfb A |
341 | or |
342 | .Cm X | |
9385eb3d A |
343 | conversion, indicates that the argument is of an unsigned type |
344 | equivalent in size to a | |
345 | .Vt ptrdiff_t . | |
346 | The | |
347 | .Cm z | |
348 | modifier, when applied to a | |
349 | .Cm d | |
5b2abdfb | 350 | or |
9385eb3d A |
351 | .Cm i |
352 | conversion, indicates that the argument is of a signed type equivalent in | |
353 | size to a | |
354 | .Vt size_t . | |
355 | Similarly, when applied to an | |
5b2abdfb | 356 | .Cm n |
9385eb3d A |
357 | conversion, it indicates that the argument is a pointer to a signed type |
358 | equivalent in size to a | |
359 | .Vt size_t . | |
360 | .Pp | |
361 | The following length modifier is valid for the | |
362 | .Cm a , A , e , E , f , F , g , | |
5b2abdfb A |
363 | or |
364 | .Cm G | |
9385eb3d A |
365 | conversion: |
366 | .Bl -column ".Sy Modifier" ".Cm a , A , e , E , f , F , g , G" | |
367 | .It Sy Modifier Ta Cm a , A , e , E , f , F , g , G | |
368 | .It Cm L Ta Vt "long double" | |
369 | .El | |
370 | .Pp | |
371 | The following length modifier is valid for the | |
372 | .Cm c | |
373 | or | |
374 | .Cm s | |
375 | conversion: | |
376 | .Bl -column ".Sy Modifier" ".Vt wint_t" ".Vt wchar_t *" | |
377 | .It Sy Modifier Ta Cm c Ta Cm s | |
378 | .It Cm l No (ell) Ta Vt wint_t Ta Vt "wchar_t *" | |
379 | .El | |
5b2abdfb A |
380 | .It |
381 | A character that specifies the type of conversion to be applied. | |
382 | .El | |
383 | .Pp | |
384 | A field width or precision, or both, may be indicated by | |
385 | an asterisk | |
386 | .Ql * | |
387 | or an asterisk followed by one or more decimal digits and a | |
388 | .Ql $ | |
389 | instead of a | |
390 | digit string. | |
391 | In this case, an | |
392 | .Vt int | |
393 | argument supplies the field width or precision. | |
394 | A negative field width is treated as a left adjustment flag followed by a | |
395 | positive field width; a negative precision is treated as though it were | |
396 | missing. | |
9385eb3d A |
397 | If a single format directive mixes positional |
398 | .Pq Li nn$ | |
5b2abdfb A |
399 | and non-positional arguments, the results are undefined. |
400 | .Pp | |
401 | The conversion specifiers and their meanings are: | |
9385eb3d | 402 | .Bl -tag -width ".Cm diouxX" |
5b2abdfb A |
403 | .It Cm diouxX |
404 | The | |
405 | .Vt int | |
406 | (or appropriate variant) argument is converted to signed decimal | |
407 | .Cm ( d | |
408 | and | |
409 | .Cm i ) , | |
410 | unsigned octal | |
411 | .Pq Cm o , | |
412 | unsigned decimal | |
413 | .Pq Cm u , | |
414 | or unsigned hexadecimal | |
415 | .Cm ( x | |
416 | and | |
417 | .Cm X ) | |
418 | notation. | |
419 | The letters | |
9385eb3d | 420 | .Dq Li abcdef |
5b2abdfb A |
421 | are used for |
422 | .Cm x | |
423 | conversions; the letters | |
9385eb3d | 424 | .Dq Li ABCDEF |
5b2abdfb A |
425 | are used for |
426 | .Cm X | |
427 | conversions. | |
428 | The precision, if any, gives the minimum number of digits that must | |
429 | appear; if the converted value requires fewer digits, it is padded on | |
430 | the left with zeros. | |
431 | .It Cm DOU | |
432 | The | |
9385eb3d | 433 | .Vt "long int" |
5b2abdfb A |
434 | argument is converted to signed decimal, unsigned octal, or unsigned |
435 | decimal, as if the format had been | |
436 | .Cm ld , lo , | |
437 | or | |
438 | .Cm lu | |
439 | respectively. | |
440 | These conversion characters are deprecated, and will eventually disappear. | |
441 | .It Cm eE | |
442 | The | |
443 | .Vt double | |
444 | argument is rounded and converted in the style | |
9385eb3d A |
445 | .Sm off |
446 | .Oo \- Oc Ar d Li \&. Ar ddd Li e \\*[Pm] Ar dd | |
447 | .Sm on | |
5b2abdfb A |
448 | where there is one digit before the |
449 | decimal-point character | |
450 | and the number of digits after it is equal to the precision; | |
451 | if the precision is missing, | |
452 | it is taken as 6; if the precision is | |
453 | zero, no decimal-point character appears. | |
454 | An | |
455 | .Cm E | |
456 | conversion uses the letter | |
9385eb3d | 457 | .Ql E |
5b2abdfb | 458 | (rather than |
9385eb3d | 459 | .Ql e ) |
5b2abdfb A |
460 | to introduce the exponent. |
461 | The exponent always contains at least two digits; if the value is zero, | |
462 | the exponent is 00. | |
9385eb3d A |
463 | .Pp |
464 | For | |
465 | .Cm a , A , e , E , f , F , g , | |
466 | and | |
467 | .Cm G | |
468 | conversions, positive and negative infinity are represented as | |
469 | .Li inf | |
470 | and | |
471 | .Li -inf | |
472 | respectively when using the lowercase conversion character, and | |
473 | .Li INF | |
474 | and | |
475 | .Li -INF | |
476 | respectively when using the uppercase conversion character. | |
477 | Similarly, NaN is represented as | |
478 | .Li nan | |
479 | when using the lowercase conversion, and | |
480 | .Li NAN | |
481 | when using the uppercase conversion. | |
482 | .It Cm fF | |
5b2abdfb A |
483 | The |
484 | .Vt double | |
485 | argument is rounded and converted to decimal notation in the style | |
9385eb3d A |
486 | .Sm off |
487 | .Oo \- Oc Ar ddd Li \&. Ar ddd , | |
488 | .Sm on | |
5b2abdfb A |
489 | where the number of digits after the decimal-point character |
490 | is equal to the precision specification. | |
491 | If the precision is missing, it is taken as 6; if the precision is | |
492 | explicitly zero, no decimal-point character appears. | |
493 | If a decimal point appears, at least one digit appears before it. | |
494 | .It Cm gG | |
495 | The | |
496 | .Vt double | |
497 | argument is converted in style | |
498 | .Cm f | |
499 | or | |
500 | .Cm e | |
501 | (or | |
9385eb3d A |
502 | .Cm F |
503 | or | |
5b2abdfb A |
504 | .Cm E |
505 | for | |
506 | .Cm G | |
507 | conversions). | |
508 | The precision specifies the number of significant digits. | |
509 | If the precision is missing, 6 digits are given; if the precision is zero, | |
510 | it is treated as 1. | |
511 | Style | |
512 | .Cm e | |
9385eb3d | 513 | is used if the exponent from its conversion is less than \-4 or greater than |
5b2abdfb A |
514 | or equal to the precision. |
515 | Trailing zeros are removed from the fractional part of the result; a | |
516 | decimal point appears only if it is followed by at least one digit. | |
9385eb3d A |
517 | .It Cm aA |
518 | The | |
519 | .Vt double | |
520 | argument is converted to hexadecimal notation in the style | |
521 | .Sm off | |
522 | .Oo \- Oc Li 0x Ar h Li \&. Ar hhhp Oo \\*[Pm] Oc Ar d , | |
523 | .Sm on | |
524 | where the number of digits after the hexadecimal-point character | |
525 | is equal to the precision specification. | |
526 | If the precision is missing, it is taken as enough to exactly | |
527 | represent the floating-point number; if the precision is | |
528 | explicitly zero, no hexadecimal-point character appears. | |
529 | This is an exact conversion of the mantissa+exponent internal | |
530 | floating point representation; the | |
531 | .Sm off | |
532 | .Oo \- Oc Li 0x Ar h Li \&. Ar hhh | |
533 | .Sm on | |
534 | portion represents exactly the mantissa; only denormalized | |
535 | mantissas have a zero value to the left of the hexadecimal | |
536 | point. | |
537 | The | |
538 | .Cm p | |
539 | is a literal character | |
540 | .Ql p ; | |
541 | the exponent is preceded by a positive or negative sign | |
542 | and is represented in decimal, using only enough characters | |
543 | to represent the exponent. | |
544 | The | |
545 | .Cm A | |
546 | conversion uses the prefix | |
547 | .Dq Li 0X | |
548 | (rather than | |
549 | .Dq Li 0x ) , | |
550 | the letters | |
551 | .Dq Li ABCDEF | |
552 | (rather than | |
553 | .Dq Li abcdef ) | |
554 | to represent the hex digits, and the letter | |
555 | .Ql P | |
556 | (rather than | |
557 | .Ql p ) | |
558 | to separate the mantissa and exponent. | |
559 | .It Cm C | |
560 | Treated as | |
561 | .Cm c | |
562 | with the | |
563 | .Cm l | |
564 | (ell) modifier. | |
5b2abdfb A |
565 | .It Cm c |
566 | The | |
567 | .Vt int | |
568 | argument is converted to an | |
9385eb3d | 569 | .Vt "unsigned char" , |
5b2abdfb | 570 | and the resulting character is written. |
9385eb3d A |
571 | .Pp |
572 | If the | |
573 | .Cm l | |
574 | (ell) modifier is used, the | |
575 | .Vt wint_t | |
576 | argument shall be converted to a | |
577 | .Vt wchar_t , | |
578 | and the (potentially multi-byte) sequence representing the | |
579 | single wide character is written, including any shift sequences. | |
580 | If a shift sequence is used, the shift state is also restored | |
581 | to the original state after the character. | |
582 | .It Cm S | |
583 | Treated as | |
584 | .Cm s | |
585 | with the | |
586 | .Cm l | |
587 | (ell) modifier. | |
5b2abdfb A |
588 | .It Cm s |
589 | The | |
9385eb3d | 590 | .Vt "char *" |
5b2abdfb A |
591 | argument is expected to be a pointer to an array of character type (pointer |
592 | to a string). | |
593 | Characters from the array are written up to (but not including) | |
594 | a terminating | |
595 | .Dv NUL | |
596 | character; | |
597 | if a precision is specified, no more than the number specified are | |
598 | written. | |
599 | If a precision is given, no null character | |
600 | need be present; if the precision is not specified, or is greater than | |
601 | the size of the array, the array must contain a terminating | |
602 | .Dv NUL | |
603 | character. | |
9385eb3d A |
604 | .Pp |
605 | If the | |
606 | .Cm l | |
607 | (ell) modifier is used, the | |
608 | .Vt "wchar_t *" | |
609 | argument is expected to be a pointer to an array of wide characters | |
610 | (pointer to a wide string). | |
611 | For each wide character in the string, the (potentially multi-byte) | |
612 | sequence representing the | |
613 | wide character is written, including any shift sequences. | |
614 | If any shift sequence is used, the shift state is also restored | |
615 | to the original state after the string. | |
616 | Wide characters from the array are written up to (but not including) | |
617 | a terminating wide | |
618 | .Dv NUL | |
619 | character; | |
620 | if a precision is specified, no more than the number of bytes specified are | |
621 | written (including shift sequences). | |
622 | Partial characters are never written. | |
623 | If a precision is given, no null character | |
624 | need be present; if the precision is not specified, or is greater than | |
625 | the number of bytes required to render the multibyte representation of | |
626 | the string, the array must contain a terminating wide | |
627 | .Dv NUL | |
628 | character. | |
5b2abdfb A |
629 | .It Cm p |
630 | The | |
9385eb3d | 631 | .Vt "void *" |
5b2abdfb A |
632 | pointer argument is printed in hexadecimal (as if by |
633 | .Ql %#x | |
634 | or | |
635 | .Ql %#lx ) . | |
636 | .It Cm n | |
637 | The number of characters written so far is stored into the | |
638 | integer indicated by the | |
9385eb3d | 639 | .Vt "int *" |
5b2abdfb A |
640 | (or variant) pointer argument. |
641 | No argument is converted. | |
642 | .It Cm % | |
643 | A | |
644 | .Ql % | |
645 | is written. | |
646 | No argument is converted. | |
647 | The complete conversion specification | |
648 | is | |
649 | .Ql %% . | |
650 | .El | |
651 | .Pp | |
9385eb3d A |
652 | The decimal point |
653 | character is defined in the program's locale (category | |
654 | .Dv LC_NUMERIC ) . | |
655 | .Pp | |
5b2abdfb | 656 | In no case does a non-existent or small field width cause truncation of |
9385eb3d A |
657 | a numeric field; if the result of a conversion is wider than the field |
658 | width, the | |
5b2abdfb A |
659 | field is expanded to contain the conversion result. |
660 | .Sh EXAMPLES | |
661 | To print a date and time in the form | |
662 | .Dq Li "Sunday, July 3, 10:02" , | |
663 | where | |
664 | .Fa weekday | |
665 | and | |
666 | .Fa month | |
667 | are pointers to strings: | |
668 | .Bd -literal -offset indent | |
669 | #include <stdio.h> | |
670 | fprintf(stdout, "%s, %s %d, %.2d:%.2d\en", | |
671 | weekday, month, day, hour, min); | |
672 | .Ed | |
673 | .Pp | |
674 | To print \*(Pi | |
675 | to five decimal places: | |
676 | .Bd -literal -offset indent | |
677 | #include <math.h> | |
678 | #include <stdio.h> | |
679 | fprintf(stdout, "pi = %.5f\en", 4 * atan(1.0)); | |
680 | .Ed | |
681 | .Pp | |
682 | To allocate a 128 byte string and print into it: | |
683 | .Bd -literal -offset indent | |
684 | #include <stdio.h> | |
685 | #include <stdlib.h> | |
686 | #include <stdarg.h> | |
687 | char *newfmt(const char *fmt, ...) | |
688 | { | |
9385eb3d A |
689 | char *p; |
690 | va_list ap; | |
691 | if ((p = malloc(128)) == NULL) | |
692 | return (NULL); | |
693 | va_start(ap, fmt); | |
694 | (void) vsnprintf(p, 128, fmt, ap); | |
695 | va_end(ap); | |
696 | return (p); | |
697 | } | |
698 | .Ed | |
699 | .Sh SECURITY CONSIDERATIONS | |
700 | The | |
701 | .Fn sprintf | |
702 | and | |
703 | .Fn vsprintf | |
704 | functions are easily misused in a manner which enables malicious users | |
705 | to arbitrarily change a running program's functionality through | |
706 | a buffer overflow attack. | |
707 | Because | |
708 | .Fn sprintf | |
709 | and | |
710 | .Fn vsprintf | |
711 | assume an infinitely long string, | |
712 | callers must be careful not to overflow the actual space; | |
713 | this is often hard to assure. | |
714 | For safety, programmers should use the | |
715 | .Fn snprintf | |
716 | interface instead. | |
717 | For example: | |
718 | .Bd -literal | |
719 | void | |
720 | foo(const char *arbitrary_string, const char *and_another) | |
721 | { | |
722 | char onstack[8]; | |
723 | ||
724 | #ifdef BAD | |
725 | /* | |
726 | * This first sprintf is bad behavior. Do not use sprintf! | |
727 | */ | |
728 | sprintf(onstack, "%s, %s", arbitrary_string, and_another); | |
729 | #else | |
730 | /* | |
731 | * The following two lines demonstrate better use of | |
732 | * snprintf(). | |
733 | */ | |
734 | snprintf(onstack, sizeof(onstack), "%s, %s", arbitrary_string, | |
735 | and_another); | |
736 | #endif | |
5b2abdfb A |
737 | } |
738 | .Ed | |
9385eb3d A |
739 | .Pp |
740 | The | |
741 | .Fn printf | |
742 | and | |
743 | .Fn sprintf | |
744 | family of functions are also easily misused in a manner | |
745 | allowing malicious users to arbitrarily change a running program's | |
746 | functionality by either causing the program | |
747 | to print potentially sensitive data | |
748 | .Dq "left on the stack" , | |
749 | or causing it to generate a memory fault or bus error | |
750 | by dereferencing an invalid pointer. | |
751 | .Pp | |
752 | .Cm %n | |
753 | can be used to write arbitrary data to potentially carefully-selected | |
754 | addresses. | |
755 | Programmers are therefore strongly advised to never pass untrusted strings | |
756 | as the | |
757 | .Fa format | |
758 | argument, as an attacker can put format specifiers in the string | |
759 | to mangle your stack, | |
760 | leading to a possible security hole. | |
761 | This holds true even if the string was built using a function like | |
762 | .Fn snprintf , | |
763 | as the resulting string may still contain user-supplied conversion specifiers | |
764 | for later interpolation by | |
765 | .Fn printf . | |
766 | .Pp | |
767 | Always use the proper secure idiom: | |
768 | .Pp | |
769 | .Dl "snprintf(buffer, sizeof(buffer), \*q%s\*q, string);" | |
770 | .Sh ERRORS | |
771 | In addition to the errors documented for the | |
772 | .Xr write 2 | |
773 | system call, the | |
774 | .Fn printf | |
775 | family of functions may fail if: | |
776 | .Bl -tag -width Er | |
777 | .It Bq Er EILSEQ | |
778 | An invalid wide character code was encountered. | |
779 | .It Bq Er ENOMEM | |
780 | Insufficient storage space is available. | |
781 | .El | |
5b2abdfb A |
782 | .Sh SEE ALSO |
783 | .Xr printf 1 , | |
9385eb3d A |
784 | .Xr fmtcheck 3 , |
785 | .Xr scanf 3 , | |
786 | .Xr setlocale 3 , | |
787 | .Xr wprintf 3 | |
788 | .Rs | |
789 | .%T "The FreeBSD Security Architecture" | |
790 | .Re | |
791 | (See | |
792 | .Pa "/usr/share/doc/{to be determined}" . ) | |
5b2abdfb | 793 | .Sh STANDARDS |
9385eb3d A |
794 | Subject to the caveats noted in the |
795 | .Sx BUGS | |
796 | section below, the | |
5b2abdfb A |
797 | .Fn fprintf , |
798 | .Fn printf , | |
799 | .Fn sprintf , | |
800 | .Fn vprintf , | |
801 | .Fn vfprintf , | |
802 | and | |
803 | .Fn vsprintf | |
804 | functions | |
805 | conform to | |
9385eb3d A |
806 | .St -ansiC |
807 | and | |
808 | .St -isoC-99 . | |
809 | With the same reservation, the | |
810 | .Fn snprintf | |
811 | and | |
812 | .Fn vsnprintf | |
813 | functions conform to | |
814 | .St -isoC-99 . | |
5b2abdfb A |
815 | .Sh HISTORY |
816 | The functions | |
817 | .Fn asprintf | |
818 | and | |
819 | .Fn vasprintf | |
820 | first appeared in the | |
821 | .Tn GNU C | |
822 | library. | |
823 | These were implemented by | |
824 | .An Peter Wemm Aq peter@FreeBSD.org | |
825 | in | |
826 | .Fx 2.2 , | |
827 | but were later replaced with a different implementation | |
828 | from | |
829 | .An Todd C. Miller Aq Todd.Miller@courtesan.com | |
830 | for | |
831 | .Ox 2.3 . | |
832 | .Sh BUGS | |
833 | The conversion formats | |
834 | .Cm \&%D , \&%O , | |
835 | and | |
836 | .Cm %U | |
837 | are not standard and | |
838 | are provided only for backward compatibility. | |
839 | The effect of padding the | |
840 | .Cm %p | |
841 | format with zeros (either by the | |
842 | .Cm 0 | |
843 | flag or by specifying a precision), and the benign effect (i.e., none) | |
844 | of the | |
845 | .Cm # | |
846 | flag on | |
847 | .Cm %n | |
848 | and | |
849 | .Cm %p | |
850 | conversions, as well as other | |
851 | nonsensical combinations such as | |
852 | .Cm %Ld , | |
853 | are not standard; such combinations | |
854 | should be avoided. | |
855 | .Pp | |
9385eb3d A |
856 | The |
857 | .Nm | |
858 | family of functions currently lack the ability to use the | |
859 | .Cm ' | |
860 | flag in conjunction with the | |
861 | .Cm f | |
862 | conversion specifier. | |
863 | The | |
864 | .Cm a | |
5b2abdfb | 865 | and |
9385eb3d A |
866 | .Cm A |
867 | conversion specifiers have not yet been implemented. | |
868 | The | |
869 | .Cm L | |
870 | modifier for floating point formats simply round the | |
871 | .Vt "long double" | |
872 | argument to | |
873 | .Vt double , | |
874 | providing no additional precision. | |
875 | .Pp | |
876 | The | |
877 | .Nm | |
878 | family of functions do not correctly handle multibyte characters in the | |
879 | .Fa format | |
880 | argument. |