查询目录下文件内容

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <strings.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <pwd.h>
#include <time.h>
int main(int argc, char *argv[])
{
    if(argc < 2){
        printf("please enter %s filename\n", argv[0]);
        return -1;
    }

    struct stat statbuf;
    if(-1 == lstat(argv[1], &statbuf)){
        perror("lstat");
        return -1;
    }

    //printf("size: %ld\n", statbuf.st_size);
    
    switch(statbuf.st_mode & S_IFMT){
        case S_IFSOCK:
            printf("s");
            break;
        case S_IFLNK:
            printf("l");
            break;
        case S_IFREG:
            printf("-");
            break;
        case S_IFBLK:
            printf("b");
            break;
        case S_IFDIR:
            printf("d");
            break;
        case S_IFCHR:
            printf("c");
            break;
        case S_IFIFO:
            printf("p");
            break;
    }
    
     /*if(S_ISLNK(statbuf.st_mode)){
        printf("--> l");
     }*/
    
    int n = 8;
    for(; n > 0; n--){
        if(statbuf.st_mode &(1 << n)){ //判断权限位的低9位 0-8
            switch(n % 3){
                case 2:
                    printf("r");
                    break;
                case 1:
                    printf("w");
                    break;
                case 0:
                    printf("x");
                    break;
            }
        }else{
            printf("-");
        }
    }
    printf(" %ld", statbuf.st_nlink);

    struct passwd * uidp = getpwuid(statbuf.st_uid);
    printf(" %s\n" ,uidp->pw_name);
    struct group * uidp = getgrid(statbuf.st_uid);
    printf(" %s\n" ,uidp->gr_name);
    printf(" %6ld\n" ,uidp->st_size);
    struct tm *b=localtime(&statbuf.st_mtime);
    printf(" %d-%d-%d  %d:%d:%d\n",b->tm_year+1900,b->tm_mon+1,b->tm_mday,b->tm_hour,b->tm_min,b->tm_sec);

    return 0;
}
 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

鸿蒙小开

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值