2019-2020-1 20175313 《信息安全系统设计基础》实现mystat
目录
一、题目要求
学习使用stat(1),并用C语言实现
- 提交学习stat(1)的截图
 - man -k ,grep -r的使用
 - 伪代码
 - 产品代码 mystate.c,提交码云链接
 - 测试代码,mystat 与stat(1)对比,提交截图
 
二、题目理解
Linux下stat的功能:在命令行输入man 1 stat进行查看:
- 命令格式:stat [选项]
 - 命令功能:显示的是文件的I节点信息。Linux文件系统以块为单位存储信息,为了找到某一个文件所在存储空间的位置,用I节点对每个文件进行索引,I节点包含了描述文件所必要的全部信息,其中包含了文件的大小,类型,存取权限,文件的所有者。
 - 常用参数:
 
- -f 不显示文件本身的信息,显示文件所在文件系统的信息。
 - -L 显示符号链接。
 - -t 只显示摘要信息。
 
发现see also里面推荐我们查看stat(2),所以在命令行输入man 2 stat进行查看:
Linux下的stat函数:int stat(const char *file_name, struct stat *buf );
- 函数功能:通过文件名filename获取文件信息,并保存在buf所指的结构体stat中
 - 头文件:
 
#include <sys/stat.h> #include <unistd.h>
- 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;     //最后一次改变时间(指属性)
  };三、伪代码
- 指定文件名filename
 - 定义stat结构体,调用stat()函数,将filename中的信息储存再stat结构体中
 - 用原点标记符得到stat中的属性,并用printf将其输出
 
四、代码实现
#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>\n", 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\n");
        break;
    case S_IFCHR:
        printf("character device\n");
        break;
    case S_IFDIR:
        printf("directory\n");
        break;
    case S_IFIFO:
        printf("FIFO/pipe\n");
        break;
    case S_IFLNK:
        printf("symlink\n");
        break;
    case S_IFREG:
        printf("regular file\n");
        break;
    case S_IFSOCK:
        printf("socket\n");
        break;
    default:
        printf("unknown?\n");
        break;
    }
    printf("文件: '%s'\n",argv[1]);
    printf("大小: %lld      ",(long long) sb.st_size);
    printf("块: %lld               ",(long long) sb.st_blocks);
    printf("IO块: %ld\n",(long) sb.st_blksize);
    printf("设备: %d      ",sb.st_dev);//文件设备编号
    printf("Inode: %d      ",sb.st_ino);//文件i节点标号
    printf("硬链接: %ld\n", (long) sb.st_nlink);
    printf("权限: %lo (octal)      ",(unsigned long) sb.st_mode);
    printf("Uid=%ld       Gid=%ld\n",(long) sb.st_uid, (long) sb.st_gid);
    printf("最近更改: %s", ctime(&sb.st_ctime));
    printf("最近访问: %s", ctime(&sb.st_atime));
    printf("最近改动: %s", ctime(&sb.st_mtime));
    printf("创建时间: -\n");
    exit(EXIT_SUCCESS);
}五、运行结果截图
相关推荐
  zyccsdn    2015-07-27  
   wenjs00    2011-08-15  
   fsfsdfsdw    2012-10-17  
   85463363    2012-04-11  
   iamplane    2011-08-16  
   vickytong0    2011-06-16  
   herryyy    2014-09-16  
   xiefei0    2011-04-17  
   secondid    2010-04-25  
   banana000    2019-07-01  
   chengqiang00    2019-07-01  
   87540499    2013-11-08  
   83433161    2013-07-30  
   81991935    2012-12-31  
   fansenjun    2019-06-28  
   吾日五省我身    2014-10-13  
   bluetears    2019-06-26  
 



