mirror of git://git.musl-libc.org/musl
fix regression in getopt_long support for non-option arguments
commitb72cd07f17
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 commit91184c4f16
, 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 ':'.
This commit is contained in:
parent
c574321d75
commit
699d4532f6
|
@ -23,7 +23,6 @@ static int __getopt_long_core(int argc, char *const *argv, const char *optstring
|
||||||
static int __getopt_long(int argc, char *const *argv, const char *optstring, const struct option *longopts, int *idx, int longonly)
|
static int __getopt_long(int argc, char *const *argv, const char *optstring, const struct option *longopts, int *idx, int longonly)
|
||||||
{
|
{
|
||||||
int ret, skipped, resumed;
|
int ret, skipped, resumed;
|
||||||
const char *optstring2 = optstring + 1;
|
|
||||||
if (!optind || __optreset) {
|
if (!optind || __optreset) {
|
||||||
__optreset = 0;
|
__optreset = 0;
|
||||||
__optpos = 0;
|
__optpos = 0;
|
||||||
|
@ -38,10 +37,9 @@ static int __getopt_long(int argc, char *const *argv, const char *optstring, con
|
||||||
if (argv[i][0] == '-' && argv[i][1]) break;
|
if (argv[i][0] == '-' && argv[i][1]) break;
|
||||||
}
|
}
|
||||||
optind = i;
|
optind = i;
|
||||||
optstring2 = optstring;
|
|
||||||
}
|
}
|
||||||
resumed = optind;
|
resumed = optind;
|
||||||
ret = __getopt_long_core(argc, argv, optstring2, longopts, idx, longonly);
|
ret = __getopt_long_core(argc, argv, optstring, longopts, idx, longonly);
|
||||||
if (resumed > skipped) {
|
if (resumed > skipped) {
|
||||||
int i, cnt = optind-resumed;
|
int i, cnt = optind-resumed;
|
||||||
for (i=0; i<cnt; i++)
|
for (i=0; i<cnt; i++)
|
||||||
|
@ -58,6 +56,7 @@ static int __getopt_long_core(int argc, char *const *argv, const char *optstring
|
||||||
((longonly && argv[optind][1]) ||
|
((longonly && argv[optind][1]) ||
|
||||||
(argv[optind][1] == '-' && argv[optind][2])))
|
(argv[optind][1] == '-' && argv[optind][2])))
|
||||||
{
|
{
|
||||||
|
int colon = optstring[optstring[0]=='+'||optstring[0]=='-']==':';
|
||||||
int i, cnt, match;
|
int i, cnt, match;
|
||||||
char *opt;
|
char *opt;
|
||||||
for (cnt=i=0; longopts[i].name; i++) {
|
for (cnt=i=0; longopts[i].name; i++) {
|
||||||
|
@ -79,7 +78,7 @@ static int __getopt_long_core(int argc, char *const *argv, const char *optstring
|
||||||
optopt = longopts[i].val;
|
optopt = longopts[i].val;
|
||||||
if (*opt == '=') {
|
if (*opt == '=') {
|
||||||
if (!longopts[i].has_arg) {
|
if (!longopts[i].has_arg) {
|
||||||
if (optstring[0] == ':' || !opterr)
|
if (colon || !opterr)
|
||||||
return '?';
|
return '?';
|
||||||
__getopt_msg(argv[0],
|
__getopt_msg(argv[0],
|
||||||
": option does not take an argument: ",
|
": option does not take an argument: ",
|
||||||
|
@ -91,8 +90,8 @@ static int __getopt_long_core(int argc, char *const *argv, const char *optstring
|
||||||
} else {
|
} else {
|
||||||
if (longopts[i].has_arg == required_argument) {
|
if (longopts[i].has_arg == required_argument) {
|
||||||
if (!(optarg = argv[optind])) {
|
if (!(optarg = argv[optind])) {
|
||||||
if (optstring[0] == ':' || !opterr)
|
if (colon) return ':';
|
||||||
return ':';
|
if (!opterr) return '?';
|
||||||
__getopt_msg(argv[0],
|
__getopt_msg(argv[0],
|
||||||
": option requires an argument: ",
|
": option requires an argument: ",
|
||||||
longopts[i].name,
|
longopts[i].name,
|
||||||
|
@ -110,7 +109,7 @@ static int __getopt_long_core(int argc, char *const *argv, const char *optstring
|
||||||
return longopts[i].val;
|
return longopts[i].val;
|
||||||
}
|
}
|
||||||
if (argv[optind][1] == '-') {
|
if (argv[optind][1] == '-') {
|
||||||
if (optstring[0] != ':' && opterr)
|
if (!colon && opterr)
|
||||||
__getopt_msg(argv[0], cnt ?
|
__getopt_msg(argv[0], cnt ?
|
||||||
": option is ambiguous: " :
|
": option is ambiguous: " :
|
||||||
": unrecognized option: ",
|
": unrecognized option: ",
|
||||||
|
|
Loading…
Reference in New Issue