]> git.ipfire.org Git - thirdparty/rrdtool-1.x.git/commitdiff
make optparse consider argc (and not rely on argv[argc] == 0)
authorTobias Oetiker <tobi@oetiker.ch>
Tue, 28 Jul 2015 13:59:18 +0000 (15:59 +0200)
committerTobias Oetiker <tobi@oetiker.ch>
Tue, 28 Jul 2015 13:59:18 +0000 (15:59 +0200)
src/optparse.c
src/optparse.h
src/rrd_xport.c

index 8889b9470bc68d77e5f8ef9143a544f30fd57ef5..911a56bef5610e08d385853855a25a29ca70bab8 100644 (file)
@@ -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 '?';
index 3e70dc047d275aa4be39adec7e2a0518dcbc8314..c4b0ec19cc3adcfefa1b475eecbcc8648915c535 100644 (file)
@@ -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.
index 2c15325815d85ecd0b5fde9203a9f09b5c600449..52bb7a69e318bdfc5f65ea81b356e4a4e81e35c5 100644 (file)
@@ -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 <sys/stat.h>
@@ -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! */