Xref: utzoo comp.unix.wizards:19842 comp.sys.hp:3815 Path: utzoo!utgpu!jarvis.csri.toronto.edu!cs.utexas.edu!usc!wuarchive!mailrus!cornell!uw-beaver!uw-entropy!dataio!pilchuck!pacer!davidb From: davidb@Pacer.UUCP (David Barts) Newsgroups: comp.unix.wizards,comp.sys.hp Subject: pwd Keywords: pwd file descriptor kernel Message-ID: <263@zircon.UUCP> Date: 19 Dec 89 23:29:00 GMT Followup-To: poster Organization: Pacer Corp., Bothell, WA Lines: 30 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@pacer.uucp ...!fluke!pacer!davidb