stat命令的实现-mysate

stat命令的实现-mysate

任务详情

学习使用stat(1),并用C语言实现

  1. 提交学习stat(1)的截图
  2. man -k ,grep -r的使用
  3. 伪代码
  4. 产品代码 mystate.c,提交码云链接
  5. 测试代码,mystat 与stat(1)对比,提交截图

任务实现

一、学习使用stat(1)

  1. 使用 man -k stat 查看 stat 的相关信息

  1. 使用 man 1 stat 查看 stat(1) 的详细信息

  1. 使用 man 2 stat 继续了解关于 stat 的详细信息

  1. 了解到关于stat(1)的相关知识

    • stat命令(可参考Linux系统stat指令用法)主要用于显示文件或文件系统的详细信息,该命令的语法格式如下:
      • -f:不显示文件本身的信息,显示文件所在文件系统的信息
      • -L:显示符号链接
      • -t:简洁模式,只显示摘要信息
    • stat命令显示的是文件的I节点信息。Linux文件系统以块为单位存储信息,为了找到某一个文件所在存储空间的位置,用I节点对每个文件进行索引,I节点包含了描述文件所必要的全部信息,其中包含了文件的大小,类型,存取权限,文件的所有者。
  2. 相关头文件与函数

#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);

#include <fcntl.h>           /* Definition of AT_* constants */
#include <sys/stat.h>

int fstatat(int dirfd, const char *pathname, struct stat *statbuf, int flags);
  1. 相关结构体

struct stat {
dev_t     st_dev;  /* 包含文件的设备的ID */
ino_t     st_ino;  /* Inode number */
mode_t    st_mode; /* 索引节点号 */
nlink_t   st_nlink;/* 硬链接数 */
uid_t     st_uid;  /* 所有者的用户ID */
gid_t     st_gid;  /* 所有者的组ID */
dev_t     st_rdev; /* 设备ID(如果是特殊文件) */
off_t     st_size; /* 总大小(字节) */
blksize_t st_blksize;     /* 文件系统I/O的块大小 */
blkcnt_t  st_blocks;      /* 分配的512B块数 */

 /* Since Linux 2.6, the kernel supports nanosecond
    precision for the following timestamp fields.
    For the details before Linux 2.6, see NOTES. */

struct timespec st_atim;  /* 上次访问时间 */
struct timespec st_mtim;  /* 上次修改时间 */
struct timespec st_ctim;  /* 上次状态更改时间 */

#define st_atime st_atim.tv_sec      /* 向后兼容性 */
#define st_mtime st_mtim.tv_sec
#define st_ctime st_ctim.tv_sec
};

二、C语言实现

  1. 在通过 man 2 stat 中,给出了一个例子,通过例子实现调用lstat()并在返回的stat结构中显示选定的字段

  1. 与 stat + 文件名对比

  1. mystate.c对比

三、代码托管

码云链接

posted @ 2019-12-28 13:51  20175218陈敬勇  阅读(242)  评论(0编辑  收藏  举报