获取文件信息,代码实现ls -l 操作

实现ls -l 操作,需要得到以下文件信息:文件类型,文件权限,链接数量,用户id,组id,最后修改时间,文件名

获取文件信息的原理,详见 文件 IO 笔记  获取文件属性方面


#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <grp.h>
#include <pwd.h>
#include <dirent.h>

int main(int argc, char const *argv[])
{
    // 定义struct stat 类型的结构体变量
    struct stat st;
    struct dirent *p;        // 定义结构体变量 接收 readir 返回值
    DIR *dir = opendir("."); // 目录流

    // 循环判断读取的文件
    while ((p = readdir(dir)) != NULL)
    {

        stat(p->d_name, &st);
        // 判断文件类型
        // 通过位与运算得出文件类型
        switch (st.st_mode & S_IFMT)
        {
        case S_IFREG:
            printf("-");
            break;
        case S_IFDIR:
            printf("d");
            break;
        case S_IFCHR:
            printf("c");
            break;
        case S_IFIFO:
            printf("f");
            break;
        case S_IFLNK:
            printf("l");
            break;
        case S_IFBLK:
            printf("b");
            break;
        case S_IFSOCK:
            printf("s");
            break;
        default:
            printf("mode err\n");
            break;
        }
        
        // 用户权限
        if (st.st_mode & S_IRUSR)
        {
            printf("r");
        }
        else
        {
            printf("-");
        }

        if (st.st_mode & S_IWUSR)
        {
            printf("w");
        }
        else
        {
            printf("-");
        }
        if (st.st_mode & S_IXUSR)
        {
            printf("x");
        }
        else
        {
            printf("-");
        }

        // 组员权限
        if (st.st_mode & S_IRGRP)
        {
            printf("r");
        }
        else
        {
            printf("-");
        }
        if (st.st_mode & S_IWGRP)
        {
            printf("w");
        }
        else
        {
            printf("-");
        }
        if (st.st_mode & S_IXGRP)
        {
            printf("x");
        }
        else
        {
            printf("-");
        }

        // 其他人权限
        if (st.st_mode & S_IROTH)
        {
            printf("r");
        }
        else
        {
            printf("-");
        }
        if (st.st_mode & S_IWOTH)
        {
            printf("w");
        }
        else
        {
            printf("-");
        }
        if (st.st_mode & S_IXOTH)
        {
            printf("x");
        }
        else
        {
            printf("-");
        }

        // 硬链接数
        printf(" %d ", st.st_nlink);
        // 用户名
        printf(" %s", getpwuid(st.st_uid)->pw_name);
        // 组名
        printf(" %s", getgrgid(st.st_gid)->gr_name);
        // 大小
        printf(" %ld ", st.st_size);
        // 修改时间
        printf(" %.12s ", ctime(&st.st_mtime) + 4);
        // 文件名
        printf(" %s", p->d_name);
        printf("\n");
    }

    return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值