[PATCH] getopt: Add support for '+' in optstring
Sascha Hauer
s.hauer at pengutronix.de
Wed Jan 5 00:44:20 PST 2022
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 at 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
More information about the barebox
mailing list