Commit | Line | Data |
---|---|---|
19b14bf7 C |
1 | .TH VARARGS 3 "19 January 1983" |
2 | .SH NAME | |
3 | varargs \- 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 | |
18 | f = | |
19 | .B va_arg\c | |
20 | .RI ( pvar , | |
21 | .IR type ); | |
22 | .br | |
23 | .B va_end\c | |
24 | .RI ( pvar ); | |
25 | .SH DESCRIPTION | |
26 | This set of macros provides a means of writing portable procedures that | |
27 | accept variable argument lists. | |
28 | Routines having variable argument lists (such as | |
29 | .IR printf (3)) | |
30 | that do not use varargs are inherently nonportable, since different | |
31 | machines use different argument passing conventions. | |
32 | .PP | |
33 | .B va_alist | |
34 | is used in a function header to declare a variable argument list. | |
35 | .PP | |
36 | .B va_dcl | |
37 | is a declaration for | |
38 | .BR va_alist . | |
39 | Note that there is no semicolon after | |
40 | .B va_dcl. | |
41 | .PP | |
42 | .B va_list | |
43 | is a type which can be used for the variable | |
44 | .IR pvar , | |
45 | which is used to traverse the list. | |
46 | One such variable must always be declared. | |
47 | .PP | |
48 | .B va_start\c | |
49 | .RI (pvar) | |
50 | is called to initialize | |
51 | .I pvar | |
52 | to the beginning of the list. | |
53 | .PP | |
54 | .B va_arg\c | |
55 | .RI ( pvar , | |
56 | .IR type ) | |
57 | will return the next argument in the list pointed to by | |
58 | .IR pvar . | |
59 | .I Type | |
60 | is the type the argument is expected to be. | |
61 | Different types can be mixed, but it is up | |
62 | to the routine to know what type of argument is | |
63 | expected, since it cannot be determined at runtime. | |
64 | .PP | |
65 | .B va_end\c | |
66 | .RI ( pvar ) | |
67 | is used to finish up. | |
68 | .PP | |
69 | Multiple traversals, each bracketed by | |
70 | .B va_start | |
71 | \&... | |
72 | .B va_end, | |
73 | are 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 | |
94 | It is up to the calling routine to determine how many arguments | |
95 | there are, since it is not possible to determine this from the | |
96 | stack frame. For example, | |
97 | .I execl | |
98 | passes a 0 to signal the end of the list. | |
99 | .I Printf | |
100 | can tell how many arguments are supposed to be there by the format. |