Go to the first, previous, next, last section, table of contents.


Testing the Type of a File

The file mode, stored in the st_mode field of the file attributes, contains two kinds of information: the file type code, and the access permission bits. This section discusses only the type code, which you can use to tell whether the file is a directory, whether it is a socket, and so on. For information about the access permission, section The Mode Bits for Access Permission.

There are two predefined ways you can access the file type portion of the file mode. First of all, for each type of file, there is a predicate macro which examines a file mode value and returns true or false--is the file of that type, or not. Secondly, you can mask out the rest of the file mode to get just a file type code. You can compare this against various constants for the supported file types.

All of the symbols listed in this section are defined in the header file `sys/stat.h'.

The following predicate macros test the type of a file, given the value m which is the st_mode field returned by stat on that file:

Macro: int S_ISDIR (mode_t m)
This macro returns nonzero if the file is a directory.

Macro: int S_ISCHR (mode_t m)
This macro returns nonzero if the file is a character special file (a device like a terminal).

Macro: int S_ISBLK (mode_t m)
This macro returns nonzero if the file is a block special file (a device like a disk).

Macro: int S_ISREG (mode_t m)
This macro returns nonzero if the file is a regular file.

Macro: int S_ISFIFO (mode_t m)
This macro returns nonzero if the file is a FIFO special file, or a pipe. See section Pipes and FIFOs.

Macro: int S_ISLNK (mode_t m)
This macro returns nonzero if the file is a symbolic link. See section Symbolic Links.

Macro: int S_ISSOCK (mode_t m)
This macro returns nonzero if the file is a socket. See section Sockets.

An alterate non-POSIX method of testing the file type is supported for compatibility with BSD. The mode can be bitwise ANDed with S_IFMT to extract the file type code, and compared to the appropriate type code constant. For example,

S_ISCHR (mode)

is equivalent to:

((mode & S_IFMT) == S_IFCHR)

Macro: int S_IFMT
This is a bit mask used to extract the file type code portion of a mode value.

These are the symbolic names for the different file type codes:

S_IFDIR
This macro represents the value of the file type code for a directory file.
S_IFCHR
This macro represents the value of the file type code for a character-oriented device file.
S_IFBLK
This macro represents the value of the file type code for a block-oriented device file.
S_IFREG
This macro represents the value of the file type code for a regular file.
S_IFLNK
This macro represents the value of the file type code for a symbolic link.
S_IFSOCK
This macro represents the value of the file type code for a socket.
S_IFIFO
This macro represents the value of the file type code for a FIFO or pipe.


Go to the first, previous, next, last section, table of contents.