From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 10138 invoked by alias); 2 Jan 2015 21:24:30 -0000 Mailing-List: contact zsh-users-help@zsh.org; run by ezmlm Precedence: bulk X-No-Archive: yes List-Id: Zsh Users List List-Post: List-Help: X-Seq: 19667 Received: (qmail 18750 invoked from network); 2 Jan 2015 21:24:26 -0000 X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.2 X-Originating-IP: [86.6.153.127] X-Spam: 0 X-Authority: v=2.1 cv=AoZg3YNP c=1 sm=1 tr=0 a=39NrsSuza2clQiZR/7fYWQ==:117 a=39NrsSuza2clQiZR/7fYWQ==:17 a=kj9zAlcOel0A:10 a=NLZqzBF-AAAA:8 a=RJkZ50OIzr09Y_896GkA:9 a=CjuIK1q_8ugA:10 Date: Fri, 2 Jan 2015 21:24:22 +0000 From: Peter Stephenson To: zsh-users@zsh.org Subject: Re: symlink chain. Message-ID: <20150102212422.3a761af5@ntlworld.com> In-Reply-To: <54A6E6B1.6070201@eastlink.ca> References: <549E3A7B.9010209@eastlink.ca> <20150102170307.7d2e644a@ntlworld.com> <54A6E6B1.6070201@eastlink.ca> MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit On Fri, 02 Jan 2015 10:42:57 -0800 Ray Andrews wrote: > I'm using a cut down 'namei' to give this: > > l zsh -> /etc/alternatives/zsh-usrbin > l zsh-usrbin -> /usr/local/bin/zsh > l zsh -> zsh-5.0.7-165-g2194da1 > > > ... which is prettier. > > Do you think 'whence' would benefit from showing the chain if '-s' is > specified? Yes, I think that would be useful --- I've got confused before now about something in /etc/alternatives appearing as an intermediate link and started to whimper. I've used a separate option, -S, to give you the extra detail, to avoid confusing anyone who doesn't care or is parsing the old syntax. java is /usr/bin/java -> /etc/alternatives/java -> /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.25-5.b18.fc21.x86_64/jre/bin/java java is /bin/java -> /usr/bin/java -> /etc/alternatives/java -> /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.25-5.b18.fc21.x86_64/jre/bin/java To check relative paths got expanded, I added symlinks bar1 -> foo1 and foo1 -> /bin/java in my bin directory... bar1 is /home/pws/bin/bar1 -> /home/pws/bin/foo1 -> /bin/java -> /usr/bin/java -> /etc/alternatives/java -> /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.25-5.b18.fc21.x86_64/jre/bin/java pws diff --git a/Doc/Zsh/builtins.yo b/Doc/Zsh/builtins.yo index 38788d3..b4f4b67 100644 --- a/Doc/Zsh/builtins.yo +++ b/Doc/Zsh/builtins.yo @@ -1667,7 +1667,7 @@ See also the shell variable tt(STTY) for a means of initialising the tty before running external commands. ) findex(type) -item(tt(type) [ tt(-wfpams) ] var(name) ...)( +item(tt(type) [ tt(-wfpamsS) ] var(name) ...)( Equivalent to tt(whence -v). ) findex(typeset) @@ -2083,7 +2083,7 @@ the user is potentially interested in both, so this problem is intrinsic to process IDs. ) findex(whence) -item(tt(whence) [ tt(-vcwfpams) ] var(name) ...)( +item(tt(whence) [ tt(-vcwfpamsS) ] var(name) ...)( For each name, indicate how it would be interpreted if used as a command name. @@ -2126,6 +2126,11 @@ of these patterns. item(tt(-s))( If a pathname contains symlinks, print the symlink-free pathname as well. ) +item(tt(-S))( +As tt(-s), but if the pathname had to be resolved by following +multiple symlinks, the intermediate steps are printed, too. The +symlink resolved at each step might be anywhere in the path. +) enditem() ) findex(where) diff --git a/Src/builtin.c b/Src/builtin.c index 264169c..47d1aa0 100644 --- a/Src/builtin.c +++ b/Src/builtin.c @@ -119,7 +119,7 @@ static struct builtin builtins[] = BUILTIN("times", BINF_PSPECIAL, bin_times, 0, 0, 0, NULL, NULL), BUILTIN("trap", BINF_PSPECIAL | BINF_HANDLES_OPTS, bin_trap, 0, -1, 0, NULL, NULL), BUILTIN("true", 0, bin_true, 0, -1, 0, NULL, NULL), - BUILTIN("type", 0, bin_whence, 0, -1, 0, "ampfsw", "v"), + BUILTIN("type", 0, bin_whence, 0, -1, 0, "ampfsSw", "v"), BUILTIN("typeset", BINF_PLUSOPTS | BINF_MAGICEQUALS | BINF_PSPECIAL, bin_typeset, 0, -1, 0, "AE:%F:%HL:%R:%TUZ:%afghi:%klprtuxmz", NULL), BUILTIN("umask", 0, bin_umask, 0, 1, 0, "S", NULL), BUILTIN("unalias", 0, bin_unhash, 1, -1, 0, "ms", "a"), @@ -128,7 +128,7 @@ static struct builtin builtins[] = BUILTIN("unset", BINF_PSPECIAL, bin_unset, 1, -1, 0, "fmv", NULL), BUILTIN("unsetopt", 0, bin_setopt, 0, -1, BIN_UNSETOPT, NULL, NULL), BUILTIN("wait", 0, bin_fg, 0, -1, BIN_WAIT, NULL, NULL), - BUILTIN("whence", 0, bin_whence, 0, -1, 0, "acmpvfsw", NULL), + BUILTIN("whence", 0, bin_whence, 0, -1, 0, "acmpvfsSw", NULL), BUILTIN("where", 0, bin_whence, 0, -1, 0, "pmsw", "ca"), BUILTIN("which", 0, bin_whence, 0, -1, 0, "ampsw", "c"), BUILTIN("zmodload", 0, bin_zmodload, 0, -1, 0, "AFRILP:abcfdilmpue", NULL), @@ -3331,8 +3331,8 @@ bin_whence(char *nam, char **argv, Options ops, int func) if (v && !csh) zputs(*argv, stdout), fputs(" is ", stdout); zputs(buf, stdout); - if (OPT_ISSET(ops,'s')) - print_if_link(buf); + if (OPT_ISSET(ops,'s') || OPT_ISSET(ops, 'S')) + print_if_link(buf, OPT_ISSET(ops, 'S')); fputc('\n', stdout); } informed = 1; @@ -3352,8 +3352,8 @@ bin_whence(char *nam, char **argv, Options ops, int func) if (v && !csh) zputs(*argv, stdout), fputs(" is ", stdout); zputs(cnam, stdout); - if (OPT_ISSET(ops,'s')) - print_if_link(cnam); + if (OPT_ISSET(ops,'s') || OPT_ISSET(ops,'S')) + print_if_link(cnam, OPT_ISSET(ops,'S')); fputc('\n', stdout); } } else { diff --git a/Src/utils.c b/Src/utils.c index 893a8ca..2b2a815 100644 --- a/Src/utils.c +++ b/Src/utils.c @@ -719,7 +719,7 @@ slashsplit(char *s) /**/ static int -xsymlinks(char *s) +xsymlinks(char *s, int full) { char **pp, **opp; char xbuf2[PATH_MAX*3], xbuf3[PATH_MAX*2]; @@ -758,14 +758,49 @@ xsymlinks(char *s) } else { ret = 1; metafy(xbuf3, t0, META_NOALLOC); + if (!full) { + /* + * If only one expansion requested, ensure the + * full path is in xbuf. + */ + zulong len = xbuflen; + if (*xbuf3 == '/') + strcpy(xbuf, xbuf3); + else if ((len += strlen(xbuf3) + 1) < sizeof(xbuf)) { + strcpy(xbuf + xbuflen, "/"); + strcpy(xbuf + xbuflen + 1, xbuf3); + } else { + *xbuf = 0; + ret = -1; + break; + } + + while (*++pp) { + zulong newlen = len + strlen(*pp) + 1; + if (newlen < sizeof(xbuf)) { + strcpy(xbuf + len, "/"); + strcpy(xbuf + len + 1, *pp); + len = newlen; + } else { + *xbuf = 01; + ret = -1; + break; + } + } + /* + * No need to update xbuflen, we're finished + * the expansion (for now). + */ + break; + } if (*xbuf3 == '/') { strcpy(xbuf, ""); - if (xsymlinks(xbuf3 + 1) < 0) + if (xsymlinks(xbuf3 + 1, 0) < 0) ret = -1; else xbuflen = strlen(xbuf); } else - if (xsymlinks(xbuf3) < 0) + if (xsymlinks(xbuf3, 0) < 0) ret = -1; else xbuflen = strlen(xbuf); @@ -787,7 +822,7 @@ xsymlink(char *s) if (*s != '/') return NULL; *xbuf = '\0'; - if (xsymlinks(s + 1) < 0) + if (xsymlinks(s + 1, 1) < 0) zwarn("path expansion failed, using root directory"); if (!*xbuf) return ztrdup("/"); @@ -796,12 +831,30 @@ xsymlink(char *s) /**/ void -print_if_link(char *s) +print_if_link(char *s, int all) { if (*s == '/') { *xbuf = '\0'; - if (xsymlinks(s + 1) > 0) - printf(" -> "), zputs(*xbuf ? xbuf : "/", stdout); + if (all) { + char *start = s + 1; + char xbuflink[PATH_MAX]; + for (;;) { + if (xsymlinks(start, 0) > 0) { + printf(" -> "); + zputs(*xbuf ? xbuf : "/", stdout); + if (!*xbuf) + break; + strcpy(xbuflink, xbuf); + start = xbuflink + 1; + *xbuf = '\0'; + } else { + break; + } + } + } else { + if (xsymlinks(s + 1, 1) > 0) + printf(" -> "), zputs(*xbuf ? xbuf : "/", stdout); + } } }