显示指定路径下所有文件的权限 硬链接数 时间 以及名字

该C程序用于遍历当前目录下的文件,获取并打印每个文件的权限模式、组ID、修改时间、权限表示字符串、链接数和大小信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

char* get_filePermission(mode_t m)
{
	static char getPer[10]="";
	char per[]="rwx";
	
	for(int i=0;i<=8;i++)
	{
		if((m&(0400>>i))==0)
		{
			getPer[i]='-';
			continue;
		}
		getPer[i]=per[i%3];
	
	}
 	return getPer;
}
int main(int argc, const char *argv[])
{
 DIR *dp = opendir("./");
	
    if(NULL == dp)
    {
        perror("opendir");
        return -1;
    }

    int count = 1;
    struct dirent* rp = NULL;
	int fd;
	struct stat buf;
    while(1)
    {
        rp = readdir(dp);
        if(NULL == rp)
        {
            if(0 == errno)
            {
				printf("目录文件读取完毕\n");
				break;                             
			}
			else
			{
				perror("readdir");
				return -1;
			}
		}
		if((rp->d_name)[0]=='.')
		{
			continue;
		}

		printf("[%d] %s\n", count++, rp->d_name);
		if(stat(rp->d_name,&buf)<0)
		{
			perror("stat");
			return -1;
		}
		printf("mode:%o\n",buf.st_mode);
		printf(" %d\n",buf.st_gid);

		printf("time:%ld\n",buf.st_ctime);

		printf("%s\n",get_filePermission(buf.st_mode));
		printf(" %ld",buf.st_nlink);
		printf(" %ld ",buf.st_size);
		putchar(10);
		putchar(10);
		putchar(10);
	

	}
	return 0;
}

结果:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值