#include #include #include #include #include extern "C" { #include #include #include } #include #include #if __SIZEOF_LONG__ == 4 #define BIG_UNSIGNED_LONG "4294967295" #define TOO_BIG_UNSIGNED_LONG "4294967296" #elif __SIZEOF_LONG__ == 8 #define BIG_UNSIGNED_LONG "18446744073709551615" #define TOO_BIG_UNSIGNED_LONG "18446744073709551616" #else #error unexpected size of long #endif int true_main(const char * exec); int main(int argc, char * argv[]) { if(!getenv("CFATEST_FORK_EXEC_TEXT")) return true_main(argv[0]); int i = -3; unsigned u = 3; unsigned long ul = 3; unsigned long long ull = 3; double d = 3.3; cfa_option options[] = { { 'i', "int", "test int", i }, { 'u', "unsigned", "test unsigned", u }, { 'l', "unsignedlong", "test unsigned long", ul }, { 'L', "unsignedlonglong", "test unsigned long long", ull }, { 'd', "double", "test double", d }, }; int options_cnt = sizeof(options) / sizeof(cfa_option); char **left; parse_args( options, options_cnt, "[OPTIONS]...\ntesting bool parameters", left); sout | "int :" | i; sout | "unsigned :" | u; sout | "unsigned long :" | ul; sout | "unsigned long long :" | ull; sout | "double :" | d; } 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, "parsenums", (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 0 arg:\n"); if(pid_t child = strict_fork(); child == 0) { int ret = execle(path, "parsenums", "-i=0", "-u=0", "-l=0", "-L=0", "-d=0", (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("negative vals arg:\n"); if(pid_t child = strict_fork(); child == 0) { int ret = execle(path, "parsenums", "-i=-1", "-d=-1", (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("funky notation arg:\n"); if(pid_t child = strict_fork(); child == 0) { int ret = execle(path, "parsenums", "-i=0x10", "-u=0x20", "-l=0x300", "-L=0x4000", "-d=5e6", (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("big values arg:\n"); if(pid_t child = strict_fork(); child == 0) { int ret = execle(path, "parsenums", "-i=2147483647", "-u=4294967295", "-l=" BIG_UNSIGNED_LONG, "-L=18446744073709551615", "-d=5e6", (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("too big values arg:\n"); if(pid_t child = strict_fork(); child == 0) { int ret = execle(path, "parsenums", "-i=2147483648", (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"); if(pid_t child = strict_fork(); child == 0) { int ret = execle(path, "parsenums", "-u=4294967296", (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"); if(pid_t child = strict_fork(); child == 0) { int ret = execle(path, "parsenums", "-l=" TOO_BIG_UNSIGNED_LONG, (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"); if(pid_t child = strict_fork(); child == 0) { int ret = execle(path, "parsenums", "-L=18446744073709551616", (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("negative errors arg:\n"); if(pid_t child = strict_fork(); child == 0) { int ret = execle(path, "parsenums", "-u=-1", (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"); if(pid_t child = strict_fork(); child == 0) { int ret = execle(path, "parsenums", "-l=-1", (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"); if(pid_t child = strict_fork(); child == 0) { int ret = execle(path, "parsenums", "-L=-1", (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; }