2019-2020-1 20175313 "Information security system design basis" to achieve mystat

First, the questions asked

Learn to use stat (1), and using C language

  1. Submitted learn stat (1) screenshot
  2. Use man -k, grep -r of
  3. Fake code
  4. Product code mystate.c, submit code link cloud
  5. Test code, and MYSTAT stat (1) Comparative submit screenshot

Second, understand the topic

Linux, stat functions: command line input man 1 statfor viewing:

  1. Format: stat [Options]
  2. Function: displays the I node information file. Linux file system to store information in units of blocks, to find a location where a file storage space, with the index I for each file node, the node I contains all the information necessary for the profile, which contains the size of the file, owner type, access rights, file.
  3. Common parameters:
  • -f file itself does not display information display system information file is located.
  • -L display symbolic links.
  • -t only the summary information.

See also discovered inside the recommended we look at stat (2), so at the command line man 2 statto view:

stat function under Linux:int stat(const char *file_name, struct stat *buf );


  1. Function Function: filename get file information by file name, and save the structure stat buf referred to in
  2. head File:
#include <sys/stat.h>
#include <unistd.h>
  1. stat structure:
 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;     //最后一次改变时间(指属性)
  };

Third, pseudocode

  • Specify the file name filename
  • Stat structure definitions, call stat () function, then the information is stored in the stat structure filename in
  • Stat properties obtained with the origin in the tag, and outputs it using printf

Fourth, code implementation

#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 &lt;pathname&gt;\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);
}

6.3.5 Operating results Screenshot

Guess you like

Origin www.cnblogs.com/xiannvyeye/p/12111203.html