| 1 |
/* Test variable number of arguments passed to functions. For now this is |
| 2 |
just a simple test to see if it's working. */ |
| 3 |
|
| 4 |
#include <stdarg.h> |
| 5 |
#include "defines.h" |
| 6 |
|
| 7 |
|
| 8 |
#define ARG_INT 1 |
| 9 |
#define ARG_DOUBLE 2 |
| 10 |
#define ARG_POINTER 3 |
| 11 |
|
| 12 |
union types |
| 13 |
{ |
| 14 |
int ivalue; |
| 15 |
double dvalue; |
| 16 |
void *pvalue; |
| 17 |
}; |
| 18 |
|
| 19 |
struct arg |
| 20 |
{ |
| 21 |
int type; |
| 22 |
union types value; |
| 23 |
}; |
| 24 |
|
| 25 |
struct arg *arglist; |
| 26 |
|
| 27 |
/* This tests the argumentlist to see if it matches the format string which |
| 28 |
is printf-like. Nothing will be printed of course. It can handle ints, |
| 29 |
doubles and void pointers. The given value will be tested against the |
| 30 |
values given in arglist. |
| 31 |
This test only assures that the variable argument passing is working. |
| 32 |
No attempt is made to see if argument passing is done the right way. |
| 33 |
Since the ABI doesn't say how it's done, checking this is not really |
| 34 |
relevant. */ |
| 35 |
void |
| 36 |
my_noprintf (char *format, ...) |
| 37 |
{ |
| 38 |
va_list va_arglist; |
| 39 |
char *c; |
| 40 |
|
| 41 |
int ivalue; |
| 42 |
double dvalue; |
| 43 |
void *pvalue; |
| 44 |
struct arg *argp = arglist; |
| 45 |
|
| 46 |
va_start (va_arglist, format); |
| 47 |
for (c = format; *c; c++) |
| 48 |
if (*c == '%') |
| 49 |
{ |
| 50 |
switch (*++c) |
| 51 |
{ |
| 52 |
case 'd': |
| 53 |
assert (argp->type == ARG_INT); |
| 54 |
ivalue = va_arg (va_arglist, int); |
| 55 |
assert (argp->value.ivalue == ivalue); |
| 56 |
break; |
| 57 |
case 'f': |
| 58 |
assert (argp->type == ARG_DOUBLE); |
| 59 |
dvalue = va_arg (va_arglist, double); |
| 60 |
assert (argp->value.dvalue == dvalue); |
| 61 |
break; |
| 62 |
case 'p': |
| 63 |
assert (argp->type == ARG_POINTER); |
| 64 |
pvalue = va_arg (va_arglist, void *); |
| 65 |
assert (argp->value.pvalue == pvalue); |
| 66 |
break; |
| 67 |
default: |
| 68 |
abort (); |
| 69 |
} |
| 70 |
|
| 71 |
argp++; |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
int |
| 76 |
main (void) |
| 77 |
{ |
| 78 |
#ifdef CHECK_VARARGS |
| 79 |
struct arg al[5]; |
| 80 |
|
| 81 |
al[0].type = ARG_INT; |
| 82 |
al[0].value.ivalue = 256; |
| 83 |
al[1].type = ARG_DOUBLE; |
| 84 |
al[1].value.dvalue = 257.0; |
| 85 |
al[2].type = ARG_POINTER; |
| 86 |
al[2].value.pvalue = al; |
| 87 |
al[3].type = ARG_DOUBLE; |
| 88 |
al[3].value.dvalue = 258.0; |
| 89 |
al[4].type = ARG_INT; |
| 90 |
al[4].value.ivalue = 259; |
| 91 |
|
| 92 |
arglist = al; |
| 93 |
my_noprintf("%d%f%p%f%d", 256, 257.0, al, 258.0, 259); |
| 94 |
#endif |
| 95 |
|
| 96 |
return 0; |
| 97 |
} |