From f2aaead9a6f5595f20728e5582574e50adac9308 Mon Sep 17 00:00:00 2001 From: midipix Date: Sat, 7 Jan 2017 09:43:10 -0500 Subject: [PATCH] getopt_long(): record successful completed parsing of all arguments. --- src/misc/getopt_long.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/misc/getopt_long.c b/src/misc/getopt_long.c index 568ae7b..d20d4f3 100644 --- a/src/misc/getopt_long.c +++ b/src/misc/getopt_long.c @@ -5,6 +5,7 @@ #include extern int __optpos, __optreset; +static int __getopt_err = 0; static void permute(char *const *argv, int dest, int src) { @@ -26,8 +27,13 @@ static int __getopt_long(int argc, char *const *argv, const char *optstring, con if (!optind || __optreset) { __optreset = 0; __optpos = 0; + __getopt_err = 0; optind = 1; } + if (optind == argc && !__getopt_err) { + optopt = 0; + return -1; + } if (optind >= argc || !argv[optind]) return -1; skipped = optind; if (optstring[0] != '+' && optstring[0] != '-') { @@ -46,6 +52,7 @@ static int __getopt_long(int argc, char *const *argv, const char *optstring, con permute(argv, skipped, optind-1); optind = skipped + cnt; } + __getopt_err |= ((ret == '?') || (ret == ':')); return ret; } -- 2.8.3