zoukankan      html  css  js  c++  java
  • stat命令的实现-mysate

    stat命令的实现-mysate

    学习任务

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

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

    学习stat(1)命令

    首先在终端中输入man 1 stat 命令查看stat命令的详细功能

    stat命令的功能为:显示文件或文件系统状态。
    stat命令的可执行的选择项如下(对这些选项逐一进行了尝试)

    • -L:显示符号链接
    • -f:不显示文件本身信息,但显示文件所在的文件系统信息
    • -t:简介模式,只显示摘要信息
    • -C:显示文件的权限(其中包含了一些参数,这里列举几个例子)
      • %a:给出8进制访问权限
      • %A:可读格式的访问权限
      • %b:显示被分配的块数
      • %c:SELinux安全上下文字符串
      • %d:十进制设备编号

    man -k ,grep -r的使用

    我们使用man -k stat | grep 2 命令查看相关的系统调用函数,可以观察到有一个系统调用为2的stat()函数,与刚刚的为1的系统调用不同。

    我们在终端中输入命令man 2 stat 查看相关函数。

    伪代码实现

    依次输出文件的相关信息:
    文件名
    文件的大小   文件使用的数据块总数    IO块的大小    文件的类型(常规文件)    设备编号    Inode号    链接数
    文件的权限    文件所有权的GID和UID
    文件的访问时间Access Time(当文件被访问时更新)
    文件的修改时间Modify Time(当文件中数据内容被修改时更新)
    文件的状态时间Change Time(当文件状态被修改时更新)
    

    产品代码

    #include <stdio.h>  
    #include <unistd.h>  
    #include <sys/stat.h>  
    #include <sys/types.h>  
    #include <stdlib.h>  
    #include <time.h>  
    int main(int argc, char **argv){
    	struct stat st;  
    	if(argc != 2){  
       		fprintf(stderr, "Usage: %s <file_pathname> 
    ", argv[0]);  
            	exit(EXIT_FAILURE);  
        	}  
     	if(stat(argv[1], &st) == -1){  
            	perror("stat");  
            	exit(EXIT_SUCCESS);  
        	}
     	printf("File type:                ");  
        	switch(st.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) st.st_ino);  
      	printf("Mode:                     %lo (octal)
    ", (unsigned long) st.st_mode);  
      	printf("Link count:               %ld
    ", (long) st.st_nlink);  
      	printf("Ownership:                UID=%ld   GID=%ld
    ", (long) st.st_uid, (long) st.st_gid);  
      	printf("device containing file id:%ld 
    ", (long) st.st_dev);  
      	printf("device id:                %ld 
    ", (long) st.st_rdev);  
      	printf("File size:                %lld bytes
    ", (long long) st.st_size);  
      	printf("Preferred I/O block size: %ld bytes
    ", (long) st.st_blksize);  
      	printf("Blocks allocated:         %lld
    ", (long long) st.st_blocks);  
      	printf("Last status change:       %s", ctime(&st.st_ctime));  
      	printf("Last file access:         %s", ctime(&st.st_atime));  
      	printf("Last file modification:   %s", ctime(&st.st_mtime));  
     	exit(EXIT_SUCCESS);  
    }
    

    运行结果如下:

    测试代码及对比

    stat代码学习详解

  • 相关阅读:
    spring cloud微服务docker启动
    docker安装mysql5.7
    Spring Boot CommandLineRunner的使用
    IDEA Java 源发行版 8 需要目标发行版 1.8
    Centos6.5安装Python2.7.9
    Hive在drop表的时候报错
    反向读取Mysql数据库表结构到PowerDesigner中
    SpringCloud与Consul集成实现负载均衡
    Mac系统安装和卸载brew包管理
    Consul集群搭建
  • 原文地址:https://www.cnblogs.com/wangzihong0213/p/12108477.html
Copyright © 2011-2022 走看看