2 .\" Copyright (c) 2006, Sun Microsystems, Inc., All Rights Reserved.
3 .\" Copyright 1989 AT&T
4 .\" The contents of this file are subject to the terms of the Common Development and Distribution License (the "License"). You may not use this file except in compliance with the License.
5 .\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE or http://www.opensolaris.org/os/licensing. See the License for the specific language governing permissions and limitations under the License.
6 .\" When distributing Covered Code, include this CDDL HEADER in each file and include the License file at usr/src/OPENSOLARIS.LICENSE. If applicable, add the following below this CDDL HEADER, with the fields enclosed by brackets "[]" replaced with your own identifying information: Portions Copyright [yyyy] [name of copyright owner]
7 .TH VA_ARG 9F "Mar 21, 2017"
9 va_arg, va_start, va_copy, va_end \- handle variable argument list
13 #include <sys/varargs.h>
17 \fBvoid\fR \fBva_start\fR(\fBva_list\fR \fIpvar\fR, \fB\fR\fIname\fR);
22 \fB(type *)\fR \fBva_arg\fR(\fBva_list\fR \fIpvar\fR, \fB\fR\fItype\fR);
27 \fBvoid\fR \fBva_copy\fR(\fBva_list\fR \fIdest\fR, \fBva_list\fR \fIsrc\fR);
32 \fBvoid\fR \fBva_end\fR(\fBva_list\fR \fIpvar\fR);
37 Solaris DDI specific (Solaris DDI).
39 .SS "\fBva_start()\fR"
45 Pointer to variable argument list.
54 Identifier of rightmost parameter in the function definition.
63 Pointer to variable argument list.
72 Type name of the next argument to be returned.
81 Destination variable argument list.
90 Source variable argument list.
99 Pointer to variable argument list.
104 This set of macros allows portable procedures that accept variable argument
105 lists to be written. Routines that have variable argument lists but do not use
106 the \fBvarargs\fR macros are inherently non-portable, as different machines use
107 different argument-passing conventions. Routines that accept a variable
108 argument list can use these macros to traverse the list.
111 \fBva_list\fR is the type defined for the variable used to traverse the list of
115 \fBva_start()\fR is called to initialize \fIpvar\fR to the beginning of the
116 variable argument list. \fBva_start()\fR must be invoked before any access to
117 the unnamed arguments. The parameter \fIname\fR is the identifier of the
118 rightmost parameter in the variable parameter list in the function definition
119 (the one just before the "\fB, .\|.\|.\|\fR"). If this parameter is declared
120 with the \fBregister\fR storage class or with a function or array type, or with
121 a type that is not compatible with the type that results after application of
122 the default argument promotions, the behavior is undefined.
125 \fBva_arg()\fR expands to an expression that has the type and value of the next
126 argument in the call. The parameter \fIpvar\fR must be initialized by
127 \fBva_start()\fR. Each invocation of \fBva_arg()\fR modifies \fIpvar\fR so that
128 the values of successive arguments are returned in turn. The parameter
129 \fItype\fR is the type name of the next argument to be returned. The type name
130 must be specified in such a way that the type of pointer to an object that has
131 the specified type can be obtained by postfixing a \fB*\fR to \fItype\fR. If
132 there is no actual next argument, or if \fBtype\fR is not compatible with the
133 type of the actual next argument (as promoted according to the default argument
134 promotions), the behavior is undefined.
137 The \fBva_copy()\fR macro saves the state represented by the
138 \fBva_list\fR \fIsrc\fR in the \fBva_list\fR \fIdest\fR. The \fBva_list\fR
139 passed as \fIdest\fR should not be initialized by a previous call to
140 \fBva_start()\fR. It then must be passed to \fBva_end()\fR before being reused
141 as a parameter to \fBva_start()\fR or as the \fIdest\fR parameter of a
142 subsequent call to \fBva_copy()\fR. The behavior is undefined if any of these
143 restrictions are not met.
146 The \fBva_end()\fR macro is used to clean up. It invalidates \fIpvar\fR for use
147 (unless \fBva_start()\fR is invoked again).
150 Multiple traversals, each bracketed by a call to \fBva_start()\fR and
151 \fBva_end()\fR, are possible.
154 \fBExample 1 \fRCreating a Variable Length Command
157 The following example uses these routines to create a variable length command.
158 This might be useful for a device that provides for a variable-length command
159 set. \fBncmdbytes\fR is the number of bytes in the command. The new command is
160 written to \fBcmdp\fR.
166 xx_write_cmd(uchar_t *cmdp, int ncmdbytes, ...)
172 * Write variable-length command to destination
174 va_start(ap, ncmdbytes);
175 for (i = 0; i < ncmdbytes; i++) {
176 *cmdp++ = va_arg(ap, uchar_t);
185 \fBvcmn_err\fR(9F), \fBvsprintf\fR(9F)
188 It is up to the calling routine to specify in some manner how many arguments
189 there are, since it is not always possible to determine the number of arguments
190 from the stack frame.
193 Specifying a second argument of \fBchar\fR or \fBshort\fR to \fBva_arg\fR makes
194 your code non-portable, because arguments seen by the called function are not
195 \fBchar\fR or \fBshort\fR. C converts \fBchar\fR and \fBshort\fR arguments to
196 \fBint\fR before passing them to a function.