1.\" $NetBSD: printf.3,v 1.68 2017/12/31 07:22:55 dholland 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 November 19, 2015 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 x , 283and 284.Cm X ) , 285the 286.Cm 0 287flag is ignored. 288.It Sq Cm \- 289A negative field width flag; 290the converted value is to be left adjusted on the field boundary. 291Except for 292.Cm n 293conversions, the converted value is padded on the right with blanks, 294rather than on the left with blanks or zeros. 295A 296.Sq Cm \- 297overrides a 298.Sq Cm \&0 299if both are given. 300.It So "\ " Sc (space) 301A blank should be left before a positive number 302produced by a signed conversion 303.Pf ( Cm a , 304.Cm A 305.Cm d , 306.Cm e , 307.Cm E , 308.Cm f , 309.Cm F , 310.Cm g , 311.Cm G , 312or 313.Cm i ) . 314.It Sq Cm + 315A sign must always be placed before a 316number produced by a signed conversion. 317A 318.Sq Cm + 319overrides a space if both are used. 320.It Sq Cm ' 321Decimal conversions 322.Cm ( d , u , 323or 324.Cm i ) 325or the integral portion of a floating point conversion 326.Cm ( f 327or 328.Cm F ) 329should be grouped and separated by thousands using 330the non-monetary separator returned by 331.Xr localeconv 3 . 332.El 333.It 334An optional decimal digit string specifying a minimum field width. 335If the converted value has fewer characters than the field width, it will 336be padded with spaces on the left (or right, if the left-adjustment 337flag has been given) to fill out the field width. 338.It 339An optional precision, in the form of a period 340.Sq Cm \&. 341followed by an optional digit string. 342If the digit string is omitted, the precision is taken as zero. 343This gives the minimum number of digits to appear for 344.Cm d , 345.Cm i , 346.Cm o , 347.Cm u , 348.Cm x , 349and 350.Cm X 351conversions, the number of digits to appear after the decimal-point for 352.Cm a , 353.Cm A , 354.Cm e , 355.Cm E , 356.Cm f , 357and 358.Cm F 359conversions, the maximum number of significant digits for 360.Cm g 361and 362.Cm G 363conversions, or the maximum number of characters to be printed from a 364string for 365.Cm s 366conversions. 367.It 368An optional length modifier, that specifies the size of the argument. 369The following length modifiers are valid for the 370.Cm d , i , n , o , u , x , 371or 372.Cm X 373conversions: 374.Bl -column ".Cm q Em (deprecated)" ".Vt signed char" ".Vt unsigned long long" ".Vt long long *" 375.It Sy Modifier Ta Cm d , i Ta Cm o , u , x , X Ta Cm n 376.It Cm hh Ta Vt "signed char" Ta Vt "unsigned char" Ta Vt "signed char *" 377.It Cm h Ta Vt short Ta Vt "unsigned short" Ta Vt "short *" 378.It Cm l No (ell) Ta Vt long Ta Vt "unsigned long" Ta Vt "long *" 379.It Cm ll No (ell ell) Ta Vt "long long" Ta Vt "unsigned long long" Ta Vt "long long *" 380.It Cm j Ta Vt intmax_t Ta Vt uintmax_t Ta Vt "intmax_t *" 381.It Cm t Ta Vt ptrdiff_t Ta (see note) Ta Vt "ptrdiff_t *" 382.It Cm z Ta (see note) Ta Vt size_t Ta (see note) 383.It Cm q Em (deprecated) Ta Vt quad_t Ta Vt u_quad_t Ta Vt "quad_t *" 384.El 385.Pp 386Note: 387the 388.Cm t 389modifier, when applied to a 390.Cm o , u , x , 391or 392.Cm X 393conversion, indicates that the argument is of an unsigned type 394equivalent in size to a 395.Vt ptrdiff_t . 396The 397.Cm z 398modifier, when applied to a 399.Cm d 400or 401.Cm i 402conversion, indicates that the argument is of a signed type equivalent in 403size to a 404.Vt size_t . 405Similarly, when applied to an 406.Cm n 407conversion, it indicates that the argument is a pointer to a signed type 408equivalent in size to a 409.Vt size_t . 410.Pp 411Note: 412if the standard integer types described in 413.Xr stdint 3 414are used, it is recommended that the predefined format string specifier 415macros are used when possible. 416These are further described in 417.Xr inttypes 3 . 418.Pp 419The following length modifiers are valid for the 420.Cm a , 421.Cm A , 422.Cm e , 423.Cm E , 424.Cm f , 425.Cm F , 426.Cm g , 427or 428.Cm G 429conversions: 430.Bl -column ".Sy Modifier" ".Cm a , A , e , E , f , F , g , G" 431.It Sy Modifier Ta Cm a , A , e , E , f , F , g , G 432.It Cm l No (ell) Ta Vt double 433(ignored, same behavior as without it) 434.It Cm L Ta Vt "long double" 435.El 436.Pp 437The following length modifier is valid for the 438.Cm c 439or 440.Cm s 441conversions: 442.Bl -column ".Sy Modifier" ".Vt wint_t" ".Vt wchar_t *" 443.It Sy Modifier Ta Cm c Ta Cm s 444.It Cm l No (ell) Ta Vt wint_t Ta Vt "wchar_t *" 445.El 446.It 447A character that specifies the type of conversion to be applied. 448.El 449.Pp 450A field width or precision, or both, may be indicated by 451an asterisk 452.Ql * 453or an asterisk followed by one or more decimal digits and a 454.Ql $ 455instead of a 456digit string. 457In this case, an 458.Vt int 459argument supplies the field width or precision. 460A negative field width is treated as a left adjustment flag followed by a 461positive field width; a negative precision is treated as though it were 462missing. 463If a single format directive mixes positional 464.Pq Li nn$ 465and non-positional arguments, the results are undefined. 466.Pp 467The conversion specifiers and their meanings are: 468.Bl -tag -width ".Cm diouxX" 469.It Cm diouxX 470The 471.Vt int 472(or appropriate variant) argument is converted to signed decimal 473.Pf ( Cm d 474and 475.Cm i ) , 476unsigned octal 477.Pq Cm o , 478unsigned decimal 479.Pq Cm u , 480or unsigned hexadecimal 481.Pf ( Cm x 482and 483.Cm X ) 484notation. 485The letters 486.Dq Li abcdef 487are used for 488.Cm x 489conversions; the letters 490.Dq Li ABCDEF 491are used for 492.Cm X 493conversions. 494The precision, if any, gives the minimum number of digits that must 495appear; if the converted value requires fewer digits, it is padded on 496the left with zeros. 497.It Cm DOU 498The 499.Vt long int 500argument is converted to signed decimal, unsigned octal, or unsigned 501decimal, as if the format had been 502.Cm ld , 503.Cm lo , 504or 505.Cm lu 506respectively. 507These conversion characters are deprecated, and will eventually disappear. 508.It Cm eE 509The 510.Vt double 511argument is rounded and converted in the style 512.Sm off 513.Oo \- Oc Ar d Li \&. Ar ddd Li e \*[Pm] Ar dd 514.Sm on 515where there is one digit before the 516decimal-point character 517and the number of digits after it is equal to the precision; 518if the precision is missing, 519it is taken as 6; if the precision is 520zero, no decimal-point character appears. 521An 522.Cm E 523conversion uses the letter 524.Ql E 525(rather than 526.Ql e ) 527to introduce the exponent. 528The exponent always contains at least two digits; if the value is zero, 529the exponent is 00. 530.Pp 531For 532.Cm a , 533.Cm A , 534.Cm e , 535.Cm E , 536.Cm f , 537.Cm F , 538.Cm g , 539and 540.Cm G 541conversions, positive and negative infinity are represented as 542.Li inf 543and 544.Li -inf 545respectively when using the lowercase conversion character, and 546.Li INF 547and 548.Li -INF 549respectively when using the uppercase conversion character. 550Similarly, NaN is represented as 551.Li nan 552when using the lowercase conversion, and 553.Li NAN 554when using the uppercase conversion. 555.It Cm fF 556The 557.Vt double 558argument is rounded and converted to decimal notation in the style 559.Sm off 560.Oo \- Oc Ar ddd Li \&. Ar ddd , 561.Sm on 562where the number of digits after the decimal-point character 563is equal to the precision specification. 564If the precision is missing, it is taken as 6; if the precision is 565explicitly zero, no decimal-point character appears. 566If a decimal point appears, at least one digit appears before it. 567.It Cm gG 568The 569.Vt double 570argument is converted in style 571.Cm f 572or 573.Cm e 574(or in style 575.Cm F 576or 577.Cm E 578for 579.Cm G 580conversions). 581The precision specifies the number of significant digits. 582If the precision is missing, 6 digits are given; if the precision is zero, 583it is treated as 1. 584Style 585.Cm e 586is used if the exponent from its conversion is less than \-4 or greater than 587or equal to the precision. 588Trailing zeros are removed from the fractional part of the result; a 589decimal point appears only if it is followed by at least one digit. 590.It Cm aA 591The 592.Vt double 593argument is rounded and converted to hexadecimal notation in the style 594.Sm off 595.Oo \- Oc Li 0x Ar h Li \&. Ar hhhp Oo \*[Pm] Oc Ar d , 596.Sm on 597where the number of digits after the hexadecimal-point character 598is equal to the precision specification. 599If the precision is missing, it is taken as enough to represent 600the floating-point number exactly, and no rounding occurs. 601If the precision is zero, no hexadecimal-point character appears. 602The 603.Cm p 604is a literal character 605.Ql p , 606and the exponent consists of a positive or negative sign 607followed by a decimal number representing an exponent of 2. 608The 609.Cm A 610conversion uses the prefix 611.Dq Li 0X 612(rather than 613.Dq Li 0x ) , 614the letters 615.Dq Li ABCDEF 616(rather than 617.Dq Li abcdef ) 618to represent the hex digits, and the letter 619.Ql P 620(rather than 621.Ql p ) 622to separate the mantissa and exponent. 623.Pp 624Note that there may be multiple valid ways to represent floating-point 625numbers in this hexadecimal format. 626For example, 627.Li 0x3.24p+0 , 0x6.48p-1 628and 629.Li 0xc.9p-2 630are all equivalent. 631The format chosen depends on the internal representation of the 632number, but the implementation guarantees that the length of the 633mantissa will be minimized. 634Zeroes are always represented with a mantissa of 0 (preceded by a 635.Ql - 636if appropriate) and an exponent of 637.Li +0 . 638.It Cm C 639Treated as 640.Cm c 641with the 642.Cm l 643(ell) modifier. 644.It Cm c 645The 646.Vt int 647argument is converted to an 648.Vt "unsigned char" , 649and the resulting character is written. 650.Pp 651If the 652.Cm l 653(ell) modifier is used, the 654.Vt wint_t 655argument shall be converted to a 656.Vt wchar_t , 657and the (potentially multi-byte) sequence representing the 658single wide character is written, including any shift sequences. 659If a shift sequence is used, the shift state is also restored 660to the original state after the character. 661.It Cm S 662Treated as 663.Cm s 664with the 665.Cm l 666(ell) modifier. 667.It Cm s 668The 669.Vt "char *" 670argument is expected to be a pointer to an array of character type (pointer 671to a string). 672Characters from the array are written up to (but not including) 673a terminating 674.Dv NUL 675character; 676if a precision is specified, no more than the number specified are 677written. 678If a precision is given, no null character 679need be present; if the precision is not specified, or is greater than 680the size of the array, the array must contain a terminating 681.Dv NUL 682character. 683.Pp 684If the 685.Cm l 686(ell) modifier is used, the 687.Vt "wchar_t *" 688argument is expected to be a pointer to an array of wide characters 689(pointer to a wide string). 690For each wide character in the string, the (potentially multi-byte) 691sequence representing the 692wide character is written, including any shift sequences. 693If any shift sequence is used, the shift state is also restored 694to the original state after the string. 695Wide characters from the array are written up to (but not including) 696a terminating wide 697.Dv NUL 698character; 699if a precision is specified, no more than the number of bytes specified are 700written (including shift sequences). 701Partial characters are never written. 702If a precision is given, no null character 703need be present; if the precision is not specified, or is greater than 704the number of bytes required to render the multibyte representation of 705the string, the array must contain a terminating wide 706.Dv NUL 707character. 708.It Cm p 709The 710.Vt "void *" 711pointer argument is printed in hexadecimal (as if by 712.Ql %#x 713or 714.Ql %#lx ) . 715.It Cm n 716The number of characters written so far is stored into the 717integer indicated by the 718.Vt "int *" 719(or variant) pointer argument. 720No argument is converted. 721.It Cm % 722A 723.Ql % 724is written. 725No argument is converted. 726The complete conversion specification is 727.Ql %% . 728.El 729.Pp 730The decimal point 731character is defined in the program's locale (category 732.Dv LC_NUMERIC ) . 733.Pp 734In no case does a non-existent or small field width cause truncation of 735a numeric field; if the result of a conversion is wider than the field 736width, the 737field is expanded to contain the conversion result. 738.Sh RETURN VALUES 739These functions return 740the number of characters printed, or that would be printed if there 741was adequate space in case of 742.Fn snprintf , 743.Fn vsnprintf , 744and 745.Fn vsnprintf_ss 746(not including the trailing 747.Ql \e0 748used to end output to strings). 749If an output error was encountered, these functions shall return a 750negative value. 751.Sh EXAMPLES 752To print a date and time in the form 753.Dq Li "Sunday, July 3, 10:02" , 754where 755.Fa weekday 756and 757.Fa month 758are pointers to strings: 759.Bd -literal -offset indent 760#include <stdio.h> 761fprintf(stdout, "%s, %s %d, %.2d:%.2d\en", 762 weekday, month, day, hour, min); 763.Ed 764.Pp 765To print \*(Pi 766to five decimal places: 767.Bd -literal -offset indent 768#include <math.h> 769#include <stdio.h> 770fprintf(stdout, "pi = %.5f\en", 4 * atan(1.0)); 771.Ed 772.Pp 773To allocate a 128 byte string and print into it: 774.Bd -literal -offset indent 775#include <stdio.h> 776#include <stdlib.h> 777#include <stdarg.h> 778char *newfmt(const char *fmt, ...) 779{ 780 char *p; 781 va_list ap; 782 if ((p = malloc(128)) == NULL) 783 return (NULL); 784 va_start(ap, fmt); 785 (void) vsnprintf(p, 128, fmt, ap); 786 va_end(ap); 787 return (p); 788} 789.Ed 790.Sh ERRORS 791In addition to the errors documented for the 792.Xr write 2 793system call, the 794.Fn printf 795family of functions may fail if: 796.Bl -tag -width Er 797.It Bq Er EILSEQ 798An invalid wide-character code was encountered. 799.It Bq Er ENOMEM 800Insufficient storage space is available. 801.It Bq Er EOVERFLOW 802The 803.Fa size 804argument exceeds 805.Dv INT_MAX , 806or the return value would be too large to be represented by an 807.Vt int . 808.El 809.Sh SEE ALSO 810.Xr printf 1 , 811.Xr fmtcheck 3 , 812.Xr scanf 3 , 813.Xr setlocale 3 , 814.Xr wprintf 3 , 815.Xr printf 9 816.Sh STANDARDS 817Subject to the caveats noted in the 818.Sx BUGS 819section below, the 820.Fn fprintf , 821.Fn printf , 822.Fn sprintf , 823.Fn vprintf , 824.Fn vfprintf , 825and 826.Fn vsprintf 827functions 828conform to 829.St -ansiC 830and 831.St -isoC-99 . 832With the same reservation, the 833.Fn snprintf 834and 835.Fn vsnprintf 836functions conform to 837.St -isoC-99 . 838.Sh HISTORY 839The functions 840.Fn snprintf 841and 842.Fn vsnprintf 843first appeared in 844.Bx 4.4 . 845The functions 846.Fn asprintf 847and 848.Fn vasprintf 849are modeled on the ones that first appeared in the GNU C library. 850The function 851.Fn vsnprintf_ss 852is non-standard and appeared in 853.Nx 4.0 . 854The functions 855.Fn dprintf 856and 857.Fn vdprintf 858are parts of 859.St -p1003.1-2008 860and appeared in 861.Nx 6.0 . 862.Sh CAVEATS 863Because 864.Fn sprintf 865and 866.Fn vsprintf 867assume an infinitely long string, callers must be careful not to 868overflow the actual space; this is often impossible to assure. 869For safety, programmers should use the 870.Fn snprintf 871and 872.Fn asprintf 873family of interfaces instead. 874Unfortunately, the 875.Fn snprintf 876interfaces are not available on older 877systems and the 878.Fn asprintf 879interfaces are not yet portable. 880.Pp 881It is important never to pass a string with user-supplied data as a 882format without using 883.Ql %s . 884An attacker can put format specifiers in the string to mangle your stack, 885leading to a possible security hole. 886This holds true even if you have built the string 887.Dq by hand 888using a function like 889.Fn snprintf , 890as the resulting string may still contain user-supplied conversion specifiers 891for later interpolation by 892.Fn printf . 893.Pp 894Be sure to use the proper secure idiom: 895.Bd -literal -offset indent 896snprintf(buffer, sizeof(buffer), "%s", string); 897.Ed 898.Pp 899There is no way for 900.Fn printf 901to know the size of each argument passed. 902If you use positional arguments you must ensure that all parameters, up to the 903last positionally specified parameter, are used in the format string. 904This allows for the format string to be parsed for this information. 905Failure to do this will mean your code is non-portable and liable to fail. 906.Pp 907In this implementation, passing a 908.Dv NULL 909.Vt char * 910argument to the 911.Cm %s 912format specifier will output 913.Em "(null)" 914instead of crashing. 915Programs that depend on this behavior are non-portable and may crash 916on other systems or in the future. 917.Sh BUGS 918The conversion formats 919.Cm \&%D , 920.Cm \&%O , 921and 922.Cm \&%U 923are not standard and are provided only for backward compatibility. 924The effect of padding the 925.Cm %p 926format with zeros (either by the 927.Sq Cm 0 928flag or by specifying a precision), and the benign effect (i.e. none) 929of the 930.Sq Cm # 931flag on 932.Cm %n 933and 934.Cm %p 935conversions, as well as other nonsensical combinations such as 936.Cm %Ld , 937are not standard; such combinations should be avoided. 938.Pp 939The 940.Fn printf 941family of functions do not correctly handle multibyte characters in the 942.Fa format 943argument. 944.Sh SECURITY CONSIDERATIONS 945The 946.Fn sprintf 947and 948.Fn vsprintf 949functions are easily misused in a manner which enables malicious users 950to arbitrarily change a running program's functionality through 951a buffer overflow attack. 952Because 953.Fn sprintf 954and 955.Fn vsprintf 956assume an infinitely long string, 957callers must be careful not to overflow the actual space; 958this is often hard to assure. 959For safety, programmers should use the 960.Fn snprintf 961interface instead. 962For example: 963.Bd -literal 964void 965foo(const char *arbitrary_string, const char *and_another) 966{ 967 char onstack[8]; 968 969#ifdef BAD 970 /* 971 * This first sprintf is bad behavior. Do not use sprintf! 972 */ 973 sprintf(onstack, "%s, %s", arbitrary_string, and_another); 974#else 975 /* 976 * The following two lines demonstrate better use of 977 * snprintf(). 978 */ 979 snprintf(onstack, sizeof(onstack), "%s, %s", arbitrary_string, 980 and_another); 981#endif 982} 983.Ed 984.Pp 985The 986.Fn printf 987and 988.Fn sprintf 989family of functions are also easily misused in a manner 990allowing malicious users to arbitrarily change a running program's 991functionality by either causing the program 992to print potentially sensitive data 993.Dq "left on the stack" , 994or causing it to generate a memory fault or bus error 995by dereferencing an invalid pointer. 996.Pp 997.Cm %n 998can be used to write arbitrary data to potentially carefully-selected 999addresses. 1000Programmers are therefore strongly advised to never pass untrusted strings 1001as the 1002.Fa format 1003argument, as an attacker can put format specifiers in the string 1004to mangle your stack, 1005leading to a possible security hole. 1006This holds true even if the string was built using a function like 1007.Fn snprintf , 1008as the resulting string may still contain user-supplied conversion specifiers 1009for later interpolation by 1010.Fn printf . 1011.Pp 1012Always use the proper secure idiom: 1013.Pp 1014.Dl "snprintf(buffer, sizeof(buffer), \*q%s\*q, string);" 1015