/bin/pwd

Tom Armistead toma at ozdaltx.UUCP
Tue Sep 18 12:30:33 AEST 1990


In article <2488 at idunno.Princeton.EDU>, pfalstad at 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



More information about the Comp.unix.questions mailing list