mirror of
git://git.musl-libc.org/musl
synced 2025-01-28 01:22:49 +00:00
add support for non-option arguments extension to getopt
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.
This commit is contained in:
parent
d8dc2b7c02
commit
b72cd07f17
@ -24,8 +24,20 @@ int getopt(int argc, char * const argv[], const char *optstring)
|
|||||||
optind = 1;
|
optind = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (optind >= argc || !argv[optind] || argv[optind][0] != '-' || !argv[optind][1])
|
if (optind >= argc || !argv[optind])
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
|
if (argv[optind][0] != '-') {
|
||||||
|
if (optstring[0] == '-') {
|
||||||
|
optarg = argv[optind++];
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!argv[optind][1])
|
||||||
|
return -1;
|
||||||
|
|
||||||
if (argv[optind][1] == '-' && !argv[optind][2])
|
if (argv[optind][1] == '-' && !argv[optind][2])
|
||||||
return optind++, -1;
|
return optind++, -1;
|
||||||
|
|
||||||
@ -43,6 +55,9 @@ int getopt(int argc, char * const argv[], const char *optstring)
|
|||||||
optpos = 0;
|
optpos = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (optstring[0] == '-')
|
||||||
|
optstring++;
|
||||||
|
|
||||||
for (i=0; (l = mbtowc(&d, optstring+i, MB_LEN_MAX)) && d!=c; i+=l>0?l:1);
|
for (i=0; (l = mbtowc(&d, optstring+i, MB_LEN_MAX)) && d!=c; i+=l>0?l:1);
|
||||||
|
|
||||||
if (d != c) {
|
if (d != c) {
|
||||||
|
@ -12,9 +12,10 @@ static int __getopt_long(int argc, char *const *argv, const char *optstring, con
|
|||||||
__optpos = 0;
|
__optpos = 0;
|
||||||
optind = 1;
|
optind = 1;
|
||||||
}
|
}
|
||||||
if (optind >= argc || !argv[optind] || argv[optind][0] != '-') return -1;
|
if (optind >= argc || !argv[optind]) return -1;
|
||||||
if ((longonly && argv[optind][1]) ||
|
if (argv[optind][0] == '-' &&
|
||||||
(argv[optind][1] == '-' && argv[optind][2]))
|
((longonly && argv[optind][1]) ||
|
||||||
|
(argv[optind][1] == '-' && argv[optind][2])))
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
for (i=0; longopts[i].name; i++) {
|
for (i=0; longopts[i].name; i++) {
|
||||||
|
Loading…
Reference in New Issue
Block a user