C-language stat () function: Get file status

Source: Internet
Author: User

Related functions: Fstat, Lstat, chmod, Chown, Readlink, Utime

Header files: #include <sys/stat.h> #include <uninstd.h>

Define functions: Int stat (const char * file_name, struct stat *buf);

Function Description: Stat () is used to copy the file state referred to in parameter file_name to the structure referred to in parameter buf.

The following is a description of the parameters within the struct stat:

1 structStat {2dev_t St_dev;//device number of a device file3ino_t St_ino;//I-node of the Inode file4mode_t St_mode;//types of protection files and permissions to access them5nlink_t St_nlink;//number of hard links connects to the file with a file value of 1 that has just been created.6uid_t St_uid;//user ID of the owner of the owning file7gid_t St_gid;//group ID of the owner of the file that is the identity of the8dev_t St_rdev;//Device Type If this file is an appliance device file, it will be the unit number9off_t st_size;//Total size, in bytes file size, in bytesTenUnsignedLongSt_blksize;//blocksize the I/O buffer size for the filesystem I/O file system. OneU nsignedLongSt_blocks;//number of blocks allocated occupies a chunk of files, each chunk size is 512 bytes. Atime_t St_atime;//time of lastaccess file was last accessed or executed, and generally only changed with Mknod, Utime, read, write, and tructate. -time_t St_mtime;//time of the last modification file was modified at the end of the day, typically only when used with Mknod, Utime, and write -time_t St_ctime;//time of last changes i-node the most recent change, this parameter is updated when the file owner, group, and permissions are changed the};

The previously described St_mode defines the following cases:

1 1, s_ifmt0170000bit masks for file types2 2, S_ifsock0140000Scoket3 3, S_iflnk0120000Symbolic Connection4 4, S_ifreg0100000General Documents5 5, s_ifblk0060000Block Device6 6, S_ifdir0040000Catalogue7 7, S_IFCHR0020000character Device8 8, S_ififo0010000Advanced First Out9 9, S_isuid04000of the file (Setuser-ID on execution) bitTen Ten, S_isgid02000of the file (Setgroup-ID on execution) bit One  One, S_ISVTX01000sticky bits of the file A  A, S_irusr (S_iread)00400file owners have Read permissions -  -, S_iwusr (S_iwrite)00200file owner with writable permission -  -, S_ixusr (s_iexec)00100file owner has executable permissions the  the, S_IRGRP00040user groups with readable permissions -  -, S_IWGRP00020user groups have writable permissions -  -, S_IXGRP00010user groups with executable permissions -  -, S_iroth00004other users have Read permissions +  +, S_iwoth00002other users have writable permissions -  -, S_ixoth00001other users with executable permissions The above file types are defined in POSIX to check these types of macro definitions +  +, S_islnk (st_mode) to determine if it is a symbolic connection A  A, S_isreg (St_mode) is a generic file at  at, S_isdir (St_mode) is a directory -  -, S_ISCHR (St_mode) is a character device file -  -, S_isblk (s3e) is FIFO -  -, S_issock (St_mode) is a socket if a directory has a sticky bit (s_isvtx), then the file in this directory can only be deleted or renamed by the file owner, this directory owner, or root.

Return value: Execution succeeds returns 0, failure returns-1, error code is stored in errno.

Error code:

1 , ENOENT parameter file_name the specified file does not  exist 2  3 4 5)67  
Example:

Main () {4 struct stat buf;5 stat ("/etc/passwd", &buf); 6 printf ("/etc/passwd file size =%d \ n", Buf.st_size);
7}

Execution:/etc/passwd File size = 705

Reprint: Http://wenku.baidu.com/link?url=FUroWFwmMxc3teZpYMTIxf3gOu-FsYe_NIER218D7XZOA5VgiLbSsvfVjrFtphpkM_ Tqtovjort2aqeabmgkhg-lhobbjiqjkao3ohkydqq

C-language stat () function: Get file status

Related Article

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.