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