summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTodd C. Miller <millert@cvs.openbsd.org>2002-12-08 07:23:10 +0000
committerTodd C. Miller <millert@cvs.openbsd.org>2002-12-08 07:23:10 +0000
commit3c31501090f94f0c29dd721b356cbdcd8290f036 (patch)
tree10f16eb29b84a15a5363e57542c91e42709e061e
parentd8bdabe7fcd0751650be1503bc4bef6d391a49d5 (diff)
BSD getopt() supports '-' in the optstring so we should too.
This is used by a few programs such as man and su.
-rw-r--r--lib/libc/stdlib/getopt_long.c23
1 files changed, 15 insertions, 8 deletions
diff --git a/lib/libc/stdlib/getopt_long.c b/lib/libc/stdlib/getopt_long.c
index c4f09cb23e8..2eec98530a6 100644
--- a/lib/libc/stdlib/getopt_long.c
+++ b/lib/libc/stdlib/getopt_long.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: getopt_long.c,v 1.7 2002/12/07 19:48:32 millert Exp $ */
+/* $OpenBSD: getopt_long.c,v 1.8 2002/12/08 07:23:09 millert Exp $ */
/* $NetBSD: getopt_long.c,v 1.15 2002/01/31 22:43:40 tv Exp $ */
/*
@@ -64,7 +64,7 @@
*/
#if defined(LIBC_SCCS) && !defined(lint)
-static char *rcsid = "$OpenBSD: getopt_long.c,v 1.7 2002/12/07 19:48:32 millert Exp $";
+static char *rcsid = "$OpenBSD: getopt_long.c,v 1.8 2002/12/08 07:23:09 millert Exp $";
#endif /* LIBC_SCCS and not lint */
#include <err.h>
@@ -348,8 +348,7 @@ start:
nonopt_start = nonopt_end = -1;
return (-1);
}
- if ((*(place = nargv[optind]) != '-')
- || (place[1] == '\0')) { /* found non-option */
+ if (*(place = nargv[optind]) != '-') { /* found non-option */
place = EMSG;
if (flags & FLAG_ALLARGS) {
/*
@@ -383,8 +382,11 @@ start:
if (nonopt_start != -1 && nonopt_end == -1)
nonopt_end = optind;
- /* check for "--" or "--foo" with no long options */
- if (*++place == '-' &&
+ /*
+ * Check for "--" or "--foo" with no long options
+ * but if place is simply "-" leave it unmolested.
+ */
+ if (place[1] != '\0' && *++place == '-' &&
(place[1] == '\0' || long_options == NULL)) {
optind++;
place = EMSG;
@@ -402,8 +404,13 @@ start:
}
}
- /* check long options if we have any */
- if (long_options != NULL &&
+ /*
+ * Check long options if:
+ * 1) we were passed some
+ * 2) the arg is not just "-"
+ * 3) either the arg starts with -- we are getopt_long_only()
+ */
+ if (long_options != NULL && place != nargv[optind] &&
(*place == '-' || (flags & FLAG_LONGONLY))) {
short_too = 0;
if (*place == '-')