From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 16550 invoked from network); 7 Mar 1998 14:57:46 -0000 Received: from math.gatech.edu (list@130.207.146.50) by ns1.primenet.com.au with SMTP; 7 Mar 1998 14:57:46 -0000 Received: (from list@localhost) by math.gatech.edu (8.8.5/8.8.5) id JAA19336; Sat, 7 Mar 1998 09:39:02 -0500 (EST) Resent-Date: Sat, 7 Mar 1998 09:39:02 -0500 (EST) Sender: rz2a022@uni-hamburg.de Message-ID: <35015C89.AC3024EF@rrz.uni-hamburg.de> Date: Sat, 07 Mar 1998 15:41:13 +0100 From: Bernd Eggink Organization: Regionales Rechenzentrum der Uni Hamburg X-Mailer: Mozilla 4.04 [en] (X11; I; Linux 2.0.30 i586) MIME-Version: 1.0 To: Zsh-workers Subject: Patch: getopts (zsh-3.1.2-zefram3) Content-Type: multipart/mixed; boundary="------------28EC23DCCFD2E23709871023" Resent-Message-ID: <"ccF352.0.3k4.5mL0r"@math> Resent-From: zsh-workers@math.gatech.edu X-Mailing-List: archive/latest/3797 X-Loop: zsh-workers@math.gatech.edu Precedence: list Resent-Sender: zsh-workers-request@math.gatech.edu This is a multi-part message in MIME format. --------------28EC23DCCFD2E23709871023 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit The attached patch corrects some minor bugs and a major one in function bin_getopts() in builtin.c. The minor bugs are: - optbuf is unitialized, but optbuf[1] is checked for ':'. - The check for required arguments is incorrect. - If an optional argument is missing, OPTARG keeps the old value instead of being reset to empty. The major bug, which didn't exist in zsh-3.1.2: If an an empty string is used as an optional argument, getopts returns 1, leaving all remaining options unprocessed. I'm also not sure about the statement starting in line 2537: if (zoptind < 1) ... Actually zoptind is NEVER set to 0 in the sources. I left it, though, because the user might set OPTIND=0 deliberately, although I'm in doubt whether that should be considered legal. Regards, Bernd -- Bernd Eggink Regionales Rechenzentrum der Universitaet Hamburg eggink@rrz.uni-hamburg.de http://www.rrz.uni-hamburg.de/eggink/BEggink.html --------------28EC23DCCFD2E23709871023 Content-Type: text/plain; charset=us-ascii; name="builtin.dif" Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename="builtin.dif" --- builtin.c.old Sun Jan 11 20:23:15 1998 +++ builtin.c Sat Mar 7 15:27:15 1998 @@ -2531,7 +2531,7 @@ char *optstr = unmetafy(*argv++, &lenoptstr), *var = *argv++; char **args = (*argv) ? argv : pparams; static int optcind = 0; - char *str, optbuf[2], *p, opch; + char *str, optbuf[2] = " ", *p, opch; /* zoptind keeps count of the current argument number */ if (zoptind < 1) { @@ -2550,11 +2550,11 @@ /* find place in relevant argument */ str = unmetafy(dupstring(args[zoptind - 1]), &lenstr); - if(optcind && optcind >= lenstr) { + if(optcind >= lenstr) { + optcind = 0; if(!args[zoptind++]) return 1; str = unmetafy(dupstring(args[zoptind - 1]), &lenstr); - optcind = 0; } if(!optcind) { if(lenstr < 2 || (*str != '-' && *str != '+')) @@ -2577,20 +2577,21 @@ if(optbuf[1] == ':' || !(p = memchr(optstr, opch, lenoptstr))) { p = "?"; err: + zsfree(zoptarg); if(quiet) { setsparam(var, ztrdup(p)); - zsfree(zoptarg); zoptarg = metafy(optbuf, lenoptbuf, META_DUP); } else { zerr(*p == '?' ? "bad option: -%c" : "argument expected after -%c option", NULL, opch); + zoptarg=ztrdup(""); errflag = 0; } return 0; } /* check for required argument */ - if(p != optstr+lenoptstr && p[1] == ':') { + if(p != optstr+lenoptstr-1 && p[1] == ':') { if(optcind == lenstr) { if(!args[zoptind]) { p = ":"; --------------28EC23DCCFD2E23709871023--