File: stdarg.c

package info (click to toggle)
cc65 2.19-2
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 20,268 kB
  • sloc: ansic: 117,151; asm: 66,339; pascal: 4,248; makefile: 1,009; perl: 607
file content (94 lines) | stat: -rw-r--r-- 3,011 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*
  !!DESCRIPTION!! variable argument lists
  !!ORIGIN!!      LCC 4.1 Testsuite
  !!LICENCE!!     own, freely distributeable for non-profit. read CPYRIGHT.LCC
*/

#include "common.h"
#include <stdarg.h>

#ifndef NO_FUNCS_TAKE_STRUCTS
struct node
{
        int a[4];
} x =
{
#ifdef NO_SLOPPY_STRUCT_INIT
        {
#endif
                1,2,3,4
#ifdef NO_SLOPPY_STRUCT_INIT
        }
#endif
};
#endif

print(char *fmt, ...);

main()
{
        print("test 1\n");
        print("test %s\n", "2");
        print("test %d%c", 3, '\n');
        print("%s%s %w%c", "te", "st", 4, '\n');
    #ifdef NO_FLOATS
                print("%s%s %f%c", "te", "st", (signed long) 5, '\n');
        #else
                print("%s%s %f%c", "te", "st", 5.0, '\n');
    #endif
        #ifndef NO_FUNCS_TAKE_STRUCTS
        print("%b %b %b %b %b %b\n", x, x, x, x, x, x);
        #endif
        return 0;
}

print(char *fmt, ...) {
        va_list ap;
        va_start(ap, fmt);
        for (; *fmt; fmt++)
        {
                if (*fmt == '%')
                        switch (*++fmt) {
            case 'b': {
                                        #ifdef NO_FUNCS_TAKE_STRUCTS
                        printf("(1 2 3 4)");
                                        #else
                        struct node x =
                                                        va_arg(
                                                                ap,
                                                                struct node
                                                                );
                        printf("(%d %d %d %d)", x.a[0], x.a[1], x.a[2], x.a[3]);
                                        #endif
                    break;
                    }
                        case 'c':
                                /* printf("%c", va_arg(ap, char)); */
                                printf("%c", va_arg(ap, int));
                                break;
                        case 'd':
                                printf("%d", va_arg(ap, int));
                                break;
                        case 'w':
                                /* printf("%x", va_arg(ap, short)); */
                                printf("%x", va_arg(ap, int));
                                break;
                        case 's':
                                printf("%s", va_arg(ap, char *));
                                break;
                        case 'f':
                #ifdef NO_FLOATS
                                        printf("%ld.000000", va_arg(ap, signed long));
                                #else
                                        printf("%f", va_arg(ap, double));
                #endif
                                break;
                        default:
                                printf("%c", *fmt);
                                break;
                        }
                 else
                        printf("%c", *fmt);
        }
        va_end(ap);
}