]> Savannah Git Hosting - gnulib.git/commitdiff
getopt: merge from glibc: repetition reduction
authorZack Weinberg <zackw@panix.com>
Thu, 6 Apr 2017 18:14:14 +0000 (11:14 -0700)
committerPaul Eggert <eggert@cs.ucla.edu>
Thu, 6 Apr 2017 22:42:03 +0000 (15:42 -0700)
The definitions of the entry point functions 'getopt' and
'__posix_getopt' can be made substantially less repetitive with a
helper macro.

While I was merging the const-correctness changes from gnulib into
glibc I noticed there are still some unnecessary casts in
_getopt_internal_r.

* lib/getopt.c (getopt, __posix_getopt): Eliminate repetition with
a macro.  Consistently cast 'argv' to 'char **' when calling
_getopt_internal.
(_getopt_internal_r): Remove unnecessary casts when calling exchange.

ChangeLog
lib/getopt.c

index 06fe3b81d76d00d14fae0d471a9eb52308f80051..dc573dbec4287f6f338f9e0e9fdfa9946c38276a 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,21 @@
 2017-04-06  Zack Weinberg  <zackw@panix.com>
 
+       getopt: merge from glibc: repetition reduction
+
+       The definitions of the entry point functions 'getopt' and
+       '__posix_getopt' can be made substantially less repetitive with a
+       helper macro.
+
+       While I was merging the const-correctness changes from gnulib into
+       glibc I noticed there are still some unnecessary casts in
+       _getopt_internal_r.
+
+       * lib/getopt.c (getopt, __posix_getopt): Eliminate repetition with
+       a macro.  Consistently cast 'argv' to 'char **' when calling
+       _getopt_internal.
+       (_getopt_internal_r): Remove unnecessary casts when calling exchange.
+
+
        getopt: clean up error reporting
 
        getopt can print a whole bunch of error messages, and when used
index ddf3e4c9f5b0d507f367cadb00abcb40703db793..77528728970f9879877b9012fa6f2681fad336dd 100644 (file)
@@ -318,7 +318,7 @@ _getopt_internal_r (int argc, char **argv, const char *optstring,
 
          if (d->__first_nonopt != d->__last_nonopt
              && d->__last_nonopt != d->optind)
-           exchange ((char **) argv, d);
+           exchange (argv, d);
          else if (d->__last_nonopt != d->optind)
            d->__first_nonopt = d->optind;
 
@@ -341,7 +341,7 @@ _getopt_internal_r (int argc, char **argv, const char *optstring,
 
          if (d->__first_nonopt != d->__last_nonopt
              && d->__last_nonopt != d->optind)
-           exchange ((char **) argv, d);
+           exchange (argv, d);
          else if (d->__first_nonopt == d->__last_nonopt)
            d->__first_nonopt = d->optind;
          d->__last_nonopt = argc;
@@ -824,32 +824,23 @@ _getopt_internal (int argc, char **argv, const char *optstring,
   return result;
 }
 
-/* glibc gets a LSB-compliant getopt.
-   Standalone applications get a POSIX-compliant getopt.  */
-#if _LIBC
-enum { POSIXLY_CORRECT = 0 };
-#else
-enum { POSIXLY_CORRECT = 1 };
-#endif
-
-int
-getopt (int argc, char *const *argv, const char *optstring)
-{
-  return _getopt_internal (argc, (char **) argv, optstring,
-                          (const struct option *) 0,
-                          (int *) 0,
-                          0, POSIXLY_CORRECT);
-}
+/* glibc gets a LSB-compliant getopt and a POSIX-complaint __posix_getopt.
+   Standalone applications just get a POSIX-compliant getopt.
+   POSIX and LSB both require these functions to take 'char *const *argv'
+   even though this is incorrect (because of the permutation).  */
+#define GETOPT_ENTRY(NAME, POSIXLY_CORRECT)                    \
+  int                                                          \
+  NAME (int argc, char *const *argv, const char *optstring)    \
+  {                                                            \
+    return _getopt_internal (argc, (char **)argv, optstring,   \
+                            0, 0, 0, POSIXLY_CORRECT);         \
+  }
 
 #ifdef _LIBC
-int
-__posix_getopt (int argc, char *const *argv, const char *optstring)
-{
-  return _getopt_internal (argc, argv, optstring,
-                          (const struct option *) 0,
-                          (int *) 0,
-                          0, 1);
-}
+GETOPT_ENTRY(getopt, 0)
+GETOPT_ENTRY(__posix_getopt, 1)
+#else
+GETOPT_ENTRY(getopt, 1)
 #endif
 
 \f