From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.org/gmane.linux.lib.musl.general/4182 Path: news.gmane.org!not-for-mail From: Michael Forney Newsgroups: gmane.linux.lib.musl.general Subject: [PATCH] getopt_long: Support abbreviated option matching Date: Mon, 4 Nov 2013 21:45:43 -0800 Message-ID: <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 1383630363 27148 80.91.229.3 (5 Nov 2013 05:46:03 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Tue, 5 Nov 2013 05:46:03 +0000 (UTC) To: musl@lists.openwall.com Original-X-From: musl-return-4186-gllmg-musl=m.gmane.org@lists.openwall.com Tue Nov 05 06:46:09 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 1VdZSq-0004DM-IL for gllmg-musl@plane.gmane.org; Tue, 05 Nov 2013 06:46:08 +0100 Original-Received: (qmail 9386 invoked by uid 550); 5 Nov 2013 05:46:08 -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 9378 invoked from network); 5 Nov 2013 05:46:07 -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; bh=KsodUZ7yAySfcy8EKa3SCYMD1lRM8HCRus9g6tfBQc4=; b=l68TRFp2bWKNjUzbVidE5DczYxiNeL4WdEydB+EIAg4F+rYMygg+38F3TL9+iqumnd IO0OsskeYL70Rm32KUlB0Rzb1oo6ltBDTmGCbORitncfefAJU52F829JURaIK2nanANz g1ExPsib7ZBvB3kZL/nadP4TAtENzw+jzrTqpJpkLZCwi0KiHqD/2zMnN7lSP6azYTvJ iR+hKLF/C/K+2/0ZPqYv2s11QJMtrfT+zhb+EbaMsm+X47D36IbxGXCLoHkSc3OSz0+0 in18sS67cT/wsS7MgpkDxvF4LcsLl1Gbjss0Gshr0rEED7YS+rhiaWB6dm4mNJBirzkg PN2Q== X-Gm-Message-State: ALoCoQnX8+S0cjhiFhowJ1oma8IcrPHlo6lcd87g8IQqB1P0I/AfLuwLCOJVEJQZq/uoNhVpsBvs X-Received: by 10.236.85.97 with SMTP id t61mr16688191yhe.11.1383630355869; Mon, 04 Nov 2013 21:45:55 -0800 (PST) X-Mailer: git-send-email 1.8.4.2 Xref: news.gmane.org gmane.linux.lib.musl.general:4182 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 --- This patch is fairly straightforward and provides more compatibility with glibc's getopt_long. I've run into at least one case where a script called getopt from util-linux with the abbreviation --long instead of the full option name --longoptions. src/misc/getopt_long.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/misc/getopt_long.c b/src/misc/getopt_long.c index 4ef5a5c..65bc14c 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,14 +17,19 @@ 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; } else { -- 1.8.4.2