• stat命令的实现mystat


    1.使用stat截图

    2.man -k grep -r的使用

    3.伪代码
    首先判断输入中是否包含文件参数,如果有则继续,没有则提示用户输入错误。
    然后查找并将文件的相关参数进行保存,声明结构体,并调用stat()函数给结构体赋值,将文件的设备编号、节点、文件的类型和存取的权限、连到该文件的硬链接数目等按顺序输出。

    4.代码

    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 a;
    if (argc != 2) {
    fprintf(stderr, "Usage: %s \n", argv[0]);
    exit(EXIT_FAILURE);
    }
    if (stat(argv[1], &a) == -1) {
    perror("stat");
    exit(EXIT_FAILURE);
    }
    printf("文件: %s\n",argv[1]);
    printf("Inode: %ld\n", (long) a.st_ino);
    printf("硬链接: %ld\n", (long) a.st_nlink);
    printf("权限: Uid = %ld Gid = %ld\n",(long) a.st_uid, (long) a.st_gid);
    printf("IO块: %ld ",(long) a.st_blksize);
    switch (a.st_mode & S_IFMT)
    {
    case S_IFBLK: printf("块设备\n");
    break;
    case S_IFCHR: printf("character device\n");
    break;
    case S_IFDIR: printf("目录\n");
    break;
    case S_IFIFO: printf("FIFO/管道\n");
    break;
    case S_IFLNK: printf("符号链接\n");
    break;
    case S_IFREG: printf("普通文件\n");
    break;
    case S_IFSOCK: printf("socket\n");
    break;
    default: printf("未知?\n");
    break;
    }
    printf("大小: %lld bytes\n",(long long) a.st_size);
    printf("块: %lld\n",(long long) a.st_blocks);
    printf("最近访问: %s", ctime(&a.st_atime));
    printf("最近更改: %s", ctime(&a.st_mtime));
    printf("最近改动: %s", ctime(&a.st_ctime));
    exit(EXIT_SUCCESS);
    }

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

  • 相关阅读:
    Use MVS Dsbame convensions. windows下ftp.exe客户端上传错误
    Sqlserver 2005:数据库快照
    Oracle:使用ASM自动存储管理, 严重推荐
    Thunderbird 邮件客户端:windows 和 ubuntu 或 liunx 下共用的方法
    Oracle:Oracle 10 RAC 安装群集件的准备工作
    SSH
    STL
    ASP生成静态Html文件技术杂谈
    Nessus:网络和主机漏洞评估程序安装试用
    table 的 id 属性不被 document.getElementById支持
  • 原文地址:https://www.cnblogs.com/quicci/p/16796299.html
Copyright © 2020-2023  润新知