#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>\n", 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\n");
            break;
    case S_IFCHR:   printf("character device\n");
            break;
    case S_IFDIR:   printf("directory\n");
            break;
    case S_IFIFO:   printf("FIFO/pipe\n");
            break;
    case S_IFLNK:   printf("symlink\n");
            break;
    case S_IFREG:   printf("regular file\n");
            break;
    case S_IFSOCK:  printf("socket\n");
            break;
    default:        printf("unknown?\n");
            break;
    }
    printf("I-node number:            %ld\n", (long) sb.st_ino);
    printf("Mode:                     %lo (octal)\n",(unsigned long) sb.st_mode);
    printf("Link count:               %ld\n", (long) sb.st_nlink);
    printf("Ownership:                UID=%ld   GID=%ld\n",(long) sb.st_uid, (long) sb.st_gid);
    printf("Preferred I/O block size: %ld bytes\n",(long) sb.st_blksize);
    printf("File size:                %lld bytes\n",(long long) sb.st_size);
    printf("Blocks allocated:         %lld\n",(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);
}
posted @ 2019-12-25 16:38  20175201张驰  阅读(150)  评论(0编辑  收藏  举报