aboutsummaryrefslogtreecommitdiffstats
path: root/main/musl/0011-add-support-for-non-option-arguments-extension-to-ge.patch
blob: 7bf8e75e85034aab6ed56f8500d4cc3cc59da5fb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
From b72cd07f176b876aa51864d93aa8101477b1d732 Mon Sep 17 00:00:00 2001
From: Gianluca Anzolin <gianluca@sottospazio.it>
Date: Tue, 25 Nov 2014 08:56:03 +0100
Subject: [PATCH] add support for non-option arguments extension to getopt

this is a GNU extension, activated by including '-' as the first
character of the options string, whereby non-option arguments are
processed as if they were arguments to an option character '\1' rather
than ending option processing.
---
 src/misc/getopt.c      | 17 ++++++++++++++++-
 src/misc/getopt_long.c |  7 ++++---
 2 files changed, 20 insertions(+), 4 deletions(-)

diff --git a/src/misc/getopt.c b/src/misc/getopt.c
index f94c4f7..a698c8d 100644
--- a/src/misc/getopt.c
+++ b/src/misc/getopt.c
@@ -24,8 +24,20 @@ int getopt(int argc, char * const argv[], const char *optstring)
 		optind = 1;
 	}
 
-	if (optind >= argc || !argv[optind] || argv[optind][0] != '-' || !argv[optind][1])
+	if (optind >= argc || !argv[optind])
 		return -1;
+
+	if (argv[optind][0] != '-') {
+		if (optstring[0] == '-') {
+			optarg = argv[optind++];
+			return 1;
+		}
+		return -1;
+	}
+
+	if (!argv[optind][1])
+		return -1;
+
 	if (argv[optind][1] == '-' && !argv[optind][2])
 		return optind++, -1;
 
@@ -43,6 +55,9 @@ int getopt(int argc, char * const argv[], const char *optstring)
 		optpos = 0;
 	}
 
+	if (optstring[0] == '-')
+		optstring++;
+
 	for (i=0; (l = mbtowc(&d, optstring+i, MB_LEN_MAX)) && d!=c; i+=l>0?l:1);
 
 	if (d != c) {
diff --git a/src/misc/getopt_long.c b/src/misc/getopt_long.c
index 4ef5a5c..3d318ce 100644
--- a/src/misc/getopt_long.c
+++ b/src/misc/getopt_long.c
@@ -12,9 +12,10 @@ static int __getopt_long(int argc, char *const *argv, const char *optstring, con
 		__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]))
+	if (optind >= argc || !argv[optind]) return -1;
+	if (argv[optind][0] == '-' &&
+		((longonly && argv[optind][1]) ||
+		 (argv[optind][1] == '-' && argv[optind][2])))
 	{
 		int i;
 		for (i=0; longopts[i].name; i++) {
-- 
2.2.0