summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorнаб <nabijaczleweli@nabijaczleweli.xyz>2023-01-04 12:33:45 +0100
committerHerbert Xu <herbert@gondor.apana.org.au>2023-01-08 20:03:41 +0800
commit54485578e01017534dae30731f7682abadb38a09 (patch)
treef6ec11850cf8c92ab5e9ee7ae29afe50bfaf1c39
parentb4ecd84eb4048522648bc16920d3615cb243a6bf (diff)
downloaddash-54485578e01017534dae30731f7682abadb38a09.tar.gz
builtin: Ignore first -- in getopts per POSIX
Issue 7, XCU, getopts, OPTIONS reads "None.", and getopts isn't a special built-in listed in sexion 2.14 ‒ this means that XCU, 1. Introduction, 1.4 Utility Description Defaults, OPTIONS, Default Behavior applies: Default Behavior: When this section is listed as "None.", it means that the implementation need not support any options. Standard utilities that do not accept options, but that do accept operands, shall recognize "--" as a first argument to be discarded. Test with: getopts -- d: a Correct output is no output, exit 1 Wrong output errors out with d: being an invalid argument name Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
-rw-r--r--src/options.c3
1 files changed, 3 insertions, 0 deletions
diff --git a/src/options.c b/src/options.c
index 3158498..2d4bd3b 100644
--- a/src/options.c
+++ b/src/options.c
@@ -409,6 +409,9 @@ getoptscmd(int argc, char **argv)
{
char **optbase;
+ nextopt(nullstr);
+ argc -= argptr - argv - 1;
+ argv = argptr - 1;
if (argc < 3)
sh_error("Usage: getopts optstring var [arg...]");
else if (argc == 3) {