Remarks

Exercise 1 - stat ()

Using the system call stat(), write a program that displays the inode number, size, modification date and type (file, directory, or otherwise) of a file passed as an argument. No need to format the date change: simply post it as is (in seconds since January 1, 1970). For the type, just print "d", "f", or "?" to specify a directory, file, or something else. To know the type of a file, use S_ISDIR macros family on the field st_mode of the structure passed to stat().

Exercise 2 - lstat ()

Modify the previous program to indicate whether the file is a link (with the letter "l") and give the link destination. For this, use the system call lstat() (note: stat() always follows links, and never says if a file is linked or not! Moreover, if you ask stat() to follow an invalid link, it will return an error), and the system call readlink() for the link destination. Read the manpage of readlink () ...

Exercise 3 - My mini-version of ls

Using the library functions opendir() and readdir() (note: there also exists a system call readdir(), use the one described in section 3 of man pages), write a program that lists the contents of the current directory, displaying just the name of each file. Do not forget the closedir() at the end. Then, display the same information as the prior exercise (code reuse) - by adding the name of each file, of course. Warning: you must append the directory name and file name, we recommend the use of snprintf ().

Modify the previous program so that:

  • if there is no argument, it functions as the previous program
  • if the first argument is a file, it works as the program of Exercise 2
  • if the first argument is a directory, it lists the contents of this directory
  • if there are several arguments, he treats them one after the other

Exercise 4 - Monitoring File

Using the modification date, write a program that "watches" a file supplied as an argument until this file is changed. The program should always check the modification date (with stat()) until it changes, then exit. You can use the functions sleep() or usleep() between calls to stat() so that the program does not consume any CPU time. Reminder: To edit a file, you can do touch the_file, or echo >> the_files for example. Test the program with a directory.

Exercise 5 - Bonus Question

Modify the program in Exercise 3 so that it can use the "-R" to run recursively, as ls. Use getopt() to process the arguments of the command line.