Why use pwd(1) for getpwd(3C)? (Re: Why use find?)

Dan Bernstein brnstnd at kramden.acf.nyu.edu
Fri Oct 12 22:02:50 AEST 1990


In article <1990Oct11.140654.22117 at virtech.uucp> cpcahil at virtech.UUCP (Conor P. Cahill) writes:
> The shells that have pwd built-in usually don't run getpwd()/getcwd() to 
> get the directory each time.  They run it once at start up time and then
> keep track of where they go.  This causes problems if you run pwd (the
> shell built-in) from a directory that you got to via a symbolic link
> and it gives you the incorrect information (whereas /bin/pwd will give
> you the right stuff).

But the shell can read the symbolic link and figure out where it's
going. It can detect race conditions as follows: lstat() to tell it's a
symbolic link, readlink(), figure out the pathname of the target
directory, stat() the target directory, chdir() to the symbolic link,
stat(".") to make sure everything's okay, and getwd() in the exceptional
situation that the link or target has moved.

The real problem is that the directory could be moved, or even removed,
without the shell finding out.

---Dan



More information about the Comp.unix.programmer mailing list