Go to the previous, next section.
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:
This macro returns nonzero if the file is a directory.
This macro returns nonzero if the file is a character special file (a device like a terminal).
This macro returns nonzero if the file is a block special file (a device like a disk).
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.
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)
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
S_IFCHR
S_IFBLK
S_IFREG
S_IFLNK
S_IFSOCK
S_IFIFO
Go to the previous, next section.