C language to obtain the file state of the relevant functions

  • 2020-04-02 03:21:52
  • OfStack

The C language stat() function: gets the file status
The header file:


#include <sys/stat.h>  #include <unistd.h>

Definition function:


int stat(const char * file_name, struct stat *buf);

Stat () is used to copy the file state referred to by the parameter file_name into the structure referred to by the parameter buf.

The following is a description of each parameter in struct stat:


struct stat
{
  dev_t st_dev; //Device number for the device file
  ino_t st_ino; //The i-node of the inode file
  mode_t st_mode; //Type of protection file and access rights
  nlink_t st_nlink; //Number of hard links to this file. The value of the newly created file is 1.
  uid_t st_uid; //The user ID of the owner of the file
  gid_t st_gid; //The group ID of the owner of the file
  dev_t st_rdev; //If this file is a device device file, it is the device number
  off_t st_size; //Total size, in bytes
  unsigned long st_blksize; //Blocksize for filesystem I/O buffer size.
  unsigned long st_blocks; //Number of blocks allocated to take up the number of file blocks, each block size of 512 bytes.
  time_t st_atime; //The last time a time of lastaccess file was accessed or executed is generally changed only with mknod, utime, read, write, and tructate.
  time_t st_mtime; //The time of the last modification file is generally changed only when mknod, utime, and write are used
  time_t st_ctime; //Time of last change i-node last changed, this parameter is updated when the file owner, group, and permissions are changed
};

The st_mode described earlier defines the following situations:
1. Bit mask for S_IFMT 0170000 file type
2. S_IFSOCK 0140,000 ket
3. S_IFLNK 0120000 symbolic connection
4. S_IFREG 0100000 general file
5. S_IFBLK 0060000 block device
6. S_IFDIR 0040000
7. S_IFCHR 0020000 character device
8. S_IFIFO 0010000 first in, first out
9. The (set user-id on execution) bit of S_ISUID 04000 file
10. The (set group-id on execution) bit of the S_ISGID 02000 file
11. Sticky bit of S_ISVTX 01000 file
12. S_IRUSR (S_IREAD) 00400 file owner has readable rights
13. S_IWUSR (S_IWRITE)00200 file owner has writable rights
14. S_IXUSR (S_IEXEC) 00100 file owner has executable rights
15. S_IRGRP 00040 user group has readable permissions
16. S_IWGRP 00020 user group has writable permissions
17. S_IXGRP 00010 user group has executable rights
18. S_IROTH 00004 other users have readable permissions
19. S_IWOTH 00002 other users have writable permissions
20, S_IXOTH 00001 other users with executable permissions the above file types have macro definitions defined in POSIX to check for these types
21. S_ISLNK (st_mode) determines whether it is a symbolic connection
22. Is S_ISREG (st_mode) a general file
23. Is S_ISDIR (st_mode) a directory
24. Whether S_ISCHR (st_mode) is a character device file
25. Whether S_ISBLK (s3e) is fifo or not
If a directory has sticky bits (S_ISVTX), files in that directory can only be deleted or renamed by the file owner, the directory owner, or root.

Return value: 0 on success, -1 on failure, error code in errno.

Error code:
1. The file specified by ENOENT parameter file_name 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 symbol connection problem, the upper limit is 16 symbol connection
4. The EFAULT parameter buf is an invalid pointer to the memory space that cannot exist
EACCESS is denied access to files
6. ENOMEM is out of core memory
7. The path name of the ENAMETOOLONG parameter file_name is too long

sample


#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 fstat() function: gets the file status from the file descriptor
The header file:


 #include <sys/stat.h>  #include <unistd.h>

Definition function:


int fstat(int fildes, struct stat *buf);

Fstat() is used to copy the file state referred to by the parameter fildes into the structure referred to by the parameter buf (struct stat).

Return value: 0 on success, -1 on failure, error code in errno.

sample


#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 +%dn ", buf.st_size);
}

Perform:


/etc/passwd file size = 705

C language lstat() function: gets the file status from the file descriptor
The header file:


#include <sys/stat.h>  #include <unistd.h>

Definition function:


int lstat (const char * file_name, struct stat * buf);

Function description: lstat() does exactly the same thing as stat() to get the file state referred to by the parameter file_name, except that when the file is a symbolic connection, lstat() returns the status of the link itself. See stat() for details.

Return value: 0 on success, -1 on failure, error code in errno.

Example: refer to stat().


Related articles: