diff options
author | Sascha Hauer <s.hauer@pengutronix.de> | 2022-01-05 09:41:53 +0100 |
---|---|---|
committer | Sascha Hauer <s.hauer@pengutronix.de> | 2022-01-05 09:41:55 +0100 |
commit | ffa2f5c0a11665b9f8e20ac28f4d22c947f59c6c (patch) | |
tree | cf10709eb4bb4f656681079e43ea46a3402b23a5 /lib/getopt.c | |
parent | cb0d339074cf17fa806633126ad5146cd2909db7 (diff) | |
download | barebox-ffa2f5c0a11665b9f8e20ac28f4d22c947f59c6c.tar.gz |
getopt: Add support for '+' in optstring
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>
Diffstat (limited to 'lib/getopt.c')
-rw-r--r-- | lib/getopt.c | 17 |
1 files 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 */ |