zsh-workers
 help / color / mirror / code / Atom feed
From: Zoltan Hidvegi <hzoli@cs.elte.hu>
To: zsh-workers@math.gatech.edu (Zsh hacking and development)
Subject: PWD parameter
Date: Sat, 23 May 1998 23:44:28 -0500 (CDT)	[thread overview]
Message-ID: <199805240444.XAA08814@hzoli.home> (raw)

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));


             reply	other threads:[~1998-05-24  4:47 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1998-05-24  4:44 Zoltan Hidvegi [this message]
1998-05-24  5:57 ` Bart Schaefer
1998-05-24  7:51   ` Zoltan Hidvegi
1998-05-24 16:42     ` Bart Schaefer
1998-05-24 17:38       ` Zoltan Hidvegi
1998-05-25  2:16         ` Bart Schaefer
1998-05-25  3:02           ` Zoltan Hidvegi
1998-05-25  4:02             ` Bart Schaefer

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=199805240444.XAA08814@hzoli.home \
    --to=hzoli@cs.elte.hu \
    --cc=zsh-workers@math.gatech.edu \
    /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).