Commit Graph
18 Commits
Author SHA1 Message Date
Rich Felker 9be4ed5d89 getopt_long_only: don't prefix-match long-options that match short ones
for getopt_long, partial (prefix) matches of long options always begin
with "--" and thus can never be ambiguous with a short option. for
getopt_long_only, though, a single-character option can match both a
short option and as a prefix for a long option. in this case, we
wrongly interpreted it as a prefix for the long option.

introduce a new pass, only in long-only mode, to check the prefix
match against short options before accepting it. the only reason
there's a slightly nontrivial loop being introduced rather than strchr
is that our getopt already supports multibyte short options, and
getopt_long_long should handle them consistently. a temp buffer and
strstr could have been used, but the code to set it up would be just
as large as what's introduced here and it would unnecessarily pull in
relatively large code for strstr.
2018-04-27 11:22:39 -04:00
Samuel HollandandRich Felker 6f03b61b46 getopt_long: accept prefix match of long options containing equals signs
Consider the first equals sign found in the option to be the delimiter
between it and its argument, even if it matches an equals sign in the
option name. This avoids consuming the equals sign, which would prevent
finding the argument. Instead, it forces a partial match of the part of
the option name before the equals sign.

Maintainer's note: GNU getopt_long does not explicitly document this
behavior, but it can be seen as a consequence of how partial matches
are specified, and at least GNU (bfd) ld is known to make use of it.
2018-01-31 22:04:50 -05:00
Samuel HollandandRich Felker f1abc29bd0 fix getopt_long arguments to partial matches
If we find a partial option name match, we need to keep looking for
ambiguous/conflicting options. However, we need to remember the position
in the candidate argument to find its option-argument later, if there is
one. This fixes e.g. option "foobar" being given as "--fooba=baz".
2018-01-31 22:02:49 -05:00
Rich Felker 786fda875a fix getopt[_long] clobbering of optopt on success
getopt is only specified to modify optopt on error, and some software
apparently infers an error from optopt!=0.

getopt_long is changed analogously. the resulting behavior differs
slightly from the behavior of the GNU implementation of getopt_long,
which keeps an internal shadow copy of optopt and copies it to the
public one on return, but since the GNU implementation also exhibits
this shadow-copy behavior for plain getopt where is is non-conforming,
I think this can reasonably be considered a bug rather than an
intentional behavior that merits mimicing.
2017-01-04 19:43:59 -05:00
Rich Felker b24f1d2520 fix getopt_long_only misinterpreting "--" as an option 2016-10-20 12:13:33 -04:00
Rich Felker 63cac4e29a simplify part of getopt_long
as a result of commit e8e4e56a8c,
the later code path for setting optarg to a null pointer is no longer
necessary, and removing it eliminates an indention level and arguably
makes the code more readable.
2015-01-21 13:28:40 -05:00
Rich Felker e8e4e56a8c always set optarg in getopt_long
the standard getopt does not touch optarg unless processing an option
with an argument. however, programs using the GNU getopt API, which we
attempt to provide in getopt_long, expect optarg to be a null pointer
after processing an option without an argument.

before argument permutation support was added, such programs typically
detected its absence and used their own replacement getopt_long,
masking the discrepency in behavior.
2015-01-21 13:16:15 -05:00
Rich Felker 699d4532f6 fix regression in getopt_long support for non-option arguments
commit b72cd07f17 added support for a
this feature in getopt, but it was later broken in the case where
getopt_long is used as a side effect of the changes made in commit
91184c4f16, which prevented the
underlying getopt call from seeing the leading '-' or '+' character in
optstring.

this commit changes the logic in the getopt_long core to check for a
leading colon, possibly after the leading '-' or '+', without
depending on the latter having been skipped by the caller. a minor
incorrectness in the return value for one error condition in
getopt_long is also fixed when opterr has been set to zero but
optstring has no leading ':'.
2015-01-11 16:32:47 -05:00
Rich Felker 0217ed72f9 set optopt in getopt_long
this is undocumented but possibly expected behavior of GNU
getopt_long, and useful when error message printing has been
suppressed.
2014-12-20 19:49:19 -05:00
Rich Felker 91184c4f16 add error message printing to getopt_long and make related improvements
some related changes are also made to getopt, and the return value of
getopt_long in the case of missing arguments is fixed.
2014-12-20 19:44:37 -05:00
Rich Felker da16224fc5 simplify getopt_long argv permutation loop logic 2014-12-13 14:22:58 -05:00
Rich Felker 567cc81c7e fix handling of "--" with getopt_long argv permutation
if argv permutation is used, the option terminator "--" should be
moved before any skipped non-option arguments rather than being left
in the argv tail where the caller will see and interpret it.
2014-12-13 01:04:21 -05:00
Rich Felker b4ef1830b7 accept null longopts pointer in getopt_long
this is an undocumented feature of GNU getopt_long that the BSD
version also mimics, and is reportedly needed by some programs.
2014-12-11 01:07:02 -05:00
Rich Felker cfd7b4acd5 support abbreviated options in getopt_long 2014-12-10 21:26:49 -05:00
Rich Felker 59f6232f85 support options after non-option arguments in getopt_long (argv permutation) 2014-12-10 20:25:32 -05:00
Gianluca AnzolinandRich Felker b72cd07f17 add support for non-option arguments extension to getopt
this is a GNU extension, activated by including '-' as the first
character of the options string, whereby non-option arguments are
processed as if they were arguments to an option character '\1' rather
than ending option processing.
2014-12-02 19:03:04 -05:00
Rich Felker 030e526392 add getopt reset support
based on proposed patches by Daniel Cegiełka, with minor changes:
- use a weak symbol for optreset so it doesn't clash with namespace
- also reset optpos (position in multi-option arg like -lR)
- also make getopt_long support reset
2012-09-30 20:00:38 -04:00
Rich Felker b9bb8f67bb cleanup src/linux and src/misc trees, etc.
previously, it was pretty much random which one of these trees a given
function appeared in. they have now been organized into:

src/linux: non-POSIX linux syscalls (possibly shard with other nixen)
src/legacy: various obsolete/legacy functions, mostly wrappers
src/misc: still mostly uncategorized; some misc POSIX, some nonstd
src/crypt: crypt hash functions

further cleanup will be done later.
2012-09-07 00:48:25 -04:00