diff mbox series

[v2,1/2] options: getopts: ignore first --, per POSIX

Message ID 728796762ed62a1e9a38cf62762e57509e5027a2.1672831998.git.nabijaczleweli@nabijaczleweli.xyz (mailing list archive)
State Accepted
Delegated to: Herbert Xu
Headers show
Series [v2,1/2] options: getopts: ignore first --, per POSIX | expand

Commit Message

наб Jan. 4, 2023, 11:33 a.m. UTC
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
---
 src/options.c | 3 +++
 1 file changed, 3 insertions(+)

Comments

Herbert Xu Jan. 8, 2023, 12:06 p.m. UTC | #1
On Wed, Jan 04, 2023 at 12:33:45PM +0100, наб wrote:
> 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
> ---
>  src/options.c | 3 +++
>  1 file changed, 3 insertions(+)

All applied.  Thanks.
diff mbox series

Patch

diff --git a/src/options.c b/src/options.c
index a46c23b..337a8ee 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) {