From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.org/gmane.linux.lib.musl.general/4187 Path: news.gmane.org!not-for-mail From: Michael Forney Newsgroups: gmane.linux.lib.musl.general Subject: [PATCH v2] getopt_long: Support abbreviated option matching Date: Tue, 5 Nov 2013 01:27:06 -0800 Message-ID: <1383643626-6690-1-git-send-email-mforney@mforney.org> References: <1383630343-2787-1-git-send-email-mforney@mforney.org> Reply-To: musl@lists.openwall.com NNTP-Posting-Host: plane.gmane.org X-Trace: ger.gmane.org 1383643654 32643 80.91.229.3 (5 Nov 2013 09:27:34 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Tue, 5 Nov 2013 09:27:34 +0000 (UTC) To: musl@lists.openwall.com Original-X-From: musl-return-4191-gllmg-musl=m.gmane.org@lists.openwall.com Tue Nov 05 10:27:37 2013 Return-path: Envelope-to: gllmg-musl@plane.gmane.org Original-Received: from mother.openwall.net ([195.42.179.200]) by plane.gmane.org with smtp (Exim 4.69) (envelope-from ) id 1Vdcv6-0003W3-Nu for gllmg-musl@plane.gmane.org; Tue, 05 Nov 2013 10:27:32 +0100 Original-Received: (qmail 13819 invoked by uid 550); 5 Nov 2013 09:27:30 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: Original-Received: (qmail 13807 invoked from network); 5 Nov 2013 09:27:29 -0000 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references; bh=0RFfdpd/hPPxWmVuapcFpeUwRzA6jwGRwCbPDfudqqk=; b=MT8ei4/Uoi/C1Si0xuCf1+JQgFa7wKoPRFAzHtAFP/IW6K6pTCQzKwko4O2G5I3/51 UHBsVB2RwZ56a+70JKpXXjPGMT8rPQofv4lZ/oTeayKxBZwoIo0h6aLj8j9guG3wOhtB K5c61ZLMTkuf7SOSjrJFRtgTaC/Eb4O5qLJiUnq9nSq731FGwEiJt/PBNKeP5zyvD5m2 lKLpE11k/hx+oX9BJOMbGpavnyxCUollktKlVIXz7wi0k30A6cIspi1XYJt/uVpCM0XA JlrBXXpIEY3imgsNlpWN+StgFmgWNiSULiMTZGRlB/kOUF1h9l9YWXxGzEGgTL3jrp3Z De5Q== X-Gm-Message-State: ALoCoQlDCZr1+88SimeDRY3g5yyKDygf2w9kWbclGYl3ZAQ8Flv6DW4Hz84PzzFM4VtRM7Zcri9h X-Received: by 10.236.44.42 with SMTP id m30mr17441616yhb.7.1383643637686; Tue, 05 Nov 2013 01:27:17 -0800 (PST) X-Mailer: git-send-email 1.8.4.2 In-Reply-To: <1383630343-2787-1-git-send-email-mforney@mforney.org> Xref: news.gmane.org gmane.linux.lib.musl.general:4187 Archived-At: >From the getopt_long manpage: Long option names may be abbreviated if the abbreviation is unique or is an exact match for some defined option --- The previous version missed changing "optarg = opt+1;" to "optarg = c+1;", breaking long options with =. src/misc/getopt_long.c | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/misc/getopt_long.c b/src/misc/getopt_long.c index 4ef5a5c..9bf8e87 100644 --- a/src/misc/getopt_long.c +++ b/src/misc/getopt_long.c @@ -2,6 +2,7 @@ #include #include #include +#include extern int __optpos, __optreset; @@ -16,16 +17,21 @@ static int __getopt_long(int argc, char *const *argv, const char *optstring, con if ((longonly && argv[optind][1]) || (argv[optind][1] == '-' && argv[optind][2])) { - int i; + int i, j; for (i=0; longopts[i].name; i++) { const char *name = longopts[i].name; - char *opt = argv[optind]+1; + char *opt = argv[optind]+1, *c; if (*opt == '-') opt++; - for (; *name && *name == *opt; name++, opt++); - if (*name || (*opt && *opt != '=')) continue; - if (*opt == '=') { + for (c = opt; *name && *name == *c; name++, c++); + if (*c && *c != '=') continue; + if (*name) { + if (name == longopts[i].name) continue; + for (j=i+1; longopts[j].name && strncmp(opt, longopts[j].name, c - opt); j++); + if (longopts[j].name) continue; + } + if (*c == '=') { if (!longopts[i].has_arg) continue; - optarg = opt+1; + optarg = c+1; } else { if (longopts[i].has_arg == required_argument) { if (!(optarg = argv[++optind])) -- 1.8.4.2