• mystat


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

    1.提交学习stat(1)的截图

    首先man一下

    可以看出,stat命令可以用来显示文件的详细信息,包括inode, atime, mtime, ctime

    stat -f 显示文件系统的信息

    stat -t 以简洁的方式输出

    stat * 显示该目录下所有文件及子目录的信息

    stat显示了三个很重要的时间,分别是access、modify和change time,他们的区别如下:

    • 当我们仅仅只是读取文件时,access time 改变,而modify,change time 不会改变
    • 当修改文件时,access,modify,change time 都会跟着改变
    • 当修改文件属性时,change time 改变,而access,modify time 不变。

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

    man -k xxx可以搜索出所有带有xxx关键字的命令,比如

    因为Linux命令可分为以下八种

    MANUAL SECTIONS
        The standard sections of the manual include:
     
        1      User Commands
        2      System Calls
        3      C Library Functions
        4      Devices and Special Files
        5      File Formats and Conventions
        6      Games et. Al.
        7      Miscellanea
        8      System Administration tools and Deamons
    

    所以我们可以通过命令man -k stat | grep 2来查看stat命令的系统调用

    发现了一个结构体

    从这里我们可以知道只要调用stat函数就可以实现stat命令的功能,此外,文件类型的输出是一个整数,我们还要加一步判断

    3.伪代码

    由上面的分析我们可以得出伪代码

    1.判断输入的文件是否存在或者正确
    2.调用结构体并赋值
    3.输出结构体  
    

    4.产品代码 mystate.c,提交码云链接

    #include <sys/types.h>
    #include <sys/stat.h>
    #include <time.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc, char *argv[])
    {
        struct stat sb;
        if (argc != 2) {
            fprintf(stderr, "Usage: %s <pathname>
    ", argv[0]);
            exit(EXIT_FAILURE);
        }
        if (stat(argv[1], &sb) == -1) {
            perror("stat");
            exit(EXIT_FAILURE);
        }
        printf("文件: %s
    ",argv[1]);
        printf("大小: %lld bytes
    ",(long long) sb.st_size);
        printf("块: %lld
    ",(long long) sb.st_blocks);
        printf("IO块: %ld ",(long) sb.st_blksize);
        switch (sb.st_mode & S_IFMT) {
            case S_IFBLK:   printf("块设备
    ");
                break;
        case S_IFCHR:   printf("character device
    ");
                break;
        case S_IFDIR:   printf("目录
    ");
                break;
        case S_IFIFO:   printf("FIFO/管道
    ");
                break;
        case S_IFLNK:   printf("符号链接
    ");
                break;
        case S_IFREG:   printf("普通文件
    ");  
                break;
        case S_IFSOCK:  printf("socket
    ");
                break;
        default:        printf("未知?
    ");
                break;
        }
        printf("I-node: %ld
    ", (long) sb.st_ino);
        printf("硬连接: %ld
    ", (long) sb.st_nlink);
        printf("权限: UID=%ld   GID=%ld
    ",(long) sb.st_uid, (long) sb.st_gid);
        printf("最近访问: %s", ctime(&sb.st_atime));
        printf("最近更改: %s", ctime(&sb.st_mtime));
        printf("最近改动: %s", ctime(&sb.st_ctime));
        exit(EXIT_SUCCESS);
    }
    

    码云链接
    https://gitee.com/qiu_yu_wang/code/issues/I4H706

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

    参考:
    https://blog.csdn.net/jerry_1126/article/details/52716571
    https://www.cnblogs.com/ggjucheng/archive/2013/01/13/2856896.html

  • 相关阅读:
    学习hadoop
    贵在坚持
    保护好自己的毕业论文
    博客园与CSDN的选择
    Matlab中的“prod”函数
    js实现HashMap()
    js常用正则表达式
    苹果手机使用替代onkeyup的方法
    keydown
    ArrayAndString(数组和字符串)
  • 原文地址:https://www.cnblogs.com/ffffatal/p/15510928.html
Copyright © 2020-2023  润新知