$24
Laboratory Exercise
1. There’s one task for this week: Write a program, mypwd.c, that performs the same task as /bin/pwd without using the getcwd(3) function described by POSIX. I want you to traverse the directories and do it yourself. The technique is described in Stevens, Section 4.22.
If there are multiple paths to (possible names for) any partiular directory, return the one that appears earliest in each directory that is traversed.
Your program should produce the same results as /bin/pwd. In case of error, call perror() with the string “mypwd” to report the error and exit. If the pathname is too long (see below), simply print “path too long” and exit. Similarly, if for whatever reason, you are unable to determine the present working directory (e.g. it’s been unlinked), print “cannot get current directory.”
Just to make life easier, you may assume you will get no paths deeper than PATH_MAX, defined in limits.h. If PATH_MAX is undefined, you may define it to be 2048 characters.
1
Tricks and Tools
You will probably want to look into Stevens, Chapter 4, and:
opendir(3)
open a directory for reading
readdir(3)
read a directory entry
rewinddir(3)
rewind a directory to the beginning
closedir(3)
close a directory
stat(2)
get file status
lstat(2)
get file status
What To Turn In
Submit the program described above to the lab05 directory of ngonella via handin.
2