Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!sdd.hp.com!decwrl!sun-barr!newstop!texsun!letni!void!ozdaltx!toma From: toma@ozdaltx.UUCP (Tom Armistead) Newsgroups: comp.unix.questions Subject: Re: /bin/pwd Summary: opendir, dirent.d_ino Message-ID: <6960@ozdaltx.UUCP> Date: 18 Sep 90 02:30:33 GMT References: <2488@idunno.Princeton.EDU> Organization: AIDS INFO EXCG/OZ BBS - Dallas, TX Lines: 31 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 > program until I tried to write my own without getcwd(3). getcwd is not > a system call, so it must be possible. The way I think it works is > quite complicated, so I thought there would be an easier way. > > David Hemmings appeared by permission of the National Forestry Commission. I don't know how /bin/pwd works - but I ran into the same problem and this is how I tackled it... Open "." using stat(2), get the inode number and save it. Open ".." using stat, get the inode number, if equal to the inode for "." then you are at the root dir "/". If they are not equal: open ".." using opendir(3C) and read the entries from it using readdir(3C) until you find the inode number for ".", this will give you the name of the current dir from the parent. Repeat this process walking back to "../..", then "../../..", etc, until the 2 inodes are equal... It sounds kinda messy, but it really isn't after you think about???? I have this coded and working (for System V 3.2), if you would like a copy, just let me know... Tom -- ------------------------------- {uunet,smu,ames}!sulaco!ozdaltx!toma (Tom Armistead @ Garland, Texas) {mic,void,egsner}!ozdaltx!toma