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

    一、学习stat(1)#

    1.在命令行中输入man 1 stat进行查看

    2.在命令行中输入man 2 stat进行查看

    二、伪代码#

    1.指定文件名filename;
    2.定义stat结构体,调用stat()函数,将filename中的信息储存再stat结构体中;
    3.用原点标记符得到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 &lt;pathname&gt;
    ", 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("文件: '%s'
    ",argv[1]);
        printf("大小: %lld      ",(long long) sb.st_size);
        printf("块: %lld               ",(long long) sb.st_blocks);
        printf("IO块: %ld
    ",(long) sb.st_blksize);
        printf("设备: %d      ",sb.st_dev);//文件设备编号
        printf("Inode: %d      ",sb.st_ino);//文件i节点标号
        printf("硬链接: %ld
    ", (long) sb.st_nlink);
        printf("权限: %lo (octal)      ",(unsigned long) sb.st_mode);
        printf("Uid=%ld       Gid=%ld
    ",(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("创建时间: -
    ");
        exit(EXIT_SUCCESS);
    }
    

    四、运行结果#

  • 相关阅读:
    centos ppp拨号
    Xen、KVM和VirtualBox比拼
    static用法一
    linux浏览器,邮件客户端,输入法,双屏设置,应用软件,gnome-screenshot/scrot -s截图,office
    OpenGl学习总结
    DICOM医学图像处理:DCMTK在VS2012中的配置
    linux下用mail发送邮件
    (HLS播放器之中的一个)HLS协议之M3U8解析
    tcpdump抓包分析具体解释
    对账简单说
  • 原文地址:https://www.cnblogs.com/gexvyang/p/12114170.html
Copyright © 2011-2022 走看看