From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.org/gmane.linux.lib.musl.general/6619 Path: news.gmane.org!not-for-mail From: Gianluca Anzolin Newsgroups: gmane.linux.lib.musl.general Subject: [PATCH v2] getopt: add support for non-option arguments Date: Tue, 25 Nov 2014 08:56:03 +0100 Message-ID: <1416902163-8922-1-git-send-email-gianluca@sottospazio.it> Reply-To: musl@lists.openwall.com NNTP-Posting-Host: plane.gmane.org X-Trace: ger.gmane.org 1416902197 9374 80.91.229.3 (25 Nov 2014 07:56:37 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Tue, 25 Nov 2014 07:56:37 +0000 (UTC) Cc: Gianluca Anzolin To: musl@lists.openwall.com Original-X-From: musl-return-6632-gllmg-musl=m.gmane.org@lists.openwall.com Tue Nov 25 08:56:28 2014 Return-path: Envelope-to: gllmg-musl@m.gmane.org Original-Received: from mother.openwall.net ([195.42.179.200]) by plane.gmane.org with smtp (Exim 4.69) (envelope-from ) id 1XtAz6-0005Ct-69 for gllmg-musl@m.gmane.org; Tue, 25 Nov 2014 08:56:28 +0100 Original-Received: (qmail 27793 invoked by uid 550); 25 Nov 2014 07:56:26 -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 27780 invoked from network); 25 Nov 2014 07:56:24 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=sottospazio.it; h=x-mailer:message-id:date:date:subject:subject:from:from; s= mail; t=1416902172; x=1418716573; bh=HtVPkqkt8yHOogdc80wN1CC7Egk X+OPrAyIVVQvdDPc=; b=gTfrm0cJc7GJVgs6fRoMlm1CTVRzLiEE6+a0bkc5Man BAhyl2BbHwCIr4Na7WEPI4n7Ox0NOxcCfqFb0NXsa2AC3NaUcMEaX3giXElhwJq+ oEreNuz+SE5gBSN9yjvViDaz5zaWoXUgIm0UWhSKHUcKdwkiK9jB/uETVQTO5vg0 = X-Mailer: git-send-email 1.7.10.4 Xref: news.gmane.org gmane.linux.lib.musl.general:6619 Archived-At: Currently getopt() doesn't handle the GNU getopt extension that allows to parse non-option arguments when optstring starts with '-'. This extensions is used by some common utilities, notably iptables, that currently return with errors even with perfectly valid invocations, for example: $ iptables -A INPUT -p tcp ! --syn -m state --state NEW -j DROP The patch add the code needed to implement this extension to getopt.c and getopt_long.c v2: do not add a second path to return getopt(...) to getopt_long.c Signed-off-by: Gianluca Anzolin --- src/misc/getopt.c | 17 ++++++++++++++++- src/misc/getopt_long.c | 7 ++++--- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/src/misc/getopt.c b/src/misc/getopt.c index f94c4f7..a698c8d 100644 --- a/src/misc/getopt.c +++ b/src/misc/getopt.c @@ -24,8 +24,20 @@ int getopt(int argc, char * const argv[], const char *optstring) optind = 1; } - if (optind >= argc || !argv[optind] || argv[optind][0] != '-' || !argv[optind][1]) + if (optind >= argc || !argv[optind]) return -1; + + if (argv[optind][0] != '-') { + if (optstring[0] == '-') { + optarg = argv[optind++]; + return 1; + } + return -1; + } + + if (!argv[optind][1]) + return -1; + if (argv[optind][1] == '-' && !argv[optind][2]) return optind++, -1; @@ -43,6 +55,9 @@ int getopt(int argc, char * const argv[], const char *optstring) optpos = 0; } + if (optstring[0] == '-') + optstring++; + for (i=0; (l = mbtowc(&d, optstring+i, MB_LEN_MAX)) && d!=c; i+=l>0?l:1); if (d != c) { diff --git a/src/misc/getopt_long.c b/src/misc/getopt_long.c index 4ef5a5c..3d318ce 100644 --- a/src/misc/getopt_long.c +++ b/src/misc/getopt_long.c @@ -12,9 +12,10 @@ static int __getopt_long(int argc, char *const *argv, const char *optstring, con __optpos = 0; optind = 1; } - if (optind >= argc || !argv[optind] || argv[optind][0] != '-') return -1; - if ((longonly && argv[optind][1]) || - (argv[optind][1] == '-' && argv[optind][2])) + if (optind >= argc || !argv[optind]) return -1; + if (argv[optind][0] == '-' && + ((longonly && argv[optind][1]) || + (argv[optind][1] == '-' && argv[optind][2]))) { int i; for (i=0; longopts[i].name; i++) { -- 2.1.3