zsh-workers
 help / color / mirror / code / Atom feed
From: Peter Stephenson <Peter.Stephenson@csr.com>
To: zsh-workers@zsh.org
Subject: Re: Bug in zsh builtin pwd when option CHASE_LINKS is set
Date: Wed, 9 Jun 2010 10:25:38 +0100	[thread overview]
Message-ID: <20100609102538.0c5e9f1d@csr.com> (raw)
In-Reply-To: <20100607180046.20860b48@csr.com>

On Mon, 7 Jun 2010 18:00:46 +0100
Peter Stephenson <Peter.Stephenson@csr.com> 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@csr.com>            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


  reply	other threads:[~2010-06-09  9:26 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-09-24  7:48 Vincent Lefevre
2010-06-07 13:13 ` Vincent Lefevre
2010-06-07 16:43   ` Peter Stephenson
2010-06-07 17:00     ` Peter Stephenson
2010-06-09  9:25       ` Peter Stephenson [this message]
2010-06-07 18:20     ` Bart Schaefer
2010-06-09 10:08   ` Vincent Lefevre

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=20100609102538.0c5e9f1d@csr.com \
    --to=peter.stephenson@csr.com \
    --cc=zsh-workers@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).