use St_mode to determine file types in Linux2012-12-11 12:41 14214 People read Comments (4) favorite reports Classification:Linux (8)C + + (+)
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
In Linux, you can use the stat () function to get the state of a file
[CPP]View PlainCopy
- #include <sys/stat.h>
- #include <unistd.h>
- int stat (const char *file_name, struct stat *buf);
This function performs a successful return of 0, and the failure returns-1. The obtained file status is stored in the struct stat structure that the BUF pointer points to, and the struct stat is defined as follows:
[CPP]View PlainCopy
- struct STAT
- {
- dev_t St_dev; /* ID of device containing file-id*/of the devices on which the file resides
- ino_t St_ino; / * Inode number-inode Node number * /
- mode_t St_mode; /* File type and access permissions */
- nlink_t St_nlink; / * Number of hard links-how many connections to this file are linked to (rigid connection) */
- uid_t St_uid; / * User ID of Owner-user id*/
- gid_t St_gid; / * Group ID of Owner-group id*/
- dev_t St_rdev; / * Device ID (if special file)-unit number, for device file * /
- off_t st_size; /* Total size, in bytes-File size, Bytes */
- blksize_t st_blksize; /* BlockSize for filesystem I/O-the size of the system block */
- blkcnt_t st_blocks; /* Number of blocks allocated-file occupies */
- time_t st_atime; /* Time of last access-recent access times */
- time_t st_mtime; /* Time of last modification-most recent modified */
- time_t st_ctime; /* Time of last status change-*/
- };
Where st_mode this variable is used to determine the file type.
St_mode is a feature bit to represent a file type, the feature bit is defined as follows:
[CPP]View PlainCopy
- S_ifmt 0170000 file Type bit mask
- S_ifsock 0140000 Socket
- S_iflnk 0120000 Symbolic Link (symbolic link)
- S_ifreg 1,000,001-like file
- S_IFBLK 0060000 block Unit (block device)
- S_ifdir 0040000 Catalogue
- S_IFCHR 0020000 character unit (character device)
- S_ififo 0010000 FIFO
- S_isuid 0004000 file (set User-id on execution) bit
- S_isgid 0002000 file (set Group-id on execution) bit
- Sticky bits of s_isvtx 0001000 file
- S_irwxu 00700 file Owner's matte value (that is, ownership limit)
- S_irusr 00400 file owner with readable permissions
- S_iwusr 00200 file owner with writable permission
- S_ixusr 00100 file owner with executable permissions
- S_IRWXG 00070 User Group mask value (i.e., ownership limit value)
- S_IRGRP 00040 user groups with readable permissions
- S_IWGRP 00020 user groups with writable permissions
- S_IXGRP 00010 user groups with executable permissions
- S_irwxo 00007 Mask value for other users (i.e., ownership limit)
- S_iroth 00004 Other users have read access
- S_iwoth 00002 Other users have writable permissions
- S_ixoth 00001 other users with executable permissions
- From the Linux C function Library Reference manual
When judging the file type, use the St_mode value of the file to compare with the value given above. Like what:
[CPP]View PlainCopy
- #include <sys/stat.h>
- #include <unistd.h>
- #include <stdio.h>
- int main ()
- {
- int ABC;
- struct stat buf;
- Stat ("/Home", &BUF);
- ABC = buf.st_mode & S_ifdir; //with the corresponding sign phase and
- if (abc = = S_IFDIR) //result vs. flag bit
- printf ("It ' s a directory.\n");
- return 0;
- }
Operation Result:
It ' s a directory.
There is a simple way to do this, and the file type defines the macro definitions that check these types in POSIX:
[CPP]View PlainCopy
- S_islingk (St_mode) to determine if a bit symbolic link
- S_isreg (St_mode) is a generic file
- S_isdir (St_mode) is a directory
- S_ISCHR (St_mode) is a bit character device file
- S_ISBLK (s3e) whether FIFO
- S_issock (St_mode) is the socket
It can be judged based on the return value of these functions, and if so, 1 is returned. (I tried it, as if it were) from: http://blog.csdn.net/simmerlee/article/details/8281399
Use St_mode to determine file types in Linux