Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: _canonical_path not working on *BSD



Peter Stephenson wrote:
> Bart Schaefer wrote:
> > On Mar 26, 11:44am, Baptiste Daroussin wrote:
> > }
> > } _canonical_path is not working on freebsd, it rely on readlink
> > 
> > Try this.  I don't actually have any mount points that are symlink
> > targets so it's hard to test, but I think this is right:
> > 
> > -if (( ! $+commands[readlink] )); then
> > +if ! zmodload -F zsh/stat b:zstat 2>/dev/null; then
> 
> That's probably a better solution; it doesn't necessarily work on all
> systems, particularly if the shell is statically linked, but it should
> be at least as widely applicable as, and easier to test for than,
> anything involving readlink.

Hmm... sorry about all the traffic... actually, it still doesn't
guarantee to give a canonical path as "readlink -f" does, since it
doesn't check if the value returned is itself a symbolic link, and also
it returns empty instead of the original file if it wasn't a link.  We would
need to do something like the following... if I've correctly divined
that the intention in both cases is that if a file exists at all we
should always use the name, but converted to the canonical form.

Index: Completion/Unix/Type/_canonical_paths
===================================================================
RCS file: /cvsroot/zsh/zsh/Completion/Unix/Type/_canonical_paths,v
retrieving revision 1.1
diff -u -r1.1 _canonical_paths
--- Completion/Unix/Type/_canonical_paths	28 May 2006 18:36:06 -0000	1.1
+++ Completion/Unix/Type/_canonical_paths	26 Mar 2008 15:58:52 -0000
@@ -27,18 +27,28 @@
 
 shift 2
 
-if (( ! $+commands[readlink] )); then
+if ! zmodload -F zsh/stat b:zstat 2>/dev/null; then
   _wanted "$tag" expl "$desc" compadd $__gopts $@ && ret=0
   return ret
 fi
 
+typeset addfile newfile
 typeset -a matches files
 
 if (( $__opts[(I)-N] )); then
   files=($@)
 else
   for __index in $@; do
-    files+=$(readlink -qf $__index)
+    addfile=$__index
+    while true; do
+      newfile=$(zstat +link $addfile)
+      if [[ -n $newfile ]]; then
+	addfile=$newfile
+      else
+	break
+      fi
+    done
+    files+=($addfile)
   done
 fi
 
@@ -48,8 +58,18 @@
   expref=${~origpref}
   [[ $origpref == (|*/). ]] && rltrim=.
   curpref=${${expref%$rltrim}:-./}
-  canpref=$(readlink -qf $curpref)
-  if [[ $? -eq 0 ]]; then
+  if zstat $curpref >&/dev/null; then
+    canpref=$curpref
+    while true; do
+      newfile=$(zstat +link $canpref)
+      if [[ -n $newfile ]]; then
+	canpref=$newfile
+      else
+	break
+      fi
+    done
+  fi
+  if [[ -n "$canpref" ]]; then
     [[ $curpref == */ && $canpref == *[^/] ]] && canpref+=/
     canpref+=$rltrim
     [[ $expref == *[^/] && $canpref == */ ]] && origpref+=/


-- 
Peter Stephenson <pws@xxxxxxx>                  Software Engineer
CSR PLC, Churchill House, Cambridge Business Park, Cowley Road
Cambridge, CB4 0WZ, UK                          Tel: +44 (0)1223 692070



Messages sorted by: Reverse Date, Date, Thread, Author