mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH] getopt: Add support for '+' in optstring
@ 2022-01-05  8:44 Sascha Hauer
  0 siblings, 0 replies; only message in thread
From: Sascha Hauer @ 2022-01-05  8:44 UTC (permalink / raw)
  To: Barebox List

Stop option parsing at nonoptions when the first character of optstring is
'+'. This is analog to getopt(3).

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 lib/getopt.c | 17 ++++++++++++++---
 1 file changed, 14 insertions(+), 3 deletions(-)

diff --git a/lib/getopt.c b/lib/getopt.c
index 55852ba133..356fc2ff4e 100644
--- a/lib/getopt.c
+++ b/lib/getopt.c
@@ -61,13 +61,24 @@ int getopt(int argc, char *argv[], const char *optstring)
 {
 	char curopt;   /* current option character */
 	const char *curoptp; /* pointer to the current option in optstring */
+	bool stop_nonopt = false;
+
+	if (*optstring == '+') {
+		stop_nonopt = true;
+		optstring++;
+	}
 
 	while(1) {
 		debug("optindex: %d nonopts: %d optind: %d\n", optindex, nonopts, optind);
 
-		if (optindex == 1 && argv[optind] && !strcmp(argv[optind], "--")) {
-			optind++;
-			return -1;
+		if (optindex == 1 && argv[optind]) {
+			if (!strcmp(argv[optind], "--")) {
+				optind++;
+				return -1;
+			}
+
+			if (stop_nonopt && *argv[optind] != '-')
+				return -1;
 		}
 
 		/* first put nonopts to the end */
-- 
2.30.2


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2022-01-05  8:46 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-01-05  8:44 [PATCH] getopt: Add support for '+' in optstring Sascha Hauer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox