stat命令的实现-mysate

stat命令的实现-mysate

任务详情

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

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

学习过程

一、通过man命令查看stat

  • 使用man 1 stat

  • 使用stat --help

  • 使用man -k stat | grep 2

二、学习使用stat

  • stat 文件

  • stat -L/-f/-t 文件

三、查看stat (2)头文件和结构体

stat (2)结构体

       #include <sys/types.h>
       #include <sys/stat.h>
       #include <unistd.h>

struct stat {
               dev_t     st_dev;     /* ID of device containing file */
               ino_t     st_ino;     /* inode number */
               mode_t    st_mode;    /* protection */
               nlink_t   st_nlink;   /* number of hard links */
               uid_t     st_uid;     /* user ID of owner */
               gid_t     st_gid;     /* group ID of owner */
               dev_t     st_rdev;    /* device ID (if special file) */
               off_t     st_size;    /* total size, in bytes */
               blksize_t st_blksize; /* blocksize for file system I/O */
               blkcnt_t  st_blocks;  /* number of 512B blocks allocated */
               time_t    st_atime;   /* time of last access */
               time_t    st_mtime;   /* time of last modification */
               time_t    st_ctime;   /* time of last status change */
           };

三、编写mystate伪代码和代码

查询文件状态信息
打印文件状态信息

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <time.h>

void main(int argc, char *argv[])
{
    struct stat state;

    stat(argv[1], &state);

    printf("  文件:‘%s'
", argv[1]);
    printf("  大小:%lld	", (long long)state.st_size);
    printf("块:%lld	", (long long)state.st_blocks);
    printf("IO块:%ld	", (long)state.st_blksize);
    switch(state.st_mode & S_IFMT)
    {
    case S_IFBLK:
        printf("块设备文件");
        break;
    case S_IFCHR:
        printf("字符设备文件");
        break;
    case S_IFDIR:
        printf("目录");
        break;
    case S_IFIFO:
        printf("管道文件");
        break;
    case S_IFLNK:
        printf("符号链接文件");
        break;
    case S_IFREG:
        printf("普通文件");
        break;
    case S_IFSOCK:
        printf("套接字文件");
        break;
    default:
        break;
    }
    printf("
");

    printf("设备:%xh/%ldd	", (long)state.st_dev, (long)state.st_dev);
    printf("Inode:%ld	", (long)state.st_ino);
    printf("硬链接:%ld
", (long)state.st_nlink);
    printf("权限:(%o)	", (unsigned int)(state.st_mode & ~S_IFMT));
    printf("Uid:(%ld)	", (long)state.st_uid);
    printf("Gid:(%ld)
", (long)state.st_gid);
    printf("最近访问:%s", ctime(&state.st_atim));
    printf("最近更改:%s", ctime(&state.st_ctim));
    printf("最近改动:%s", ctime(&state.st_mtim));
    printf("创建时间:-");
    printf("
");
}

四、实现mystate伪代码

四、测试代码,mystatstat(1)对比,提交截图

码云链接:https://gitee.com/besti20175216/20175216_snow_plains/blob/master/20175216zxy/mystate.c

原文地址:https://www.cnblogs.com/besti20175216/p/12104196.html