Linux Stat function explanation

Source: Internet
Author: User

The Linux stat function explains:

Table header files: #include <sys/stat.h>
#include <unistd.h>
Define functions: Int stat (const char *file_name, struct stat *buf);

Function Description: Obtains file information by filename, and is stored in the structure stat referred to by buf
Return value: Execution succeeds returns 0, failure returns-1, error code stored in errno
Error code:
ENOENT parameter file_name The specified file does not exist
Directories in the Enotdir path exist but are not real directories
Eloop the file you want to open has too many symbolic connection problems, with a maximum of 16 symbolic connections
Efault parameter BUF is invalid pointer, pointing to memory space that cannot exist
Eaccess denied when accessing file
Enomem Core memory is low
Enametoolong parameter file_name The path 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 number of the file
ino_t St_ino; Node
mode_t St_mode; Type of file and permissions to access
nlink_t St_nlink; The number of hard connections to the file, the file value that was just created 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, 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; Last change time (refers to attribute)
The previously described St_mode defines the following cases:
S_ifmt 0170000 file Type bit mask
S_ifsock 0140000 Scoket
S_iflnk 0120000 Symbolic Connection
S_ifreg 1,000,001-like file
S_IFBLK 0060000 block Unit
S_ifdir 0040000 Catalogue
S_IFCHR 0020000-character device
S_ififo 0010000 Advanced First Out
S_isuid 04000 file (set User-id on execution) bit
S_isgid 02000 file (set Group-id on execution) bit
Sticky bits of s_isvtx 01000 file
S_IRUSR (s_iread) 00400 file owner with readable permissions
S_IWUSR (s_iwrite) 00200 file owner with writable permission
S_IXUSR (s_iexec) 00100 file owner with executable permissions
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_iroth 00004 Other users have read access
S_iwoth 00002 Other users have writable permissions
S_ixoth 00001 other users with executable permissions
The above file types are defined in POSIX to check the macro definitions for these types:
S_islnk (St_mode) determines whether a symbolic connection
S_isreg (St_mode) is a generic file
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 the 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.

The most likely use of the stat function is the ls-l command, which allows you to get all the information about a file.
The 1 function is to get the attributes of the file (normal file, directory, pipe, socket, character, block ().
Function prototypes
#include <sys/stat.h>
int stat (const char *restrict pathname, struct stat *restrict buf);
Provide the file name and get the file corresponding attribute.
int fstat (int filedes, struct stat *buf);
Gets the properties of the file with the file descriptor.
int Lstat (const char *restrict pathname, struct stat *restrict buf);
The connection file describes the life and gets the file attributes.

Copy to Google TranslateTranslation Results



(turn) Linux stat function explanation

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