Linux stat Functions

Source: Internet
Author: User
Tags lstat

Stat Function Description

Header file: # include <sys/STAT. h>
# Include <unistd. h>
Define the function: int Stat (const char * file_name, struct stat * BUF );
Function Description: get the file information through the filename file name and store it in the struct stat referred to by the Buf.
Returned value: if the execution is successful, 0 is returned. If the execution fails,-1 is returned. The error code is stored in errno.

Error code:
The file specified by the enoent parameter file_name does not exist.
The directory in the enotdir path exists but is not a real directory
The file to be opened by eloop has too many symbolic connections. The upper limit is 16.
The efault parameter Buf is an invalid pointer and points to the memory space that cannot exist.
The eaccess is denied when accessing the file.
Insufficient enomem core memory
The path name of the enametoolong parameter file_name is too long.

# Include <sys/stat. h>
# Include <unistd. h>
# Include <stdio. h>

Int main (){
Struct stat buf;
Stat ("/etc/hosts", & buf );
Printf ("/etc/hosts file size = % d \ n", buf. st_size );

Struct stat {
Dev_t st_dev; // device ID of the file
Ino_t st_ino; // Node
Mode_t st_mode; // file type and access permission
Nlink_t st_nlink; // number of hard connections to the file. The value of the created file is 1.
Uid_t st_uid; // user ID
Gid_t st_gid; // group ID
Dev_t st_rdev; // (device type) if this file is a device file, it is its device number
Off_t st_size; // number of file bytes (file size)
Unsigned long st_blksize; // block size (File System I/O buffer size)
Unsigned long st_blocks; // number of blocks
Time_t st_atime; // last access time
Time_t st_mtime; // last modification time
Time_t st_ctime; // the last change time (attribute)

The st_mode described earlier defines the following situations:
S_IFMT 0170000 bit masks for file types
S_ifsock0140000 scoket
S_IFLNK 0120000 symbolic connection
S_IFREG 0100000 general file
S_IFBLK 0060000 block Device
S_IFDIR 0040000 directory
S_IFCHR 0020000 character device
S_IFIFO 0010000 first-in-first-out

S_ISUID 04000 file (set user-id on execution) Bit
S_ISGID 02000 (set group-id on execution) bit of the file
S_ISVTX 01000 file sticky Bit

S_IRUSR (S_IREAD) 00400 the file owner has the read permission
S_IWUSR (S_IWRITE) 00200 the file owner has the write permission
S_IXUSR (S_IEXEC) 00100 the file owner has executable permissions

S_IRGRP 00040 user group with read permission
S_IWGRP 00020 user group with write permission
S_IXGRP 00010 user group with executable permissions

S_IROTH 00004 other users have read permission
S_IWOTH 00002 other users have write permission
S_IXOTH 00001 other users have executable permissions

The above file type defines in POSIX to check the macro definitions of these types:
S_ISLNK (st_mode) determines whether it is a symbolic connection
S_ISREG (st_mode) is a normal file
S_ISDIR (st_mode) is a directory
S_ISCHR (st_mode) is a character Device File
S_ISBLK (s3e) for FIFO
S_ISSOCK (st_mode) is socket

If a directory has a sticky bit (s_isvtx), the files under this directory can only be deleted or renamed by the file owner, the directory owner, or root.

Struct statfs {
Long f_type; // file system type
Long f_bsize; // block size
Long f_blocks; // number of blocks
Long f_bfree; // idle Block
Long f_bavail; // available Block
Long f_files; // total file nodes
Long f_ffree; // idle file Node
Fsid_t f_fsid; // File System ID
Long f_namelen; // The maximum Object Name Length.
Long f_spare [6]; // spare for later

Stat, fstat, and lstat functions (UNIX)

# Include <sys/types. h>
# Include <sys/stat. h>
Int stat (const char * restrict pathname, struct stat * restrict buf );
Provide the file name to obtain the corresponding attributes of the file. This operation is generally performed when the file is not opened.
Int fstat (int filedes, struct stat * buf );
Obtain the attributes of a file using the file descriptor. This operation is performed after the file is opened.
Int lstat (const char * restrict pathname, struct stat * restrict buf );
Connection File

Returns the result of the three functions: 0 if the operation succeeds, and-1 if an error occurs.
Given a pathname, the stat function returns an information structure related to the named file. The fstat function obtains information about the file opened on the descriptor filedes. The lstat function is similar to stat, but when the named file is a symbolic connection, lstat returns information about the symbolic connection, rather than the information of the file referenced by the symbolic connection.

The second parameter is a pointer pointing to a structure we should provide. These functions are in the structure directed by the buf. The actual definition of this structure may vary with implementation, but its basic form is:

Struct stat {
Mode_t st_mode;/* file tpye & mode (permissions )*/
Ino_t st_ino;/* I = node number (serial number )*/
Dev_t st_rdev;/* device number for special files */
Nlink_t st_nlink;/* number of links */
Uid_t st_uid;/* user id of owner */
Gid_t st_gid;/* group ID of owner */
Off_t st_size;/* size in bytes for regular files */
Time_t st_atime;/* time of last access */
Time_t st_mtime;/* time of last modification */
Time_t st_ctime;/* time of last file status change */
Long st_blksize;/* best I/O block size */
Long st_blocks;/* number of 512-byte blocks allocated */
Note that, except the last two, all Members are of the basic system data type. We will describe each member of this structure to understand the file attributes.

The most commonly used stat function is the ls-l command, which can be used to obtain all information about a file.
The 1 function is used to obtain attributes of files (common files, directories, pipelines, sockets, characters, blocks.
Function prototype
# Include <sys/stat. h>

Int stat (const char * restrict pathname, struct stat * restrict buf );
Provide the file name to obtain the corresponding attributes of the file.
Int fstat (int filedes, struct stat * buf );
Obtain the attributes of a file using the file descriptor.
Int lstat (const char * restrict pathname, struct stat * restrict buf );
The description of the connection file to obtain the file attributes.
2. File Attributes
Struct stat {
Mode_t st_mode; // file mode, file, directory, etc.
Ino_t st_ino; // inode node number
Dev_t st_dev; // device number
Dev_t st_rdev; // special device number
Nlink_t st_nlink; // number of file connections
Uid_t st_uid; // file owner
Gid_t st_gid; // group corresponding to the file owner
Off_t st_size; // common file, corresponding to the number of bytes
Time_t st_atime; // time when the object was last accessed
Time_t st_mtime; // The last modification time of the file content
Time_t st_ctime; // File status change time
Blksize_t st_blksize; // block size corresponding to the file content
Blkcnt_t st_blocks; // Number of chunks corresponding to Wei Jian content

You can use the functions provided above to return a struct that stores the file information.

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: 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.