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