BSD 4_2 development
[unix-history] / usr / man / man3 / varargs.3
CommitLineData
19b14bf7
C
1.TH VARARGS 3 "19 January 1983"
2.SH NAME
3varargs \- variable argument list
4.SH SYNOPSIS
5.B "#include <varargs.h>"
6.PP
7.I function\c
8.RB ( va_alist )
9.br
10.B va_dcl
11.br
12.B va_list
13.IR pvar ;
14.br
15.B va_start\c
16.RI ( pvar );
17.br
18f =
19.B va_arg\c
20.RI ( pvar ,
21.IR type );
22.br
23.B va_end\c
24.RI ( pvar );
25.SH DESCRIPTION
26This set of macros provides a means of writing portable procedures that
27accept variable argument lists.
28Routines having variable argument lists (such as
29.IR printf (3))
30that do not use varargs are inherently nonportable, since different
31machines use different argument passing conventions.
32.PP
33.B va_alist
34is used in a function header to declare a variable argument list.
35.PP
36.B va_dcl
37is a declaration for
38.BR va_alist .
39Note that there is no semicolon after
40.B va_dcl.
41.PP
42.B va_list
43is a type which can be used for the variable
44.IR pvar ,
45which is used to traverse the list.
46One such variable must always be declared.
47.PP
48.B va_start\c
49.RI (pvar)
50is called to initialize
51.I pvar
52to the beginning of the list.
53.PP
54.B va_arg\c
55.RI ( pvar ,
56.IR type )
57will return the next argument in the list pointed to by
58.IR pvar .
59.I Type
60is the type the argument is expected to be.
61Different types can be mixed, but it is up
62to the routine to know what type of argument is
63expected, since it cannot be determined at runtime.
64.PP
65.B va_end\c
66.RI ( pvar )
67is used to finish up.
68.PP
69Multiple traversals, each bracketed by
70.B va_start
71\&...
72.B va_end,
73are possible.
74.SH EXAMPLE
75.nf
76 \fB#include\fP <varargs.h>
77 execl(\fBva_alist\fP)
78 \fBva_dcl\fP
79 {
80 \fBva_list\fP ap;
81 \fBchar\fP *file;
82 \fBchar\fP *args[100];
83 \fBint\fP argno = 0;
84
85 \fBva_start\fP(ap);
86 file = \fBva_arg(ap, \fBchar\fP *);
87 \fBwhile\fP (args[argno++] = \fBva_arg\fP(ap, \fBchar\fP *))
88 \fB;\fP
89 \fBva_end\fP(ap);
90 \fBreturn\fP execv(file, args);
91 }
92.fi
93.SH BUGS
94It is up to the calling routine to determine how many arguments
95there are, since it is not possible to determine this from the
96stack frame. For example,
97.I execl
98passes a 0 to signal the end of the list.
99.I Printf
100can tell how many arguments are supposed to be there by the format.