| The GNU C Library | www.imodulo.com · 2003-04-05 | ||
| [ Software | Documentation | Contact ] |
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, socket, symbolic link, and so on. For details about access permissions see Permission Bits.
There are two ways you can access the file type information in a file mode. Firstly, for each file type there is a predicate macro which examines a given file mode and returns whether it is of that type or not. Secondly, you can mask out the rest of the file mode to leave just the file type code, and compare this against constants for each of 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 non-zero if the file is a directory.
This macro returns non-zero if the file is a character special file (a device like a terminal).
This macro returns non-zero if the file is a block special file (a device like a disk).
This macro returns non-zero if the file is a regular file.
This macro returns non-zero if the file is a FIFO special file, or a pipe. Pipes and FIFOs.
This macro returns non-zero if the file is a symbolic link. Symbolic Links.
This macro returns non-zero if the file is a socket. Sockets.
An alternate non-POSIX method of testing the file type is supported for compatibility with BSD. The mode can be bitwise AND-ed with S_IFMT to extract the file type code, and compared to the appropriate 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 from a mode value.
These are the symbolic names for the different file type codes:
S_IFDIRThis is the file type constant of a directory file.
S_IFCHRThis is the file type constant of a character-oriented device file.
S_IFBLKThis is the file type constant of a block-oriented device file.
S_IFREGThis is the file type constant of a regular file.
S_IFLNKThis is the file type constant of a symbolic link.
S_IFSOCKThis is the file type constant of a socket.
S_IFIFOThis is the file type constant of a FIFO or pipe.
The POSIX.1b standard introduced a few more objects which possibly can be implemented as object in the filesystem. These are message queues, semaphores, and shared memory objects. To allow differentiating these objects from other files the POSIX standard introduces three new test macros. But unlike the other macros it does not take the value of the st_mode field as the parameter. Instead they expect a pointer to the whole struct stat structure.
If the system implement POSIX message queues as distinct objects and the file is a message queue object, this macro returns a non-zero value. In all other cases the result is zero.
If the system implement POSIX semaphores as distinct objects and the file is a semaphore object, this macro returns a non-zero value. In all other cases the result is zero.
If the system implement POSIX shared memory objects as distinct objects and the file is an shared memory object, this macro returns a non-zero value. In all other cases the result is zero.
| © Free Software Foundation, Inc. |