File Attributes (ITS8020)

Allikas: Kursused
Redaktsioon seisuga 26. oktoober 2018, kell 08:36 kasutajalt Irve (arutelu | kaastöö) (Uus lehekülg: '''We practice using structures in standard library and read file information'' == Task == Write a program, which takes a filename as an argument and then tries to show the att...')
(erin) ←Vanem redaktsioon | Viimane redaktsiooni (erin) | Uuem redaktsioon→ (erin)
Mine navigeerimisribale Mine otsikasti

We practice using structures in standard library and read file information

Task

Write a program, which takes a filename as an argument and then tries to show the attributes of the file: basically calls the stat() function.

When the file is a directory, the program shows the files in that directory (don't show hidden files). When the user runs the program without extra arguments, just show the contents of the working directory. For symbolic links, show the data about the target, not the link itself.

Only show these attributes:

  • File size
  • Can the owner read?
  • Can the owner write?
  • Can the owner execute?

Hints

  • File existence can be checked with function access().
  • You can get the file attributes using man stat() function.
  • The owner permissions are easier to understand using man chmod() (or man stat in the terminal), which provides you with the list of constants which can be compared using if and & to check for the particurlar permission.
  • File is hidden if it starts with a dot.
  • For the directory you can use opendir() and readdir() functions.

A Philosophical Detour

You may note that the file size attribute has off_t as a type. How to print its contents? Which character should one use with % sign for printf()?

Since off_t can depend on the system, the most rational choice would be to print an unsigned long int and to convert off_t into the corresponding long int using a (cast).