Author: jilles
Date: Sat Apr 17 14:35:46 2010
New Revision: 206759
URL: http://svn.freebsd.org/changeset/base/206759

Log:
  sh: On startup of the shell, use PWD from the environment if it is valid.
  Unset PWD if it is incorrect and no value for it can be determined.
  This preserves the logical current directory across shell invocations.
  
  Example (assuming /home is a symlink):
  $ cd
  $ pwd
  /home/foo
  $ sh
  $ pwd
  /home/foo
  
  Formerly the second pwd would show the physical path (symlinks resolved).

Added:
  head/tools/regression/bin/sh/parameters/pwd2.0   (contents, props changed)
Modified:
  head/bin/sh/cd.c
  head/bin/sh/cd.h
  head/bin/sh/main.c

Modified: head/bin/sh/cd.c
==============================================================================
--- head/bin/sh/cd.c    Sat Apr 17 12:22:44 2010        (r206758)
+++ head/bin/sh/cd.c    Sat Apr 17 14:35:46 2010        (r206759)
@@ -70,6 +70,7 @@ STATIC int docd(char *, int, int);
 STATIC char *getcomponent(void);
 STATIC char *findcwd(char *);
 STATIC void updatepwd(char *);
+STATIC char *getpwd(void);
 STATIC char *getpwd2(void);
 
 STATIC char *curdir = NULL;    /* current working directory */
@@ -351,7 +352,7 @@ pwdcmd(int argc, char **argv)
 /*
  * Get the current directory and cache the result in curdir.
  */
-char *
+STATIC char *
 getpwd(void)
 {
        char *p;
@@ -374,7 +375,6 @@ getpwd(void)
 STATIC char *
 getpwd2(void)
 {
-       struct stat stdot, stpwd;
        char *pwd;
        int i;
 
@@ -387,12 +387,29 @@ getpwd2(void)
                        break;
        }
 
-       pwd = getenv("PWD");
+       return NULL;
+}
+
+/*
+ * Initialize PWD in a new shell.
+ * If the shell is interactive, we need to warn if this fails.
+ */
+void
+pwd_init(int warn)
+{
+       char *pwd;
+       struct stat stdot, stpwd;
+
+       pwd = lookupvar("PWD");
        if (pwd && *pwd == '/' && stat(".", &stdot) != -1 &&
            stat(pwd, &stpwd) != -1 &&
            stdot.st_dev == stpwd.st_dev &&
            stdot.st_ino == stpwd.st_ino) {
-               return pwd;
+               if (curdir)
+                       ckfree(curdir);
+               curdir = savestr(pwd);
        }
-       return NULL;
+       if (getpwd() == NULL && warn)
+               out2fmt_flush("sh: cannot determine working directory\n");
+       setvar("PWD", curdir, VEXPORT);
 }

Modified: head/bin/sh/cd.h
==============================================================================
--- head/bin/sh/cd.h    Sat Apr 17 12:22:44 2010        (r206758)
+++ head/bin/sh/cd.h    Sat Apr 17 14:35:46 2010        (r206759)
@@ -29,6 +29,6 @@
  * $FreeBSD$
  */
 
-char   *getpwd(void);
+void    pwd_init(int);
 int     cdcmd (int, char **);
 int     pwdcmd(int, char **);

Modified: head/bin/sh/main.c
==============================================================================
--- head/bin/sh/main.c  Sat Apr 17 12:22:44 2010        (r206758)
+++ head/bin/sh/main.c  Sat Apr 17 14:35:46 2010        (r206759)
@@ -153,10 +153,7 @@ main(int argc, char *argv[])
        init();
        setstackmark(&smark);
        procargs(argc, argv);
-       if (getpwd() == NULL && iflag)
-               out2fmt_flush("sh: cannot determine working directory\n");
-       if (getpwd() != NULL)
-               setvar ("PWD", getpwd(), VEXPORT);
+       pwd_init(iflag);
        if (iflag)
                chkmail(1);
        if (argv[0] && argv[0][0] == '-') {

Added: head/tools/regression/bin/sh/parameters/pwd2.0
==============================================================================
--- /dev/null   00:00:00 1970   (empty, because file is newly added)
+++ head/tools/regression/bin/sh/parameters/pwd2.0      Sat Apr 17 14:35:46 
2010        (r206759)
@@ -0,0 +1,24 @@
+# $FreeBSD$
+# Check that PWD is exported and accepted from the environment.
+set -e
+
+T=$(mktemp -d ${TMPDIR:-/tmp}/sh-test.XXXXXX)
+trap 'rm -rf $T' 0
+cd -P $T
+TP=$(pwd)
+mkdir test1
+ln -s test1 link
+cd link
+[ "$PWD" = "$TP/link" ]
+[ "$(pwd)" = "$TP/link" ]
+[ "$(pwd -P)" = "$TP/test1" ]
+[ "$(sh -c pwd)" = "$TP/link" ]
+[ "$(sh -c pwd\ -P)" = "$TP/test1" ]
+cd ..
+[ "$(pwd)" = "$TP" ]
+cd -P link
+[ "$PWD" = "$TP/test1" ]
+[ "$(pwd)" = "$TP/test1" ]
+[ "$(pwd -P)" = "$TP/test1" ]
+[ "$(sh -c pwd)" = "$TP/test1" ]
+[ "$(sh -c pwd\ -P)" = "$TP/test1" ]
_______________________________________________
svn-src-all@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/svn-src-all
To unsubscribe, send any mail to "svn-src-all-unsubscr...@freebsd.org"

Reply via email to