* [PATCH] getopt: fix optional argument processing
@ 2014-10-21 11:03 Felix Fietkau
2014-10-21 11:35 ` Rich Felker
0 siblings, 1 reply; 2+ messages in thread
From: Felix Fietkau @ 2014-10-21 11:03 UTC (permalink / raw)
To: musl
Processing an option character with optional argument fails if the
option is last on the command line. This happens because the
if (optind >= argc) check runs first before testing for optional
argument.
---
src/misc/getopt.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/src/misc/getopt.c b/src/misc/getopt.c
index 8a2e4d5..7ce3789 100644
--- a/src/misc/getopt.c
+++ b/src/misc/getopt.c
@@ -55,7 +55,9 @@ int getopt(int argc, char * const argv[], const char *optstring)
return '?';
}
if (optstring[i+1] == ':') {
+ if (optstring[i+2] == ':') optarg = 0;
if (optind >= argc) {
+ if (optstring[i+2] == ':') return c;
if (optstring[0] == ':') return ':';
if (opterr) {
write(2, argv[0], strlen(argv[0]));
@@ -65,7 +67,6 @@ int getopt(int argc, char * const argv[], const char *optstring)
}
return '?';
}
- if (optstring[i+2] == ':') optarg = 0;
if (optstring[i+2] != ':' || optpos) {
optarg = argv[optind++] + optpos;
optpos = 0;
--
2.0.4
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [PATCH] getopt: fix optional argument processing
2014-10-21 11:03 [PATCH] getopt: fix optional argument processing Felix Fietkau
@ 2014-10-21 11:35 ` Rich Felker
0 siblings, 0 replies; 2+ messages in thread
From: Rich Felker @ 2014-10-21 11:35 UTC (permalink / raw)
To: musl
On Tue, Oct 21, 2014 at 01:03:28PM +0200, Felix Fietkau wrote:
> Processing an option character with optional argument fails if the
> option is last on the command line. This happens because the
> if (optind >= argc) check runs first before testing for optional
> argument.
> ---
> src/misc/getopt.c | 3 ++-
> 1 file changed, 2 insertions(+), 1 deletion(-)
>
> diff --git a/src/misc/getopt.c b/src/misc/getopt.c
> index 8a2e4d5..7ce3789 100644
> --- a/src/misc/getopt.c
> +++ b/src/misc/getopt.c
> @@ -55,7 +55,9 @@ int getopt(int argc, char * const argv[], const char *optstring)
> return '?';
> }
> if (optstring[i+1] == ':') {
> + if (optstring[i+2] == ':') optarg = 0;
> if (optind >= argc) {
> + if (optstring[i+2] == ':') return c;
Couldn't this be:
if (optstring[i+2] == ':') optarg = 0;
else if (optind >= argc) {
I'm not sure if that's nicer but it eliminates the additional return
path you're adding.
Rich
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2014-10-21 11:35 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-10-21 11:03 [PATCH] getopt: fix optional argument processing Felix Fietkau
2014-10-21 11:35 ` Rich Felker
Code repositories for project(s) associated with this public inbox
https://git.vuxu.org/mirror/musl/
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).