NetBSD-Bugs archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: bin/45390: more sh/$PWD follies

The following reply was made to PR bin/45390; it has been noted by GNATS.

From: Alan Barrett <>
Subject: Re: bin/45390: more sh/$PWD follies
Date: Fri, 23 Sep 2011 21:54:19 +0200

 On Wed, 21 Sep 2011, wrote:
 > - pwd is supposed to accept both -L and -R options, but the -R option
 >   is not accepted:
 >      % mkdir /tmp/foo
 >      % env PWD= sh -c 'cd /tmp/foo; pwd -R'
 >      pwd: Illegal option -R
 >      Exit 2
 >      %
 Did you mean "pwd -P"?  That seems to work.
 > - while cd -L is not supported, cd is supposed to accept the -R
 >   option, which is the default, but it doesn't:
 >      % sh -c 'cd -R /'
 >      cd: Illegal option -R
 >      Exit 2
 >      %
 Here too, I think you meant "cd -P", not "cd -R".
 > - cd is supposed to print the name of the directory it changes into
 >   if it is not the name given, and the shell is interactive, but it
 >   does not:
 >      % mkdir /tmp/foo
 >      % mkdir /tmp/foo/test
 >      % ln -s test /tmp/foo/link
 >      % sh -i
 >      $ cd /tmp/foo/link
 >      $ /bin/pwd
 >      /tmp/foo/test
 >      $ exit
 >        %
 I'd argue that the cd command really did chdir to /tmp/foo/link.  That's
 the string it passed to the syscall.
 > - pwd -L is not supposed to print the cached internal value (which is
 >   related to but separate from $PWD) unless it names the current
 >   directory. However, at least in some circumstances it will, such as
 >   this:
 >      % mkdir /tmp/foo
 >      % mkdir /tmp/foo/alpha
 >      % mkdir /tmp/foo/beta
 >      % env PWD= sh -c 'cd /tmp/foo/beta; mv /tmp/foo/beta 
 > /tmp/foo/alpha/beta; pwd'
 >      /tmp/foo/beta
 >      %
 That's a bug.
 > - cd is supposed to both change to the named directory and update
 >   $PWD with it, but it does not always do this correctly:
 >      % mkdir /tmp/foo
 >      % mkdir /tmp/foo/alpha
 >      % mkdir /tmp/foo/beta
 >      % env PWD= sh -c 'cd /tmp/foo/beta; mv /tmp/foo/beta 
 > /tmp/foo/alpha/beta; cd ..; echo $PWD'
 >      /tmp/foo
 >      %
 >   (In this case the internal cached value used by the pwd builtin is also
 >   incorrectly set to /tmp/foo and then also, as in the previous example,
 >   incorrectly printed.)
 There's at least one bug there.  According to
 "cd -L" is supposed to be the default, and "cd -L .." would legitimately
 end up in /tmp/foo, by performing string manipulation on $PWD.  NetBSD
 /bin/sh does not support "cd -L", and even if it did support it, I think
 it's really confusing for "cd -L" to be the default.
 >Also, the man page is not explicit about what "update PWD with the
 >specified directory" actually means if the specified directory is not
 >absolute. The wording needs to be improved.
 >Since I wrote that text, I'm allowed to say what I intended, which is
 >that PWD be updated with the/a name for the specified physical
 >directory, not that the value of $PWD should be used in preference to
 >reality when it's wrong, updated relatively, and then used in a
 >chdir() call. ksh will do the latter in at least some circumstances
 >but it's clearly broken:
 I agree.  But POSIX specifies the broken behaviour, unless you use
 "cd -P".
 --apb (Alan Barrett)

Home | Main Index | Thread Index | Old Index