about summary refs log tree commit diff
path: root/posix/getopt.c
diff options
context:
space:
mode:
authorZack Weinberg <zackw@panix.com>2017-04-01 12:42:41 -0400
committerZack Weinberg <zackw@panix.com>2017-04-07 07:50:31 -0400
commitc1af8775f2de694bd567813af51164e2d978a78d (patch)
treefb0696a62187bc79ad4ed5f3186c9d65389ad79a /posix/getopt.c
parent7a7be6c9a2a89ac5783d4f27d67b0fae3218228f (diff)
downloadglibc-c1af8775f2de694bd567813af51164e2d978a78d.tar.gz
glibc-c1af8775f2de694bd567813af51164e2d978a78d.tar.xz
glibc-c1af8775f2de694bd567813af51164e2d978a78d.zip
getopt: tidy up _getopt_initialize a bit
_getopt_data.__posixly_correct is completely redundant to
_getopt_data.__ordering, and some work that logically belongs in
_getopt_initialize was being done by _getopt_internal_r, making the
code harder to understand.

As a side effect, getenv will no longer be called if the first
character of the options string is '+' or '-', which is probably a
Good Thing.  (Perhaps we should have a flag character that
specifically asks for the permutation behavior?)

	* posix/getopt_int.h (_getopt_data): Remove __posixly_correct field.
	* posix/getopt.c (_getopt_internal_r): Move some initialization code...
	(_getopt_initialize): ...here. Don't set d->__posixly_correct.
Diffstat (limited to 'posix/getopt.c')
-rw-r--r--posix/getopt.c18
1 files changed, 6 insertions, 12 deletions
diff --git a/posix/getopt.c b/posix/getopt.c
index f54bc2d1cf..609638c193 100644
--- a/posix/getopt.c
+++ b/posix/getopt.c
@@ -188,15 +188,13 @@ _getopt_initialize (int argc, char **argv, const char *optstring,
   /* Start processing options with ARGV-element 1 (since ARGV-element 0
      is the program name); the sequence of previously skipped
      non-option ARGV-elements is empty.  */
+  if (d->optind == 0)
+    d->optind = 1;
 
   d->__first_nonopt = d->__last_nonopt = d->optind;
-
   d->__nextchar = NULL;
 
-  d->__posixly_correct = posixly_correct || !!getenv ("POSIXLY_CORRECT");
-
   /* Determine how to handle the ordering of options and nonoptions.  */
-
   if (optstring[0] == '-')
     {
       d->__ordering = RETURN_IN_ORDER;
@@ -207,11 +205,12 @@ _getopt_initialize (int argc, char **argv, const char *optstring,
       d->__ordering = REQUIRE_ORDER;
       ++optstring;
     }
-  else if (d->__posixly_correct)
+  else if (posixly_correct || !!getenv ("POSIXLY_CORRECT"))
     d->__ordering = REQUIRE_ORDER;
   else
     d->__ordering = PERMUTE;
 
+  d->__initialized = 1;
   return optstring;
 }
 
@@ -284,15 +283,10 @@ _getopt_internal_r (int argc, char **argv, const char *optstring,
   d->optarg = NULL;
 
   if (d->optind == 0 || !d->__initialized)
-    {
-      if (d->optind == 0)
-	d->optind = 1;  /* Don't scan ARGV[0], the program name.  */
-      optstring = _getopt_initialize (argc, argv, optstring, d,
-				      posixly_correct);
-      d->__initialized = 1;
-    }
+    optstring = _getopt_initialize (argc, argv, optstring, d, posixly_correct);
   else if (optstring[0] == '-' || optstring[0] == '+')
     optstring++;
+
   if (optstring[0] == ':')
     print_errors = 0;