标签:lob ted bre cas mode number oca default access
学习使用stat(1),并用C语言实现
一、通过man
命令查看stat
使用man 1 stat
使用stat --help
使用man -k stat | grep 2
二、学习使用stat
stat 文件
stat -L/-f/-t 文件
三、查看stat (2)
头文件和结构体
stat (2)
结构体
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
struct stat {
dev_t st_dev; /* ID of device containing file */
ino_t st_ino; /* inode number */
mode_t st_mode; /* protection */
nlink_t st_nlink; /* number of hard links */
uid_t st_uid; /* user ID of owner */
gid_t st_gid; /* group ID of owner */
dev_t st_rdev; /* device ID (if special file) */
off_t st_size; /* total size, in bytes */
blksize_t st_blksize; /* blocksize for file system I/O */
blkcnt_t st_blocks; /* number of 512B blocks allocated */
time_t st_atime; /* time of last access */
time_t st_mtime; /* time of last modification */
time_t st_ctime; /* time of last status change */
};
三、编写mystate
伪代码和代码
查询文件状态信息
打印文件状态信息
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <time.h>
void main(int argc, char *argv[])
{
struct stat state;
stat(argv[1], &state);
printf(" 文件:‘%s'\n", argv[1]);
printf(" 大小:%lld\t", (long long)state.st_size);
printf("块:%lld\t", (long long)state.st_blocks);
printf("IO块:%ld\t", (long)state.st_blksize);
switch(state.st_mode & S_IFMT)
{
case S_IFBLK:
printf("块设备文件");
break;
case S_IFCHR:
printf("字符设备文件");
break;
case S_IFDIR:
printf("目录");
break;
case S_IFIFO:
printf("管道文件");
break;
case S_IFLNK:
printf("符号链接文件");
break;
case S_IFREG:
printf("普通文件");
break;
case S_IFSOCK:
printf("套接字文件");
break;
default:
break;
}
printf("\n");
printf("设备:%xh/%ldd\t", (long)state.st_dev, (long)state.st_dev);
printf("Inode:%ld\t", (long)state.st_ino);
printf("硬链接:%ld\n", (long)state.st_nlink);
printf("权限:(%o)\t", (unsigned int)(state.st_mode & ~S_IFMT));
printf("Uid:(%ld)\t", (long)state.st_uid);
printf("Gid:(%ld)\n", (long)state.st_gid);
printf("最近访问:%s", ctime(&state.st_atim));
printf("最近更改:%s", ctime(&state.st_ctim));
printf("最近改动:%s", ctime(&state.st_mtim));
printf("创建时间:-");
printf("\n");
}
四、实现mystate
伪代码
四、测试代码,mystat
与stat(1)
对比,提交截图
码云链接:https://gitee.com/besti20175216/20175216_snow_plains/blob/master/20175216zxy/mystate.c
标签:lob ted bre cas mode number oca default access
原文地址:https://www.cnblogs.com/besti20175216/p/12104196.html