zoukankan      html  css  js  c++  java
  • #20175201 stat命令的实现-mystate

    一 任务详情
    学习使用stat(1),并用C语言实现

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


    关于stat(1)

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

    三、实验截图



    四、实验代码

    #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("File type:                ");
        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("I-node number:            %ld
    ", (long) sb.st_ino);
        printf("Mode:                     %lo (octal)
    ",(unsigned long) sb.st_mode);
        printf("Link count:               %ld
    ", (long) sb.st_nlink);
        printf("Ownership:                UID=%ld   GID=%ld
    ",(long) sb.st_uid, (long) sb.st_gid);
        printf("Preferred I/O block size: %ld bytes
    ",(long) sb.st_blksize);
        printf("File size:                %lld bytes
    ",(long long) sb.st_size);
        printf("Blocks allocated:         %lld
    ",(long long) sb.st_blocks);
        printf("Last status change:       %s", ctime(&sb.st_ctime));
        printf("Last file access:         %s", ctime(&sb.st_atime));
        printf("Last file modification:   %s", ctime(&sb.st_mtime));
        exit(EXIT_SUCCESS);
    }
    
  • 相关阅读:
    1105 Spiral Matrix (25分)(蛇形填数)
    1104 Sum of Number Segments (20分)(long double)
    1026 Table Tennis (30分)(模拟)
    1091 Acute Stroke (30分)(bfs,连通块个数统计)
    1095 Cars on Campus (30分)(排序)
    1098 Insertion or Heap Sort (25分)(堆排序和插入排序)
    堆以及堆排序详解
    1089 Insert or Merge (25分)
    1088 Rational Arithmetic (20分)(模拟)
    1086 Tree Traversals Again (25分)(树的重构与遍历)
  • 原文地址:https://www.cnblogs.com/20175201zc/p/12097147.html
Copyright © 2011-2022 走看看