From 2dcee3a7916d833622476ddd464a549db9907f1b Mon Sep 17 00:00:00 2001 From: Zack Weinberg Date: Thu, 6 Apr 2017 11:14:13 -0700 Subject: [PATCH] getopt: harmonize comments with glibc The comments explaining how the behavior of 'getopt' varies depending on whether it's the standalone version and whether there are special characters at the beginning of the options string were inconsistent between gnulib and glibc, and also out of sync with the code. * lib/getopt.c, lib/getopt_int.h: Harmonize comments with glibc. --- ChangeLog | 10 ++++++++++ lib/getopt.c | 31 +++++++++++++++++++------------ lib/getopt_int.h | 29 ++++++++++------------------- 3 files changed, 39 insertions(+), 31 deletions(-) diff --git a/ChangeLog b/ChangeLog index 0e51f1d5ac..2c8109197b 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,15 @@ 2017-04-06 Zack Weinberg + getopt: harmonize comments with glibc + + The comments explaining how the behavior of 'getopt' varies depending + on whether it's the standalone version and whether there are special + characters at the beginning of the options string were inconsistent + between gnulib and glibc, and also out of sync with the code. + + * lib/getopt.c, lib/getopt_int.h: Harmonize comments with glibc. + + getopt: remove USE_NONOPTION_FLAGS getopt includes code to parse an environment variable named diff --git a/lib/getopt.c b/lib/getopt.c index 157a737f9e..65ad56780f 100644 --- a/lib/getopt.c +++ b/lib/getopt.c @@ -41,20 +41,27 @@ # include #endif -/* This version of 'getopt' appears to the caller like standard Unix 'getopt' - but it behaves differently for the user, since it allows the user - to intersperse the options with the other arguments. - - As 'getopt_long' works, it permutes the elements of ARGV so that, - when it is done, all the options precede everything else. Thus - all application programs are extended to handle flexible argument order. - - Using 'getopt' or setting the environment variable POSIXLY_CORRECT +/* This implementation of 'getopt' has three modes for handling + options interspersed with non-option arguments. It can stop + scanning for options at the first non-option argument encountered, + as POSIX specifies. It can continue scanning for options after the + first non-option argument, but permute 'argv' as it goes so that, + after 'getopt' is done, all the options precede all the non-option + arguments and 'optind' points to the first non-option argument. + Or, it can report non-option arguments as if they were arguments to + the option character '\x01'. + + The default behavior of 'getopt_long' is to permute the argument list. + When this implementation is used standalone, the default behavior of + 'getopt' is to stop at the first non-option argument, but when it is + used as part of GNU libc it also permutes the argument list. In both + cases, setting the environment variable POSIXLY_CORRECT to any value disables permutation. - Then the behavior is completely standard. - GNU application programs can use a third alternative mode in which - they can distinguish the relative order of options and other arguments. */ + If the first character of the OPTSTRING argument to 'getopt' or + 'getopt_long' is '+', both functions will stop at the first + non-option argument. If it is '-', both functions will report + non-option arguments as arguments to the option character '\x01'. */ #include "getopt_int.h" diff --git a/lib/getopt_int.h b/lib/getopt_int.h index 5a5528c626..762679a305 100644 --- a/lib/getopt_int.h +++ b/lib/getopt_int.h @@ -32,28 +32,20 @@ extern int _getopt_internal (int ___argc, char **___argv, /* Describe how to deal with options that follow non-option ARGV-elements. - If the caller did not specify anything, - the default is REQUIRE_ORDER if the environment variable - POSIXLY_CORRECT is defined, PERMUTE otherwise. - - REQUIRE_ORDER means don't recognize them as options; - stop option processing when the first non-option is seen. - This is what Unix does. - This mode of operation is selected by either setting the environment - variable POSIXLY_CORRECT, or using '+' as the first character - of the list of option characters, or by calling getopt. - - PERMUTE is the default. We permute the contents of ARGV as we - scan, so that eventually all the non-options are at the end. - This allows options to be given in any order, even with programs - that were not written to expect this. + REQUIRE_ORDER means don't recognize them as options; stop option + processing when the first non-option is seen. This is what POSIX + specifies should happen. + + PERMUTE means permute the contents of ARGV as we scan, so that + eventually all the non-options are at the end. This allows options + to be given in any order, even with programs that were not written + to expect this. RETURN_IN_ORDER is an option available to programs that were written to expect options and other ARGV-elements in any order and that care about the ordering of the two. We describe each non-option ARGV-element as if it were the argument of an option - with character code 1. Using '-' as the first character of the - list of option characters selects this mode of operation. + with character code 1. The special argument '--' forces an end of option-scanning regardless of the value of 'ordering'. In the case of RETURN_IN_ORDER, only @@ -91,8 +83,7 @@ struct _getopt_data /* See __ord above. */ enum __ord __ordering; - /* If the POSIXLY_CORRECT environment variable is set - or getopt was called. */ + /* True if behaving strictly as specified by POSIX. */ int __posixly_correct; -- 2.39.5