From: Tobias Oetiker Date: Tue, 28 Jul 2015 13:59:18 +0000 (+0200) Subject: make optparse consider argc (and not rely on argv[argc] == 0) X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=7eee0942576014987ecc6b2b29057a45458382bf;p=thirdparty%2Frrdtool-1.x.git make optparse consider argc (and not rely on argv[argc] == 0) --- diff --git a/src/optparse.c b/src/optparse.c index 8889b947..911a56be 100644 --- a/src/optparse.c +++ b/src/optparse.c @@ -4,9 +4,13 @@ #define opterror(options, format, args...) \ snprintf(options->errmsg, sizeof(options->errmsg), format, args); -void optparse_init(struct optparse *options, char **argv) +#define options_argv(i) \ + (i) < options->argc ? options->argv[i] : 0; + +void optparse_init(struct optparse *options, int argc, char **argv) { options->argv = argv; + options->argc = argc; options->permute = 1; options->optind = 1; options->subopt = 0; @@ -60,7 +64,7 @@ int optparse(struct optparse *options, const char *optstring) options->errmsg[0] = '\0'; options->optopt = 0; options->optarg = NULL; - char *option = options->argv[options->optind]; + char *option = options_argv(options->optind); if (option == NULL) { return -1; } else if (is_dashdash(option)) { @@ -81,7 +85,7 @@ int optparse(struct optparse *options, const char *optstring) option += options->subopt + 1; options->optopt = option[0]; int type = argtype(optstring, option[0]); - char *next = options->argv[options->optind + 1]; + char *next = options_argv(options->optind + 1); switch (type) { case -1: opterror(options, "invalid option -- '%c'", option[0]); @@ -206,7 +210,7 @@ optparse_long(struct optparse *options, const struct optparse_long *longopts, int *longindex) { - char *option = options->argv[options->optind]; + char *option = options_argv(options->optind); if (option == NULL) { return -1; } else if (is_shortopt(option)) { @@ -243,7 +247,8 @@ optparse_long(struct optparse *options, } if (arg != NULL) { options->optarg = arg; } else if (longopts[i].argtype == OPTPARSE_REQUIRED) { - options->optarg = options->argv[options->optind++]; + options->optarg = options_argv(options->optind); + options->optind++; if (options->optarg == NULL) { opterror(options, "option requires argument -- '%s'", name); return '?'; diff --git a/src/optparse.h b/src/optparse.h index 3e70dc04..c4b0ec19 100644 --- a/src/optparse.h +++ b/src/optparse.h @@ -45,6 +45,7 @@ struct optparse { char **argv; + int argc; int permute; int optind; int optopt; @@ -64,7 +65,7 @@ struct optparse_long { /** * Initializes the parser state. */ -void optparse_init(struct optparse *options, char **argv); +void optparse_init(struct optparse *options, int argc, char **argv); /** * Read the next option in the argv array. diff --git a/src/rrd_xport.c b/src/rrd_xport.c index 2c153258..52bb7a69 100644 --- a/src/rrd_xport.c +++ b/src/rrd_xport.c @@ -1,7 +1,7 @@ /**************************************************************************** * RRDtool 1.4.8 Copyright by Tobi Oetiker, 1997-2013 **************************************************************************** - * rrd_xport.c export RRD data + * rrd_xport.c export RRD data ****************************************************************************/ #include @@ -49,7 +49,7 @@ int rrd_xport( time_t *start, time_t *end, /* which time frame do you want ? * will be changed to represent reality */ - unsigned long *step, /* which stepsize do you want? + unsigned long *step, /* which stepsize do you want? * will be changed to represent reality */ unsigned long *col_cnt, /* number of data columns in the result */ char ***legend_v, /* legend entries */ @@ -60,7 +60,7 @@ int rrd_xport( rrd_time_value_t start_tv, end_tv; char *parsetime_error = NULL; struct optparse options; - optparse_init(&options, argv); + optparse_init(&options, argc, argv); struct optparse_long longopts[] = { {"start", 's', OPTPARSE_REQUIRED}, @@ -184,7 +184,7 @@ int rrd_xport_fn( time_t *start, time_t *end, /* which time frame do you want ? * will be changed to represent reality */ - unsigned long *step, /* which stepsize do you want? + unsigned long *step, /* which stepsize do you want? * will be changed to represent reality */ unsigned long *col_cnt, /* number of data columns in the result */ char ***legend_v, /* legend entries */ @@ -198,7 +198,7 @@ int rrd_xport_fn( unsigned long xport_counter = 0; int *ref_list; long *step_list; - long *step_list_ptr; + long *step_list_ptr; char **legend_list; @@ -211,7 +211,7 @@ int rrd_xport_fn( return -1; /* how many xports? */ - *col_cnt = 0; + *col_cnt = 0; for (i = 0; i < im->gdes_c; i++) { switch (im->gdes[i].gf) { case GF_XPORT: @@ -271,18 +271,18 @@ int rrd_xport_fn( legend_list[j++][0] = '\0'; break; default: - break; + break; } } - *step_list_ptr=0; + *step_list_ptr=0; /* find a common step */ *step = lcd(step_list); /* printf("step: %lu\n",*step); */ free(step_list); - + *start = im->start - im->start % (*step); *end = im->end - im->end % (*step) + (*step); - + /* room for rearranged data */ /* this is a return value! */