zoukankan      html  css  js  c++  java
  • 20165223 《信息安全系统设计基础》 stat命令的实现-mysate






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





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




    一、学习 stat 命令

    1. stat 命令了解

    • 作用:用来显示文件的详细信息,包括inode, atime, mtime, ctime
    • 格式:stat [OPTION]…FILE…

    2. stat 命令格式

    • -L:显示符号链接
    • -f:显示文件所在的文件系统信息
    • -t:以简洁的方式输出摘要信息

    3. stat 命令使用实例

    • 查看文件的详细信息:stat /文件目录

    • 查看文件系统的详细信息:stat -f /文件目录

    • -t查看简洁输出(脚本中常用来获取想要的信息):stat -t /文件目录

    • 八进制文件权限(shell脚本时会用到):stat -c "%a" /文件目录

    • 查看硬盘的信息:stat /dev/sda

    4. stat 输出内容

    • File:显示文件名
    • Size:显示文件大小
    • Blocks:文件使用的数据块总数
    • IO Block:IO块大小
    • regular file:文件类型(常规文件)
    • Device:设备编号
    • Inode:Inode号
    • Links:链接数
    • Access:文件的权限
    • Gid、Uid:文件所有权的Gid和Uid

    5. Linux下的三个时间

    • Access Time:简写为atime,表示文件的访问时间。当文件内容被访问时,更新这个时间
    • Modify Time:简写为mtime,表示文件内容的修改时间,当文件的数据内容被修改时,更新这个时间
    • Change Time:简写为ctime,表示文件的状态时间,当文件的状态被修改时,更新这个时间

    返回目录






    二、实现 mystat

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

    - 使用命令 man 1 stat 查看

    返回目录



    2. man -k ,grep -r的使用

    • 使用 man -k stat | grep 2 查找stat相关函数

    • 使用 man 2 stat 学习函数


    返回目录



    3. 编写mystate.c的伪代码

    • 用户运行 mystat.c 程序,输入文件名
    • 由man 2 stat函数学习后可知调用stat函数实现功能
    • 结构体struct stat:
    struct stat {
        dev_t         st_dev;       //文件的设备编号
        ino_t         st_ino;       //节点
        mode_t        st_mode;      //文件的类型和存取的权限
        nlink_t       st_nlink;     //连到该文件的硬连接数目,刚建立的文件值为1
        uid_t         st_uid;       //用户ID
        gid_t         st_gid;       //组ID
        dev_t         st_rdev;      //(设备类型)若此文件为设备文件,则为其设备编号
        off_t         st_size;      //文件字节数(文件大小)
        unsigned long st_blksize;   //块大小(文件系统的I/O 缓冲区大小)
        unsigned long st_blocks;    //块数
        time_t        st_atime;     //最后一次访问时间
        time_t        st_mtime;     //最后一次修改时间
        time_t        st_ctime;     //最后一次改变时间(指属性)
    };
    
    • 依次调用函数,打印输出节点ino,文件类型mode,文件的连接数nlink,用户ID uid和组ID gid,块大小blksize,字节数size,块数目blocks,以及三个时间atime、mtime、ctime

    返回目录



    4. 产品代码 mystate.c,提交码云链接

    • 产品代码(有参考) mystat.c
    #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);
    }
    
    • 编译运行 mystat

    返回目录



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

    返回目录






    #
    程序运行





    ###                    [1. 理解test.c, 说出程序功能](#6) ###                    [2. 编译运行程序,提交运行截图](#7)




    1. 理解test.c, 说出程序功能

    • 代码 test.c
    #include <stdio.h>
    #include <stdlib.h>
    #include <fcntl.h>
    #include <unistd.h>
    
    int main()
    {
    	int	fd ;
    	int	newfd;
    	char	line[100];
    
    	fgets( line, 100, stdin ); printf("%s", line );
    	fgets( line, 100, stdin ); printf("%s", line );
    	fgets( line, 100, stdin ); printf("%s", line );
    
    	fd = open("data", O_RDONLY);	
    
    	newfd = dup2(fd,0);	
    	
    	if ( newfd != 0 ){
    		fprintf(stderr,"Could not duplicate fd to 0
    ");
    		exit(1);
    	}
    	close(fd);			
    
    	fgets( line, 100, stdin ); printf("%s", line );
    	fgets( line, 100, stdin ); printf("%s", line );
    	fgets( line, 100, stdin ); printf("%s", line );
    }
    
    • data 内容
    20165223cn
    hello world
    test
    
    • 理解 test.c 功能
      • 采集用户的三次输入内容,并打印出来
      • 用只读方式打开data文件,并记为fd
      • 用newfd复制fd中的内容,并打印出来;若失败则提示"Could not duplicate fd to 0"(不能复制fd)

    返回目录



    2. 编译运行程序,提交运行截图

    • 编译运行成功

    • 复制fd失败时提示错误

    返回目录






  • 相关阅读:
    Uva12657 Boxes in a Line
    Uva11988 Broken Keyboard (a.k.a. Beiju Text)
    Uva442 Matrix Chain Multiplication
    Uva514 Rails
    一些计划
    Java基础知识强化07:打印出空心菱形
    Java基础知识强化06:使用BigDecimal计算阶乘1+1/2!+1/3!+……
    Java基础知识强化05:不借助第三个变量实现两个变量互换
    Java基础知识强化04:判断101~200之间有多少素数
    Android(java)学习笔记144:网络图片浏览器的实现(ANR)
  • 原文地址:https://www.cnblogs.com/moddy13162201/p/10258262.html
Copyright © 2011-2022 走看看