From 3c31501090f94f0c29dd721b356cbdcd8290f036 Mon Sep 17 00:00:00 2001 From: "Todd C. Miller" Date: Sun, 8 Dec 2002 07:23:10 +0000 Subject: BSD getopt() supports '-' in the optstring so we should too. This is used by a few programs such as man and su. --- lib/libc/stdlib/getopt_long.c | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) (limited to 'lib/libc/stdlib') 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 @@ -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 == '-') -- cgit v1.2.3