C Language Stat () function: Getting file status
header file:
#include <sys/stat.h> #include <unistd.h>
To define a function:
int stat (const char * file_name, struct stat *buf);
Function Description: Stat () is used to copy the file state of the parameter file_name to the structure referred to in the parameter buf.
The following is a description of the parameters in the struct stat:
struct stat
{
dev_t st_dev//device documentation ino_t
st_ino;//inode file I-node;
The type of protection file and the access permissions
nlink_t St_nlink,//number of hard links to the file, the number of hard connections to it, the newly established file value is 1.
uid_t St_uid; The user ID of owner of owner file
gid_t st_gid//group ID of owner file
dev_t st_rdev;//device type If this file is an appliance device file, then its device number
off_t st_size//total size, in bytes file size, in bytes
unsigned long st_blksize;//blocksize for Filesyste M I/O file system I/O buffer size.
unsigned long st_blocks; Number of blocks allocated occupies a block of files, each chunk size is 512 bytes.
time_t St_atime; The time of the most recent access or execution of the lastaccess file is generally only changed when using Mknod, Utime, read, write, and tructate.
time_t St_mtime; The time when the last modification file was modified, typically only when the time_t St_ctime was changed with Mknod, Utime, and write
;//time E The last time the change was made, this parameter is updated when the owner, group, and permissions of the file are changed
;
St_mode, as previously described, defines the following several situations:
1, s_ifmt 0170000 file type of bit mask
2, S_ifsock 0140000 scoket
3, S_iflnk 0120000 symbolic connection
4, S_ifreg 1,000,001-like document
5, S_IFBLK 0060000 block device
6, S_ifdir 0040000 directory
7, S_IFCHR 0020000 character device
8, S_ififo 0010000 advanced First Out
9, S_isuid 04000 file (set User-id on execution) bit
10, S_isgid 02000 file (set Group-id on execution) bit
11, s_isvtx 01000 files of the sticky bit
12, S_IRUSR (s_iread) 00400 file owner with Read permission
13, S_IWUSR (s_iwrite) 00200 file owner with writable permission
14, S_IXUSR (s_iexec) 00100 file owner with executable permissions
15, S_irgrp 00040 user group with Read permission
16, S_iwgrp 00020 user group with writable access
17, S_ixgrp 00010 user group with executable permissions
18, S_iroth 00004 other users have Read permission
19, S_iwoth 00002 Other user with writable permission
20, S_ixoth 00001 other users have executable permissions the above file types define the macro definitions that check these types in POSIX
21, S_islnk (St_mode) to determine whether the symbolic connection
22, S_isreg (St_mode) is a general document
23, S_isdir (St_mode) is a directory
24, S_ISCHR (St_mode) is a character device file
25, S_ISBLK (S3E) is advanced first out
26, S_issock (St_mode) is a socket if a directory has a sticky bit (S_ISVTX), the file in this directory can only be deleted or renamed by the owner of the file, this directory owner, or root.
Return value: Success 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, the directory in the Enotdir path exists but is not a real directory
3, Eloop to open the file has too many symbolic connection problems, the upper limit of 16 symbolic connection
4, the Efault parameter buf is invalid pointer, points to the memory space that cannot exist
5, eaccess access to the file is rejected
6, Enomem core memory is not enough
7, the Enametoolong parameter file_name path name is too long
Example
#include <sys/stat.h>
#include <unistd.h>
main ()
{
struct stat buf;
Stat ("/etc/passwd", &buf);
printf ("/etc/passwd file size =%d \ n", buf.st_size);
}
Perform:
/etc/passwd File size = 705
C language Fstat () function: Get file status from file descriptor
header file:
#include <sys/stat.h> #include <unistd.h>
To define a function:
int fstat (int fildes, struct stat *buf);
Function Description: Fstat () is used to copy the file state of the parameter fildes to the structure referred to in the parameter buf (struct stat). The Fstat () function is exactly the same as stat (), except that the passed parameter is an open file descriptor. For more information, please refer to stat ().
Return value: Success returns 0, failure returns-1, error code is stored in errno.
Example
#include <sys/stat.h>
#include <unistd.h>
#include <fcntk.h>
main ()
{
struct stat buf;
int FD;
FD = open ("/etc/passwd", o_rdonly);
Fstat (FD, &buf);
printf ("/etc/passwd File Size +%d\n", buf.st_size);
}
Perform:
/etc/passwd File size = 705
C language Lstat () function: Get file status from file descriptor
header file:
#include <sys/stat.h> #include <unistd.h>
To define a function:
int Lstat (const char * file_name, struct stat * buf);
Function Description: Lstat () is exactly the same as stat (), which is to obtain the state of the file that the parameter file_name refers to, the difference is that when the file is a symbolic connection, Lstat () returns the state of the link itself. For more information, please refer to stat ().
Return value: Success returns 0, failure returns-1, error code is stored in errno.
Example: Reference stat ().