]> git.ozlabs.org Git - ccan/blobdiff - ccan/opt/test/utils.c
opt: get rid of last remnant of getopt.
[ccan] / ccan / opt / test / utils.c
index fa0a2022e0987368e398161bbce28d60fd43b028..7378d844de74f4be986ca9ce43ea2f5fe91717b4 100644 (file)
@@ -3,7 +3,6 @@
 #include <stdarg.h>
 #include <stdlib.h>
 #include <ccan/opt/opt.h>
-#include <getopt.h>
 #include <string.h>
 #include <stdio.h>
 #include "utils.h"
@@ -29,7 +28,7 @@ void show_arg(char buf[OPT_SHOW_LEN], const char *arg)
 
 char *err_output = NULL;
 
-static void save_err_output(const char *fmt, ...)
+void save_err_output(const char *fmt, ...)
 {
        va_list ap;
        char *p;
@@ -49,7 +48,15 @@ static void save_err_output(const char *fmt, ...)
                err_output = p;
 }      
 
-/* FIXME: This leaks, BTW. */
+void reset_options(void)
+{
+       opt_free_table();
+       free(err_output);
+       err_output = NULL;
+}
+
+static bool allocated = false;
+
 bool parse_args(int *argc, char ***argv, ...)
 {
        char **a;
@@ -63,9 +70,12 @@ bool parse_args(int *argc, char ***argv, ...)
                (*argc)++;
                a = realloc(a, sizeof(*a) * (*argc + 1));
        }
+
+       if (allocated)
+               free(*argv);
+
        *argv = a;
-       /* Re-set before parsing. */
-       optind = 0;
+       allocated = true;
 
        return opt_parse(argc, *argv, save_err_output);
 }