4.3BSD-Tahoe/usr/man/cat3/varargs.0
VARARGS(3) UNIX Programmer's Manual VARARGS(3)
NNAAMMEE
varargs - variable argument list
SSYYNNOOPPSSIISS
##iinncclluuddee <<vvaarraarrggss..hh>>
_f_u_n_c_t_i_o_n(vvaa__aalliisstt)
vvaa__ddccll
vvaa__lliisstt _p_v_a_r;
vvaa__ssttaarrtt(_p_v_a_r);
f = vvaa__aarrgg(_p_v_a_r, _t_y_p_e);
vvaa__eenndd(_p_v_a_r);
DDEESSCCRRIIPPTTIIOONN
This set of macros provides a means of writing portable pro-
cedures that accept variable argument lists. Routines hav-
ing variable argument lists (such as _p_r_i_n_t_f(3)) that do not
use varargs are inherently nonportable, since different
machines use different argument passing conventions.
vvaa__aalliisstt is used in a function header to declare a variable
argument list.
vvaa__ddccll is a declaration for vvaa__aalliisstt. Note that there is no
semicolon after vvaa__ddccll..
vvaa__lliisstt is a type which can be used for the variable _p_v_a_r,
which is used to traverse the list. One such variable must
always be declared.
vvaa__ssttaarrtt(pvar) is called to initialize _p_v_a_r to the beginning
of the list.
vvaa__aarrgg(_p_v_a_r, _t_y_p_e) will return the next argument in the list
pointed to by _p_v_a_r. _T_y_p_e is the type to which the expected
argument will be converted when passed as an argument. In
standard C, arguments that are cchhaarr or sshhoorrtt should be
accessed as iinntt, uunnssiiggnneedd cchhaarr or uunnssiiggnneedd sshhoorrtt are con-
verted to uunnssiiggnneedd iinntt, and ffllooaatt arguments are converted to
ddoouubbllee. Different types can be mixed, but it is up to the
routine to know what type of argument is expected, since it
cannot be determined at runtime.
vvaa__eenndd(_p_v_a_r) is used to finish up.
Multiple traversals, each bracketed by vvaa__ssttaarrtt ... vvaa__eenndd,,
are possible.
EEXXAAMMPPLLEE
##iinncclluuddee <varargs.h>
execl(vvaa__aalliisstt)
vvaa__ddccll
Printed 7/9/88 May 15, 1986 1
VARARGS(3) UNIX Programmer's Manual VARARGS(3)
{
vvaa__lliisstt ap;
cchhaarr *file;
cchhaarr *args[100];
iinntt argno = 0;
vvaa__ssttaarrtt(ap);
file = vvaa__aarrgg((aapp,, cchhaarr **));;
wwhhiillee ((aarrggss[[aarrggnnoo++++]] == vvaa__aarrgg((aapp,, cchhaarr **))))
;;
vvaa__eenndd((aapp));;
rreettuurrnn eexxeeccvv((ffiillee,, aarrggss));;
}}
BBUUGGSS
It is up to the calling routine to determine how many argu-
ments there are, since it is not possible to determine this
from the stack frame. For example, _e_x_e_c_l passes a 0 to sig-
nal the end of the list. _P_r_i_n_t_f can tell how many arguments
are supposed to be there by the format.
The macros _v_a__s_t_a_r_t and _v_a__e_n_d may be arbitrarily complex;
for example, _v_a__s_t_a_r_t might contain an opening brace, which
is closed by a matching brace in _v_a__e_n_d. Thus, they should
only be used where they could be placed within a single com-
plex statement.
Printed 7/9/88 May 15, 1986 2