Stat function Explanation
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><unistd.h><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.
-----------------------------------------------------
struct STATFS {
Long F_type; File system type
Long f_bsize; Block size
Long f_blocks; How many blocks?
Long F_bfree; Free Blocks
Long F_bavail; Available blocks
Long F_files; Total File node
Long F_ffree; Free File node
fsid_t F_fsid; File System ID
Long F_namelen; Maximum length of file name
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 and get the file corresponding attribute. The feeling is generally that the file does not open when this operation.
int fstat (int filedes, struct stat *buf);
Gets the properties of the file with the file descriptor. This action when the file is opened
int Lstat (const char *restrict pathname, struct stat *restrict buf);
Connection file
Three function returns: 0 if successful, or 1 if an error occurs
Given a pathname,stat function that returns a structure of information related to this named file, the Fstat function obtains information about the file that has been opened on the descriptor filedes. The Lstat function is similar to stat, but when a named file is a symbolic connection, LSTAT returns information about the symbolic connection, not the file referenced by the symbolic connection.
The second parameter is a pointer to a structure that we should provide. These functions fill in the structure pointed to by BUF. The actual definition of the structure may vary depending on the 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 for the last two, each member is a basic system data type. We will explain each member of this structure to understand the file properties.
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.
2 Properties for the file
struct STAT {
mode_t St_mode; File corresponding to the mode, file, directory, etc.
ino_t St_ino; Inode Node Number
dev_t St_dev; Device number
dev_t St_rdev; Special Equipment Number
nlink_t St_nlink; Number of connections to the file
uid_t St_uid; File owner
gid_t St_gid; The group that the file owner corresponds to
off_t st_size; Normal file, corresponding file byte number
time_t St_atime; Time the file was last visited
time_t St_mtime; time when the contents of the file were last modified
time_t St_ctime; File status Change time
blksize_t st_blksize; Block size for file contents
blkcnt_t st_blocks; The number of blocks corresponding to the Wei Jian content
};
You can return a struct by using the function provided above, which holds the information of the file.
"Go" Linux C + + get file information A detailed stat function