27 .\" The contents of this file are subject to the terms of the
28 .\" Common Development and Distribution License (the "License").
29 .\" You may not use this file except in compliance with the License.
30 .\"
31 .\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
32 .\" or http://www.opensolaris.org/os/licensing.
33 .\" See the License for the specific language governing permissions
34 .\" and limitations under the License.
35 .\"
36 .\" When distributing Covered Code, include this CDDL HEADER in each
37 .\" file and include the License file at usr/src/OPENSOLARIS.LICENSE.
38 .\" If applicable, add the following below this CDDL HEADER, with the
39 .\" fields enclosed by brackets "[]" replaced with your own identifying
40 .\" information: Portions Copyright [yyyy] [name of copyright owner]
41 .\"
42 .\"
43 .\" Copyright 1989 AT&T
44 .\" Copyright (c) 2001, The IEEE and The Open Group. All Rights Reserved.
45 .\" Copyright (c) 2009, Sun Microsystems, Inc. All Rights Reserved.
46 .\"
47 .TH VPRINTF 3C "Jan 7, 2009"
48 .SH NAME
49 vprintf, vfprintf, vsprintf, vsnprintf, vasprintf \- print formatted output of
50 a variable argument list
51 .SH SYNOPSIS
52 .LP
53 .nf
54 #include <stdio.h>
55 #include <stdarg.h>
56
57 \fBint\fR \fBvprintf\fR(\fBconst char *\fR\fIformat\fR, \fBva_list\fR \fIap\fR);
58 .fi
59
60 .LP
61 .nf
62 \fBint\fR \fBvfprintf\fR(\fBFILE *\fR\fIstream\fR, \fBconst char *\fR\fIformat\fR, \fBva_list\fR \fIap\fR);
63 .fi
64
65 .LP
66 .nf
67 \fBint\fR \fBvsprintf\fR(\fBchar *\fR\fIs\fR, \fBconst char *\fR\fIformat\fR, \fBva_list\fR \fIap\fR);
68 .fi
69
70 .LP
71 .nf
72 \fBint\fR \fBvsnprintf\fR(\fBchar *\fR\fIs\fR, \fBsize_t\fR \fIn\fR, \fBconst char *\fR\fIformat\fR, \fBva_list\fR \fIap\fR);
73 .fi
74
75 .LP
76 .nf
77 \fBint\fR \fBvasprintf\fR(\fBchar **\fR\fIret\fR, \fBconst char *\fR\fIformat\fR, \fBva_list\fR \fIap\fR);
78 .fi
79
80 .SH DESCRIPTION
81 .sp
82 .LP
83 The \fBvprintf()\fR, \fBvfprintf()\fR, \fBvsprintf()\fR, \fBvsnprintf()\fR, and
84 \fBvasprintf()\fR functions are the same as \fBprintf()\fR, \fBfprintf()\fR,
85 \fBsprintf()\fR, \fBsnprintf()\fR, and \fBasprintf()\fR, respectively, except
86 that instead of being called with a variable number of arguments, they are
87 called with an argument list as defined in the \fB<stdarg.h>\fR header. See
88 \fBprintf\fR(3C).
89 .sp
90 .LP
91 The \fB<stdarg.h>\fR header defines the type \fBva_list\fR and a set of macros
92 for advancing through a list of arguments whose number and types may vary. The
93 argument \fIap\fR to the \fBvprint\fR family of functions is of type
94 \fBva_list\fR. This argument is used with the <\fBstdarg.h\fR> header file
95 macros \fBva_start()\fR, \fBva_arg()\fR, and \fBva_end()\fR (see
96 \fBstdarg\fR(3EXT)). The \fBEXAMPLES\fR section below demonstrates the use of
97 \fBva_start()\fR and \fBva_end()\fR with \fBvprintf()\fR.
98 .sp
99 .LP
100 The macro \fBva_alist()\fR is used as the parameter list in a function
101 definition, as in the function called \fBerror()\fR in the example below. The
102 macro \fBva_start(\fR\fIap, name\fR\fB),\fR where \fIap\fR is of type
103 \fBva_list\fR and \fIname\fR is the rightmost parameter (just before
104 \|.\|.\|.), must be called before any attempt to traverse and access unnamed
105 arguments is made. The \fBva_end(\fR\fIap\fR\fB)\fR macro must be invoked when
106 all desired arguments have been accessed. The argument list in \fIap\fR can be
107 traversed again if \fBva_start()\fR is called again after \fBva_end()\fR. In
108 the example below, the \fBerror()\fR arguments (\fIarg1\fR, \fIarg2\fR,
109 \&.\|.\|.) are passed to \fBvfprintf()\fR in the argument \fIap\fR.
110 .SH RETURN VALUES
111 .sp
112 .LP
113 Refer to \fBprintf\fR(3C).
114 .SH ERRORS
115 .sp
116 .LP
117 The \fBvprintf()\fR and \fBvfprintf()\fR functions will fail if either the
118 \fIstream\fR is unbuffered or the \fIstream\fR's buffer needed to be flushed
119 and:
120 .sp
121 .ne 2
122 .na
123 \fB\fBEFBIG\fR\fR
124 .ad
125 .RS 9n
126 The file is a regular file and an attempt was made to write at or beyond the
127 offset maximum.
128 .RE
129
130 .SH EXAMPLES
131 .LP
132 \fBExample 1 \fRUsing \fBvprintf()\fR to write an error routine.
133 .sp
134 .LP
135 The following demonstrates how \fBvfprintf()\fR could be used to write an error
136 routine:
137
138 .sp
139 .in +2
140 .nf
141 #include <stdio.h>
142 #include <stdarg.h>
143 \&.\|.\|.
144 /*
145 * error should be called like
146 * error(function_name, format, arg1, \&.\|.\|.);
147 */
148 void error(char *function_name, char *format, \&.\|.\|.)
149 {
150 va_list ap;
151 va_start(ap, format);
152 /* print out name of function causing error */
153 (void) fprintf(stderr, "ERR in %s: ", function_name);
154 /* print out remainder of message */
155 (void) vfprintf(stderr, format, ap);
156 va_end(ap);
157 (void) abort();
158 }
159 .fi
160 .in -2
161
162 .SH ATTRIBUTES
163 .sp
164 .LP
165 See \fBattributes\fR(5) for descriptions of the following attributes:
166 .sp
167
168 .sp
169 .TS
170 box;
171 c | c
172 l | l .
173 ATTRIBUTE TYPE ATTRIBUTE VALUE
174 _
175 Interface Stability Committed
176 _
177 MT-Level See below.
178 _
179 Standard See below.
180 .TE
181
182 .sp
183 .LP
184 All of these functions can be used safely in multithreaded applications, as
185 long as \fBsetlocale\fR(3C) is not being called to change the locale.
186 .sp
187 .LP
188 See \fBstandards\fR(5) for the standards conformance of \fBvprintf()\fR,
189 \fBvfprintf()\fR, \fBvsprintf()\fR, and \fBvsnprintf()\fR. The
190 \fBvasprintf()\fR function is modeled on the one that appears in the FreeBSD,
191 NetBSD, and GNU C libraries.
192 .SH SEE ALSO
193 .sp
194 .LP
195 \fBprintf\fR(3C), \fBattributes\fR(5), \fBstdarg\fR(3EXT), \fBattributes\fR(5),
196 \fBstandards\fR(5)
197 .SH NOTES
198 .sp
199 .LP
200 The \fBvsnprintf()\fR return value when \fIn\fR = 0 was changed in the Solaris
201 10 release. The change was based on the SUSv3 specification. The previous
202 behavior was based on the initial SUSv2 specification, where \fBvsnprintf()\fR
203 when \fIn\fR = 0 returns an unspecified value less than 1.
|
27 .\" The contents of this file are subject to the terms of the
28 .\" Common Development and Distribution License (the "License").
29 .\" You may not use this file except in compliance with the License.
30 .\"
31 .\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
32 .\" or http://www.opensolaris.org/os/licensing.
33 .\" See the License for the specific language governing permissions
34 .\" and limitations under the License.
35 .\"
36 .\" When distributing Covered Code, include this CDDL HEADER in each
37 .\" file and include the License file at usr/src/OPENSOLARIS.LICENSE.
38 .\" If applicable, add the following below this CDDL HEADER, with the
39 .\" fields enclosed by brackets "[]" replaced with your own identifying
40 .\" information: Portions Copyright [yyyy] [name of copyright owner]
41 .\"
42 .\"
43 .\" Copyright 1989 AT&T
44 .\" Copyright (c) 2001, The IEEE and The Open Group. All Rights Reserved.
45 .\" Copyright (c) 2009, Sun Microsystems, Inc. All Rights Reserved.
46 .\"
47 .Dd July 10, 2020
48 .Dt VPRINTF 3C
49 .Os
50 .Sh NAME
51 .Nm vprintf ,
52 .Nm vfprintf ,
53 .Nm vsprintf ,
54 .Nm vsnprintf ,
55 .Nm vasprintf
56 .Nd print formatted output of a variable argument list
57 .Sh LIBRARY
58 .Lb libc
59 .Sh SYNOPSIS
60 .In stdarg.h
61 .In stdio.h
62 .Ft int
63 .Fo vprintf
64 .Fa "const char *format"
65 .Fa va_list
66 .Fc
67 .Ft int
68 .Fo vfprintf
69 .Fa "FILE *stream"
70 .Fa "const char *format"
71 .Fa "va_list ap"
72 .Fc
73 .Ft int
74 .Fo vsprintf
75 .Fa "char *s"
76 .Fa "const char *format"
77 .Fa "va_list ap"
78 .Fc
79 .Ft int
80 .Fo vsnprintf
81 .Fa "char *s"
82 .Fa "size_t n"
83 .Fa "const char *format"
84 .Fa "va_list ap"
85 .Fc
86 .Ft int
87 .Fo vasprintf
88 .Fa "char **ret"
89 .Fa "const char *format"
90 .Fa "va_list ap"
91 .Fc
92 .Sh DESCRIPTION
93 The
94 .Fn vprintf ,
95 .Fn vfprintf ,
96 .Fn vsprintf ,
97 .Fn vsnprintf ,
98 and
99 .Fn vasprintf
100 functions are the same as
101 .Fn printf ,
102 .Fn fprintf ,
103 .Fn sprintf ,
104 .Fn snprintf ,
105 and
106 .Fn asprintf ,
107 respectively, except that instead of being called with a variable number of
108 arguments, they are called with an argument list as defined in the
109 .In stdarg.h
110 header.
111 See
112 .Xr printf 3C .
113 .Pp
114 The
115 .In stdarg.h
116 header defines the type
117 .Vt va_list
118 and a set of macros for advancing through a list of arguments whose number and
119 types may vary.
120 The argument
121 .Fa ap
122 to the vprint family of functions is of type
123 .Vt va_list .
124 This argument is used with the
125 .In stdarg.h
126 header file macros
127 .Fn va_start ,
128 .Fn va_arg ,
129 and
130 .Fn va_end
131 .Pq see Xr stdarg 3EXT .
132 The
133 .Sx EXAMPLES
134 section below demonstrates the use of
135 .Fn va_start
136 and
137 .Fn va_end
138 with
139 .Fn vprintf .
140 .Pp
141 The macro
142 .Fn va_alist
143 is used as the parameter list in a function definition, as in the function
144 called
145 .Fn error
146 in the example below.
147 The macro
148 .Ql va_start(ap, name) ,
149 where
150 .Va ap
151 is of type
152 .Vt va_list
153 and
154 .Va name
155 is the rightmost parameter (just before ...), must be called before any attempt
156 to traverse and access unnamed arguments is made.
157 The
158 .Ql va_end(ap)
159 macro must be invoked when all desired arguments have been accessed.
160 The argument list in
161 .Va ap
162 can be traversed again if
163 .Fn va_start
164 is called again after
165 .Fn va_end .
166 In the example below, the
167 .Fn error
168 arguments (arg1, arg2, ...) are passed to
169 .Fn vfprintf
170 in the argument
171 .Va ap .
172 .Sh RETURN VALUES
173 Refer to
174 .Xr printf 3C .
175 .Sh EXAMPLES
176 .Sy Example 1
177 Using
178 .Fn vprintf
179 to write an error routine.
180 .Pp
181 The following demonstrates how
182 .Fn vfprintf
183 could be used to write an error routine:
184 .Bd -literal
185 #include <stdarg.h>
186 #include <stdio.h>
187
188 /*
189 * error should be called like
190 * error(function_name, format, arg1, ...);
191 */
192 void
193 error(char *function_name, char *format, ...)
194 {
195 va_list ap;
196
197 va_start(ap, format);
198
199 /* Print out name of function causing error */
200 (void) fprintf(stderr, "ERR in %s: ", function_name);
201
202 /* Print out remainder of message */
203 (void) vfprintf(stderr, format, ap);
204
205 va_end(ap);
206
207 (void) abort();
208 }
209 .Ed
210 .Sh ERRORS
211 The
212 .Fn vfprintf
213 function will fail if either the
214 .Fa stream
215 is unbuffered or the
216 .Fa stream Ns 's
217 buffer needed to be flushed and:
218 .Bl -tag -width Er
219 .It Er EFBIG
220 The file is a regular file and an attempt was made to write at or beyond the
221 offset maximum.
222 .El
223 .Sh INTERFACE STABILITY
224 .Sy Committed
225 .Sh MT-LEVEL
226 All of these functions can be used safely in multithreaded applications, as
227 long as
228 .Xr setlocale 3C
229 is not being called to change the locale.
230 .Sh SEE ALSO
231 .Xr printf 3C ,
232 .Xr stdarg 3EXT ,
233 .Xr attributes 5 ,
234 .Xr standards 5
235 .Sh STANDARDS
236 See
237 .Xr standards 5
238 for the standards conformance of
239 .Fn vprintf ,
240 .Fn vfprintf ,
241 .Fn vsprintf ,
242 and
243 .Fn vsnprintf .
244 The
245 .Fn vasprintf
246 function is modeled on the one that appears in the
247 .Fx ,
248 .Nx ,
249 and GNU C libraries.
250 .Sh NOTES
251 The
252 .Fn vsnprintf
253 return value when
254 .Fa n
255 is 0 was changed in the Solaris 10 release.
256 The change was based on the SUSv3 specification.
257 The previous behavior was based on the initial SUSv2 specification, where
258 .Fn vsnprintf
259 when
260 .Fa n
261 is 0 returns an unspecified value less than 1.
|