Path: utzoo!utgpu!news-server.csri.toronto.edu!clyde.concordia.ca!mcgill-vision!snorkelwacker!usc!wuarchive!uunet!virtech!cpcahil From: cpcahil@virtech.uucp (Conor P. Cahill) Newsgroups: comp.unix.questions Subject: Re: /bin/pwd Message-ID: <1990Sep14.002232.29540@virtech.uucp> Date: 14 Sep 90 00:22:32 GMT References: <2488@idunno.Princeton.EDU> Reply-To: cpcahil@virtech.UUCP (Conor P. Cahill) Organization: Virtual Technologies Inc., Sterling VA Lines: 34 In article <2488@idunno.Princeton.EDU> pfalstad@phoenix.Princeton.EDU (Paul John Falstad) writes: >Can anyone tell me how /bin/pwd works? I thought it was a trivial On BSD style systems /bin/pwd calls the library function (I think it is getcwd(), but don't have the manual nearby). On System V style systems, getcwd(3) popen(3)'s "/bin/pwd" to get the current workind directory. Anyway, the way pwd works is something like the following: Stat the current directory (".") and get the inode number and device number Open ".." and read each entry if you find an entry with the same inode number stat it if it is the same device number you got a match, save name portion close this directory start again one directory higher if you don't find an entry with the right inode number go back and stat each entry looking for the one with the correct device number This goes on and on until you get to the top of the filesystem. (you get to the top when a stat of . & .. return the exact same stuff. -- Conor P. Cahill (703)430-9247 Virtual Technologies, Inc., uunet!virtech!cpcahil 46030 Manekin Plaza, Suite 160 Sterling, VA 22170