aboutsummaryrefslogtreecommitdiff
path: root/src/misc/getopt.c
diff options
context:
space:
mode:
authorRich Felker <dalias@aerifal.cx>2017-01-04 19:02:02 -0500
committerRich Felker <dalias@aerifal.cx>2017-01-04 19:43:59 -0500
commit786fda875a901dc1807289c940338487854cd3ba (patch)
tree88c2340198a714d912e61fd26bae9580d072f1f3 /src/misc/getopt.c
parent150747b41e1ecefe82aa45d68c84b9e957b03e29 (diff)
downloadmusl-786fda875a901dc1807289c940338487854cd3ba.zip
musl-786fda875a901dc1807289c940338487854cd3ba.tar.gz
musl-786fda875a901dc1807289c940338487854cd3ba.tar.bz2
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.
Diffstat (limited to 'src/misc/getopt.c')
-rw-r--r--src/misc/getopt.c3
1 files changed, 2 insertions, 1 deletions
diff --git a/src/misc/getopt.c b/src/misc/getopt.c
index 8290aef..e9bab41 100644
--- a/src/misc/getopt.c
+++ b/src/misc/getopt.c
@@ -60,7 +60,6 @@ int getopt(int argc, char * const argv[], const char *optstring)
c = 0xfffd; /* replacement char */
}
optchar = argv[optind]+optpos;
- optopt = c;
optpos += k;
if (!argv[optind][optpos]) {
@@ -79,6 +78,7 @@ int getopt(int argc, char * const argv[], const char *optstring)
} while (l && d != c);
if (d != c) {
+ optopt = c;
if (optstring[0] != ':' && opterr)
__getopt_msg(argv[0], ": unrecognized option: ", optchar, k);
return '?';
@@ -86,6 +86,7 @@ int getopt(int argc, char * const argv[], const char *optstring)
if (optstring[i] == ':') {
if (optstring[i+1] == ':') optarg = 0;
else if (optind >= argc) {
+ optopt = c;
if (optstring[0] == ':') return ':';
if (opterr) __getopt_msg(argv[0],
": option requires an argument: ",