about summary refs log tree commit diff
path: root/src/misc
diff options
context:
space:
mode:
Diffstat (limited to 'src/misc')
-rw-r--r--src/misc/getopt.c13
-rw-r--r--src/misc/getopt_long.c7
2 files changed, 18 insertions, 2 deletions
diff --git a/src/misc/getopt.c b/src/misc/getopt.c
index abf0e847..35880a09 100644
--- a/src/misc/getopt.c
+++ b/src/misc/getopt.c
@@ -3,10 +3,13 @@
 #include <string.h>
 #include <limits.h>
 #include <stdlib.h>
+#include "libc.h"
 
 char *optarg;
-int optind=1, opterr=1, optopt;
-static int optpos;
+int optind=1, opterr=1, optopt, __optpos, __optreset=0;
+
+#define optpos __optpos
+weak_alias(__optreset, optreset);
 
 int getopt(int argc, char * const argv[], const char *optstring)
 {
@@ -15,6 +18,12 @@ int getopt(int argc, char * const argv[], const char *optstring)
 	int k, l;
 	char *optchar;
 
+	if (!optind || __optreset) {
+		__optreset = 0;
+		__optpos = 0;
+		optind = 1;
+	}
+
 	if (optind >= argc || !argv[optind] || argv[optind][0] != '-' || !argv[optind][1])
 		return -1;
 	if (argv[optind][1] == '-' && !argv[optind][2])
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]))