Exercise 3

Inode Information

Look at the manual page for stat(2). It describes three functions, pay attention to their differences. Use the function that outputs the file type of a symbolic link and not the file type that the link points to.

man 2 stat

Now, write a C program that, for any number of files given as command-line parameters (if they exist), outputs the following information:

  • file type
  • user ID and group ID (file owner, group owner) and the name of the user (using getpwuid())
  • access bits in octal format
  • last access time
  • last inode modification time
  • last file modification time
  • file creation time

The output format of the time should correspond to the output of the date command in the shell.

Which file types does your program output for the following files (pipe=FIFO):

File regular dir pipe socket char link
/dev/random
/bin/sh
/usr/bin/tar
/var/spool
/etc/services
/tmp/.X11-unix/X0

Note: You can compare the output of your program with that of the file command.

If the specified file does not exist, an error message should be displayed (use perror() to evaluate errno).

Create a Makefile so that your program can be compiled using the make command.