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