1afe61c15SRodney W. Grimes.\" Copyright (c) 1990, 1991, 1993 2afe61c15SRodney W. Grimes.\" The Regents of the University of California. All rights reserved. 3afe61c15SRodney W. Grimes.\" 4afe61c15SRodney W. Grimes.\" This code is derived from software contributed to Berkeley by 5afe61c15SRodney W. Grimes.\" the American National Standards Committee X3, on Information 6afe61c15SRodney W. Grimes.\" Processing Systems. 7afe61c15SRodney W. Grimes.\" 8afe61c15SRodney W. Grimes.\" Redistribution and use in source and binary forms, with or without 9afe61c15SRodney W. Grimes.\" modification, are permitted provided that the following conditions 10afe61c15SRodney W. Grimes.\" are met: 11afe61c15SRodney W. Grimes.\" 1. Redistributions of source code must retain the above copyright 12afe61c15SRodney W. Grimes.\" notice, this list of conditions and the following disclaimer. 13afe61c15SRodney W. Grimes.\" 2. Redistributions in binary form must reproduce the above copyright 14afe61c15SRodney W. Grimes.\" notice, this list of conditions and the following disclaimer in the 15afe61c15SRodney W. Grimes.\" documentation and/or other materials provided with the distribution. 16afe61c15SRodney W. Grimes.\" 3. All advertising materials mentioning features or use of this software 17afe61c15SRodney W. Grimes.\" must display the following acknowledgement: 18afe61c15SRodney W. Grimes.\" This product includes software developed by the University of 19afe61c15SRodney W. Grimes.\" California, Berkeley and its contributors. 20afe61c15SRodney W. Grimes.\" 4. Neither the name of the University nor the names of its contributors 21afe61c15SRodney W. Grimes.\" may be used to endorse or promote products derived from this software 22afe61c15SRodney W. Grimes.\" without specific prior written permission. 23afe61c15SRodney W. Grimes.\" 24afe61c15SRodney W. Grimes.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25afe61c15SRodney W. Grimes.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26afe61c15SRodney W. Grimes.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27afe61c15SRodney W. Grimes.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28afe61c15SRodney W. Grimes.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29afe61c15SRodney W. Grimes.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30afe61c15SRodney W. Grimes.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31afe61c15SRodney W. Grimes.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32afe61c15SRodney W. Grimes.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33afe61c15SRodney W. Grimes.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34afe61c15SRodney W. Grimes.\" SUCH DAMAGE. 35afe61c15SRodney W. Grimes.\" 36afe61c15SRodney W. Grimes.\" @(#)stdarg.3 8.1 (Berkeley) 6/5/93 377f3dea24SPeter Wemm.\" $FreeBSD$ 38afe61c15SRodney W. Grimes.\" 39a40c540fSTim J. Robbins.Dd October 25, 2002 40afe61c15SRodney W. Grimes.Dt STDARG 3 41afe61c15SRodney W. Grimes.Os 42afe61c15SRodney W. Grimes.Sh NAME 43afe61c15SRodney W. Grimes.Nm stdarg 44afe61c15SRodney W. Grimes.Nd variable argument lists 45afe61c15SRodney W. Grimes.Sh SYNOPSIS 4632eef9aeSRuslan Ermilov.In stdarg.h 47afe61c15SRodney W. Grimes.Ft void 48afe61c15SRodney W. Grimes.Fn va_start "va_list ap" last 49afe61c15SRodney W. Grimes.Ft type 50afe61c15SRodney W. Grimes.Fn va_arg "va_list ap" type 51afe61c15SRodney W. Grimes.Ft void 5286954511SMike Barcroft.Fn va_copy "va_list dest" "va_list src" 5386954511SMike Barcroft.Ft void 54afe61c15SRodney W. Grimes.Fn va_end "va_list ap" 55afe61c15SRodney W. Grimes.Sh DESCRIPTION 56afe61c15SRodney W. GrimesA function may be called with a varying number of arguments of varying 57afe61c15SRodney W. Grimestypes. 58afe61c15SRodney W. GrimesThe include file 59fe08efe6SRuslan Ermilov.In stdarg.h 60afe61c15SRodney W. Grimesdeclares a type 61afe61c15SRodney W. Grimes.Pq Em va_list 62*484e5bddSJohn Baldwinand defines four macros for stepping 63afe61c15SRodney W. Grimesthrough a list of arguments whose number and types are not known to 64afe61c15SRodney W. Grimesthe called function. 65afe61c15SRodney W. Grimes.Pp 66afe61c15SRodney W. GrimesThe called function must declare an object of type 67afe61c15SRodney W. Grimes.Em va_list 68afe61c15SRodney W. Grimeswhich is used by the macros 69afe61c15SRodney W. Grimes.Fn va_start , 70afe61c15SRodney W. Grimes.Fn va_arg , 71a40c540fSTim J. Robbins.Fn va_copy , 72afe61c15SRodney W. Grimesand 73afe61c15SRodney W. Grimes.Fn va_end . 74afe61c15SRodney W. Grimes.Pp 75afe61c15SRodney W. GrimesThe 76afe61c15SRodney W. Grimes.Fn va_start 77afe61c15SRodney W. Grimesmacro initializes 78afe61c15SRodney W. Grimes.Fa ap 79afe61c15SRodney W. Grimesfor subsequent use by 80*484e5bddSJohn Baldwin.Fn va_arg , 81*484e5bddSJohn Baldwin.Fn va_copy , 82afe61c15SRodney W. Grimesand 83afe61c15SRodney W. Grimes.Fn va_end , 84afe61c15SRodney W. Grimesand must be called first. 85afe61c15SRodney W. Grimes.Pp 86afe61c15SRodney W. GrimesThe parameter 87afe61c15SRodney W. Grimes.Fa last 88afe61c15SRodney W. Grimesis the name of the last parameter before the variable argument list, 895203edcdSRuslan Ermilovi.e., the last parameter of which the calling function knows the type. 90afe61c15SRodney W. Grimes.Pp 91afe61c15SRodney W. GrimesBecause the address of this parameter is used in the 92afe61c15SRodney W. Grimes.Fn va_start 93afe61c15SRodney W. Grimesmacro, it should not be declared as a register variable, or as a 94afe61c15SRodney W. Grimesfunction or an array type. 95afe61c15SRodney W. Grimes.Pp 96afe61c15SRodney W. GrimesThe 97afe61c15SRodney W. Grimes.Fn va_arg 98afe61c15SRodney W. Grimesmacro expands to an expression that has the type and value of the next 99afe61c15SRodney W. Grimesargument in the call. 100afe61c15SRodney W. GrimesThe parameter 101afe61c15SRodney W. Grimes.Fa ap 102afe61c15SRodney W. Grimesis the 103afe61c15SRodney W. Grimes.Em va_list Fa ap 104afe61c15SRodney W. Grimesinitialized by 105*484e5bddSJohn Baldwin.Fn va_start 106*484e5bddSJohn Baldwinor 107*484e5bddSJohn Baldwin.Fn va_copy . 108afe61c15SRodney W. GrimesEach call to 109afe61c15SRodney W. Grimes.Fn va_arg 110afe61c15SRodney W. Grimesmodifies 111afe61c15SRodney W. Grimes.Fa ap 112afe61c15SRodney W. Grimesso that the next call returns the next argument. 113afe61c15SRodney W. GrimesThe parameter 114afe61c15SRodney W. Grimes.Fa type 115afe61c15SRodney W. Grimesis a type name specified so that the type of a pointer to an 116afe61c15SRodney W. Grimesobject that has the specified type can be obtained simply by 117afe61c15SRodney W. Grimesadding a * 118afe61c15SRodney W. Grimesto 119afe61c15SRodney W. Grimes.Fa type . 120afe61c15SRodney W. Grimes.Pp 121afe61c15SRodney W. GrimesIf there is no next argument, or if 122afe61c15SRodney W. Grimes.Fa type 123afe61c15SRodney W. Grimesis not compatible with the type of the actual next argument 124afe61c15SRodney W. Grimes(as promoted according to the default argument promotions), 125afe61c15SRodney W. Grimesrandom errors will occur. 126afe61c15SRodney W. Grimes.Pp 127afe61c15SRodney W. GrimesThe first use of the 128afe61c15SRodney W. Grimes.Fn va_arg 129afe61c15SRodney W. Grimesmacro after that of the 130afe61c15SRodney W. Grimes.Fn va_start 131afe61c15SRodney W. Grimesmacro returns the argument after 132afe61c15SRodney W. Grimes.Fa last . 133afe61c15SRodney W. GrimesSuccessive invocations return the values of the remaining 134afe61c15SRodney W. Grimesarguments. 135afe61c15SRodney W. Grimes.Pp 136afe61c15SRodney W. GrimesThe 13786954511SMike Barcroft.Fn va_copy 13886954511SMike Barcroftmacro copies a variable argument list, previously initialized by 13986954511SMike Barcroft.Fn va_start , 14086954511SMike Barcroftfrom 141d962d52aSRuslan Ermilov.Fa src 14286954511SMike Barcroftto 143d962d52aSRuslan Ermilov.Fa dest . 14486954511SMike BarcroftThe state is preserved such that it is equivalent to calling 14586954511SMike Barcroft.Fn va_start 14686954511SMike Barcroftwith the same second argument used with 147d962d52aSRuslan Ermilov.Fa src , 14886954511SMike Barcroftand calling 14986954511SMike Barcroft.Fn va_arg 15086954511SMike Barcroftthe same number of times as called with 151d962d52aSRuslan Ermilov.Fa src . 15286954511SMike Barcroft.Pp 15386954511SMike BarcroftThe 154*484e5bddSJohn Baldwin.Fn va_end 155*484e5bddSJohn Baldwinmacro cleans up any state associated with the variable argument list 156*484e5bddSJohn Baldwin.Fa ap . 157*484e5bddSJohn Baldwin.Pp 158*484e5bddSJohn BaldwinEach invocation of 159*484e5bddSJohn Baldwin.Fn va_start 160*484e5bddSJohn Baldwinor 16186954511SMike Barcroft.Fn va_copy 162*484e5bddSJohn Baldwinmust be paired with a corresponding invocation of 163*484e5bddSJohn Baldwin.Fn va_end 164*484e5bddSJohn Baldwinin the same function. 165*484e5bddSJohn Baldwin.Sh RETURN VALUES 166*484e5bddSJohn BaldwinThe 167*484e5bddSJohn Baldwin.Fn va_arg 168*484e5bddSJohn Baldwinmacro returns the value of the next argument. 16986954511SMike Barcroft.Pp 17086954511SMike BarcroftThe 171*484e5bddSJohn Baldwin.Fn va_start , 172*484e5bddSJohn Baldwin.Fn va_copy , 173*484e5bddSJohn Baldwinand 174afe61c15SRodney W. Grimes.Fn va_end 175*484e5bddSJohn Baldwinmacros return no value. 176afe61c15SRodney W. Grimes.Sh EXAMPLES 177afe61c15SRodney W. GrimesThe function 178afe61c15SRodney W. Grimes.Em foo 179afe61c15SRodney W. Grimestakes a string of format characters and prints out the argument 180afe61c15SRodney W. Grimesassociated with each format character based on the type. 181afe61c15SRodney W. Grimes.Bd -literal -offset indent 182afe61c15SRodney W. Grimesvoid foo(char *fmt, ...) 183afe61c15SRodney W. Grimes{ 184afe61c15SRodney W. Grimes va_list ap; 185afe61c15SRodney W. Grimes int d; 186cc023582SDima Dorfman char c, *s; 187afe61c15SRodney W. Grimes 188afe61c15SRodney W. Grimes va_start(ap, fmt); 189afe61c15SRodney W. Grimes while (*fmt) 190afe61c15SRodney W. Grimes switch(*fmt++) { 191afe61c15SRodney W. Grimes case 's': /* string */ 192afe61c15SRodney W. Grimes s = va_arg(ap, char *); 193afe61c15SRodney W. Grimes printf("string %s\en", s); 194afe61c15SRodney W. Grimes break; 195afe61c15SRodney W. Grimes case 'd': /* int */ 196afe61c15SRodney W. Grimes d = va_arg(ap, int); 197afe61c15SRodney W. Grimes printf("int %d\en", d); 198afe61c15SRodney W. Grimes break; 199afe61c15SRodney W. Grimes case 'c': /* char */ 20004bbb62bSJoshua Peck Macdonald /* Note: char is promoted to int. */ 20104bbb62bSJoshua Peck Macdonald c = va_arg(ap, int); 202afe61c15SRodney W. Grimes printf("char %c\en", c); 203afe61c15SRodney W. Grimes break; 204afe61c15SRodney W. Grimes } 205afe61c15SRodney W. Grimes va_end(ap); 206afe61c15SRodney W. Grimes} 207afe61c15SRodney W. Grimes.Ed 2089cbda590SRuslan Ermilov.Sh COMPATIBILITY 2099cbda590SRuslan ErmilovThese macros are 2109cbda590SRuslan Ermilov.Em not 2119cbda590SRuslan Ermilovcompatible with the historic macros they replace. 2129cbda590SRuslan ErmilovA backward compatible version can be found in the include 2139cbda590SRuslan Ermilovfile 2149cbda590SRuslan Ermilov.In varargs.h . 215afe61c15SRodney W. Grimes.Sh STANDARDS 216afe61c15SRodney W. GrimesThe 217afe61c15SRodney W. Grimes.Fn va_start , 218afe61c15SRodney W. Grimes.Fn va_arg , 219a40c540fSTim J. Robbins.Fn va_copy , 220afe61c15SRodney W. Grimesand 221afe61c15SRodney W. Grimes.Fn va_end 222afe61c15SRodney W. Grimesmacros conform to 223a40c540fSTim J. Robbins.St -isoC-99 . 224afe61c15SRodney W. Grimes.Sh BUGS 225afe61c15SRodney W. GrimesUnlike the 226afe61c15SRodney W. Grimes.Em varargs 227afe61c15SRodney W. Grimesmacros, the 2284b66483fSRuslan Ermilov.Nm 229afe61c15SRodney W. Grimesmacros do not permit programmers to 230afe61c15SRodney W. Grimescode a function with no fixed arguments. 231afe61c15SRodney W. GrimesThis problem generates work mainly when converting 232afe61c15SRodney W. Grimes.Em varargs 233afe61c15SRodney W. Grimescode to 2344b66483fSRuslan Ermilov.Nm 235afe61c15SRodney W. Grimescode, 236afe61c15SRodney W. Grimesbut it also creates difficulties for variadic functions that 237afe61c15SRodney W. Grimeswish to pass all of their arguments on to a function 238afe61c15SRodney W. Grimesthat takes a 239afe61c15SRodney W. Grimes.Em va_list 240afe61c15SRodney W. Grimesargument, such as 241afe61c15SRodney W. Grimes.Xr vfprintf 3 . 242