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