[Toybox] Add remaining pwd options

Felix Janda felix.janda at posteo.de
Thu Jan 10 12:25:13 PST 2013


On 01/02/13 at 12:41am, Rob Landley wrote:
> On 01/01/2013 06:49:00 AM, Felix Janda wrote:
> > Do I understand correctly that in the case that pwd (as a builtin of  
> > toysh)
> > can't determine the path of the current working directory with  
> > getcwd() it
> > should fall back to printing $PWD?
> 
> 1) The default value is -L, and it only looks at $PWD in -L mode. If  
> you say pwd -P with no cwd, it should fail.
> 
> 2) If $PWD is set to a value that the shell wouldn't set it to, it's  
> discarded and we fall back to -P. Specifically, if it's not an absolute  
> path, or if it contains . or .. components, it is considered invalid.
> 
> 3) If $PWD does not point to the current directory, fall back to -P.
> 
> What I did was disable #3 in the case where cwd doesn't exist. So the  
> new rule #3 is:
> 
> 3) If cwd exists and $PWD doesn't point to it, fall back to -P.

Thanks for the clarification.

Your version of 3) depends on whether pwd is builtin or not. Do you mean
something like "If getcwd() fails ..."?

> > BTW, in the case that one has deleted and recreated one's current  
> > working
> > directory one could also use "cd ." to get to the new directory.
> 
> Good to know. (This means the shell is special casing "." as well as  
> "..". I need to read the susv4 shell stuff thoroughly, it's been  
> years...)

The susv4 page special cases "." and ".." a bit, but it seems to me only
in the $CDPATH handling. Ah, I see that you don't care about $CDPATH from
the about page. Then I think one can leave out step 5 on susv4's page on
cd, and "cd ." is no more special than "cd dir"; it does a chdir to "$PWD/."
or "$PWD/dir" respectively and then updates $PWD to its canonical form. (and
modifies $OLDPWD also if necessary)


Another interesting situation is if your current directory "/dir" has been
moved to "/olddir" and say "/dir" has been recreated. Then "cd ." will move
you to new directory whereas "cd $(pwd -P)" will preserve your cwd and fix up
$PWD. (at least for a shell behaving posixly correct)

Imagine the same situation but with "/dir" not being recreated after being
moved. Then "cd ." should fail according to susv4 since "$PWD/." = "/dir/.",
which does not exist. Would you like to have "cd ." behave the same as
"cd $(pwd)" in this case? Bash does this if not in POSIX mode. Busybox ash
doesn't do this and for some reason even "cd $(pwd)" fails.

Felix

 1357849513.0


More information about the Toybox mailing list