#include #include #include #include #include extern "C" { #include #include #include } #include #include int true_main(const char * exec); int main(int argc, char * argv[]) { if(!getenv("CFATEST_FORK_EXEC_TEXT")) return true_main(argv[0]); bool YN = false; bool Yn = false; bool yn = false; bool tf = false; bool st = false; bool sf = true; cfa_option options[] = { {'e', "yesno", "test yes/no", YN, parse_yesno}, {'y', "YN", "test yes/no", Yn, parse_yesno}, {'n', "yn", "test yes/no", yn, parse_yesno}, {'t', "truefalse", "test true/false", tf, parse_truefalse}, {'s', "settrue", "test set true", st, parse_settrue}, {'u', "setfalse", "test set false", sf, parse_setfalse}, }; int options_cnt = sizeof(options) / sizeof(cfa_option); char **left; parse_args( options, options_cnt, "[OPTIONS]...\ntesting bool parameters", left); sout | "yes/no :" | YN; sout | "Y/N :" | Yn; sout | "y/n :" | yn; sout | "true/false :" | tf; sout | "set true :" | st; sout | "set false :" | sf; } int do_wait(pid_t pid) { int wstatus; int options = 0; pid_t ret = waitpid(pid, &wstatus, options); fflush(stdout); if(ret < 0) { fprintf(stderr, "Fork returned with error: %d '%s'\n", errno, strerror(errno)); exit(1); } return wstatus; } pid_t strict_fork(void) { fflush(stdout); pid_t ret = fork(); if(ret < 0) { fprintf(stderr, "Fork returned with error: %d '%s'\n", errno, strerror(errno)); exit(1); } return ret; } void print_status(int wstatus) { printf("Child status:\n"); printf(" WIFEXITED : %d", WIFEXITED(wstatus)); printf(" WEXITSTATUS : %d", WEXITSTATUS(wstatus)); printf(" WIFSIGNALED : %d", WIFSIGNALED(wstatus)); printf(" WTERMSIG : %d", WTERMSIG(wstatus)); printf(" WCOREDUMP : %d", WCOREDUMP(wstatus)); printf(" WIFSTOPPED : %d", WIFSTOPPED(wstatus)); printf(" WSTOPSIG : %d", WSTOPSIG(wstatus)); printf(" WIFCONTINUED: %d\n", WIFCONTINUED(wstatus)); } int true_main(const char * path) { char * env[] = { "CFATEST_FORK_EXEC_TEXT=1", 0p }; printf("no arg:\n"); if(pid_t child = strict_fork(); child == 0) { int ret = execle(path, "parsebools", (const char*)0p, env); if(ret < 0) { fprintf(stderr, "Execl 2 returned with error: %d '%s'\n", errno, strerror(errno)); exit(1); } } else { int status = do_wait(child); print_status(status); } printf("\n"); printf("all true/set arg:\n"); if(pid_t child = strict_fork(); child == 0) { int ret = execle(path, "parsebools", "-e=yes", "-y=Y", "-n=y", "-t=true", "-s", "-u", (const char*)0p, env); if(ret < 0) { fprintf(stderr, "Execl 2 returned with error: %d '%s'\n", errno, strerror(errno)); exit(1); } } else { int status = do_wait(child); print_status(status); } printf("\n"); printf("all false/unset arg:\n"); if(pid_t child = strict_fork(); child == 0) { int ret = execle(path, "parsebools", "-e=no", "-y=N", "-n=n", "-t=false", (const char*)0p, env); if(ret < 0) { fprintf(stderr, "Execl 2 returned with error: %d '%s'\n", errno, strerror(errno)); exit(1); } } else { int status = do_wait(child); print_status(status); } printf("\n"); printf("gibberish arg 1:\n"); if(pid_t child = strict_fork(); child == 0) { int ret = execle(path, "parsebools", "-y=true", (const char*)0p, env); if(ret < 0) { fprintf(stderr, "Execl 2 returned with error: %d '%s'\n", errno, strerror(errno)); exit(1); } } else { int status = do_wait(child); print_status(status); } printf("\n"); printf("gibberish arg 2:\n"); if(pid_t child = strict_fork(); child == 0) { int ret = execle(path, "parsebools", "-t=yes", (const char*)0p, env); if(ret < 0) { fprintf(stderr, "Execl 2 returned with error: %d '%s'\n", errno, strerror(errno)); exit(1); } } else { int status = do_wait(child); print_status(status); } printf("\n"); printf("gibberish arg 3:\n"); if(pid_t child = strict_fork(); child == 0) { int ret = execle(path, "parsebools", "-s=yes", (const char*)0p, env); if(ret < 0) { fprintf(stderr, "Execl 2 returned with error: %d '%s'\n", errno, strerror(errno)); exit(1); } } else { int status = do_wait(child); print_status(status); } printf("\n"); printf("gibberish arg 4:\n"); if(pid_t child = strict_fork(); child == 0) { int ret = execle(path, "parsebools", "-u=yes", (const char*)0p, env); if(ret < 0) { fprintf(stderr, "Execl 2 returned with error: %d '%s'\n", errno, strerror(errno)); exit(1); } } else { int status = do_wait(child); print_status(status); } printf("\n"); printf("All Done!\n"); return 0; }