Preface (including table of contents)
stat and lstat
Use of stat command:
/**
* @author IYATT-yx
* @brief 获取文件部分属性
*/
#define _GNU_SOURCE
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
int main(int argc, char **argv)
{
if (argc != 2)
{
printf("请指定一个参数为要查询大小的文件!\n");
return -1;
}
struct stat st;
int ret = stat(argv[1], &st);
if (ret == -1)
{
perror("stat");
return -1;
}
printf("%s的大小为%ld\n", argv[1], st.st_size);
// 文件类型, 1法
if (S_ISREG(st.st_mode))
{
printf("1@这是一个普通文件\n");
}
else if (S_ISDIR(st.st_mode))
{
printf("1@这是一个目录文件\n");
}
else if (S_ISLNK(st.st_mode))
{
printf("1@这是一个符号链接文件\n");
}
else if (S_ISSOCK(st.st_mode))
{
printf("1@这是一个套接字文件\n");
}
else if (S_ISBLK(st.st_mode))
{
printf("1@这是一个块设备文件\n");
}
else if (S_ISCHR(st.st_mode))
{
printf("1@这是一个字符设备文件\n");
}
else if (S_ISFIFO(st.st_mode))
{
printf("1@这是一个管道文件\n");
}
// 文件类型, 2法
int type = st.st_mode & S_IFMT;
switch (type)
{
case S_IFREG:
printf("2@这是一个普通文件\n");
break;
case S_IFDIR:
printf("2@这是一个目录文件\n");
break;
case S_IFLNK:
printf("2@这是一个符号链接文件\n");
break;
case S_IFSOCK:
printf("2@这是一个套接字文件\n");
break;
case S_IFBLK:
printf("2@这是一个块设备文件\n");
break;
case S_IFCHR:
printf("这是一个字符设备文件\n");
break;
case S_IFIFO:
printf("这是一个管道文件\n");
break;
default:
break;
}
}
The above example with the stat
function, there are similar lstat
and the fstat
main difference is in the use of stat and lstat soft link file when the query is a soft link file, stat will penetrate the query to file a soft link points, while lstat can check soft The attributes of the linked file itself. The fstat is mainly the parameter difference. Both stat and lstat are queried by file name, while fstat is queried by the return value file descriptor of the open file. Specific information can be man
queried by commands. It should also be easy notes that, stat family of functions can check file attributes and ls -l
content displayed almost the same.
stat structure
struct stat
{
// 文件的设备编号
dev_t st_dev;
// 节点
ino_t st_ino;
// 文件的类型和存取的权限
mode_t st_mode;
// 链到该文件的硬链接数目
nlink_t st_nlink;
// 用户ID
uid_t st_uid;
// 组ID
gid_t st_gid;
// (设备类型)若此文件为设备文件,则为其设备编号
dev_t st_rdev;
// 文件字节数
off_t st_size;
// 块大小 (文件系统的I/O缓冲区大小)
blksize_t st_blksize;
// 块数
blkcnt_t st_blocks;
// 最后一次访问时间
time_t st_atime;
// 最后一次内容修改时间
time_t st_mtime;
// 最后一次属性修改时间
time_t st_ctime;
};
st_mode (16-bit integer)
- 0~2 persons o Other users
S_IROTH | 00004 | read |
S_IWOTH | 00002 | write |
S_IXOTH | 00001 | carried out |
S_IRWXO | 00007 | Mask, filter non-o permissions |
* 3~5 digits belong to the user group
S_IRGRP | 00040 | read |
S_IWGRP | 00020 | write |
S_IXGRP | 00010 | carried out |
S_IRWXG | 00070 | Mask, filter non-g permissions |
* 6~8 u file owners
S_IRUSR | 00400 | read |
S_IWUSR | 00200 | write |
S_IXUSR | 00100 | carried out |
S_IRWXU | 00700 | Mask, filter non-u permissions |
* 12~15 bit file type
S_IFSOCK | 0140000 | Socket |
S_IFLNK | 0120000 | Symbolic link (soft link) |
S_IFREG | 0100000 | Normal file |
S_IFBLK | 0060000 | Block device |
S_IFDIR | 0040000 | table of Contents |
S_IFCHR | 0020000 | Character device |
S_IFIFO | 0010000 | pipeline |
S_IFMT | 0170000 | Flood code, filter non-file type information |
/**
* @author IYATT-yx
* @brief 检测文件属性: 存在,读,写,操作
*/
#include <stdio.h>
#include <unistd.h>
int main(int argc, char **argv)
{
if (argc != 2)
{
printf("请指定一个参数为要查询的文件!\n");
return -1;
}
if (access(argv[1], F_OK) == -1)
{
perror("access");
return -1;
}
else
{
if (access(argv[1], R_OK) == 0)
{
printf("R");
}
if (access(argv[1], W_OK) == 0)
{
printf("W");
}
if (access(argv[1], X_OK) == 0)
{
printf("X");
}
printf("\n");
}
}
Modify file permissions: chmod
function
Modify file owner or group: chown
function
Modify file size: truncate
function
Change attributes of opened files: fcntl
function