From 72e2001bdabce7d0b2a787029eaabeb31778f168 Mon Sep 17 00:00:00 2001 From: millert <> Date: Sun, 8 Dec 2002 07:23:09 +0000 Subject: [PATCH] BSD getopt() supports '-' in the optstring so we should too. This is used by a few programs such as man and su. --- src/lib/libc/stdlib/getopt_long.c | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/src/lib/libc/stdlib/getopt_long.c b/src/lib/libc/stdlib/getopt_long.c index c4f09cb2..2eec9853 100644 --- a/src/lib/libc/stdlib/getopt_long.c +++ b/src/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 == '-')