summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorSascha Hauer <s.hauer@pengutronix.de>2022-01-05 09:41:53 +0100
committerSascha Hauer <s.hauer@pengutronix.de>2022-01-05 09:41:55 +0100
commitffa2f5c0a11665b9f8e20ac28f4d22c947f59c6c (patch)
treecf10709eb4bb4f656681079e43ea46a3402b23a5 /lib
parentcb0d339074cf17fa806633126ad5146cd2909db7 (diff)
downloadbarebox-ffa2f5c0a11665b9f8e20ac28f4d22c947f59c6c.tar.gz
barebox-ffa2f5c0a11665b9f8e20ac28f4d22c947f59c6c.tar.xz
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')
-rw-r--r--lib/getopt.c17
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 */