Use St_mode to determine file types in Linux

Source: Internet
Author: User

 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
    1. #include <sys/stat.h>
    2. #include <unistd.h>
    3. 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
  1. struct STAT
  2. {
  3. dev_t St_dev; /* ID of device containing file-id*/of the devices on which the file resides
  4. ino_t St_ino; / * Inode number-inode Node number * /
  5. mode_t St_mode; /* File type and access permissions */
  6. nlink_t St_nlink; / * Number of hard links-how many connections to this file are linked to (rigid connection) */
  7. uid_t St_uid; / * User ID of Owner-user id*/
  8. gid_t St_gid; / * Group ID of Owner-group id*/
  9. dev_t St_rdev; / * Device ID (if special file)-unit number, for device file * /
  10. off_t st_size; /* Total size, in bytes-File size, Bytes */
  11. blksize_t st_blksize; /* BlockSize for filesystem I/O-the size of the system block */
  12. blkcnt_t st_blocks; /* Number of blocks allocated-file occupies */
  13. time_t st_atime; /* Time of last access-recent access times */
  14. time_t st_mtime; /* Time of last modification-most recent modified */
  15. time_t st_ctime; /* Time of last status change-*/
  16. };

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
  1. S_ifmt 0170000 file Type bit mask
  2. S_ifsock 0140000 Socket
  3. S_iflnk 0120000 Symbolic Link (symbolic link)
  4. S_ifreg 1,000,001-like file
  5. S_IFBLK 0060000 block Unit (block device)
  6. S_ifdir 0040000 Catalogue
  7. S_IFCHR 0020000 character unit (character device)
  8. S_ififo 0010000 FIFO
  9. S_isuid 0004000 file (set User-id on execution) bit
  10. S_isgid 0002000 file (set Group-id on execution) bit
  11. Sticky bits of s_isvtx 0001000 file
  12. S_irwxu 00700 file Owner's matte value (that is, ownership limit)
  13. S_irusr 00400 file owner with readable permissions
  14. S_iwusr 00200 file owner with writable permission
  15. S_ixusr 00100 file owner with executable permissions
  16. S_IRWXG 00070 User Group mask value (i.e., ownership limit value)
  17. S_IRGRP 00040 user groups with readable permissions
  18. S_IWGRP 00020 user groups with writable permissions
  19. S_IXGRP 00010 user groups with executable permissions
  20. S_irwxo 00007 Mask value for other users (i.e., ownership limit)
  21. S_iroth 00004 Other users have read access
  22. S_iwoth 00002 Other users have writable permissions
  23. S_ixoth 00001 other users with executable permissions
  24. 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
  1. #include <sys/stat.h>
  2. #include <unistd.h>
  3. #include <stdio.h>
  4. int main ()
  5. {
  6. int ABC;
  7. struct stat buf;
  8. Stat ("/Home", &BUF);
  9. ABC = buf.st_mode & S_ifdir; //with the corresponding sign phase and
  10. if (abc = = S_IFDIR) //result vs. flag bit
  11. printf ("It ' s a directory.\n");
  12. return 0;
  13. }
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
    1. S_islingk (St_mode) to determine if a bit symbolic link
    2. S_isreg (St_mode) is a generic file
    3. S_isdir (St_mode) is a directory
    4. S_ISCHR (St_mode) is a bit character device file
    5. S_ISBLK (s3e) whether FIFO
    6. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.