From 030e52639248ac8417a4934298caa78c21a228d1 Mon Sep 17 00:00:00 2001 From: Rich Felker Date: Sun, 30 Sep 2012 20:00:38 -0400 Subject: add getopt reset support MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit based on proposed patches by Daniel Cegiełka, with minor changes: - use a weak symbol for optreset so it doesn't clash with namespace - also reset optpos (position in multi-option arg like -lR) - also make getopt_long support reset --- src/misc/getopt_long.c | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'src/misc/getopt_long.c') diff --git a/src/misc/getopt_long.c b/src/misc/getopt_long.c index 6d3a4a6e..4ef5a5c7 100644 --- a/src/misc/getopt_long.c +++ b/src/misc/getopt_long.c @@ -3,8 +3,15 @@ #include #include +extern int __optpos, __optreset; + static int __getopt_long(int argc, char *const *argv, const char *optstring, const struct option *longopts, int *idx, int longonly) { + if (!optind || __optreset) { + __optreset = 0; + __optpos = 0; + optind = 1; + } if (optind >= argc || !argv[optind] || argv[optind][0] != '-') return -1; if ((longonly && argv[optind][1]) || (argv[optind][1] == '-' && argv[optind][2])) -- cgit 1.4.1