1

My task is to implement the Unix command pwd using inodes in C. I have figured out how I can go back recursively until the inode of '..' is the same as the inode of '.'. The problem I am having now is that I don't know how to get the directory name of '..'. Using the stat struct I can get everything about the file like size, inode, etc but I need to get the file so I can make it output something like:

/home/GONZAGA/tcosentino/documents/OS

How can I get these directory names?

I have the stat struct so it can print out this so far:

[tcosentino@ada2 OS]$ ./a.out .
filename: .
 device: 64768
 inode: 55804237
 protection: 40755
 number of hard links: 5
 user ID of owner: 81963576
 group ID of owner: 501
 device type (if inode device): 0
 total size, in bytes: 4096
 blocksize for filesystem I/O: 4096
 number of blocks allocated: 8
 time of last access: 1354817261 : Thu Dec  6 10:07:41 2012
 time of last modification: 1354817249 : Thu Dec  6 10:07:29 2012
 time of last change: 1354817249 : Thu Dec  6 10:07:29 2012
1

1 Answer 1

0

The simple-minded, semi-brute force approach to finding the name of the current directory means you have to find the name of the parent directory, and then scan the names listed in the parent directory to find the one with the same inode number and device number as the current directory. How do you find the name of the parent directory? Well, ... you recurse up the directory tree, stopping when you reach the root of the system, which is where the inode number and device number of .. are the same as for ..

Be aware that this is the simple-minded approach. It can yield devastatingly awful performance if the current directory is NFS mounted and there are lots of home directories to be automounted. Consequently, there has to be another way to do it without triggering the automounts, etc, but I've not investigated what that is.

I do have code which has the devastating performance, version 1.1 from 1987, version 2.5 from 2008 (and it compiled on Mac OS X 10.10.1 and while I thought it failed, closer scrutiny shows it ran correctly, but I misinterpreted the printed output; version 2.6 will have clearer printing so I don't get confused again).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.