stat函数
穿透(追踪)函数 – 软链接
文件:english.txt
大小:109055 块:216 IO 块:4096 普通文件
设备:805h/2053d Inode:19401874(fd信息) 硬链接:1(没有额外的硬链接)
权限:(0664/-rw-rw-r--) Uid:( 1000/ hinmer) Gid:( 1000/ hinmer)
最近访问:2022-05-01 14:45:31.690375458 +0800
最近更改:2022-05-01 13:52:09.696576007 +0800
最近改动:2022-05-01 13:52:09.712575975 +0800
创建时间:2022-05-01 13:52:09.612576178 +0800
NAME
stat, fstat, lstat, fstatat - get file status
头文件
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
函数解释
路径 传出参数(申请的缓冲区)
int stat(const char *pathname, struct stat *statbuf);
int fstat(int fd, struct stat *statbuf);
int lstat(const char *pathname, struct stat *statbuf);
文件描述符stat(inode)
struct stat {
dev_t st_dev; /* 文件设备编号 /
ino_t st_ino; / 节点编号 /
mode_t st_mode; / 文件读取权限 /
nlink_t st_nlink; / 硬链接数目 /
uid_t st_uid; / 用户ID /
gid_t st_gid; / 组ID /
dev_t st_rdev; / 如果是设备,设备编号*/
off_t st_size; /* 文件大小 /
blksize_t st_blksize; / 文件块大小*/
blkcnt_t st_blocks; /* 所占块数*/
/* Since Linux 2.6, the kernel supports nanosecond
precision for the following timestamp fields.
For the details before Linux 2.6, see NOTE S. /
struct timespec st_atim; / 最后访问时间 /
struct timespec st_mtim; / 最后修改时间 /
struct timespec st_ctim; / 最后属性改变时间 */
#define st_atime st_atim.tv_sec /* Backward compatibility */
#define st_mtime st_mtim.tv_sec
#define st_ctime st_ctim.tv_sec
};
st_mode(权限划分)
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-P31mklMy-1651503817616)(/media/202205/3_st_mode_1651503626.png)]
S_IRWXU 00700 所有者拥有读、写和执行权限
S_IRUSR 00400 拥有者有读权限
S_IWUSR 00200 拥有者有写权限
S_IXUSR 00100 拥有者有执行权限
S_IRWXG 00070 组有读、写、执行权限
S_IRGRP 00040 组有读权限
S_IWGRP 00020 组有写权限
S_IXGRP 00010 组有执行权限
S_IRWXO 00007 其他人(不在组中)具有读、写和执行权限
使命
S_IROTH 00004 其他人有读权限
S_IWOTH 00002 其他人有写权限
S_IXOTH 00001 其他人有执行权限
lstat函数
不穿透(追踪),只能显示软连接的属性
其余不穿透的函数或操作如 rm ls -l
用stat函数编程实现ll的功能
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <time.h>
#include <pwd.h>
#include <grp.h>
int main(int argc, char* argv[])
{
if(argc < 2)
{
printf("./a.out filename\n");
exit(1);
}
struct stat st;
int ret = stat(argv[1], &st);
if(ret == -1)
{
perror("stat");
exit(1);
}
// 存储文件类型和访问权限
char perms[11] = {0};
// 判断文件类型
switch(st.st_mode & S_IFMT)
{
case S_IFLNK:
perms[0] = 'l';
break;
case S_IFDIR:
perms[0] = 'd';
break;
case S_IFREG:
perms[0] = '-';
break;
case S_IFBLK:
perms[0] = 'b';
break;
case S_IFCHR:
perms[0] = 'c';
break;
case S_IFSOCK:
perms[0] = 's';
break;
case S_IFIFO:
perms[0] = 'p';
break;
default:
perms[0] = '?';
break;
}
// 判断文件的访问权限
// 文件所有者
perms[1] = (st.st_mode & S_IRUSR) ? 'r' : '-';
perms[2] = (st.st_mode & S_IWUSR) ? 'w' : '-';
perms[3] = (st.st_mode & S_IXUSR) ? 'x' : '-';
// 文件所属组
perms[4] = (st.st_mode & S_IRGRP) ? 'r' : '-';
perms[5] = (st.st_mode & S_IWGRP) ? 'w' : '-';
perms[6] = (st.st_mode & S_IXGRP) ? 'x' : '-';
// 其他人
perms[7] = (st.st_mode & S_IROTH) ? 'r' : '-';
perms[8] = (st.st_mode & S_IWOTH) ? 'w' : '-';
perms[9] = (st.st_mode & S_IXOTH) ? 'x' : '-';
// 硬链接计数
int linkNum = st.st_nlink;
// 文件所有者
char* fileUser = getpwuid(st.st_uid)->pw_name;
// 文件所属组
char* fileGrp = getgrgid(st.st_gid)->gr_name;
// 文件大小
int fileSize = (int)st.st_size;
// 修改时间
char* time = ctime(&st.st_mtime);
char mtime[512] = {0};
strncpy(mtime, time, strlen(time)-1);
char buf[1024];
sprintf(buf, "%s %d %s %s %d %s %s", perms, linkNum, fileUser, fileGrp, fileSize, mtime, argv[1]);
printf("%s\n", buf);
return 0;
}