zoukankan      html  css  js  c++  java
  • stat命令的实现

    stat命令的实现

    任务详情

    • 学习使用stat(1),并用C语言实现
    • 提交学习stat(1)的截图
    • man -k ,grep -r的使用
    • 伪代码
    • 产品代码 mystate.c,提交码云链接
    • 测试代码,mystat 与stat(1)对比,提交截图

    一、学习过程

    • 通过man命令查看stat

    • 首先使用man -k stat | grep 1man 1 stat查看stat(1)

    • 使用man -k stat | grep 2查看相关的系统调用

    二、学习使用stat

    • 使用stat查看文件

    • 使用stat -L查看文件

    • 使用stat -f查看文件

    • 使用stat -t查看文件

    三、伪代码

    - 查看并存储文件各个属性
    - 依次打印
    

    四、实现

    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <unistd.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("文件类型:                ");
        switch (sb.st_mode & S_IFMT) {
            case S_IFBLK:   printf("block device
    ");
                break;
        case S_IFCHR:   printf("character device
    ");
                break;
        case S_IFDIR:   printf("directory
    ");
                break;
        case S_IFIFO:   printf("FIFO/pipe
    ");
                break;
        case S_IFLNK:   printf("symlink
    ");
                break;
        case S_IFREG:   printf("regular file
    ");
                break;
        case S_IFSOCK:  printf("socket
    ");
                break;
        default:        printf("unknown?
    ");
                break;
        }
        printf("大小:       %lld bytes
    ",(long long) sb.st_size);
        printf("块:         %lld
    ",(long long) sb.st_blocks);
        printf("Inode:      %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_ctime));
        printf("最近改动:   %s", ctime(&sb.st_mtime));
        exit(EXIT_SUCCESS);
    }
    
    

    五、测试

  • 相关阅读:
    LeetCode-Cycle Detection,Find the Duplicate Number
    LeetCode-Symmetric Tree
    剑指offer-打印链表倒数第k个结点
    Http协议中Get和Post的区别
    ORDER BY 语句
    AND 和 OR 运算符
    WHERE 子句
    SQL SELECT DISTINCT 语句
    SQL SELECT 语句
    SQL DML 和 DDL
  • 原文地址:https://www.cnblogs.com/fzlzc/p/12109401.html
Copyright © 2011-2022 走看看