This source file includes following definitions.
- write_nul_delimited_argv
- main
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 #include <config.h>
20
21 #include "argv-iter.h"
22
23 #include <stdio.h>
24 #include <string.h>
25
26 #include "macros.h"
27
28 #define ARRAY_CARDINALITY(Array) (sizeof (Array) / sizeof *(Array))
29 #define STREQ(a, b) (strcmp (a, b) == 0)
30
31 static FILE *
32 write_nul_delimited_argv (char **argv)
33 {
34 FILE *fp = tmpfile ();
35 ASSERT (fp);
36 while (*argv)
37 {
38 size_t len = strlen (*argv) + 1;
39 ASSERT (fwrite (*argv, len, 1, fp) == 1);
40 argv++;
41 }
42 ASSERT (fflush (fp) == 0);
43 rewind (fp);
44 return fp;
45 }
46
47 int
48 main (void)
49 {
50 static char one[] = "1";
51 static char two[] = "2";
52 static char three[] = "3";
53 static char *av[][4] = {
54 {NULL},
55 {one, NULL},
56 {one, two, NULL},
57 {one, two, three, NULL}
58 };
59
60 int use_stream;
61 for (use_stream = 0; use_stream < 2; use_stream++)
62 {
63 size_t i;
64 for (i = 0; i < ARRAY_CARDINALITY (av); i++)
65 {
66 FILE *fp;
67 struct argv_iterator *ai;
68 size_t n_found = 0;
69 if (use_stream)
70 {
71
72 ASSERT ((fp = write_nul_delimited_argv (av[i])) != NULL);
73 ai = argv_iter_init_stream (fp);
74 }
75 else
76 {
77 fp = NULL;
78 ai = argv_iter_init_argv (av[i]);
79 }
80 ASSERT (ai);
81
82 while (1)
83 {
84 enum argv_iter_err ai_err;
85 char *s = argv_iter (ai, &ai_err);
86 ASSERT ((i == n_found) == (ai_err == AI_ERR_EOF));
87 ASSERT ((s == NULL) ^ (ai_err == AI_ERR_OK));
88 ASSERT (ai_err == AI_ERR_OK || ai_err == AI_ERR_EOF);
89 if (ai_err == AI_ERR_OK)
90 ++n_found;
91 if (ai_err == AI_ERR_EOF)
92 break;
93
94
95 ASSERT (use_stream
96 ? STREQ (s, av[i][n_found - 1])
97 : s == av[i][n_found - 1]);
98 }
99 ASSERT (argv_iter_n_args (ai) == i);
100 argv_iter_free (ai);
101 if (fp)
102 ASSERT (fclose (fp) == 0);
103 }
104 }
105
106 return 0;
107 }