]> cloudbase.mooo.com Git - z180-stamp.git/blobdiff - avr/getopt-min.c
Bugfix: reset getopt vars on start of a new argument line
[z180-stamp.git] / avr / getopt-min.c
index f5ad912f281e1a73085160f6282e9754e04d1bea..8508f401a725329c5aeb35766efaa06392c43d79 100644 (file)
@@ -13,7 +13,7 @@
 #include "common.h"            /* definition of FLASH */
 #include <string.h>
 
-int    optind = 1;                     /* next argv[] index */
+int    optind = 0;                     /* next argv[] index */
 char *optarg;          /* option parameter if any */
 
 
@@ -23,12 +23,16 @@ getopt(                             /* returns letter, '?', EOF */
        char    *const argv[],  /* argument vector from main */
        const FLASH char *optstring )           /* allowed args, e.g. "ab:c" */
 {
-       static int      sp = 1;         /* position within argument */
+       static int      sp;             /* position within argument */
        int             osp;            /* saved `sp' for param test */
        int             c;              /* option letter */
        const FLASH char *cp;           /* -> option in `optstring' */
 
        optarg = NULL;
+       if (optind == 0) {              /* start a new argument scan */
+               optind = 1;
+               sp = 1;
+       }
 
        if ( sp == 1 )                  /* fresh argument */
        {