xref: /illumos-gate/usr/src/man/man9f/sprintf.9f (revision 1e3b90b08c46284a9abda475fbd0124dd2689de5)
1*1e3b90b0SYuri Pankov.\"
2*1e3b90b0SYuri Pankov.\" The contents of this file are subject to the terms of the
3*1e3b90b0SYuri Pankov.\" Common Development and Distribution License (the "License").
4*1e3b90b0SYuri Pankov.\" You may not use this file except in compliance with the License.
5*1e3b90b0SYuri Pankov.\"
6*1e3b90b0SYuri Pankov.\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
7*1e3b90b0SYuri Pankov.\" or http://www.opensolaris.org/os/licensing.
8*1e3b90b0SYuri Pankov.\" See the License for the specific language governing permissions
9*1e3b90b0SYuri Pankov.\" and limitations under the License.
10*1e3b90b0SYuri Pankov.\"
11*1e3b90b0SYuri Pankov.\" When distributing Covered Code, include this CDDL HEADER in each
12*1e3b90b0SYuri Pankov.\" file and include the License file at usr/src/OPENSOLARIS.LICENSE.
13*1e3b90b0SYuri Pankov.\" If applicable, add the following below this CDDL HEADER, with the
14*1e3b90b0SYuri Pankov.\" fields enclosed by brackets "[]" replaced with your own identifying
15*1e3b90b0SYuri Pankov.\" information: Portions Copyright [yyyy] [name of copyright owner]
16*1e3b90b0SYuri Pankov.\"
17*1e3b90b0SYuri Pankov.\"
18*1e3b90b0SYuri Pankov.\" Copyright 1989 AT&T
19*1e3b90b0SYuri Pankov.\" Copyright (c) 2006, Sun Microsystems, Inc. All Rights Reserved.
20*1e3b90b0SYuri Pankov.\" Copyright 2016 Nexenta Systems, Inc.
21*1e3b90b0SYuri Pankov.\"
22*1e3b90b0SYuri Pankov.Dd Dec 1, 2016
23*1e3b90b0SYuri Pankov.Dt SPRINTF 9F
24*1e3b90b0SYuri Pankov.Os
25*1e3b90b0SYuri Pankov.Sh NAME
26*1e3b90b0SYuri Pankov.Nm sprintf, snprintf, vsprintf, vsnprintf
27*1e3b90b0SYuri Pankov.Nd format characters in memory
28*1e3b90b0SYuri Pankov.Sh SYNOPSIS
29*1e3b90b0SYuri Pankov.In sys/cmn_err.h
30*1e3b90b0SYuri Pankov.In sys/ddi.h
31*1e3b90b0SYuri Pankov.In sys/sunddi.h
32*1e3b90b0SYuri Pankov.Ft "char *"
33*1e3b90b0SYuri Pankov.Fo sprintf
34*1e3b90b0SYuri Pankov.Fa "char *s"
35*1e3b90b0SYuri Pankov.Fa "const char *format"
36*1e3b90b0SYuri Pankov.Fa ...
37*1e3b90b0SYuri Pankov.Fc
38*1e3b90b0SYuri Pankov.Ft size_t
39*1e3b90b0SYuri Pankov.Fo snprintf
40*1e3b90b0SYuri Pankov.Fa "char *s"
41*1e3b90b0SYuri Pankov.Fa "size_t n"
42*1e3b90b0SYuri Pankov.Fa "const char *format"
43*1e3b90b0SYuri Pankov.Fa ...
44*1e3b90b0SYuri Pankov.Fc
45*1e3b90b0SYuri Pankov.In sys/varargs.h
46*1e3b90b0SYuri Pankov.Ft "char *"
47*1e3b90b0SYuri Pankov.Fo vsprintf
48*1e3b90b0SYuri Pankov.Fa "char *s"
49*1e3b90b0SYuri Pankov.Fa "const char *format"
50*1e3b90b0SYuri Pankov.Fa "va_list ap"
51*1e3b90b0SYuri Pankov.Fc
52*1e3b90b0SYuri Pankov.Ft size_t
53*1e3b90b0SYuri Pankov.Fo vsnprintf
54*1e3b90b0SYuri Pankov.Fa "char *s"
55*1e3b90b0SYuri Pankov.Fa "size_t n"
56*1e3b90b0SYuri Pankov.Fa "const char *format"
57*1e3b90b0SYuri Pankov.Fa "va_list ap"
58*1e3b90b0SYuri Pankov.Fc
59*1e3b90b0SYuri Pankov.Sh INTERFACE LEVEL
60*1e3b90b0SYuri Pankovillumos DDI specific
61*1e3b90b0SYuri Pankov.Sh PARAMETERS
62*1e3b90b0SYuri Pankov.Bl -tag -width Ds
63*1e3b90b0SYuri Pankov.It Fa s
64*1e3b90b0SYuri PankovPointer to a character string.
65*1e3b90b0SYuri Pankov.It Fa n
66*1e3b90b0SYuri PankovSize of the buffer pointed to by
67*1e3b90b0SYuri Pankov.Fa s .
68*1e3b90b0SYuri Pankov.It Fa format
69*1e3b90b0SYuri PankovPointer to a character string.
70*1e3b90b0SYuri Pankov.It Fa ap
71*1e3b90b0SYuri PankovPointer to a variable argument list.
72*1e3b90b0SYuri Pankov.El
73*1e3b90b0SYuri Pankov.Sh DESCRIPTION
74*1e3b90b0SYuri PankovThe
75*1e3b90b0SYuri Pankov.Fn sprintf
76*1e3b90b0SYuri Pankovfunction places output, followed by the null byte
77*1e3b90b0SYuri Pankov.Pq \e0 ,
78*1e3b90b0SYuri Pankovin consecutive bytes starting at
79*1e3b90b0SYuri Pankov.Fa s ;
80*1e3b90b0SYuri Pankovit is the user's responsibility to ensure that enough storage is available.
81*1e3b90b0SYuri Pankov.Pp
82*1e3b90b0SYuri PankovThe
83*1e3b90b0SYuri Pankov.Fn snprintf
84*1e3b90b0SYuri Pankovfunction is identical to
85*1e3b90b0SYuri Pankov.Fn sprintf
86*1e3b90b0SYuri Pankovwith the addition of the argument
87*1e3b90b0SYuri Pankov.Fa n ,
88*1e3b90b0SYuri Pankovwhich specifies the size of the buffer referred to by
89*1e3b90b0SYuri Pankov.Fa s .
90*1e3b90b0SYuri PankovIf
91*1e3b90b0SYuri Pankov.Fa n
92*1e3b90b0SYuri Pankovis 0, nothing is written and
93*1e3b90b0SYuri Pankov.Fa s
94*1e3b90b0SYuri Pankovcan be a null pointer.
95*1e3b90b0SYuri PankovOtherwise, output bytes beyond the
96*1e3b90b0SYuri Pankov.Fa n Ns \-1st
97*1e3b90b0SYuri Pankovare discarded instead of being written to the array and a null byte is written
98*1e3b90b0SYuri Pankovat the end of the bytes actually written into the array.
99*1e3b90b0SYuri Pankov.Pp
100*1e3b90b0SYuri PankovThe
101*1e3b90b0SYuri Pankov.Fn vsprintf
102*1e3b90b0SYuri Pankovand
103*1e3b90b0SYuri Pankov.Fn vsnprintf
104*1e3b90b0SYuri Pankovfunctions are the same as
105*1e3b90b0SYuri Pankov.Fn sprintf
106*1e3b90b0SYuri Pankovand
107*1e3b90b0SYuri Pankov.Fn snprintf ,
108*1e3b90b0SYuri Pankovrespectively, except that instead of being called with a variable number of
109*1e3b90b0SYuri Pankovarguments, they are called with an argument list,
110*1e3b90b0SYuri Pankov.Fa ap ,
111*1e3b90b0SYuri Pankovused by the conversion specifications in
112*1e3b90b0SYuri Pankov.Fa format .
113*1e3b90b0SYuri Pankov.Fa ap
114*1e3b90b0SYuri Pankovis a variable argument list and must be initialized by calling
115*1e3b90b0SYuri Pankov.Xr va_start 9F .
116*1e3b90b0SYuri Pankov.Xr va_end 9F
117*1e3b90b0SYuri Pankovis used to clean up and must be called after each traversal of the list.
118*1e3b90b0SYuri PankovMultiple traversals of the argument list, each bracketed by
119*1e3b90b0SYuri Pankov.Xr va_start 9F
120*1e3b90b0SYuri Pankovand
121*1e3b90b0SYuri Pankov.Xr va_end 9F ,
122*1e3b90b0SYuri Pankovare possible.
123*1e3b90b0SYuri Pankov.Pp
124*1e3b90b0SYuri PankovEach of these functions converts, formats, and prints its arguments under
125*1e3b90b0SYuri Pankovcontrol of the
126*1e3b90b0SYuri Pankov.Fa format .
127*1e3b90b0SYuri PankovThe
128*1e3b90b0SYuri Pankov.Fa format
129*1e3b90b0SYuri Pankovis composed of zero or more directives: ordinary characters, which are simply
130*1e3b90b0SYuri Pankovcopied to the output stream and conversion specifications, each of which results
131*1e3b90b0SYuri Pankovin the fetching of zero or more arguments.
132*1e3b90b0SYuri PankovThe results are undefined if there are insufficient arguments for the
133*1e3b90b0SYuri Pankov.Fa format .
134*1e3b90b0SYuri PankovIf the
135*1e3b90b0SYuri Pankov.Fa format
136*1e3b90b0SYuri Pankovis exhausted while arguments remain, the excess arguments are evaluated but are
137*1e3b90b0SYuri Pankovotherwise ignored.
138*1e3b90b0SYuri Pankov.Ss Conversion Specifications
139*1e3b90b0SYuri PankovEach conversion specification is introduced by the
140*1e3b90b0SYuri Pankov.Qq Sy %
141*1e3b90b0SYuri Pankovcharacter after which the following appear in sequence:
142*1e3b90b0SYuri Pankov.Bl -bullet
143*1e3b90b0SYuri Pankov.It
144*1e3b90b0SYuri PankovZero or more flags
145*1e3b90b0SYuri Pankov.Pq in any order ,
146*1e3b90b0SYuri Pankovwhich modify the meaning of the conversion specification.
147*1e3b90b0SYuri Pankov.It
148*1e3b90b0SYuri PankovAn optional minimum field width.
149*1e3b90b0SYuri PankovIf the converted value has fewer bytes than the field width, it will be padded
150*1e3b90b0SYuri Pankovwith spaces by default on the left; it will be padded on the right, if the
151*1e3b90b0SYuri Pankovleft-adjustment flag
152*1e3b90b0SYuri Pankov.Pq Qq Sy ‐ ,
153*1e3b90b0SYuri Pankovdescribed below, is given to the field width.
154*1e3b90b0SYuri PankovThe field width takes the form of an asterisk
155*1e3b90b0SYuri Pankov.Pq Qq Sy * ,
156*1e3b90b0SYuri Pankovdescribed below, or a decimal integer.
157*1e3b90b0SYuri Pankov.It
158*1e3b90b0SYuri PankovAn optional precision that gives the minimum number of digits to appear for the
159*1e3b90b0SYuri Pankov.Sy d , D , o , O , x , X ,
160*1e3b90b0SYuri Pankovor
161*1e3b90b0SYuri Pankov.Sy u
162*1e3b90b0SYuri Pankovconversions
163*1e3b90b0SYuri Pankov.Pq the field is padded with leading zeros ;
164*1e3b90b0SYuri Pankovor the maximum number of bytes to be printed from a string in s conversion.
165*1e3b90b0SYuri PankovThe precision takes the form of a period
166*1e3b90b0SYuri Pankov.Pq Qq Sy \&.
167*1e3b90b0SYuri Pankovfollowed either by an asterisk
168*1e3b90b0SYuri Pankov.Pq Qq Sy * ,
169*1e3b90b0SYuri Pankovdescribed below, or an optional decimal digit string, where a null digit string
170*1e3b90b0SYuri Pankovis treated as 0.
171*1e3b90b0SYuri PankovIf a precision appears with any other conversion specifier, the behavior is
172*1e3b90b0SYuri Pankovundefined.
173*1e3b90b0SYuri Pankov.It
174*1e3b90b0SYuri PankovAn optional length modifier that specified the size of the argument.
175*1e3b90b0SYuri Pankov.It
176*1e3b90b0SYuri PankovA conversion specifier that indicates the type of conversion to be applied.
177*1e3b90b0SYuri Pankov.El
178*1e3b90b0SYuri Pankov.Pp
179*1e3b90b0SYuri PankovA field width, or precision, or both can be indicated by an asterisk
180*1e3b90b0SYuri Pankov.Pq Qq Sy * .
181*1e3b90b0SYuri PankovIn this case, an argument of type int supplies the field width or precision.
182*1e3b90b0SYuri PankovArguments specifying field width, or precision, or both must appear in that
183*1e3b90b0SYuri Pankovorder before the argument, if any, to be converted.
184*1e3b90b0SYuri PankovA negative field width is taken as a
185*1e3b90b0SYuri Pankov.Qq Sy \-
186*1e3b90b0SYuri Pankovflag followed by a positive field width.
187*1e3b90b0SYuri PankovA negative precision is taken as if the precision were omitted.
188*1e3b90b0SYuri Pankov.Ss Flag Characters
189*1e3b90b0SYuri PankovThe flag characters and their meanings are:
190*1e3b90b0SYuri Pankov.Bl -tag -width Ds
191*1e3b90b0SYuri Pankov.It Sy \-
192*1e3b90b0SYuri PankovThe result of the conversion will be left-justified within the field.
193*1e3b90b0SYuri PankovThe conversion will be right-justified if this flag is not specified.
194*1e3b90b0SYuri Pankov.It Sy 0
195*1e3b90b0SYuri PankovFor
196*1e3b90b0SYuri Pankov.Sy d , D , o , O , x , X ,
197*1e3b90b0SYuri Pankovor
198*1e3b90b0SYuri Pankov.Sy u
199*1e3b90b0SYuri Pankovconversions, leading zeros
200*1e3b90b0SYuri Pankov.Pq following any indication of sign or base
201*1e3b90b0SYuri Pankovare used to pad to the field width; no space padding is performed.
202*1e3b90b0SYuri PankovIf the
203*1e3b90b0SYuri Pankov.Sy 0
204*1e3b90b0SYuri Pankovand
205*1e3b90b0SYuri Pankov.Sy \-
206*1e3b90b0SYuri Pankovflags both appear, the
207*1e3b90b0SYuri Pankov.Sy 0
208*1e3b90b0SYuri Pankovflag will be ignored.
209*1e3b90b0SYuri PankovIf a precision is specified, the
210*1e3b90b0SYuri Pankov.Sy 0
211*1e3b90b0SYuri Pankovflag will be ignored.
212*1e3b90b0SYuri PankovFor other conversions, the behavior is undefined.
213*1e3b90b0SYuri Pankov.El
214*1e3b90b0SYuri Pankov.Ss Length Modifiers
215*1e3b90b0SYuri PankovThe length modifiers and their meanings are:
216*1e3b90b0SYuri Pankov.Bl -tag -width Ds
217*1e3b90b0SYuri Pankov.It Sy h
218*1e3b90b0SYuri PankovSpecifies that a following
219*1e3b90b0SYuri Pankov.Sy d , D , o , O , x , X ,
220*1e3b90b0SYuri Pankovor
221*1e3b90b0SYuri Pankov.Sy u
222*1e3b90b0SYuri Pankovconversion specifier applies to a short or unsigned
223*1e3b90b0SYuri Pankovshort argument
224*1e3b90b0SYuri Pankov.Po the argument will have been promoted according to the integer promotions,
225*1e3b90b0SYuri Pankovbut its value will be converted to short or unsigned short before printing
226*1e3b90b0SYuri Pankov.Pc .
227*1e3b90b0SYuri Pankov.It Sy hh
228*1e3b90b0SYuri PankovSpecifies that a following
229*1e3b90b0SYuri Pankov.Sy d , D , o , O , x , X ,
230*1e3b90b0SYuri Pankovor
231*1e3b90b0SYuri Pankov.Sy u
232*1e3b90b0SYuri Pankovconversion specifier applies to a signed char or unsigned char argument
233*1e3b90b0SYuri Pankov.Po the argument will have been promoted according to the integer promotions,
234*1e3b90b0SYuri Pankovbut its value will be converted to signed char or unsigned char before printing
235*1e3b90b0SYuri Pankov.Pc .
236*1e3b90b0SYuri Pankov.It Sy l
237*1e3b90b0SYuri PankovSpecifies that a following
238*1e3b90b0SYuri Pankov.Sy d , D , o , O , x , X ,
239*1e3b90b0SYuri Pankovor
240*1e3b90b0SYuri Pankov.Sy u
241*1e3b90b0SYuri Pankovconversion specifier applies to a long or unsigned long argument.
242*1e3b90b0SYuri Pankov.It Sy ll
243*1e3b90b0SYuri PankovSpecifies that a following
244*1e3b90b0SYuri Pankov.Sy d , D , o , O , x , X ,
245*1e3b90b0SYuri Pankovor
246*1e3b90b0SYuri Pankov.Sy u
247*1e3b90b0SYuri Pankovconversion specifier applies to a long long or unsigned long long argument.
248*1e3b90b0SYuri Pankov.El
249*1e3b90b0SYuri Pankov.Ss Conversion Specifiers
250*1e3b90b0SYuri PankovEach conversion specifier results in fetching zero or more arguments.
251*1e3b90b0SYuri PankovThe results are undefined if there are insufficient arguments for the
252*1e3b90b0SYuri Pankov.Fa format .
253*1e3b90b0SYuri PankovIf the
254*1e3b90b0SYuri Pankov.Fa format
255*1e3b90b0SYuri Pankovis exhausted while arguments remain, the excess arguments are ignored.
256*1e3b90b0SYuri Pankov.Pp
257*1e3b90b0SYuri PankovThe conversion specifiers and their meanings are:
258*1e3b90b0SYuri Pankov.Bl -tag -width Ds
259*1e3b90b0SYuri Pankov.It Sy d , D , o , O , x , X , u
260*1e3b90b0SYuri PankovThe integer argument is converted to signed decimal
261*1e3b90b0SYuri Pankov.Pq Sy d , D ,
262*1e3b90b0SYuri Pankovunsigned octal
263*1e3b90b0SYuri Pankov.Pq Sy o , O ,
264*1e3b90b0SYuri Pankovunsigned hexadecimal
265*1e3b90b0SYuri Pankov.Pq Sy x , X ,
266*1e3b90b0SYuri Pankovor unsigned decimal
267*1e3b90b0SYuri Pankov.Pq Sy u ,
268*1e3b90b0SYuri Pankovrespectively.
269*1e3b90b0SYuri PankovThe letters
270*1e3b90b0SYuri Pankov.Qq Sy abcdef
271*1e3b90b0SYuri Pankovare used for
272*1e3b90b0SYuri Pankov.Sy x
273*1e3b90b0SYuri Pankovand letters
274*1e3b90b0SYuri Pankov.Qq Sy ABCDEF
275*1e3b90b0SYuri Pankovfor
276*1e3b90b0SYuri Pankov.Sy X
277*1e3b90b0SYuri Pankovconversions.
278*1e3b90b0SYuri Pankov.It Sy c
279*1e3b90b0SYuri PankovThe character value of the argument is printed.
280*1e3b90b0SYuri Pankov.It Sy b
281*1e3b90b0SYuri PankovThe
282*1e3b90b0SYuri Pankov.Sy %b
283*1e3b90b0SYuri Pankovconversion specification allows bit values to be printed meaningfully.
284*1e3b90b0SYuri PankovEach
285*1e3b90b0SYuri Pankov.Sy %b
286*1e3b90b0SYuri Pankovtakes an integer value and a format string from the argument list.
287*1e3b90b0SYuri PankovThe first character of the format string should be the output base encoded as a
288*1e3b90b0SYuri Pankovcontrol character.
289*1e3b90b0SYuri PankovThis base is used to print the integer argument.
290*1e3b90b0SYuri PankovThe remaining groups of characters in the format string consist of a bit number
291*1e3b90b0SYuri Pankov.Pq between 1 and 32, also encoded as a control character
292*1e3b90b0SYuri Pankovand the next characters
293*1e3b90b0SYuri Pankov.Pq up to the next control character or '\e0'
294*1e3b90b0SYuri Pankovgive the name of the bit field.
295*1e3b90b0SYuri PankovThe string corresponding to the bit fields set in the integer argument is
296*1e3b90b0SYuri Pankovprinted after the numerical value.
297*1e3b90b0SYuri Pankov.It Sy p
298*1e3b90b0SYuri PankovThe argument is taken to be a pointer; the value of the pointer is printed in
299*1e3b90b0SYuri Pankovunsigned hexadecimal.
300*1e3b90b0SYuri PankovThe print format is equivalent to
301*1e3b90b0SYuri Pankov.Sy %lx .
302*1e3b90b0SYuri PankovTo avoid lint warnings, cast pointers to type
303*1e3b90b0SYuri Pankov.Ft "void *"
304*1e3b90b0SYuri Pankovwhen using the
305*1e3b90b0SYuri Pankov.Sy %p
306*1e3b90b0SYuri Pankovformat specifier.
307*1e3b90b0SYuri Pankov.It Sy s
308*1e3b90b0SYuri PankovThe argument is taken to be a string
309*1e3b90b0SYuri Pankov.Pq character pointer ,
310*1e3b90b0SYuri Pankovand characters from the string are printed until a null character is ecountered.
311*1e3b90b0SYuri PankovIf the character pointer is
312*1e3b90b0SYuri Pankov.Sy NULL ,
313*1e3b90b0SYuri Pankovthe string
314*1e3b90b0SYuri Pankov.Qq <null string>
315*1e3b90b0SYuri Pankovis used in its place.
316*1e3b90b0SYuri Pankov.It Sy %
317*1e3b90b0SYuri PankovCopy a
318*1e3b90b0SYuri Pankov.Sy % ;
319*1e3b90b0SYuri Pankovno argument is converted.
320*1e3b90b0SYuri Pankov.El
321*1e3b90b0SYuri Pankov.Sh CONTEXT
322*1e3b90b0SYuri PankovThese functions can be called from user, kernel, interrupt, or
323*1e3b90b0SYuri Pankovhigh-level interrupt context.
324*1e3b90b0SYuri Pankov.Sh RETURN VALUES
325*1e3b90b0SYuri Pankov.Fn sprintf
326*1e3b90b0SYuri Pankovand
327*1e3b90b0SYuri Pankov.Fn vsprintf
328*1e3b90b0SYuri Pankovreturn
329*1e3b90b0SYuri Pankov.Fa s .
330*1e3b90b0SYuri Pankov.Pp
331*1e3b90b0SYuri Pankov.Fn snprintf
332*1e3b90b0SYuri Pankovand
333*1e3b90b0SYuri Pankov.Fn vsnprintf
334*1e3b90b0SYuri Pankovreturn the number of bytes that would have been written to
335*1e3b90b0SYuri Pankov.Fa s
336*1e3b90b0SYuri Pankovif
337*1e3b90b0SYuri Pankov.Fa n
338*1e3b90b0SYuri Pankovhad been sufficiently large
339*1e3b90b0SYuri Pankov.Pq excluding the terminating null byte .
340*1e3b90b0SYuri Pankov.Sh SEE ALSO
341*1e3b90b0SYuri Pankov.Xr cmn_err 9F ,
342*1e3b90b0SYuri Pankov.Xr va_arg 9F ,
343*1e3b90b0SYuri Pankov.Xr va_end 9F ,
344*1e3b90b0SYuri Pankov.Xr va_start 9F
345