sh: In getopts, unset OPTARG where POSIX says we should.
This commit is contained in:
parent
ca5e4fe970
commit
c8fb3e69d0
@ -446,6 +446,7 @@ getopts(char *optstr, char *optvar, char **optfirst, char ***optnext,
|
|||||||
int ind = 0;
|
int ind = 0;
|
||||||
int err = 0;
|
int err = 0;
|
||||||
char s[10];
|
char s[10];
|
||||||
|
const char *optarg = NULL;
|
||||||
|
|
||||||
if ((p = *optptr) == NULL || *p == '\0') {
|
if ((p = *optptr) == NULL || *p == '\0') {
|
||||||
/* Current word is done, advance */
|
/* Current word is done, advance */
|
||||||
@ -471,14 +472,10 @@ getopts(char *optstr, char *optvar, char **optfirst, char ***optnext,
|
|||||||
if (optstr[0] == ':') {
|
if (optstr[0] == ':') {
|
||||||
s[0] = c;
|
s[0] = c;
|
||||||
s[1] = '\0';
|
s[1] = '\0';
|
||||||
err |= setvarsafe("OPTARG", s, 0);
|
optarg = s;
|
||||||
}
|
}
|
||||||
else {
|
else
|
||||||
out2fmt_flush("Illegal option -%c\n", c);
|
out2fmt_flush("Illegal option -%c\n", c);
|
||||||
INTOFF;
|
|
||||||
(void) unsetvar("OPTARG");
|
|
||||||
INTON;
|
|
||||||
}
|
|
||||||
c = '?';
|
c = '?';
|
||||||
goto out;
|
goto out;
|
||||||
}
|
}
|
||||||
@ -491,14 +488,11 @@ getopts(char *optstr, char *optvar, char **optfirst, char ***optnext,
|
|||||||
if (optstr[0] == ':') {
|
if (optstr[0] == ':') {
|
||||||
s[0] = c;
|
s[0] = c;
|
||||||
s[1] = '\0';
|
s[1] = '\0';
|
||||||
err |= setvarsafe("OPTARG", s, 0);
|
optarg = s;
|
||||||
c = ':';
|
c = ':';
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
out2fmt_flush("No arg for -%c option\n", c);
|
out2fmt_flush("No arg for -%c option\n", c);
|
||||||
INTOFF;
|
|
||||||
(void) unsetvar("OPTARG");
|
|
||||||
INTON;
|
|
||||||
c = '?';
|
c = '?';
|
||||||
}
|
}
|
||||||
goto out;
|
goto out;
|
||||||
@ -506,16 +500,21 @@ getopts(char *optstr, char *optvar, char **optfirst, char ***optnext,
|
|||||||
|
|
||||||
if (p == **optnext)
|
if (p == **optnext)
|
||||||
(*optnext)++;
|
(*optnext)++;
|
||||||
setvarsafe("OPTARG", p, 0);
|
optarg = p;
|
||||||
p = NULL;
|
p = NULL;
|
||||||
}
|
}
|
||||||
else
|
|
||||||
setvarsafe("OPTARG", "", 0);
|
|
||||||
|
|
||||||
out:
|
out:
|
||||||
if (*optnext != NULL)
|
if (*optnext != NULL)
|
||||||
ind = *optnext - optfirst + 1;
|
ind = *optnext - optfirst + 1;
|
||||||
*optptr = p;
|
*optptr = p;
|
||||||
|
if (optarg != NULL)
|
||||||
|
err |= setvarsafe("OPTARG", optarg, 0);
|
||||||
|
else {
|
||||||
|
INTOFF;
|
||||||
|
err |= unsetvar("OPTARG");
|
||||||
|
INTON;
|
||||||
|
}
|
||||||
fmtstr(s, sizeof(s), "%d", ind);
|
fmtstr(s, sizeof(s), "%d", ind);
|
||||||
err |= setvarsafe("OPTIND", s, VNOFUNC);
|
err |= setvarsafe("OPTIND", s, VNOFUNC);
|
||||||
s[0] = c;
|
s[0] = c;
|
||||||
|
@ -85,6 +85,7 @@ FILES+= getopts4.0
|
|||||||
FILES+= getopts5.0
|
FILES+= getopts5.0
|
||||||
FILES+= getopts6.0
|
FILES+= getopts6.0
|
||||||
FILES+= getopts7.0
|
FILES+= getopts7.0
|
||||||
|
FILES+= getopts8.0 getopts8.0.stdout
|
||||||
FILES+= hash1.0 hash1.0.stdout
|
FILES+= hash1.0 hash1.0.stdout
|
||||||
FILES+= hash2.0 hash2.0.stdout
|
FILES+= hash2.0 hash2.0.stdout
|
||||||
FILES+= hash3.0 hash3.0.stdout
|
FILES+= hash3.0 hash3.0.stdout
|
||||||
|
8
bin/sh/tests/builtins/getopts8.0
Normal file
8
bin/sh/tests/builtins/getopts8.0
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
# $FreeBSD$
|
||||||
|
|
||||||
|
set -- -yz -wx
|
||||||
|
opt=wrong1 OPTARG=wrong2
|
||||||
|
while getopts :x opt; do
|
||||||
|
echo "$opt:${OPTARG-unset}"
|
||||||
|
done
|
||||||
|
echo "OPTIND=$OPTIND"
|
5
bin/sh/tests/builtins/getopts8.0.stdout
Normal file
5
bin/sh/tests/builtins/getopts8.0.stdout
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
?:y
|
||||||
|
?:z
|
||||||
|
?:w
|
||||||
|
x:unset
|
||||||
|
OPTIND=3
|
Loading…
Reference in New Issue
Block a user