X-Git-Url: https://git.ozlabs.org/?a=blobdiff_plain;ds=sidebyside;f=ccan%2Fopt%2Ftest%2Frun-helpers.c;h=589962fe4d8fadaffe6dc4cafd2764b2b5838a22;hb=c583682a322ead6031ff552fa8f7d99d4c4cc735;hp=e0fbd32e6bcf6f6a28c17e9818a169ff22997747;hpb=abde990702b6d6c3739edf85578af8529608ade5;p=ccan diff --git a/ccan/opt/test/run-helpers.c b/ccan/opt/test/run-helpers.c index e0fbd32e..589962fe 100644 --- a/ccan/opt/test/run-helpers.c +++ b/ccan/opt/test/run-helpers.c @@ -27,13 +27,6 @@ static void *saved_malloc(size_t size); #include #include -static void reset_options(void) -{ - free(opt_table); - opt_table = NULL; - opt_count = opt_num_short = opt_num_short_arg = opt_num_long = 0; -} - static char *output = NULL; static int saved_vprintf(const char *fmt, va_list ap) @@ -79,6 +72,18 @@ static void *saved_malloc(size_t size) return last_allocation = malloc(size); } +static void set_args(int *argc, char ***argv, ...) +{ + va_list ap; + *argv = malloc(sizeof(**argv) * 20); + + va_start(ap, argv); + for (*argc = 0; + ((*argv)[*argc] = va_arg(ap, char*)) != NULL; + (*argc)++); + va_end(ap); +} + /* Test helpers. */ int main(int argc, char *argv[]) { @@ -992,11 +997,7 @@ int main(int argc, char *argv[]) /* parse_args allocates argv */ free(argv); - argc = 2; - argv = malloc(sizeof(argv[0]) * 3); - argv[0] = (char *)"thisprog"; - argv[1] = (char *)"-a"; - argv[2] = NULL; + set_args(&argc, &argv, "thisprog", "-a", NULL); exitval = setjmp(exited); if (exitval == 0) { @@ -1018,11 +1019,7 @@ int main(int argc, char *argv[]) opt_register_noarg("-a", opt_usage_and_exit, "[args]", ""); - argc = 2; - argv = malloc(sizeof(argv[0]) * 3); - argv[0] = (char *)"thisprog"; - argv[1] = (char *)"-a"; - argv[2] = NULL; + set_args(&argc, &argv, "thisprog", "-a", NULL); exitval = setjmp(exited); if (exitval == 0) { @@ -1157,11 +1154,7 @@ int main(int argc, char *argv[]) opt_register_noarg("-a", opt_usage_and_exit, "[args]", ""); - argc = 2; - argv = malloc(sizeof(argv[0]) * 3); - argv[0] = (char *)"thisprog"; - argv[1] = (char *)"--garbage"; - argv[2] = NULL; + set_args(&argc, &argv, "thisprog", "--garbage", NULL); ok1(!opt_parse(&argc, argv, opt_log_stderr)); ok1(!strcmp(output, "thisprog: --garbage: unrecognized option\n")); @@ -1176,11 +1169,7 @@ int main(int argc, char *argv[]) reset_options(); opt_register_noarg("-a", opt_usage_and_exit, "[args]", ""); - argc = 2; - argv = malloc(sizeof(argv[0]) * 3); - argv[0] = (char *)"thisprog"; - argv[1] = (char *)"--garbage"; - argv[2] = NULL; + set_args(&argc, &argv, "thisprog", "--garbage", NULL); exitval = setjmp(exited); if (exitval == 0) { opt_parse(&argc, argv, opt_log_stderr_exit);