cs4213@umbc5.umbc.edu (cs4213) (03/07/91)
Using only system calls in a C program, how can you find out what the name of the current directory is, if you are at the top of a file system?? Normally, you can find the name of the current directory by recording the inode of the current directory, and the inode of the current parent directory, then examing the parent directory to find the name attached to that inode. However, if the current directory is the top of a filesystem, the inode of both the current directory and the parent directory is 2. Because the two numbers are the same, there is no way I can envision of examing the parent directory. I also can find no way of determining the location where the current file system is mounted in the parent file system. Can anyone illuminate me??
jik@athena.mit.edu (Jonathan I. Kamens) (03/07/91)
The algorithm (simplified to allow chdir'ing; it could be written without it, in order to use it as a library function that doesn't have to fork to work properly) looks something like this (starting with an accumulated path of ""): 1. stat(".") 2. stat("..") 3. If the devices of "." and ".." *and* the inodes of "." and ".." are equal, then: a. append "/" to the beginning of the accumulated path b. return the accumulated path. 4. opendir("..") 5. for each file (readdir("..")), do: a. stat("file") b. If the device and inode of "file" is equal to the device and inode of "." retrieved in step (1), then: i. append "/" to the beginning of the accumulated path ii. append "file" to the beginning of the accumulated path iii. chdir("..") iv. goto (1) 6. signal an error, indicating that no file in ".." matching "." was found The algorithm you described is wrong for two reasons: (1) It compares only inodes, and not both inodes and device numbers; and (2) It forgets about going through the ".." directory looking for an entry matching ".". -- Jonathan Kamens USnail: MIT Project Athena 11 Ashford Terrace jik@Athena.MIT.EDU Allston, MA 02134 Office: 617-253-8085 Home: 617-782-0710