diff options
author | jilles <jilles@FreeBSD.org> | 2014-05-10 19:18:49 +0000 |
---|---|---|
committer | jilles <jilles@FreeBSD.org> | 2014-05-10 19:18:49 +0000 |
commit | aa09279cea9f52c37d4c07acd51053f2de726203 (patch) | |
tree | 1eb64ce821fdc1b0c352c7e59a44f4087a27f32b /bin/sh/options.c | |
parent | e86f4c348276e0fec90c66af0830ba55e3df88bd (diff) | |
download | FreeBSD-src-aa09279cea9f52c37d4c07acd51053f2de726203.zip FreeBSD-src-aa09279cea9f52c37d4c07acd51053f2de726203.tar.gz |
sh: In getopts, unset OPTARG where POSIX says we should.
Diffstat (limited to 'bin/sh/options.c')
-rw-r--r-- | bin/sh/options.c | 25 |
1 files changed, 12 insertions, 13 deletions
diff --git a/bin/sh/options.c b/bin/sh/options.c index 8594c57..bf00a4e 100644 --- a/bin/sh/options.c +++ b/bin/sh/options.c @@ -446,6 +446,7 @@ getopts(char *optstr, char *optvar, char **optfirst, char ***optnext, int ind = 0; int err = 0; char s[10]; + const char *optarg = NULL; if ((p = *optptr) == NULL || *p == '\0') { /* Current word is done, advance */ @@ -471,14 +472,10 @@ atend: if (optstr[0] == ':') { s[0] = c; s[1] = '\0'; - err |= setvarsafe("OPTARG", s, 0); + optarg = s; } - else { + else out2fmt_flush("Illegal option -%c\n", c); - INTOFF; - (void) unsetvar("OPTARG"); - INTON; - } c = '?'; goto out; } @@ -491,14 +488,11 @@ atend: if (optstr[0] == ':') { s[0] = c; s[1] = '\0'; - err |= setvarsafe("OPTARG", s, 0); + optarg = s; c = ':'; } else { out2fmt_flush("No arg for -%c option\n", c); - INTOFF; - (void) unsetvar("OPTARG"); - INTON; c = '?'; } goto out; @@ -506,16 +500,21 @@ atend: if (p == **optnext) (*optnext)++; - setvarsafe("OPTARG", p, 0); + optarg = p; p = NULL; } - else - setvarsafe("OPTARG", "", 0); out: if (*optnext != NULL) ind = *optnext - optfirst + 1; *optptr = p; + if (optarg != NULL) + err |= setvarsafe("OPTARG", optarg, 0); + else { + INTOFF; + err |= unsetvar("OPTARG"); + INTON; + } fmtstr(s, sizeof(s), "%d", ind); err |= setvarsafe("OPTIND", s, VNOFUNC); s[0] = c; |