1
/******************************************************************************
5
* Copyright Abandoned, 1987, Fred Fish *
8
* This previously copyrighted work has been placed into the public *
9
* domain by the author and may be freely used for any purpose, *
10
* private or commercial. *
12
* Because of the number of inquiries I was receiving about the use *
13
* of this product in commercially developed works I have decided to *
14
* simply make it public domain to further its unrestricted use. I *
15
* specifically would be most happy to see this material become a *
16
* part of the standard Unix distributions by AT&T and the Berkeley *
17
* Computer Science Research Group, and a standard part of the GNU *
18
* system from the Free Software Foundation. *
20
* I would appreciate it, as a courtesy, if this notice is left in *
21
* all copies and derivative works. Thank you. *
23
* The author makes no warranty of any kind with respect to this *
24
* product and explicitly disclaims any implied warranties of mer- *
25
* chantability or fitness for any particular purpose. *
27
******************************************************************************
34
* vargs.h include file for environments without varargs.h
38
* @(#)vargs.h 1.2 5/8/88
46
* This file implements a varargs macro set for use in those
47
* environments where there is no system supplied varargs. This
48
* generally works because systems which don't supply a varargs
49
* package are precisely those which don't strictly need a varargs
50
* package. Using this one then allows us to minimize source
51
* code changes. So in some sense, this is a "portable" varargs
52
* since it is only used for convenience, when it is not strictly
58
* These macros allow us to rebuild an argument list on the stack
59
* given only a va_list. We can use these to fake a function like
60
* vfprintf, which gets a fixed number of arguments, the last of
61
* which is a va_list, by rebuilding a stack and calling the variable
62
* argument form fprintf. Of course this only works when vfprintf
63
* is not available in the host environment, and thus is not available
64
* for fprintf to call (which would give us an infinite loop).
66
* Note that ARGS_TYPE is a long, which lets us get several bytes
67
* at a time while also preventing lots of "possible pointer alignment
68
* problem" messages from lint. The messages are valid, because this
69
* IS nonportable, but then we should only be using it in very
70
* nonrestrictive environments, and using the real varargs where it
86
#define ARGS_TYPE long
87
#define ARGS_LIST ARG0,ARG1,ARG2,ARG3,ARG4,ARG5,ARG6,ARG7,ARG8,ARG9
88
#define ARGS_DCL auto ARGS_TYPE ARGS_LIST
91
* A pointer of type "va_list" points to a section of memory
92
* containing an array of variable sized arguments of unknown
93
* number. This pointer is initialized by the va_start
94
* macro to point to the first byte of the first argument.
95
* We can then use it to walk through the argument list by
96
* incrementing it by the size of the argument being referenced.
99
typedef char *va_list;
102
* The first variable argument overlays va_alist, which is
103
* nothing more than a "handle" which allows us to get the
104
* address of the first argument on the stack. Note that
105
* by definition, the va_dcl macro includes the terminating
106
* semicolon, which makes use of va_dcl in the source code
107
* appear to be missing a semicolon.
110
#define va_dcl ARGS_TYPE va_alist;
113
* The va_start macro takes a variable of type "va_list" and
114
* initializes it. In our case, it initializes a local variable
115
* of type "pointer to char" to point to the first argument on
119
#define va_start(list) list = (char *) &va_alist
122
* The va_end macro is a null operation for our use.
128
* The va_arg macro is the tricky one. This one takes
129
* a va_list as the first argument, and a type as the second
130
* argument, and returns a value of the appropriate type
131
* while advancing the va_list to the following argument.
132
* For our case, we first increment the va_list arg by the
133
* size of the type being recovered, cast the result to
134
* a pointer of the appropriate type, and then dereference
135
* that pointer as an array to get the previous arg (which
136
* is the one we wanted.
139
#define va_arg(list,type) ((type *) (list += sizeof (type)))[-1]