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

PWD parameter



The standard does not mention PWD or OLDPWD in the descriprion of the
shell special parameters, they are only mentioned for the `cd' command:

     The cd utility will change the working directory of the current
     shell execution environment; see Shell Execution Environment .  If
     the current working directory is successfully changed, it will save
     an absolute pathname of the old working directory in the environment
     variable OLDPWD and it will save an absolute pathname of the new
     working directory in the environment variable PWD .

This means that PWD is nothing special for the shell, it is just set by
cd.  But between cd's the user is free to use it.  And many scripts in
fact do assign PWD, but they always set it to the current directory.  For
example some configure scripts have this:

# if PWD already has a value, it is probably wrong.
if [ -n "$PWD" ]; then PWD=`pwd`; fi

On zsh PWD is read-only so the shell will complain, although the script
does not terminate.  Also zsh did not export OLDPWD as required above by
the standard.  The quoted standard is `The Single UNIX (r) Specification'
from OpenGroup, I'm not sure that POSIX has these requirements.

The patch is for 3.0.5, it does not work for 3.1.3, I'll post a patch for
that later.

Zoltan


*** Src/builtin.c.orig	Mon Aug 25 22:40:43 1997
--- Src/builtin.c	Sat May 23 23:29:36 1998
***************
*** 986,991 ****
--- 986,1024 ----
  
  /* cd, chdir, pushd, popd */
  
+ /**/
+ void
+ set_pwd_env(void)
+ {
+     Param pm;
+ 
+     pm = (Param) paramtab->getnode(paramtab, "PWD");
+     if (pm && PM_TYPE(pm->flags) != PM_SCALAR) {
+ 	pm->flags &= ~PM_READONLY;
+ 	unsetparam_pm(pm, 0);
+     }
+ 
+     pm = (Param) paramtab->getnode(paramtab, "OLDPWD");
+     if (pm && PM_TYPE(pm->flags) != PM_SCALAR) {
+ 	pm->flags &= ~PM_READONLY;
+ 	unsetparam_pm(pm, 0);
+     }
+ 
+     setsparam("PWD", ztrdup(pwd));
+     setsparam("OLDPWD", ztrdup(oldpwd));
+ 
+     pm = (Param) paramtab->getnode(paramtab, "PWD");
+     if (!(pm->flags & PM_EXPORTED)) {
+ 	pm->flags |= PM_EXPORTED;
+ 	pm->env = addenv("PWD", pwd);
+     }
+     pm = (Param) paramtab->getnode(paramtab, "OLDPWD");
+     if (!(pm->flags & PM_EXPORTED)) {
+ 	pm->flags |= PM_EXPORTED;
+ 	pm->env = addenv("PWD", pwd);
+     }
+ }
+ 
  /* The main pwd changing function.  The real work is done by other     *
   * functions.  cd_get_dest() does the initial argument processing;     *
   * cd_do_chdir() actually changes directory, if possible; cd_new_pwd() *
***************
*** 1023,1028 ****
--- 1056,1062 ----
  	    chdir(unmeta(pwd));
  	}
      }
+     set_pwd_env();
      return 0;
  }
  
*** Src/hashtable.h.orig	Tue Jun  3 02:01:01 1997
--- Src/hashtable.h	Sat May 23 23:05:11 1998
***************
*** 144,154 ****
  IPDEF5("OPTIND", &zoptind, intvarsetfn),
  IPDEF5("SHLVL", &shlvl, intvarsetfn),
  
- #define IPDEF6(A,B) {NULL,A,PM_SCALAR|PM_READONLY|PM_SPECIAL,BR(NULL),SFN(nullsetfn),BR(strvargetfn),0,(void *)B,NULL,NULL,NULL,0}
- IPDEF6("PWD", &pwd),
- 
  #define IPDEF7(A,B) {NULL,A,PM_SCALAR|PM_SPECIAL,BR(NULL),BR(strvarsetfn),BR(strvargetfn),0,(void *)B,NULL,NULL,NULL,0}
- IPDEF7("OLDPWD", &oldpwd),
  IPDEF7("OPTARG", &zoptarg),
  IPDEF7("NULLCMD", &nullcmd),
  IPDEF7("POSTEDIT", &postedit),
--- 144,150 ----
*** Src/params.c.orig	Thu May 14 23:32:24 1998
--- Src/params.c	Sat May 23 23:23:56 1998
***************
*** 135,145 ****
  	    pm->flags |= PM_EXPORTED;
  	    pm->env = addenv("HOME", home);
  	}
- 	pm = (Param) paramtab->getnode(paramtab, "PWD");
- 	if (!(pm->flags & PM_EXPORTED)) {
- 	    pm->flags |= PM_EXPORTED;
- 	    pm->env = addenv("PWD", pwd);
- 	}
  	pm = (Param) paramtab->getnode(paramtab, "LOGNAME");
  	if (!(pm->flags & PM_EXPORTED)) {
  	    pm->flags |= PM_EXPORTED;
--- 135,140 ----
***************
*** 152,157 ****
--- 147,153 ----
  	pm->env = addenv("SHLVL", buf);
  
  	/* Add the standard non-special parameters */
+ 	set_pwd_env();
  	setsparam("MACHTYPE", ztrdup(MACHTYPE));
  	setsparam("OSTYPE", ztrdup(OSTYPE));
  	setsparam("TTY", ztrdup(ttystrname));



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