• Linux下编写mystat


    Linux下编写mystat

    1.学习stat(1)

    • 在Linux下通过 man stat 命令查看stat(1)的详细信息

      我们可以了解到,stat()函数的功能是打印文件的状态(inode内容)

    • 语法
      stat [OPTION]...FILE

    • 实例演示

    • 函数说明
      函数说明: 通过文件名filename获取文件信息,并保存在buf所指的结构体stat中。
      返回值: 执行成功则返回0,失败返回-1,错误代码存于errno。
      结构体内部如下:

    点击查看代码
    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 */ 最后一次改变该文件状态的时间   
    };
    
    

    2. man -k ,grep -r的使用

    man -k的使用

    • 在linux下我们可以使用 man man 命令来查看man指令的用法
    • man -k,grep -r功能

      可以看到,man -k命令 给予了用户输入正则表达式以查询函数或指令,grep -r即为后面的正则表达式的输入方式。
    • 实例展示
      如果我们要查询1章节下的stat指令,那么可以使用以下指令:

      系统会打印在1章节下所有包含“stat”关键词的指令或函数,这样查找指令或函数就方便多了。

    3.mystat伪代码

    • 1.输入的文件路径到argv中

    • 2.将文件的相关信息存入到结构体中

    • 3.系统调用stat函数输出文件的相关信息

    4.mystat代码编写

    https://gitee.com/wudiyangsai/mycode/blob/master/mystat.c

    5.测试代码,mystat 与stat(1)对比,提交截图

    • 测试代码截图
    • 系统stat(1)与mystat对比
  • 相关阅读:
    jquery选择器(复习向)
    如何使元素/文字 垂直居中?
    mvc与mvvm的区别与联系
    python16_day26【crm 增、改、查】
    python16_day25【crm】
    python16_day24【restful、crm表构、认证】
    python16_day23【cmdb前端】
    django 【认证】
    python16_day22【cmdb注释】
    django【F和Q】
  • 原文地址:https://www.cnblogs.com/wdys12138/p/16785762.html
Copyright © 2020-2023  润新知