From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.org/gmane.linux.lib.musl.general/6617 Path: news.gmane.org!not-for-mail From: Gianluca Anzolin Newsgroups: gmane.linux.lib.musl.general Subject: Re: [PATCH] getopt: add support for non-option arguments Date: Tue, 25 Nov 2014 08:33:50 +0100 Message-ID: <20141125073350.GA8419@sottospazio.it> References: <1416854345-5252-1-git-send-email-gianluca@sottospazio.it> <20141125024801.GP29621@brightrain.aerifal.cx> Reply-To: musl@lists.openwall.com NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: ger.gmane.org 1416900850 20969 80.91.229.3 (25 Nov 2014 07:34:10 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Tue, 25 Nov 2014 07:34:10 +0000 (UTC) To: musl@lists.openwall.com Original-X-From: musl-return-6630-gllmg-musl=m.gmane.org@lists.openwall.com Tue Nov 25 08:34:05 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 1XtAdQ-0004vU-U3 for gllmg-musl@m.gmane.org; Tue, 25 Nov 2014 08:34:05 +0100 Original-Received: (qmail 7862 invoked by uid 550); 25 Nov 2014 07:34:03 -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 7851 invoked from network); 25 Nov 2014 07:34:03 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=sottospazio.it; h=user-agent:in-reply-to:content-disposition:content-type :content-type:mime-version:references:message-id:subject:subject :from:from:date:date; s=mail; t=1416900830; x=1418715231; bh=Plw /idsafCEaHGEtQdAAZLACGaubHexjy/1aA7drPXI=; b=KDhT0vSNRlKbbhbZTCQ OOi4ELm0VoH1tHruJFHN+NAZrrfi+5hG7n3gUPhayOEFUMde4LIm9bDlnCRGE3c5 L4sNblVLBgknEMC1DWzf9TfsYe3aYHubCyJAOa6bK+TQEPwwSIT020bu601FrDdT v6/Rz9haBYsNhjpTCSRRZ8Xo= Mail-Followup-To: musl@lists.openwall.com Content-Disposition: inline In-Reply-To: <20141125024801.GP29621@brightrain.aerifal.cx> User-Agent: Mutt/1.5.21 (2010-09-15) Xref: news.gmane.org gmane.linux.lib.musl.general:6617 Archived-At: Hello, On Mon, Nov 24, 2014 at 09:48:01PM -0500, Rich Felker wrote: > On Mon, Nov 24, 2014 at 07:39:05PM +0100, Gianluca Anzolin wrote: > > diff --git a/src/misc/getopt_long.c b/src/misc/getopt_long.c > > index 4ef5a5c..d8b2b66 100644 > > --- a/src/misc/getopt_long.c > > +++ b/src/misc/getopt_long.c > > @@ -12,7 +12,11 @@ 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 (optind >= argc || !argv[optind]) return -1; > > + > > + if (argv[optind][0] != '-') > > + return getopt(argc, argv, optstring); > > + > > if ((longonly && argv[optind][1]) || > > (argv[optind][1] == '-' && argv[optind][2])) > > { > > -- > > 2.1.3 > > For getopt_long, this introduces a second code path to return > getopt(...). Wouldn't it be cleaner just to add an additional > condition of (argv[optind][0] == '-') to the if statement containing > the main body of the function? Yes, that seems better, I will do it. > I'm a little bit uncertain what's really best to do with getopt_long, > but since there's a pending patch for abbreviated long options and > there's demand to add GNU-style argv permutation to getopt_long (but > not plain getopt), this code is likely to need major changes anyway > in this release cycle, so I'm not too concerned that we do it the > "best possible way" right now anyway. So if it works we can probably > go with it. > I tested the changes and verified that they work correctly. However I found something worth mentioning, not caused by the patch itself. For a command line like the following (optstring="a:b:c:def") $ ./test -a 1 -b 2 -c 3 -d -e -f for the options d, e and f the variable optarg is "3". Maybe this is the intended behaviour, after all those options aren't supposed to get an argument, but glibc always sets optarg to NULL in these cases. > I'll see if anyone else has comments and if not commit (possibly with > the above modification to getopt_long?) soon. > > Rich I'll send a new patch soon, Thanks Gianluca