B09 Lab week 9

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 (e.g., “man readdir”) for how to use them.

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 is valid; this works on Mathlab and most Ubuntu installations. (A standard way is to use lstat(). You can choose either way.)

You will notice that readdir() gives you filenames in an arbitrary order. This is normal; ust print in the order you receive. (Automarking will perform sorting for you.)

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:

$ ./a.out /courses/courses/cscb09s25/laialber/l09
. directory
.. directory
symlink-to-list.c symlink
list.c regular 2
00-handout.html regular 1
hardlink-to-list.c regular 2

Please submit your C file as list.c