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