From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 17496 invoked by alias); 4 Oct 2015 20:14:41 -0000 Mailing-List: contact zsh-workers-help@zsh.org; run by ezmlm Precedence: bulk X-No-Archive: yes List-Id: Zsh Workers List List-Post: List-Help: X-Seq: 36773 Received: (qmail 17642 invoked from network); 4 Oct 2015 20:14:40 -0000 X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.0 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:message-id:date:in-reply-to:comments :references:to:subject:mime-version:content-type; bh=aJMNOLdauYPongnzzeu9Hza9hEjdQ9PRtooDDeKHhAs=; b=HpyY7o+yZCJBH5jYhBlVDfwoJtWPMIsb62bXEcawm61pxb8CacT9tK8CikczyespMI jJIPKeXERS1b92yr5xgMH+oCOlCjyfW/C7oLYfzH+bM3BhQ468p2t1q6CRYlcZ/wBIuK keRIsB5z7nLoLp19/c3RcBHcq27vRmYdB8QRKDqNd3sdSlaAII/gMa4XuxCOZCsrVS5w dn61WqUIAO4FRWecM1cSjeb8AZRhW3mf54qB+XypZwLILjZj9c64pI9IeQPzW6ID5onL LLCyl94pbKnys/QejhAq+nn5LpZPj1/puFzl5OnAf5oZiuYXySUOhVKsUw4aWpGxOZ+9 VbAA== X-Gm-Message-State: ALoCoQn+AwuzvtQTC3MSSWH7rMsnA2zJp64nf8U5x/fWNE/e7p4H9LCpmLUWnhzrcxOdcJCR85Il X-Received: by 10.202.89.130 with SMTP id n124mr14425010oib.21.1443989677307; Sun, 04 Oct 2015 13:14:37 -0700 (PDT) From: Bart Schaefer Message-Id: <151004131434.ZM17894@torch.brasslantern.com> Date: Sun, 4 Oct 2015 13:14:34 -0700 In-Reply-To: <151004112931.ZM3752@torch.brasslantern.com> Comments: In reply to Bart Schaefer "Re: annoying correction of directory name to non-directory name for cd" (Oct 4, 11:29am) References: <20150916232123.GA17398@zira.vinc17.org> <150916180714.ZM9139@torch.brasslantern.com> <20151004121721.GA2341@zira.vinc17.org> <151004100604.ZM3726@torch.brasslantern.com> <151004112931.ZM3752@torch.brasslantern.com> X-Mailer: OpenZMail Classic (0.9.2 24April2005) To: zsh-workers@zsh.org Subject: Re: annoying correction of directory name to non-directory name for cd MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii On Oct 4, 11:29am, Bart Schaefer wrote: } Subject: Re: annoying correction of directory name to non-directory name f } } Updated patch later ... Mostly re-indentation but it seemed prudent to throw in a check for successful allocation while here. diff --git a/Src/utils.c b/Src/utils.c index ab3b0c2..61cbe84 100644 --- a/Src/utils.c +++ b/Src/utils.c @@ -2812,7 +2812,7 @@ spckword(char **s, int hist, int cmd, int ask) * as used in spscan(), so that an autocd is chosen * * only when it is better than anything so far, and * * so we prefer directories earlier in the cdpath. */ - if ((thisdist = mindist(*pp, *s, bestcd)) < d) { + if ((thisdist = mindist(*pp, *s, bestcd, 1)) < d) { best = dupstring(bestcd); d = thisdist; } @@ -4057,7 +4057,8 @@ spname(char *oldname) thresh = 3; else if (thresh > 100) thresh = 100; - if ((thisdist = mindist(newname, spnameguess, spnamebest)) >= thresh) { + thisdist = mindist(newname, spnameguess, spnamebest, *old == '/'); + if (thisdist >= thresh) { /* The next test is always true, except for the first path * * component. We could initialize bestdist to some large * * constant instead, and then compare to that constant here, * @@ -4082,42 +4083,52 @@ spname(char *oldname) /**/ static int -mindist(char *dir, char *mindistguess, char *mindistbest) +mindist(char *dir, char *mindistguess, char *mindistbest, int wantdir) { int mindistd, nd; DIR *dd; char *fn; char *buf; + struct stat st; + size_t dirlen; if (dir[0] == '\0') dir = "."; mindistd = 100; - buf = zalloc(strlen(dir) + strlen(mindistguess) + 2); + if (!(buf = zalloc((dirlen = strlen(dir)) + strlen(mindistguess) + 2))) + return 0; sprintf(buf, "%s/%s", dir, mindistguess); - if (access(unmeta(buf), F_OK) == 0) { + if (stat(unmeta(buf), &st) == 0 && (!wantdir || S_ISDIR(st.st_mode))) { strcpy(mindistbest, mindistguess); free(buf); return 0; } - free(buf); - if (!(dd = opendir(unmeta(dir)))) - return mindistd; - while ((fn = zreaddir(dd, 0))) { - if (spnamepat && pattry(spnamepat, fn)) - continue; - nd = spdist(fn, mindistguess, - (int)strlen(mindistguess) / 4 + 1); - if (nd <= mindistd) { - strcpy(mindistbest, fn); - mindistd = nd; - if (mindistd == 0) - break; + if ((dd = opendir(unmeta(dir)))) { + while ((fn = zreaddir(dd, 0))) { + if (spnamepat && pattry(spnamepat, fn)) + continue; + nd = spdist(fn, mindistguess, + (int)strlen(mindistguess) / 4 + 1); + if (nd <= mindistd) { + if (wantdir) { + if (!(buf = zrealloc(buf, dirlen + strlen(fn) + 2))) + continue; + sprintf(buf, "%s/%s", dir, fn); + if (stat(unmeta(buf), &st) != 0 || !S_ISDIR(st.st_mode)) + continue; + } + strcpy(mindistbest, fn); + mindistd = nd; + if (mindistd == 0) + break; + } } + closedir(dd); } - closedir(dd); + free(buf); return mindistd; } -- Barton E. Schaefer