zsh-users
 help / color / mirror / code / Atom feed
From: Peter Stephenson <p.w.stephenson@ntlworld.com>
To: zsh-users@zsh.org
Subject: Re: symlink chain.
Date: Fri, 2 Jan 2015 21:24:22 +0000	[thread overview]
Message-ID: <20150102212422.3a761af5@ntlworld.com> (raw)
In-Reply-To: <54A6E6B1.6070201@eastlink.ca>

On Fri, 02 Jan 2015 10:42:57 -0800
Ray Andrews <rayandrews@eastlink.ca> 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);
+	}
     }
 }
 


  parent reply	other threads:[~2015-01-02 21:24 UTC|newest]

Thread overview: 30+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-12-27  4:50 Ray Andrews
2014-12-27 23:38 ` Ray Andrews
2015-01-02 17:03 ` Peter Stephenson
2015-01-02 18:42   ` Ray Andrews
2015-01-02 20:27     ` Lawrence Velázquez
2015-01-02 21:19       ` Ray Andrews
2015-01-02 21:24     ` Peter Stephenson [this message]
2015-01-02 21:36       ` Ray Andrews
2015-01-02 21:53       ` Ray Andrews
2015-01-02 22:21         ` Peter Stephenson
2015-01-02 23:42           ` Ray Andrews
2015-01-03  1:08             ` Ray Andrews
2015-01-03  5:03               ` Bart Schaefer
2015-01-03  5:53                 ` Ray Andrews
2015-01-03  7:17                   ` Bart Schaefer
2015-01-03 17:14                     ` Ray Andrews
2015-01-03 20:02                       ` Bart Schaefer
2015-01-03 21:13                         ` Ray Andrews
2015-01-03 21:42                           ` Peter Stephenson
2015-01-03 22:42                             ` Ray Andrews
2015-01-04  0:41                               ` Bart Schaefer
2015-01-04  3:45                                 ` Ray Andrews
2015-01-04  0:40                           ` Bart Schaefer
2015-01-04  3:35                             ` Ray Andrews
2015-01-04  8:31                               ` Bart Schaefer
2015-01-04 20:49                                 ` Ray Andrews
2015-01-05  1:34                                   ` Bart Schaefer
2015-01-05  2:28                                     ` Lawrence Velázquez
2015-01-05  4:24                                       ` Ray Andrews
2015-01-02 22:22         ` Bart Schaefer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20150102212422.3a761af5@ntlworld.com \
    --to=p.w.stephenson@ntlworld.com \
    --cc=zsh-users@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).