stat命令的实现-mysate(课上测试)

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

  2. 推导出实现stat(1)需要的系统调用,并给出实现stat(1)的伪代码

    • 需要的系统调用:readdir() 、closedir()、fstat()、lstat()、fstatat()

    • 伪代码

      include <stdio.h>

      include <stdlib.h>

      include <unistd.h>

      include <sys/stat.h>

      include <dirent.h>

      int main(int argc, char *argv[])
      {
      if (argc < 2)
      {
      printf("Usage: %s \n", argv[0]);
      return 1;
      }
      struct stat stat_info;
      int lstat_result = lstat(argv[1], &stat_info);
      if (lstat_result == -1)
      {
      printf("Error: 无法获取 %s 的状态信息\n", argv[1]);
      return 1;
      }
      printf("File status for %s:\n", argv[1]);
      printf(" st_mode: %lu\n", stat_info.st_mode);
      printf(" st_size: %lu\n", stat_info.st_size);
      printf(" st_mtime: %lu\n", stat_info.st_mtime);
      printf(" st_ctime: %lu\n", stat_info.st_ctime);
      return 0;
      }

  3. 编写产品代码 mystate.c,提交代码

    include <stdio.h>

    include <stdlib.h>

    include <unistd.h>

    include <sys/stat.h>

    include <dirent.h>

    int main(int argc, char *argv[]) {
    if (argc < 2) {
    printf("Usage: %s \n", argv[0]);
    return 1;
    }

    struct stat stat_info;
    int lstat_result = lstat(argv[1], &stat_info);

    if (lstat_result == -1) {
    printf("Error: 无法获取 %s 的状态信息\n", argv[1]);
    return 1;
    }

    printf("File status for %s:\n", argv[1]);
    printf(" st_mode: %lu\n", stat_info.st_mode);
    printf(" st_size: %lu\n", stat_info.st_size);
    printf(" st_mtime: %lu\n", stat_info.st_mtime);
    printf(" st_ctime: %lu\n", stat_info.st_ctime);

    return 0;
    }

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

posted @ 2023-11-27 14:06  20211116彭玮琪  阅读(36)  评论(0)    收藏  举报