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