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 .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.