about summary refs log tree commit diff
path: root/src/misc/getopt_long.c
diff options
context:
space:
mode:
authorRich Felker <dalias@aerifal.cx>2012-09-30 20:00:38 -0400
committerRich Felker <dalias@aerifal.cx>2012-09-30 20:00:38 -0400
commit030e52639248ac8417a4934298caa78c21a228d1 (patch)
treeb35e1d3893f3b9db44dd006d1ef944057e9f1906 /src/misc/getopt_long.c
parente44849f5cf331e655705b18d6c81c616e29d50d0 (diff)
downloadmusl-030e52639248ac8417a4934298caa78c21a228d1.tar.gz
musl-030e52639248ac8417a4934298caa78c21a228d1.tar.xz
musl-030e52639248ac8417a4934298caa78c21a228d1.zip
add getopt reset support
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
Diffstat (limited to 'src/misc/getopt_long.c')
-rw-r--r--src/misc/getopt_long.c7
1 files changed, 7 insertions, 0 deletions
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 <getopt.h>
 #include <stdio.h>
 
+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]))