Hi !

  Looking at the CVS records, it seems that the BSD implementation of
getopt(3) has be thrown away quite a time ago. Thus there shouldn't be
any need to have the REPLACE_GETOPT preprocessing symbol any
more---plus it is now defined unconditionnally. The comments have been
fixed accordingly.

  There also was a typo in the comment accompanying FLAG_ALLARGS's
definition. In the manual it is referred to as '\1'. Which should be
chosen I don't quite know.

  Finally, it turns out that the swapping inner loop runs once too
much. This was harmless, given that it assigns what is indexed by the
cstart variable to itself : when the last iteration begins, pos will
have been incremented nnonopts times by nopts and decremented nopts
times by nnonopts. The global move is nil.

  I hope this might be of some use.

  Cheers,

hyjial.

P.S. : while here I realize I have not got any feedback as regards the
patch to the mbr files. Any comment ?

? getopt.diff
Index: getopt_long.c
===================================================================
RCS file: /cvs/openbsd/src/lib/libc/stdlib/getopt_long.c,v
retrieving revision 1.23
diff -u -r1.23 getopt_long.c
--- getopt_long.c       31 Oct 2007 12:34:57 -0000      1.23
+++ getopt_long.c       26 Jun 2009 09:34:38 -0000
@@ -55,20 +55,16 @@
 #include <stdlib.h>
 #include <string.h>

-#define        REPLACE_GETOPT          /* use this getopt as the system 
getopt(3) */
-
-#ifdef REPLACE_GETOPT
 int    opterr = 1;             /* if error message should be printed */
 int    optind = 1;             /* index into parent argv vector */
 int    optopt = '?';           /* character checked for validity */
 int    optreset;               /* reset getopt */
 char    *optarg;               /* argument associated with option */
-#endif

 #define PRINT_ERROR    ((opterr) && (*options != ':'))

 #define FLAG_PERMUTE   0x01    /* permute non-options to the end of argv */
-#define FLAG_ALLARGS   0x02    /* treat non-options as args to option "-1" */
+#define FLAG_ALLARGS   0x02    /* treat non-options as args to option "1" */
 #define FLAG_LONGONLY  0x04    /* operate as getopt_long_only */

 /* return values */
@@ -135,7 +131,7 @@
        nnonopts = panonopt_end - panonopt_start;
        nopts = opt_end - panonopt_end;
        ncycle = gcd(nnonopts, nopts);
-       cyclelen = (opt_end - panonopt_start) / ncycle;
+       cyclelen = (opt_end - panonopt_start) / ncycle - 1;

        for (i = 0; i < ncycle; i++) {
                cstart = panonopt_end+i;
@@ -391,7 +387,7 @@
         * Check long options if:
         *  1) we were passed some
         *  2) the arg is not just "-"
-        *  3) either the arg starts with -- we are getopt_long_only()
+        *  3) either the arg starts with -- or we are getopt_long_only()
         */
        if (long_options != NULL && place != nargv[optind] &&
            (*place == '-' || (flags & FLAG_LONGONLY))) {
@@ -467,12 +463,9 @@
        return (optchar);
 }

-#ifdef REPLACE_GETOPT
 /*
  * getopt --
  *     Parse argc/argv argument vector.
- *
- * [eventually this will replace the BSD getopt]
  */
 int
 getopt(int nargc, char * const *nargv, const char *options)
@@ -488,7 +481,6 @@
         */
        return (getopt_internal(nargc, nargv, options, NULL, NULL, 0));
 }
-#endif /* REPLACE_GETOPT */

 /*
  * getopt_long --

Reply via email to