[Toybox] Add remaining pwd options

Felix Janda felix.janda at posteo.de
Sat Dec 29 05:38:24 PST 2012


On 12/29/12 at 03:53am, Rob Landley wrote:
> On 12/28/2012 03:24:17 PM, Felix Janda wrote:
> > Hi,
> > 
> > the first patch adds the -L and -P options to pwd as specified by  
> > POSIX.
> > The test script again uses stat. This time in order to get inode  
> > numbers
> > of directories.
> 
> For future reference adding the test in the same commit as the changes  
> being tested is probably ok.

Ok.

> I've applied this patch, but am going to have to take a closer look at  
> it in the morning. (You added a -L option which... is a NOP? Huh, what  
> posix specifies here is kind of insane, there's no way to get the raw  
> getcwd() output. The -L stuff is all about $PWD, and if that doesn't  
> have a valid value it falls back to -P which does a realpath() on the  
> data to strip symlinks...? I need to read this when I'm more awake,  
> this standard is written for a system that stores state different than  
> linux. The current working directory is a process attribute used  
> directly by the vfs, it's not an environment variable...)
> 
> I think the fix is to have -L _not_ be the default, and to have pwd  
> return the raw getcwd() output when neither -L nor -P is specified...  
> but that's a technical violation of posix...

POSIX says that "pwd" should behave the same as "pwd -L". The current
"pwd -P" should behave the same way as the previous version of "pwd". It
just returns the getcwd() output. "pwd -L" does just check whether the
environment variable PWD is also a valid current working directory and
uses that instead of the output of getcwd() if that's the case.

So according to POSIX we have:

$ cd /tmp
$ ln -s . a
$ cd a
$ export PWD=/tmp/a
$ pwd
/tmp/a
$ pwd -P
/tmp

Actually at least bash seems to update PWD automatically so that the export
statement is unnecessary.

It's maybe interesting to see what coreutils is doing. A fragment:

  /* POSIX requires a default of -L, but most scripts expect -P.  */
  bool logical = (getenv ("POSIXLY_CORRECT") != NULL);


The implementation of "pwd -L" could also use realpath instead of stat.

Taking a further look at POSIX I think that the option string should be
">0LP[-LP]" instead of ">0LP[!LP]".

Felix

> Rob

 1356788304.0


More information about the Toybox mailing list