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

mail archive of the barebox mailing list

This inbox may be cloned and mirrored by anyone:

	git clone --mirror https://lore.barebox.org/barebox/0 barebox/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 barebox barebox/ https://lore.barebox.org/barebox \
		barebox@lists.infradead.org barebox@lists.infradead.org
	public-inbox-index barebox

Example config snippet for mirrors.


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git