]> gitweb.ps.run Git - onefile/blob - varargs.c
add printself.c
[onefile] / varargs.c
1 void test(const char *format, ...)
2 {
3     va_list argp;
4     va_start(argp, format);
5
6     int n = 0;
7     static char sep[128];
8
9     while (*format != '\0')
10     {
11         if (*format == '%')
12         {
13             format++;
14             if (*format == '%')
15             {
16                 putchar('%');
17             }
18             else if (*format >= '0' && *format <= '9')
19             {
20                 n = (*format) - '0';
21                 format++;
22                 while (*format >= '0' && *format <= '9')
23                 {
24                     n *= 10;
25                     n += (*format) - '0';
26                     format++;
27                 }
28                 if (*format == '[')
29                 {
30                     format++;
31                     int i = 0;
32                     while (*format != ']')
33                     {
34                         sep[i++] = *format;
35                         format++;
36                     }
37                     sep[i] = '\0';
38                     format++;
39                 }
40                 if (*format == 'd')
41                 {
42                     int *ints = va_arg(argp, int *);
43                     for (int i = 0; i < n; i++)
44                     {
45                         if (i > 0)
46                             printf("%s", sep);
47                         printf("%d", ints[i]);
48                     }
49                 }
50                 else if (*format == 's')
51                 {
52                     const char **strs = va_arg(argp, const char **);
53                     for (int i = 0; i < n; i++)
54                     {
55                         if (i > 0)
56                             printf("%s", sep);
57                         printf("%s", strs[i]);
58                     }
59                 }
60             }
61             else
62             {
63                 fputs("Not implemented", stdout);
64                 char char_to_print = va_arg(argp, int);
65                 putchar(char_to_print);
66             }
67         }
68         else
69         {
70             putchar(*format);
71         }
72         format++;
73     }
74     va_end(argp);
75 }
76
77 int main()
78 {
79     int ints[] = {1, 2, 3, 6, 8, 2, 7, 8, 1, 245, 277254, 2642, 71, 717, 7};
80     const char *strs[2] = {"abc", "def"};
81     test("Hallo %15[ ]d %2[, ]s\n", ints, strs);
82 }