[fd90096] | 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 | int true_main(const char * exec); |
---|
| 18 | |
---|
| 19 | int main(int argc, char * argv[]) { |
---|
| 20 | if(!getenv("CFATEST_FORK_EXEC_TEXT")) return true_main(argv[0]); |
---|
| 21 | |
---|
| 22 | bool YN = false; |
---|
| 23 | bool Yn = false; |
---|
| 24 | bool yn = false; |
---|
| 25 | bool tf = false; |
---|
| 26 | bool st = false; |
---|
| 27 | bool sf = true; |
---|
| 28 | |
---|
| 29 | cfa_option options[] = { |
---|
| 30 | {'e', "yesno", "test yes/no", YN, parse_yesno}, |
---|
| 31 | {'y', "YN", "test yes/no", Yn, parse_yesno}, |
---|
| 32 | {'n', "yn", "test yes/no", yn, parse_yesno}, |
---|
| 33 | {'t', "truefalse", "test true/false", tf, parse_truefalse}, |
---|
| 34 | {'s', "settrue", "test set true", st, parse_settrue}, |
---|
| 35 | {'u', "setfalse", "test set false", sf, parse_setfalse}, |
---|
| 36 | }; |
---|
| 37 | int options_cnt = sizeof(options) / sizeof(cfa_option); |
---|
| 38 | |
---|
| 39 | char **left; |
---|
| 40 | parse_args( options, options_cnt, "[OPTIONS]...\ntesting bool parameters", left); |
---|
| 41 | |
---|
| 42 | sout | "yes/no :" | YN; |
---|
| 43 | sout | "Y/N :" | Yn; |
---|
| 44 | sout | "y/n :" | yn; |
---|
| 45 | sout | "true/false :" | tf; |
---|
| 46 | sout | "set true :" | st; |
---|
| 47 | sout | "set false :" | sf; |
---|
| 48 | } |
---|
| 49 | |
---|
| 50 | int do_wait(pid_t pid) { |
---|
| 51 | int wstatus; |
---|
| 52 | int options = 0; |
---|
| 53 | pid_t ret = waitpid(pid, &wstatus, options); |
---|
| 54 | fflush(stdout); |
---|
| 55 | if(ret < 0) { |
---|
| 56 | fprintf(stderr, "Fork returned with error: %d '%s'\n", errno, strerror(errno)); |
---|
| 57 | exit(1); |
---|
| 58 | } |
---|
| 59 | return wstatus; |
---|
| 60 | } |
---|
| 61 | |
---|
| 62 | pid_t strict_fork(void) { |
---|
| 63 | fflush(stdout); |
---|
| 64 | pid_t ret = fork(); |
---|
| 65 | if(ret < 0) { |
---|
| 66 | fprintf(stderr, "Fork returned with error: %d '%s'\n", errno, strerror(errno)); |
---|
| 67 | exit(1); |
---|
| 68 | } |
---|
| 69 | return ret; |
---|
| 70 | } |
---|
| 71 | |
---|
| 72 | void print_status(int wstatus) { |
---|
| 73 | printf("Child status:\n"); |
---|
| 74 | printf(" WIFEXITED : %d", WIFEXITED(wstatus)); |
---|
| 75 | printf(" WEXITSTATUS : %d", WEXITSTATUS(wstatus)); |
---|
| 76 | printf(" WIFSIGNALED : %d", WIFSIGNALED(wstatus)); |
---|
| 77 | printf(" WTERMSIG : %d", WTERMSIG(wstatus)); |
---|
| 78 | printf(" WCOREDUMP : %d", WCOREDUMP(wstatus)); |
---|
| 79 | printf(" WIFSTOPPED : %d", WIFSTOPPED(wstatus)); |
---|
| 80 | printf(" WSTOPSIG : %d", WSTOPSIG(wstatus)); |
---|
| 81 | printf(" WIFCONTINUED: %d\n", WIFCONTINUED(wstatus)); |
---|
| 82 | } |
---|
| 83 | |
---|
| 84 | int true_main(const char * path) { |
---|
| 85 | char * env[] = { "CFATEST_FORK_EXEC_TEXT=1", 0p }; |
---|
| 86 | |
---|
| 87 | printf("no arg:\n"); |
---|
| 88 | if(pid_t child = strict_fork(); child == 0) { |
---|
| 89 | int ret = execle(path, "parsebools", (const char*)0p, env); |
---|
| 90 | if(ret < 0) { |
---|
| 91 | fprintf(stderr, "Execl 2 returned with error: %d '%s'\n", errno, strerror(errno)); |
---|
| 92 | exit(1); |
---|
| 93 | } |
---|
| 94 | } |
---|
| 95 | else { |
---|
| 96 | int status = do_wait(child); |
---|
| 97 | print_status(status); |
---|
| 98 | } |
---|
| 99 | printf("\n"); |
---|
| 100 | |
---|
| 101 | printf("all true/set arg:\n"); |
---|
| 102 | if(pid_t child = strict_fork(); child == 0) { |
---|
| 103 | int ret = execle(path, "parsebools", "-e=yes", "-y=Y", "-n=y", "-t=true", "-s", "-u", (const char*)0p, env); |
---|
| 104 | if(ret < 0) { |
---|
| 105 | fprintf(stderr, "Execl 2 returned with error: %d '%s'\n", errno, strerror(errno)); |
---|
| 106 | exit(1); |
---|
| 107 | } |
---|
| 108 | } |
---|
| 109 | else { |
---|
| 110 | int status = do_wait(child); |
---|
| 111 | print_status(status); |
---|
| 112 | } |
---|
| 113 | printf("\n"); |
---|
| 114 | |
---|
| 115 | printf("all false/unset arg:\n"); |
---|
| 116 | if(pid_t child = strict_fork(); child == 0) { |
---|
| 117 | int ret = execle(path, "parsebools", "-e=no", "-y=N", "-n=n", "-t=false", (const char*)0p, env); |
---|
| 118 | if(ret < 0) { |
---|
| 119 | fprintf(stderr, "Execl 2 returned with error: %d '%s'\n", errno, strerror(errno)); |
---|
| 120 | exit(1); |
---|
| 121 | } |
---|
| 122 | } |
---|
| 123 | else { |
---|
| 124 | int status = do_wait(child); |
---|
| 125 | print_status(status); |
---|
| 126 | } |
---|
| 127 | printf("\n"); |
---|
| 128 | |
---|
| 129 | printf("gibberish arg 1:\n"); |
---|
| 130 | if(pid_t child = strict_fork(); child == 0) { |
---|
| 131 | int ret = execle(path, "parsebools", "-y=true", (const char*)0p, env); |
---|
| 132 | if(ret < 0) { |
---|
| 133 | fprintf(stderr, "Execl 2 returned with error: %d '%s'\n", errno, strerror(errno)); |
---|
| 134 | exit(1); |
---|
| 135 | } |
---|
| 136 | } |
---|
| 137 | else { |
---|
| 138 | int status = do_wait(child); |
---|
| 139 | print_status(status); |
---|
| 140 | } |
---|
| 141 | printf("\n"); |
---|
| 142 | |
---|
| 143 | printf("gibberish arg 2:\n"); |
---|
| 144 | if(pid_t child = strict_fork(); child == 0) { |
---|
| 145 | int ret = execle(path, "parsebools", "-t=yes", (const char*)0p, env); |
---|
| 146 | if(ret < 0) { |
---|
| 147 | fprintf(stderr, "Execl 2 returned with error: %d '%s'\n", errno, strerror(errno)); |
---|
| 148 | exit(1); |
---|
| 149 | } |
---|
| 150 | } |
---|
| 151 | else { |
---|
| 152 | int status = do_wait(child); |
---|
| 153 | print_status(status); |
---|
| 154 | } |
---|
| 155 | printf("\n"); |
---|
| 156 | |
---|
| 157 | printf("gibberish arg 3:\n"); |
---|
| 158 | if(pid_t child = strict_fork(); child == 0) { |
---|
| 159 | int ret = execle(path, "parsebools", "-s=yes", (const char*)0p, env); |
---|
| 160 | if(ret < 0) { |
---|
| 161 | fprintf(stderr, "Execl 2 returned with error: %d '%s'\n", errno, strerror(errno)); |
---|
| 162 | exit(1); |
---|
| 163 | } |
---|
| 164 | } |
---|
| 165 | else { |
---|
| 166 | int status = do_wait(child); |
---|
| 167 | print_status(status); |
---|
| 168 | } |
---|
| 169 | printf("\n"); |
---|
| 170 | |
---|
| 171 | printf("gibberish arg 4:\n"); |
---|
| 172 | if(pid_t child = strict_fork(); child == 0) { |
---|
| 173 | int ret = execle(path, "parsebools", "-u=yes", (const char*)0p, env); |
---|
| 174 | if(ret < 0) { |
---|
| 175 | fprintf(stderr, "Execl 2 returned with error: %d '%s'\n", errno, strerror(errno)); |
---|
| 176 | exit(1); |
---|
| 177 | } |
---|
| 178 | } |
---|
| 179 | else { |
---|
| 180 | int status = do_wait(child); |
---|
| 181 | print_status(status); |
---|
| 182 | } |
---|
| 183 | printf("\n"); |
---|
| 184 | |
---|
| 185 | printf("All Done!\n"); |
---|
| 186 | |
---|
| 187 | return 0; |
---|
| 188 | } |
---|