B09 Lab week 8

This lab is on learning and practicing opendir(), readdir(), closedir(), and stat() to read a directory and obtain information about the file(name)s inside. They are fairly straightforward; please read their man pages for how to use them. Just one annoying bit: For stat() you have to say man 2 stat; and for its struct, man 3type stat.

Write a C program list.c that takes 1 command-line argument for a directory name. Print the filenames and file types of the files in that directory, one per line; for regular files, also print the link count (st_nlink obtained from stat() or lstat()).

For this lab, we only identify these file types: regular, directory, symlink; all other types (and unknown) are “other”. You may assume that the non-standard d_type field from readdir() is valid; this works on Mathlab and generally Linux. (A standard-compliant way is lstat(). You can choose either way.)

You will notice that readdir() gives you filenames in an arbitrary order. This is normal; just print in the order you receive. (Automarking will accept any order.)

For simplicity, you may assume legal inputs as promised above, and omit most error handling, though it is always a good idea to check whether opendir() actually succeeds. If you add debugging/error messages for your sake, please send them to stderr only.

Sample output (after piping to sort):

$ ./a.out /courses/courses/cscb09s26/laialber/l08 | sort -k1,1
. directory
.. directory
00-handout.html regular 1
i-am-a-file.txt regular 2
i-am-a-hardlink-i-think regular 2
i-am-a-symlink symlink
list.c regular 1

Please submit your C file as list.c