Go to the previous, next section.

Reading the Attributes of a File

To examine the attributes of files, use the functions stat, fstat and lstat. They return the attribute information in a struct stat object. All three functions are declared in the header file `sys/stat.h'.

Function: int stat (const char *filename, struct stat *buf)

The stat function returns information about the attributes of the file named by filename in the structure pointed at by buf.

If filename is the name of a symbolic link, the attributes you get describe the file that the link points to. If the link points to a nonexistent file name, then stat fails, reporting a nonexistent file.

The return value is 0 if the operation is successful, and -1 on failure. In addition to the usual file name syntax errors (see section File Name Errors, the following errno error conditions are defined for this function:

ENOENT
The file named by filename doesn't exist.

Function: int fstat (int filedes, struct stat *buf)

The fstat function is like stat, except that it takes an open file descriptor as an argument instead of a file name. See section Low-Level Input/Output.

Like stat, fstat returns 0 on success and -1 on failure. The following errno error conditions are defined for fstat:

EBADF
The filedes argument is not a valid file descriptor.

Function: int lstat (const char *filename, struct stat *buf)

The lstat function is like stat, except that it does not follow symbolic links. If filename is the name of a symbolic link, lstat returns information about the link itself; otherwise, lstat works like stat. See section Symbolic Links.

Go to the previous, next section.