pwd

David Barts davidb at Pacer.UUCP
Wed Dec 20 10:29:00 AEST 1989


How does /bin/pwd work?  There is no kernel call to find your current
directory in UNIX, so it has to do some backhanded peeking in memory
somewhere, probably in kernel memory.  This suspicion is reinforced by
/bin/pwd being setuid to root on my system (HP-UX, System V
compatible).

My guess is that the system uses one of each process's file
descriptors to refer to the opened current working directory file, and
pwd takes this descriptor and somehow comes up with the name of the
file associated with it.

This brings up the possibility of doing the following:
	saved_cwd_fd = dup(cwd_fd);
	dup2(mystery_fd, cwd_fd);
	s = getcwd();  /* s now points to name of mystery file */
	dup2(saved_cwd_fd, cwd_fd);
	close(saved_cwd_fd);

(At least on our system, getcwd is not a system call; it forks
/bin/pwd and reads the current directory from a pipe.)

If so, what is the value of cwd_fd for various UNIX systems,
especially HP-UX?  Or does the system just store the cwd as a string
somewhere in the process header?

Reply to me and I'll summarize.
-- 
David Barts			Pacer Corporation
davidb at pacer.uucp		...!fluke!pacer!davidb



More information about the Comp.unix.wizards mailing list