xref: /illumos-gate/usr/src/man/man9f/sprintf.9f (revision c3b39789)
11e3b90b0SYuri Pankov.\"
21e3b90b0SYuri Pankov.\" The contents of this file are subject to the terms of the
31e3b90b0SYuri Pankov.\" Common Development and Distribution License (the "License").
41e3b90b0SYuri Pankov.\" You may not use this file except in compliance with the License.
51e3b90b0SYuri Pankov.\"
61e3b90b0SYuri Pankov.\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
71e3b90b0SYuri Pankov.\" or http://www.opensolaris.org/os/licensing.
81e3b90b0SYuri Pankov.\" See the License for the specific language governing permissions
91e3b90b0SYuri Pankov.\" and limitations under the License.
101e3b90b0SYuri Pankov.\"
111e3b90b0SYuri Pankov.\" When distributing Covered Code, include this CDDL HEADER in each
121e3b90b0SYuri Pankov.\" file and include the License file at usr/src/OPENSOLARIS.LICENSE.
131e3b90b0SYuri Pankov.\" If applicable, add the following below this CDDL HEADER, with the
141e3b90b0SYuri Pankov.\" fields enclosed by brackets "[]" replaced with your own identifying
151e3b90b0SYuri Pankov.\" information: Portions Copyright [yyyy] [name of copyright owner]
161e3b90b0SYuri Pankov.\"
171e3b90b0SYuri Pankov.\"
181e3b90b0SYuri Pankov.\" Copyright 1989 AT&T
191e3b90b0SYuri Pankov.\" Copyright (c) 2006, Sun Microsystems, Inc. All Rights Reserved.
201e3b90b0SYuri Pankov.\" Copyright 2016 Nexenta Systems, Inc.
211e3b90b0SYuri Pankov.\"
22*c3b39789SHans Rosenfeld.Dd Oct 20, 2023
231e3b90b0SYuri Pankov.Dt SPRINTF 9F
241e3b90b0SYuri Pankov.Os
251e3b90b0SYuri Pankov.Sh NAME
261e3b90b0SYuri Pankov.Nm sprintf, snprintf, vsprintf, vsnprintf
271e3b90b0SYuri Pankov.Nd format characters in memory
281e3b90b0SYuri Pankov.Sh SYNOPSIS
291e3b90b0SYuri Pankov.In sys/cmn_err.h
301e3b90b0SYuri Pankov.In sys/ddi.h
311e3b90b0SYuri Pankov.In sys/sunddi.h
321e3b90b0SYuri Pankov.Ft "char *"
331e3b90b0SYuri Pankov.Fo sprintf
341e3b90b0SYuri Pankov.Fa "char *s"
351e3b90b0SYuri Pankov.Fa "const char *format"
361e3b90b0SYuri Pankov.Fa ...
371e3b90b0SYuri Pankov.Fc
381e3b90b0SYuri Pankov.Ft size_t
391e3b90b0SYuri Pankov.Fo snprintf
401e3b90b0SYuri Pankov.Fa "char *s"
411e3b90b0SYuri Pankov.Fa "size_t n"
421e3b90b0SYuri Pankov.Fa "const char *format"
431e3b90b0SYuri Pankov.Fa ...
441e3b90b0SYuri Pankov.Fc
451e3b90b0SYuri Pankov.In sys/varargs.h
461e3b90b0SYuri Pankov.Ft "char *"
471e3b90b0SYuri Pankov.Fo vsprintf
481e3b90b0SYuri Pankov.Fa "char *s"
491e3b90b0SYuri Pankov.Fa "const char *format"
501e3b90b0SYuri Pankov.Fa "va_list ap"
511e3b90b0SYuri Pankov.Fc
521e3b90b0SYuri Pankov.Ft size_t
531e3b90b0SYuri Pankov.Fo vsnprintf
541e3b90b0SYuri Pankov.Fa "char *s"
551e3b90b0SYuri Pankov.Fa "size_t n"
561e3b90b0SYuri Pankov.Fa "const char *format"
571e3b90b0SYuri Pankov.Fa "va_list ap"
581e3b90b0SYuri Pankov.Fc
591e3b90b0SYuri Pankov.Sh INTERFACE LEVEL
601e3b90b0SYuri Pankovillumos DDI specific
611e3b90b0SYuri Pankov.Sh PARAMETERS
621e3b90b0SYuri Pankov.Bl -tag -width Ds
631e3b90b0SYuri Pankov.It Fa s
641e3b90b0SYuri PankovPointer to a character string.
651e3b90b0SYuri Pankov.It Fa n
661e3b90b0SYuri PankovSize of the buffer pointed to by
671e3b90b0SYuri Pankov.Fa s .
681e3b90b0SYuri Pankov.It Fa format
691e3b90b0SYuri PankovPointer to a character string.
701e3b90b0SYuri Pankov.It Fa ap
711e3b90b0SYuri PankovPointer to a variable argument list.
721e3b90b0SYuri Pankov.El
731e3b90b0SYuri Pankov.Sh DESCRIPTION
741e3b90b0SYuri PankovThe
751e3b90b0SYuri Pankov.Fn sprintf
761e3b90b0SYuri Pankovfunction places output, followed by the null byte
771e3b90b0SYuri Pankov.Pq \e0 ,
781e3b90b0SYuri Pankovin consecutive bytes starting at
791e3b90b0SYuri Pankov.Fa s ;
801e3b90b0SYuri Pankovit is the user's responsibility to ensure that enough storage is available.
811e3b90b0SYuri Pankov.Pp
821e3b90b0SYuri PankovThe
831e3b90b0SYuri Pankov.Fn snprintf
841e3b90b0SYuri Pankovfunction is identical to
851e3b90b0SYuri Pankov.Fn sprintf
861e3b90b0SYuri Pankovwith the addition of the argument
871e3b90b0SYuri Pankov.Fa n ,
881e3b90b0SYuri Pankovwhich specifies the size of the buffer referred to by
891e3b90b0SYuri Pankov.Fa s .
901e3b90b0SYuri PankovIf
911e3b90b0SYuri Pankov.Fa n
921e3b90b0SYuri Pankovis 0, nothing is written and
931e3b90b0SYuri Pankov.Fa s
941e3b90b0SYuri Pankovcan be a null pointer.
951e3b90b0SYuri PankovOtherwise, output bytes beyond the
961e3b90b0SYuri Pankov.Fa n Ns \-1st
971e3b90b0SYuri Pankovare discarded instead of being written to the array and a null byte is written
981e3b90b0SYuri Pankovat the end of the bytes actually written into the array.
991e3b90b0SYuri Pankov.Pp
1001e3b90b0SYuri PankovThe
1011e3b90b0SYuri Pankov.Fn vsprintf
1021e3b90b0SYuri Pankovand
1031e3b90b0SYuri Pankov.Fn vsnprintf
1041e3b90b0SYuri Pankovfunctions are the same as
1051e3b90b0SYuri Pankov.Fn sprintf
1061e3b90b0SYuri Pankovand
1071e3b90b0SYuri Pankov.Fn snprintf ,
1081e3b90b0SYuri Pankovrespectively, except that instead of being called with a variable number of
1091e3b90b0SYuri Pankovarguments, they are called with an argument list,
1101e3b90b0SYuri Pankov.Fa ap ,
1111e3b90b0SYuri Pankovused by the conversion specifications in
1121e3b90b0SYuri Pankov.Fa format .
1131e3b90b0SYuri Pankov.Fa ap
1141e3b90b0SYuri Pankovis a variable argument list and must be initialized by calling
1151e3b90b0SYuri Pankov.Xr va_start 9F .
1161e3b90b0SYuri Pankov.Xr va_end 9F
1171e3b90b0SYuri Pankovis used to clean up and must be called after each traversal of the list.
1181e3b90b0SYuri PankovMultiple traversals of the argument list, each bracketed by
1191e3b90b0SYuri Pankov.Xr va_start 9F
1201e3b90b0SYuri Pankovand
1211e3b90b0SYuri Pankov.Xr va_end 9F ,
1221e3b90b0SYuri Pankovare possible.
1231e3b90b0SYuri Pankov.Pp
1241e3b90b0SYuri PankovEach of these functions converts, formats, and prints its arguments under
1251e3b90b0SYuri Pankovcontrol of the
1261e3b90b0SYuri Pankov.Fa format .
1271e3b90b0SYuri PankovThe
1281e3b90b0SYuri Pankov.Fa format
1291e3b90b0SYuri Pankovis composed of zero or more directives: ordinary characters, which are simply
1301e3b90b0SYuri Pankovcopied to the output stream and conversion specifications, each of which results
1311e3b90b0SYuri Pankovin the fetching of zero or more arguments.
1321e3b90b0SYuri PankovThe results are undefined if there are insufficient arguments for the
1331e3b90b0SYuri Pankov.Fa format .
1341e3b90b0SYuri PankovIf the
1351e3b90b0SYuri Pankov.Fa format
1361e3b90b0SYuri Pankovis exhausted while arguments remain, the excess arguments are evaluated but are
1371e3b90b0SYuri Pankovotherwise ignored.
1381e3b90b0SYuri Pankov.Ss Conversion Specifications
1391e3b90b0SYuri PankovEach conversion specification is introduced by the
1401e3b90b0SYuri Pankov.Qq Sy %
1411e3b90b0SYuri Pankovcharacter after which the following appear in sequence:
1421e3b90b0SYuri Pankov.Bl -bullet
1431e3b90b0SYuri Pankov.It
1441e3b90b0SYuri PankovZero or more flags
1451e3b90b0SYuri Pankov.Pq in any order ,
1461e3b90b0SYuri Pankovwhich modify the meaning of the conversion specification.
1471e3b90b0SYuri Pankov.It
1481e3b90b0SYuri PankovAn optional minimum field width.
1491e3b90b0SYuri PankovIf the converted value has fewer bytes than the field width, it will be padded
1501e3b90b0SYuri Pankovwith spaces by default on the left; it will be padded on the right, if the
1511e3b90b0SYuri Pankovleft-adjustment flag
1521e3b90b0SYuri Pankov.Pq Qq Sy ‐ ,
1531e3b90b0SYuri Pankovdescribed below, is given to the field width.
1541e3b90b0SYuri PankovThe field width takes the form of an asterisk
1551e3b90b0SYuri Pankov.Pq Qq Sy * ,
1561e3b90b0SYuri Pankovdescribed below, or a decimal integer.
1571e3b90b0SYuri Pankov.It
1581e3b90b0SYuri PankovAn optional precision that gives the minimum number of digits to appear for the
1591e3b90b0SYuri Pankov.Sy d , D , o , O , x , X ,
1601e3b90b0SYuri Pankovor
1611e3b90b0SYuri Pankov.Sy u
1621e3b90b0SYuri Pankovconversions
1631e3b90b0SYuri Pankov.Pq the field is padded with leading zeros ;
1641e3b90b0SYuri Pankovor the maximum number of bytes to be printed from a string in s conversion.
1651e3b90b0SYuri PankovThe precision takes the form of a period
1661e3b90b0SYuri Pankov.Pq Qq Sy \&.
1671e3b90b0SYuri Pankovfollowed either by an asterisk
1681e3b90b0SYuri Pankov.Pq Qq Sy * ,
1691e3b90b0SYuri Pankovdescribed below, or an optional decimal digit string, where a null digit string
1701e3b90b0SYuri Pankovis treated as 0.
1711e3b90b0SYuri PankovIf a precision appears with any other conversion specifier, the behavior is
1721e3b90b0SYuri Pankovundefined.
1731e3b90b0SYuri Pankov.It
1741e3b90b0SYuri PankovAn optional length modifier that specified the size of the argument.
1751e3b90b0SYuri Pankov.It
1761e3b90b0SYuri PankovA conversion specifier that indicates the type of conversion to be applied.
1771e3b90b0SYuri Pankov.El
1781e3b90b0SYuri Pankov.Pp
1791e3b90b0SYuri PankovA field width, or precision, or both can be indicated by an asterisk
1801e3b90b0SYuri Pankov.Pq Qq Sy * .
1811e3b90b0SYuri PankovIn this case, an argument of type int supplies the field width or precision.
1821e3b90b0SYuri PankovArguments specifying field width, or precision, or both must appear in that
1831e3b90b0SYuri Pankovorder before the argument, if any, to be converted.
1841e3b90b0SYuri PankovA negative field width is taken as a
1851e3b90b0SYuri Pankov.Qq Sy \-
1861e3b90b0SYuri Pankovflag followed by a positive field width.
1871e3b90b0SYuri PankovA negative precision is taken as if the precision were omitted.
1881e3b90b0SYuri Pankov.Ss Flag Characters
1891e3b90b0SYuri PankovThe flag characters and their meanings are:
1901e3b90b0SYuri Pankov.Bl -tag -width Ds
1911e3b90b0SYuri Pankov.It Sy \-
1921e3b90b0SYuri PankovThe result of the conversion will be left-justified within the field.
1931e3b90b0SYuri PankovThe conversion will be right-justified if this flag is not specified.
1941e3b90b0SYuri Pankov.It Sy 0
1951e3b90b0SYuri PankovFor
1961e3b90b0SYuri Pankov.Sy d , D , o , O , x , X ,
1971e3b90b0SYuri Pankovor
1981e3b90b0SYuri Pankov.Sy u
1991e3b90b0SYuri Pankovconversions, leading zeros
2001e3b90b0SYuri Pankov.Pq following any indication of sign or base
2011e3b90b0SYuri Pankovare used to pad to the field width; no space padding is performed.
2021e3b90b0SYuri PankovIf the
2031e3b90b0SYuri Pankov.Sy 0
2041e3b90b0SYuri Pankovand
2051e3b90b0SYuri Pankov.Sy \-
2061e3b90b0SYuri Pankovflags both appear, the
2071e3b90b0SYuri Pankov.Sy 0
2081e3b90b0SYuri Pankovflag will be ignored.
2091e3b90b0SYuri PankovIf a precision is specified, the
2101e3b90b0SYuri Pankov.Sy 0
2111e3b90b0SYuri Pankovflag will be ignored.
2121e3b90b0SYuri PankovFor other conversions, the behavior is undefined.
2131e3b90b0SYuri Pankov.El
2141e3b90b0SYuri Pankov.Ss Length Modifiers
2151e3b90b0SYuri PankovThe length modifiers and their meanings are:
2161e3b90b0SYuri Pankov.Bl -tag -width Ds
2171e3b90b0SYuri Pankov.It Sy h
2181e3b90b0SYuri PankovSpecifies that a following
2191e3b90b0SYuri Pankov.Sy d , D , o , O , x , X ,
2201e3b90b0SYuri Pankovor
2211e3b90b0SYuri Pankov.Sy u
2221e3b90b0SYuri Pankovconversion specifier applies to a short or unsigned
2231e3b90b0SYuri Pankovshort argument
2241e3b90b0SYuri Pankov.Po the argument will have been promoted according to the integer promotions,
2251e3b90b0SYuri Pankovbut its value will be converted to short or unsigned short before printing
2261e3b90b0SYuri Pankov.Pc .
2271e3b90b0SYuri Pankov.It Sy hh
2281e3b90b0SYuri PankovSpecifies that a following
2291e3b90b0SYuri Pankov.Sy d , D , o , O , x , X ,
2301e3b90b0SYuri Pankovor
2311e3b90b0SYuri Pankov.Sy u
2321e3b90b0SYuri Pankovconversion specifier applies to a signed char or unsigned char argument
2331e3b90b0SYuri Pankov.Po the argument will have been promoted according to the integer promotions,
2341e3b90b0SYuri Pankovbut its value will be converted to signed char or unsigned char before printing
2351e3b90b0SYuri Pankov.Pc .
2361e3b90b0SYuri Pankov.It Sy l
2371e3b90b0SYuri PankovSpecifies that a following
2381e3b90b0SYuri Pankov.Sy d , D , o , O , x , X ,
2391e3b90b0SYuri Pankovor
2401e3b90b0SYuri Pankov.Sy u
2411e3b90b0SYuri Pankovconversion specifier applies to a long or unsigned long argument.
2421e3b90b0SYuri Pankov.It Sy ll
2431e3b90b0SYuri PankovSpecifies that a following
2441e3b90b0SYuri Pankov.Sy d , D , o , O , x , X ,
2451e3b90b0SYuri Pankovor
2461e3b90b0SYuri Pankov.Sy u
2471e3b90b0SYuri Pankovconversion specifier applies to a long long or unsigned long long argument.
2481e3b90b0SYuri Pankov.El
2491e3b90b0SYuri Pankov.Ss Conversion Specifiers
2501e3b90b0SYuri PankovEach conversion specifier results in fetching zero or more arguments.
2511e3b90b0SYuri PankovThe results are undefined if there are insufficient arguments for the
2521e3b90b0SYuri Pankov.Fa format .
2531e3b90b0SYuri PankovIf the
2541e3b90b0SYuri Pankov.Fa format
2551e3b90b0SYuri Pankovis exhausted while arguments remain, the excess arguments are ignored.
2561e3b90b0SYuri Pankov.Pp
2571e3b90b0SYuri PankovThe conversion specifiers and their meanings are:
2581e3b90b0SYuri Pankov.Bl -tag -width Ds
2591e3b90b0SYuri Pankov.It Sy d , D , o , O , x , X , u
2601e3b90b0SYuri PankovThe integer argument is converted to signed decimal
2611e3b90b0SYuri Pankov.Pq Sy d , D ,
2621e3b90b0SYuri Pankovunsigned octal
2631e3b90b0SYuri Pankov.Pq Sy o , O ,
2641e3b90b0SYuri Pankovunsigned hexadecimal
2651e3b90b0SYuri Pankov.Pq Sy x , X ,
2661e3b90b0SYuri Pankovor unsigned decimal
2671e3b90b0SYuri Pankov.Pq Sy u ,
2681e3b90b0SYuri Pankovrespectively.
2691e3b90b0SYuri PankovThe letters
2701e3b90b0SYuri Pankov.Qq Sy abcdef
2711e3b90b0SYuri Pankovare used for
2721e3b90b0SYuri Pankov.Sy x
2731e3b90b0SYuri Pankovand letters
2741e3b90b0SYuri Pankov.Qq Sy ABCDEF
2751e3b90b0SYuri Pankovfor
2761e3b90b0SYuri Pankov.Sy X
2771e3b90b0SYuri Pankovconversions.
2781e3b90b0SYuri Pankov.It Sy c
2791e3b90b0SYuri PankovThe character value of the argument is printed.
2801e3b90b0SYuri Pankov.It Sy b
2811e3b90b0SYuri PankovThe
2821e3b90b0SYuri Pankov.Sy %b
2831e3b90b0SYuri Pankovconversion specification allows bit values to be printed meaningfully.
2841e3b90b0SYuri PankovEach
2851e3b90b0SYuri Pankov.Sy %b
2861e3b90b0SYuri Pankovtakes an integer value and a format string from the argument list.
2871e3b90b0SYuri PankovThe first character of the format string should be the output base encoded as a
2881e3b90b0SYuri Pankovcontrol character.
2891e3b90b0SYuri PankovThis base is used to print the integer argument.
2901e3b90b0SYuri PankovThe remaining groups of characters in the format string consist of a bit number
291*c3b39789SHans Rosenfeld.Pq between 1 and 32, also encoded as a control character ,
2921e3b90b0SYuri Pankovand the next characters
293*c3b39789SHans Rosenfeld.Pq up to the next control character, or space, or '\e0'
294*c3b39789SHans Rosenfeldgive the name of the bit.
295*c3b39789SHans RosenfeldAs the bit number 32 encoded as a control character is the same as a space
296*c3b39789SHans Rosenfeldcharacter, the names of the bits cannot contain a space.
297*c3b39789SHans RosenfeldThe strings corresponding to the bits set in the integer argument are printed
298*c3b39789SHans Rosenfeldafter the numerical value, enclosed in angle brackets and separated by commata.
2991e3b90b0SYuri Pankov.It Sy p
3001e3b90b0SYuri PankovThe argument is taken to be a pointer; the value of the pointer is printed in
3011e3b90b0SYuri Pankovunsigned hexadecimal.
3021e3b90b0SYuri PankovThe print format is equivalent to
3031e3b90b0SYuri Pankov.Sy %lx .
3041e3b90b0SYuri PankovTo avoid lint warnings, cast pointers to type
3051e3b90b0SYuri Pankov.Ft "void *"
3061e3b90b0SYuri Pankovwhen using the
3071e3b90b0SYuri Pankov.Sy %p
3081e3b90b0SYuri Pankovformat specifier.
3091e3b90b0SYuri Pankov.It Sy s
3101e3b90b0SYuri PankovThe argument is taken to be a string
3111e3b90b0SYuri Pankov.Pq character pointer ,
3121e3b90b0SYuri Pankovand characters from the string are printed until a null character is ecountered.
3131e3b90b0SYuri PankovIf the character pointer is
3141e3b90b0SYuri Pankov.Sy NULL ,
3151e3b90b0SYuri Pankovthe string
3161e3b90b0SYuri Pankov.Qq <null string>
3171e3b90b0SYuri Pankovis used in its place.
3181e3b90b0SYuri Pankov.It Sy %
3191e3b90b0SYuri PankovCopy a
3201e3b90b0SYuri Pankov.Sy % ;
3211e3b90b0SYuri Pankovno argument is converted.
3221e3b90b0SYuri Pankov.El
3231e3b90b0SYuri Pankov.Sh CONTEXT
3241e3b90b0SYuri PankovThese functions can be called from user, kernel, interrupt, or
3251e3b90b0SYuri Pankovhigh-level interrupt context.
3261e3b90b0SYuri Pankov.Sh RETURN VALUES
3271e3b90b0SYuri Pankov.Fn sprintf
3281e3b90b0SYuri Pankovand
3291e3b90b0SYuri Pankov.Fn vsprintf
3301e3b90b0SYuri Pankovreturn
3311e3b90b0SYuri Pankov.Fa s .
3321e3b90b0SYuri Pankov.Pp
3331e3b90b0SYuri Pankov.Fn snprintf
3341e3b90b0SYuri Pankovand
3351e3b90b0SYuri Pankov.Fn vsnprintf
3361e3b90b0SYuri Pankovreturn the number of bytes that would have been written to
3371e3b90b0SYuri Pankov.Fa s
3381e3b90b0SYuri Pankovif
3391e3b90b0SYuri Pankov.Fa n
3401e3b90b0SYuri Pankovhad been sufficiently large
3411e3b90b0SYuri Pankov.Pq excluding the terminating null byte .
3421e3b90b0SYuri Pankov.Sh SEE ALSO
3431e3b90b0SYuri Pankov.Xr cmn_err 9F ,
3441e3b90b0SYuri Pankov.Xr va_arg 9F ,
3451e3b90b0SYuri Pankov.Xr va_end 9F ,
3461e3b90b0SYuri Pankov.Xr va_start 9F
347