1 | #include <stdlib.h> |
---|
2 | #include <stdio.h> |
---|
3 | #include <string.h> |
---|
4 | |
---|
5 | #include <errno.h> |
---|
6 | #include <signal.h> |
---|
7 | |
---|
8 | extern "C" { |
---|
9 | #include <sys/types.h> |
---|
10 | #include <sys/wait.h> |
---|
11 | #include <unistd.h> |
---|
12 | } |
---|
13 | |
---|
14 | #include <parseargs.hfa> |
---|
15 | #include <fstream.hfa> |
---|
16 | |
---|
17 | #if __SIZEOF_LONG__ == 4 |
---|
18 | #define BIG_UNSIGNED_LONG "4294967295" |
---|
19 | #define TOO_BIG_UNSIGNED_LONG "4294967296" |
---|
20 | #elif __SIZEOF_LONG__ == 8 |
---|
21 | #define BIG_UNSIGNED_LONG "18446744073709551615" |
---|
22 | #define TOO_BIG_UNSIGNED_LONG "18446744073709551616" |
---|
23 | #else |
---|
24 | #error unexpected size of long |
---|
25 | #endif |
---|
26 | |
---|
27 | int true_main(const char * exec); |
---|
28 | |
---|
29 | int main(int argc, char * argv[]) { |
---|
30 | if(!getenv("CFATEST_FORK_EXEC_TEXT")) return true_main(argv[0]); |
---|
31 | |
---|
32 | int i = -3; |
---|
33 | unsigned u = 3; |
---|
34 | unsigned long ul = 3; |
---|
35 | unsigned long long ull = 3; |
---|
36 | double d = 3.3; |
---|
37 | |
---|
38 | |
---|
39 | cfa_option options[] = { |
---|
40 | { 'i', "int", "test int", i }, |
---|
41 | { 'u', "unsigned", "test unsigned", u }, |
---|
42 | { 'l', "unsignedlong", "test unsigned long", ul }, |
---|
43 | { 'L', "unsignedlonglong", "test unsigned long long", ull }, |
---|
44 | { 'd', "double", "test double", d }, |
---|
45 | }; |
---|
46 | int options_cnt = sizeof(options) / sizeof(cfa_option); |
---|
47 | |
---|
48 | char **left; |
---|
49 | parse_args( options, options_cnt, "[OPTIONS]...\ntesting bool parameters", left); |
---|
50 | |
---|
51 | sout | "int :" | i; |
---|
52 | sout | "unsigned :" | u; |
---|
53 | sout | "unsigned long :" | ul; |
---|
54 | sout | "unsigned long long :" | ull; |
---|
55 | sout | "double :" | d; |
---|
56 | } |
---|
57 | |
---|
58 | int do_wait(pid_t pid) { |
---|
59 | int wstatus; |
---|
60 | int options = 0; |
---|
61 | pid_t ret = waitpid(pid, &wstatus, options); |
---|
62 | fflush(stdout); |
---|
63 | if(ret < 0) { |
---|
64 | fprintf(stderr, "Fork returned with error: %d '%s'\n", errno, strerror(errno)); |
---|
65 | exit(1); |
---|
66 | } |
---|
67 | return wstatus; |
---|
68 | } |
---|
69 | |
---|
70 | pid_t strict_fork(void) { |
---|
71 | fflush(stdout); |
---|
72 | pid_t ret = fork(); |
---|
73 | if(ret < 0) { |
---|
74 | fprintf(stderr, "Fork returned with error: %d '%s'\n", errno, strerror(errno)); |
---|
75 | exit(1); |
---|
76 | } |
---|
77 | return ret; |
---|
78 | } |
---|
79 | |
---|
80 | void print_status(int wstatus) { |
---|
81 | printf("Child status:\n"); |
---|
82 | printf(" WIFEXITED : %d", WIFEXITED(wstatus)); |
---|
83 | printf(" WEXITSTATUS : %d", WEXITSTATUS(wstatus)); |
---|
84 | printf(" WIFSIGNALED : %d", WIFSIGNALED(wstatus)); |
---|
85 | printf(" WTERMSIG : %d", WTERMSIG(wstatus)); |
---|
86 | printf(" WCOREDUMP : %d", WCOREDUMP(wstatus)); |
---|
87 | printf(" WIFSTOPPED : %d", WIFSTOPPED(wstatus)); |
---|
88 | printf(" WSTOPSIG : %d", WSTOPSIG(wstatus)); |
---|
89 | printf(" WIFCONTINUED: %d\n", WIFCONTINUED(wstatus)); |
---|
90 | } |
---|
91 | |
---|
92 | int true_main(const char * path) { |
---|
93 | char * env[] = { "CFATEST_FORK_EXEC_TEXT=1", 0p }; |
---|
94 | |
---|
95 | printf("no arg:\n"); |
---|
96 | if(pid_t child = strict_fork(); child == 0) { |
---|
97 | int ret = execle(path, "parsenums", (const char*)0p, env); |
---|
98 | if(ret < 0) { |
---|
99 | fprintf(stderr, "Execl 2 returned with error: %d '%s'\n", errno, strerror(errno)); |
---|
100 | exit(1); |
---|
101 | } |
---|
102 | } |
---|
103 | else { |
---|
104 | int status = do_wait(child); |
---|
105 | print_status(status); |
---|
106 | } |
---|
107 | printf("\n"); |
---|
108 | |
---|
109 | printf("all 0 arg:\n"); |
---|
110 | if(pid_t child = strict_fork(); child == 0) { |
---|
111 | int ret = execle(path, "parsenums", "-i=0", "-u=0", "-l=0", "-L=0", "-d=0", (const char*)0p, env); |
---|
112 | if(ret < 0) { |
---|
113 | fprintf(stderr, "Execl 2 returned with error: %d '%s'\n", errno, strerror(errno)); |
---|
114 | exit(1); |
---|
115 | } |
---|
116 | } |
---|
117 | else { |
---|
118 | int status = do_wait(child); |
---|
119 | print_status(status); |
---|
120 | } |
---|
121 | printf("\n"); |
---|
122 | |
---|
123 | printf("negative vals arg:\n"); |
---|
124 | if(pid_t child = strict_fork(); child == 0) { |
---|
125 | int ret = execle(path, "parsenums", "-i=-1", "-d=-1", (const char*)0p, env); |
---|
126 | if(ret < 0) { |
---|
127 | fprintf(stderr, "Execl 2 returned with error: %d '%s'\n", errno, strerror(errno)); |
---|
128 | exit(1); |
---|
129 | } |
---|
130 | } |
---|
131 | else { |
---|
132 | int status = do_wait(child); |
---|
133 | print_status(status); |
---|
134 | } |
---|
135 | printf("\n"); |
---|
136 | |
---|
137 | printf("funky notation arg:\n"); |
---|
138 | if(pid_t child = strict_fork(); child == 0) { |
---|
139 | int ret = execle(path, "parsenums", "-i=0x10", "-u=0x20", "-l=0x300", "-L=0x4000", "-d=5e6", (const char*)0p, env); |
---|
140 | if(ret < 0) { |
---|
141 | fprintf(stderr, "Execl 2 returned with error: %d '%s'\n", errno, strerror(errno)); |
---|
142 | exit(1); |
---|
143 | } |
---|
144 | } |
---|
145 | else { |
---|
146 | int status = do_wait(child); |
---|
147 | print_status(status); |
---|
148 | } |
---|
149 | printf("\n"); |
---|
150 | |
---|
151 | printf("big values arg:\n"); |
---|
152 | if(pid_t child = strict_fork(); child == 0) { |
---|
153 | int ret = execle(path, "parsenums", "-i=2147483647", "-u=4294967295", "-l=" BIG_UNSIGNED_LONG, "-L=18446744073709551615", "-d=5e6", (const char*)0p, env); |
---|
154 | if(ret < 0) { |
---|
155 | fprintf(stderr, "Execl 2 returned with error: %d '%s'\n", errno, strerror(errno)); |
---|
156 | exit(1); |
---|
157 | } |
---|
158 | } |
---|
159 | else { |
---|
160 | int status = do_wait(child); |
---|
161 | print_status(status); |
---|
162 | } |
---|
163 | printf("\n"); |
---|
164 | |
---|
165 | printf("too big values arg:\n"); |
---|
166 | if(pid_t child = strict_fork(); child == 0) { |
---|
167 | int ret = execle(path, "parsenums", "-i=2147483648", (const char*)0p, env); |
---|
168 | if(ret < 0) { |
---|
169 | fprintf(stderr, "Execl 2 returned with error: %d '%s'\n", errno, strerror(errno)); |
---|
170 | exit(1); |
---|
171 | } |
---|
172 | } |
---|
173 | else { |
---|
174 | int status = do_wait(child); |
---|
175 | print_status(status); |
---|
176 | } |
---|
177 | printf("\n"); |
---|
178 | |
---|
179 | if(pid_t child = strict_fork(); child == 0) { |
---|
180 | int ret = execle(path, "parsenums", "-u=4294967296", (const char*)0p, env); |
---|
181 | if(ret < 0) { |
---|
182 | fprintf(stderr, "Execl 2 returned with error: %d '%s'\n", errno, strerror(errno)); |
---|
183 | exit(1); |
---|
184 | } |
---|
185 | } |
---|
186 | else { |
---|
187 | int status = do_wait(child); |
---|
188 | print_status(status); |
---|
189 | } |
---|
190 | printf("\n"); |
---|
191 | |
---|
192 | if(pid_t child = strict_fork(); child == 0) { |
---|
193 | int ret = execle(path, "parsenums", "-l=" TOO_BIG_UNSIGNED_LONG, (const char*)0p, env); |
---|
194 | if(ret < 0) { |
---|
195 | fprintf(stderr, "Execl 2 returned with error: %d '%s'\n", errno, strerror(errno)); |
---|
196 | exit(1); |
---|
197 | } |
---|
198 | } |
---|
199 | else { |
---|
200 | int status = do_wait(child); |
---|
201 | print_status(status); |
---|
202 | } |
---|
203 | printf("\n"); |
---|
204 | |
---|
205 | if(pid_t child = strict_fork(); child == 0) { |
---|
206 | int ret = execle(path, "parsenums", "-L=18446744073709551616", (const char*)0p, env); |
---|
207 | if(ret < 0) { |
---|
208 | fprintf(stderr, "Execl 2 returned with error: %d '%s'\n", errno, strerror(errno)); |
---|
209 | exit(1); |
---|
210 | } |
---|
211 | } |
---|
212 | else { |
---|
213 | int status = do_wait(child); |
---|
214 | print_status(status); |
---|
215 | } |
---|
216 | printf("\n"); |
---|
217 | |
---|
218 | printf("negative errors arg:\n"); |
---|
219 | if(pid_t child = strict_fork(); child == 0) { |
---|
220 | int ret = execle(path, "parsenums", "-u=-1", (const char*)0p, env); |
---|
221 | if(ret < 0) { |
---|
222 | fprintf(stderr, "Execl 2 returned with error: %d '%s'\n", errno, strerror(errno)); |
---|
223 | exit(1); |
---|
224 | } |
---|
225 | } |
---|
226 | else { |
---|
227 | int status = do_wait(child); |
---|
228 | print_status(status); |
---|
229 | } |
---|
230 | printf("\n"); |
---|
231 | |
---|
232 | if(pid_t child = strict_fork(); child == 0) { |
---|
233 | int ret = execle(path, "parsenums", "-l=-1", (const char*)0p, env); |
---|
234 | if(ret < 0) { |
---|
235 | fprintf(stderr, "Execl 2 returned with error: %d '%s'\n", errno, strerror(errno)); |
---|
236 | exit(1); |
---|
237 | } |
---|
238 | } |
---|
239 | else { |
---|
240 | int status = do_wait(child); |
---|
241 | print_status(status); |
---|
242 | } |
---|
243 | printf("\n"); |
---|
244 | |
---|
245 | if(pid_t child = strict_fork(); child == 0) { |
---|
246 | int ret = execle(path, "parsenums", "-L=-1", (const char*)0p, env); |
---|
247 | if(ret < 0) { |
---|
248 | fprintf(stderr, "Execl 2 returned with error: %d '%s'\n", errno, strerror(errno)); |
---|
249 | exit(1); |
---|
250 | } |
---|
251 | } |
---|
252 | else { |
---|
253 | int status = do_wait(child); |
---|
254 | print_status(status); |
---|
255 | } |
---|
256 | printf("\n"); |
---|
257 | |
---|
258 | printf("All Done!\n"); |
---|
259 | |
---|
260 | return 0; |
---|
261 | } |
---|