X-Git-Url: http://git.ozlabs.org/?p=ccan;a=blobdiff_plain;f=ccan%2Fopt%2Ftest%2Futils.c;h=c2967fca173d51010b810fcaa626eb6ebff420df;hp=21e0b727ebd6cab4762526dc5a21a11a3eb5f072;hb=e7e57cbf6725debd99238ccd8e3bf273a8d7e61c;hpb=ac9d55d8c5af9697be8c4dd4f27de61e3cb8bf95 diff --git a/ccan/opt/test/utils.c b/ccan/opt/test/utils.c index 21e0b727..c2967fca 100644 --- a/ccan/opt/test/utils.c +++ b/ccan/opt/test/utils.c @@ -3,7 +3,6 @@ #include #include #include -#include #include #include #include "utils.h" @@ -77,12 +76,33 @@ bool parse_args(int *argc, char ***argv, ...) *argv = a; allocated = true; - /* Re-set before parsing. */ - optind = 0; return opt_parse(argc, *argv, save_err_output); } +bool parse_early_args(int *argc, char ***argv, ...) +{ + char **a; + va_list ap; + + va_start(ap, argv); + *argc = 1; + a = malloc(sizeof(*a) * (*argc + 1)); + a[0] = (*argv)[0]; + while ((a[*argc] = va_arg(ap, char *)) != NULL) { + (*argc)++; + a = realloc(a, sizeof(*a) * (*argc + 1)); + } + + if (allocated) + free(*argv); + + *argv = a; + allocated = true; + + return opt_early_parse(*argc, *argv, save_err_output); +} + struct opt_table short_table[] = { /* Short opts, different args. */ OPT_WITHOUT_ARG("-a", test_noarg, "a", "Description of a"),