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

Re: Bug in zsh builtin pwd when option CHASE_LINKS is set



On Mon, 7 Jun 2010 18:00:46 +0100
Peter Stephenson <Peter.Stephenson@xxxxxxx> wrote:
> + * We could fall back to getcwd() instead.

I can't see how this could be worse.  It gets the right answer for
.snapshot directories, i.e. it follows symbolic links further up the path
while still locating the tail end of the path.  It takes a while, though.

Index: configure.ac
===================================================================
RCS file: /cvsroot/zsh/zsh/configure.ac,v
retrieving revision 1.129
diff -p -u -r1.129 configure.ac
--- configure.ac	18 Dec 2009 19:50:45 -0000	1.129
+++ configure.ac	9 Jun 2010 09:19:26 -0000
@@ -1170,7 +1170,7 @@ AC_CHECK_FUNCS(strftime strptime mktime 
 	       regcomp regexec regerror regfree \
 	       gdbm_open getxattr \
 	       realpath canonicalize_file_name \
-	       symlink)
+	       symlink getcwd)
 AC_FUNC_STRCOLL
 
 if test x$enable_cap = xyes; then
@@ -1898,6 +1898,34 @@ if test x$zsh_cv_use_getcwd = xyes; then
   AC_DEFINE(USE_GETCWD)
 fi
 
+dnl GNU getcwd() can allocate as much space as necessary for a
+dnl directory name, preventing guessing games.
+AH_TEMPLATE([GETCWD_CALLS_MALLOC],
+[Define to 1 if getcwd() calls malloc to allocate memory.])
+if test x$ac_cv_func_getcwd = xyes; then
+  AC_CACHE_CHECK(whether getcwd calls malloc to allocate memory,
+  zsh_cv_getcwd_malloc,
+  [AC_TRY_RUN([
+#include <unistd.h>
+#include <string.h>
+int main() {
+    char buf[1024], *ptr1, *ptr2;
+    ptr1 = getcwd(buf, 1024);
+    ptr2 = getcwd(NULL, 0);
+    if (ptr1 && ptr2 && !strcmp(ptr1, ptr2)) {
+      return 0;
+    }
+    return 1;
+}
+],
+  zsh_cv_getcwd_malloc=yes,
+  zsh_cv_getcwd_malloc=no,
+  zsh_cv_getcwd_malloc=no)])
+  if test x$zsh_cv_getcwd_malloc = xyes; then
+    AC_DEFINE(GETCWD_CALLS_MALLOC)
+  fi
+fi
+
 dnl CHECK FOR setproctitle() FOR jobs -Z / ARGV0
 AH_TEMPLATE([HAVE_SETPROCTITLE],
 [Define to 1 if the system supports `setproctitle' to change process name])
Index: Src/compat.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/compat.c,v
retrieving revision 1.20
diff -p -u -r1.20 compat.c
--- Src/compat.c	8 Jun 2010 08:51:04 -0000	1.20
+++ Src/compat.c	9 Jun 2010 09:19:26 -0000
@@ -420,9 +420,9 @@ zgetdir(struct dirsav *d)
 
 /*
  * Try to find the current directory.
+ * If we couldn't work it out internally, fall back to getcwd().
  * If it fails, fall back to pwd; if zgetcwd() is being used
  * to set pwd, pwd should be NULL and we just return ".".
- * We could fall back to getcwd() instead.
  */
 
 /**/
@@ -430,6 +430,23 @@ char *
 zgetcwd(void)
 {
     char *ret = zgetdir(NULL);
+#ifdef HAVE_GETCWD
+    if (!ret) {
+#ifdef GETCWD_CALLS_MALLOC
+	char *cwd = getcwd(NULL, 0);
+	if (cwd) {
+	    ret = dupstring(cwd);
+	    free(cwd);
+	}
+#else
+	char *cwdbuf = zalloc(PATH_MAX);
+	ret = getcwd(cwdbuf, PATH_MAX);
+	if (ret)
+	    ret = dupstring(ret);
+	free(cwdbuf);
+#endif /* GETCWD_CALLS_MALLOC */
+    }
+#endif /* HAVE_GETCWD */
     if (!ret)
 	ret = pwd;
     if (!ret)

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


Member of the CSR plc group of companies. CSR plc registered in England and Wales, registered number 4187346, registered office Churchill House, Cambridge Business Park, Cowley Road, Cambridge, CB4 0WZ, United Kingdom



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