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