diff options
author | Rich Felker <dalias@aerifal.cx> | 2014-12-10 21:26:49 -0500 |
---|---|---|
committer | Rich Felker <dalias@aerifal.cx> | 2014-12-10 21:26:49 -0500 |
commit | cfd7b4acd55420deedd41e0614c9b614c73c743e (patch) | |
tree | 48bb2a3d0d37f304d4a8a21ccbac5fd3b2462fa0 /src/misc | |
parent | 59f6232f85240f448da0d1fc77b3e09fe36eeed9 (diff) | |
download | musl-cfd7b4acd55420deedd41e0614c9b614c73c743e.tar.gz musl-cfd7b4acd55420deedd41e0614c9b614c73c743e.tar.bz2 musl-cfd7b4acd55420deedd41e0614c9b614c73c743e.tar.xz musl-cfd7b4acd55420deedd41e0614c9b614c73c743e.zip |
support abbreviated options in getopt_long
Diffstat (limited to 'src/misc')
-rw-r--r-- | src/misc/getopt_long.c | 25 |
1 files changed, 18 insertions, 7 deletions
diff --git a/src/misc/getopt_long.c b/src/misc/getopt_long.c index 643e5989..469ee920 100644 --- a/src/misc/getopt_long.c +++ b/src/misc/getopt_long.c @@ -56,23 +56,34 @@ static int __getopt_long_core(int argc, char *const *argv, const char *optstring ((longonly && argv[optind][1]) || (argv[optind][1] == '-' && argv[optind][2]))) { - int i; - for (i=0; longopts[i].name; i++) { + int i, cnt, match; + char *opt; + for (cnt=i=0; longopts[i].name; i++) { const char *name = longopts[i].name; - char *opt = argv[optind]+1; + opt = argv[optind]+1; if (*opt == '-') opt++; for (; *name && *name == *opt; name++, opt++); - if (*name || (*opt && *opt != '=')) continue; + if (*opt && *opt != '=') continue; + match = i; + if (!*name) { + cnt = 1; + break; + } + cnt++; + } + if (cnt==1) { + i = match; + optind++; if (*opt == '=') { - if (!longopts[i].has_arg) continue; + if (!longopts[i].has_arg) return '?'; optarg = opt+1; } else { if (longopts[i].has_arg == required_argument) { - if (!(optarg = argv[++optind])) + if (!(optarg = argv[optind])) return ':'; + optind++; } else optarg = NULL; } - optind++; if (idx) *idx = i; if (longopts[i].flag) { *longopts[i].flag = longopts[i].val; |