Getting file attributes: stat, fstat, and lstat

Source: Internet
Author: User
Tags lstat

A unix File System is a hierarchical structure composed of directories and files. The file attribute refers to the file type (normal file or directory), file size, file owner, File Permission, And the last modification time of the file. The stat and fstat functions can obtain an information structure containing all the attributes of the file (you can use man fstat to view the help information of these two functions ).

Stat, fstat, lstat-Get File Attribute Information

SYNOPSIS
# Include <sys/types. h>
# Include <sys/stat. h>
# Include <unistd. h>

Int stat (const char * path, struct stat * buf );
Int fstat (int filedes, struct stat * buf );
Int lstat (const char * path, struct stat * buf );

DESCRIPTION

These functions obtain file attributes. You do not need any permissions on the file itself, but stat and lstat must have executable permissions on all directories on the path to the file to obtain the attribute information of the file.

The stat function counts the attribute information of the file directed by the path and stores the attribute information structure in the buf;

The functions of the lstat function are the same as those of the stat function. The difference is that if its file path is a soft connection, the statistics of the soft connection are as follows, instead of the Property Information of the file to which the link points.

The fstat function and the stat function have the same functions. The difference is that the files counted by the function are specified by the file descriptor filedes.

If the above three functions are successful, 0 is returned, and-1 is returned for failure.

The above three functions can obtain a stat struct in the file. The struct is defined as follows:

Struct stat {
Dev_t st_dev;/* ID of device containing file */
Ino_t st_ino;/* inode number */
Mode_t st_mode;/* protection */
Nlink_t st_nlink;/* number of hard links */
Uid_t st_uid;/* user ID of owner */
Gid_t st_gid;/* group ID of owner */
Dev_t st_rdev;/* device ID (if special file )*/
Off_t st_size;/* total size, in bytes */
Blksize_t st_blksize;/* blocksize for filesystem I/O */
Blkcnt_t st_blocks;/* number of blocks allocated */
Time_t st_atime;/* time of last access */
Time_t st_mtime;/* time of last modification */
Time_t st_ctime;/* time of last status change */
};

Use the stat function to obtain the file attributes:

<stdio.h><sys/types.h><sys/stat.h><unistd.h> main(, & 

Running result:

[root@localhost test]# ./

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.