]> git.sur5r.net Git - cc65/blob - test/ref/stdarg.c
remote TABs in doc/ and test/
[cc65] / test / ref / stdarg.c
1 /*
2   !!DESCRIPTION!! variable argument lists
3   !!ORIGIN!!      LCC 4.1 Testsuite
4   !!LICENCE!!     own, freely distributeable for non-profit. read CPYRIGHT.LCC
5 */
6
7 #include "common.h"
8 #include <stdarg.h>
9
10 #ifndef NO_FUNCS_TAKE_STRUCTS
11 struct node
12 {
13         int a[4];
14 } x =
15 {
16 #ifdef NO_SLOPPY_STRUCT_INIT
17         {
18 #endif
19                 1,2,3,4
20 #ifdef NO_SLOPPY_STRUCT_INIT
21         }
22 #endif
23 };
24 #endif
25
26 print(char *fmt, ...);
27
28 main()
29 {
30         print("test 1\n");
31         print("test %s\n", "2");
32         print("test %d%c", 3, '\n');
33         print("%s%s %w%c", "te", "st", 4, '\n');
34     #ifdef NO_FLOATS
35                 print("%s%s %f%c", "te", "st", (signed long) 5, '\n');
36         #else
37                 print("%s%s %f%c", "te", "st", 5.0, '\n');
38     #endif
39         #ifndef NO_FUNCS_TAKE_STRUCTS
40         print("%b %b %b %b %b %b\n", x, x, x, x, x, x);
41         #endif
42         return 0;
43 }
44
45 print(char *fmt, ...) {
46         va_list ap;
47         va_start(ap, fmt);
48         for (; *fmt; fmt++)
49         {
50                 if (*fmt == '%')
51                         switch (*++fmt) {
52             case 'b': {
53                                         #ifdef NO_FUNCS_TAKE_STRUCTS
54                         printf("(1 2 3 4)");
55                                         #else
56                         struct node x =
57                                                         va_arg(
58                                                                 ap,
59                                                                 struct node
60                                                                 );
61                         printf("(%d %d %d %d)", x.a[0], x.a[1], x.a[2], x.a[3]);
62                                         #endif
63                     break;
64                     }
65                         case 'c':
66                                 /* printf("%c", va_arg(ap, char)); */
67                                 printf("%c", va_arg(ap, int));
68                                 break;
69                         case 'd':
70                                 printf("%d", va_arg(ap, int));
71                                 break;
72                         case 'w':
73                                 /* printf("%x", va_arg(ap, short)); */
74                                 printf("%x", va_arg(ap, int));
75                                 break;
76                         case 's':
77                                 printf("%s", va_arg(ap, char *));
78                                 break;
79                         case 'f':
80                 #ifdef NO_FLOATS
81                                         printf("%ld.000000", va_arg(ap, signed long));
82                                 #else
83                                         printf("%f", va_arg(ap, double));
84                 #endif
85                                 break;
86                         default:
87                                 printf("%c", *fmt);
88                                 break;
89                         }
90                  else
91                         printf("%c", *fmt);
92         }
93         va_end(ap);
94 }