options: Fix getopts handling of colon in optstr
Putting a colon at the beginning of optstring to silence errors doesn't
mean that the colon is a valid option. Before this patch, dash treated
-: as a valid option if the optstring started with a colon. This patch
fixes that problem.
Test:
getopts :a opt -:
echo $opt$OPTARG
Correct output - ?:
Invalid output - :
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
diff --git a/src/options.c b/src/options.c
index e192191..8101cf5 100644
--- a/src/options.c
+++ b/src/options.c
@@ -467,7 +467,7 @@
}
c = *p++;
- for (q = optstr; *q != c; ) {
+ for (q = optstr[0] == ':' ? optstr + 1 : optstr; *q != c; ) {
if (*q == '\0') {
if (optstr[0] == ':') {
s[0] = c;