1 .\"
2 .\" Sun Microsystems, Inc. gratefully acknowledges The Open Group for
3 .\" permission to reproduce portions of its copyrighted documentation.
4 .\" Original documentation from The Open Group can be obtained online at
5 .\" http://www.opengroup.org/bookstore/.
6 .\"
7 .\" The Institute of Electrical and Electronics Engineers and The Open
8 .\" Group, have given us permission to reprint portions of their
9 .\" documentation.
10 .\"
11 .\" In the following statement, the phrase ``this text'' refers to portions
12 .\" of the system documentation.
13 .\"
14 .\" Portions of this text are reprinted and reproduced in electronic form
15 .\" in the SunOS Reference Manual, from IEEE Std 1003.1, 2004 Edition,
16 .\" Standard for Information Technology -- Portable Operating System
17 .\" Interface (POSIX), The Open Group Base Specifications Issue 6,
18 .\" Copyright (C) 2001-2004 by the Institute of Electrical and Electronics
19 .\" Engineers, Inc and The Open Group. In the event of any discrepancy
20 .\" between these versions and the original IEEE and The Open Group
21 .\" Standard, the original IEEE and The Open Group Standard is the referee
22 .\" document. The original Standard can be obtained online at
23 .\" http://www.opengroup.org/unix/online.html.
24 .\"
25 .\" This notice shall appear on any product containing this material.
26 .\"
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.