summaryrefslogtreecommitdiff
path: root/src/misc/getopt.c
AgeCommit message (Collapse)AuthorLines
2023-04-11getopt: fix null pointer arithmetic ubAlexey Izbyshev-1/+2
When an option that requires an argument is the last character of argv[argc-1], getopt computes argv[argc] + optpos. While optpos is always zero in this case, adding it to null pointer is still undefined.
2019-08-30add public declaration for optreset under appropriate feature profilesRich Felker-0/+1
commit 030e52639248ac8417a4934298caa78c21a228d1 added optreset, a BSD extension to getopt duplicating the functionality (also an extension) of setting optind to 0, but failed to provide a public declaration for it. according to the BSD documentation and headers, the application is not supposed to need to provide its own declaration.
2018-09-12reduce spurious inclusion of libc.hRich Felker-1/+0
libc.h was intended to be a header for access to global libc state and related interfaces, but ended up included all over the place because it was the way to get the weak_alias macro. most of the inclusions removed here are places where weak_alias was needed. a few were recently introduced for hidden. some go all the way back to when libc.h defined CANCELPT_BEGIN and _END, and all (wrongly implemented) cancellation points had to include it. remaining spurious users are mostly callers of the LOCK/UNLOCK macros and files that use the LFS64 macro to define the awful *64 aliases. in a few places, new inclusion of libc.h is added because several internal headers no longer implicitly include libc.h. declarations for __lockfile and __unlockfile are moved from libc.h to stdio_impl.h so that the latter does not need libc.h. putting them in libc.h made no sense at all, since the macros in stdio_impl.h are needed to use them correctly anyway.
2018-09-12use lighter internal stdio lock in getopt error printingRich Felker-2/+3
the public flockfile interface is significantly heavier because it has to handle the possibility of caller returning or thread exiting while holding the lock.
2018-08-22getopt: update optarg and optind correctly on missing argumentRich Felker-6/+6
the text of the specification for getopt's handling of options that require an argument, which requires updating optarg and optind, does not exclude the error case where the end of the argument list has been reached. in that case, it is expected that optarg be assigned argv[argc] (normally null) and optind be incremented by 2, resulting in a value of argc+1.
2018-02-24fix getopt wrongly treating colons in optstring as valid option charsRich Felker-1/+1
the ':' in optstring has special meaning as a flag applying to the previous option character, or to getopt's error handling behavior when it appears at the beginning. don't also accept a "-:" option based on its presence.
2017-01-04fix getopt[_long] clobbering of optopt on successRich Felker-1/+2
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.
2016-02-16fix unlikely corner cases in getopt's message printingRich Felker-2/+2
like fputs (see commit 10a17dfbad2c267d885817abc9c7589fc7ff630b), the message printing code for getopt assumed that fwrite only returns 0 on failure, but it can also happen on success if the total length to be written is zero. programs with zero-length argv[0] were affected. commit 500c6886c654fd45e4926990fee2c61d816be197 introduced this problem in getopt by fixing the fwrite behavior to conform to the requirements of ISO C. previously the wrong expectations of the getopt code were met by the fwrite implementation.
2014-12-20add error message printing to getopt_long and make related improvementsRich Felker-2/+2
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-20support translation for getopt error messagesRich Felker-0/+2
2014-12-19fix stderr locking and ferror semantics in getopt message printingRich Felker-12/+16
if writing the error message fails, POSIX requires that ferror(stderr) be set. and as a function that operates on a stdio stream, getopt is required to lock the stream it uses, stderr. fwrite calls are used instead of fprintf since there is a demand from some users not to pull in heavy stdio machinery via getopt. this mimics the original code using write.
2014-12-10fix getopt handling of initial '+' in optstringRich Felker-1/+1
in the case where an initial '+' was passed in optstring (a getopt_long feature to suppress argv permutation), getopt would fail to see a possible subsequent ':', resulting in incorrect handling of missing arguments.
2014-12-04fix getopt handling of ':' modifier for multibyte option charactersRich Felker-4/+9
the previous hard-coded offsets of +1 and +2 contained a hidden assumption that the option character matched was single-byte, despite this implementation of getopt attempting to support multibyte option characters. this patch reworks the matching logic to leave the final index pointing just past the matched character so that fixed offsets can be used to check for ':'.
2014-12-02add support for non-option arguments extension to getoptGianluca Anzolin-1/+16
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-11-15getopt: fix optional argument processingFelix Fietkau-2/+2
Processing an option character with optional argument fails if the option is last on the command line. This happens because the if (optind >= argc) check runs first before testing for optional argument.
2014-06-11support optional-argument extension to getopt via double-colonRich Felker-2/+5
this extension is not incompatible with the standard behavior of the function, not expensive, and avoids requiring a replacement getopt with full GNU extensions for a few important apps including busybox's sed with the -i option.
2013-04-05Add ABI compatability aliases.Isaac Dunham-0/+2
GNU used several extensions that were incompatible with C99 and POSIX, so they used alternate names for the standard functions. The result is that we need these to run standards-conformant programs that were linked with glibc.
2012-09-30add getopt reset supportRich Felker-2/+11
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
2011-02-12initial check-in, version 0.5.0v0.5.0Rich Felker-0/+63