输入目录路径以及名字,能够将该路径下所有文件的属性打印出来,类似ls -l

输入目录路径以及名字,能够将该路径下所有文件的树形打印出来,类似ls -l
 

主函数代码:

#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <dirent.h>
#include <errno.h>
#include "filepermision.h"
int main(int argc, const char *argv[])
{
	struct stat buf;
	//打开一个目录
	DIR *dp=opendir("../3day/");
	if(NULL==dp){
		perror("opendir");
		return -1;
	}
	struct dirent* rp=NULL;
	while(1){
		rp = readdir(dp);
		if(NULL==rp){
			if(errno != 0){
				perror("readdir");
				return -1;
			}else{
				printf("目录读取完毕\n");
				break;
			}
		}
		if(rp->d_name[0] =='.'){
			continue;
		}
		stat(rp->d_name,&buf);
		//文件的类型以及权限
		char type=get_filetype(buf.st_mode);
		printf("%c",type);
		get_filepermission(buf.st_mode);
		putchar(' ');
		//文件的硬链接数
		printf("%ld ",buf.st_nlink);
		//文件所属用户
		get_usrname(buf.st_uid);
		//文件所属组用户
		get_groupname(buf.st_gid);
		//文件的大小
		printf("%6ld ",buf.st_size);
		//文件的时间
		get_time(&buf.st_ctime);
		printf("%s\n",rp->d_name);
	}
	if(closedir(dp)<0){
		perror("closedir");
		return -1;
	}
	return 0;
}

定义函数代码:

#include <stdio.h>
#include <fcntl.h>
#include <sys/types.h>
#include <pwd.h>
#include <grp.h>
#include <time.h>

void get_filepermission(mode_t mode){
	int a=0400;
	char str[3]={'r','w','x'};
	for(int i=0;i<3;i++){
		for(int j=0;j<3;j++){
			if((mode & a) != 0){
				printf("%c",str[j]);
			}else{
				putchar('-');
			}
			a=a>>1;	
		}
	}
}

char get_filetype(mode_t mode){
	char c=0;
	switch(mode & S_IFMT){
	case S_IFSOCK: 
		c = 's'; 
		break;
	case S_IFLNK: 
		c = 'l'; 
		break;
	case S_IFREG: 
		c = '-'; 
		break;
	case S_IFBLK: 
		c = 'b'; 
		break;
	case S_IFDIR: 
		c = 'd'; 
		break;
	case S_IFCHR: 
		c = 'c'; 
		break;
	case S_IFIFO: 
		c = 'p'; 
		break;
	default:
		printf("error\n");
		break;
	}
	return c;
}

void get_usrname(uid_t st_uid){
	struct passwd *pwd=getpwuid(st_uid);
	if(NULL==pwd){
		perror("getpwuid");
		return;
	}
	printf("%s ",pwd->pw_name);
}

void get_groupname(gid_t st_gid){
	struct group *grp=getgrgid(st_gid);
	if(NULL==grp){
		perror("getgrgid");
		return;
	}
	printf("%s ",grp->gr_name);
}
void get_time(time_t *timep){
	struct tm* info=NULL;
	info=localtime(timep);
	switch(info->tm_mon+1)
	{
		case 1:
			printf("一  %d %d:%d ",info->tm_mday,info->tm_hour,info->tm_min);
			break;
		case 2:
			printf("二  %d %d:%d ",info->tm_mday,info->tm_hour,info->tm_min);
			break;
		case 3:
			printf("三  %d %d:%d ",info->tm_mday,info->tm_hour,info->tm_min);
			break;
		case 4:
			printf("四  %d %d:%d ",info->tm_mday,info->tm_hour,info->tm_min);
			break;
		case 5:
			printf("五  %d %d:%d ",info->tm_mday,info->tm_hour,info->tm_min);
			break;
		case 6:
			printf("六  %d %d:%d ",info->tm_mday,info->tm_hour,info->tm_min);
			break;
		case 7:
			printf("七  %d %d:%d ",info->tm_mday,info->tm_hour,info->tm_min);
			break;
		case 8:
			printf("八  %d %d:%d ",info->tm_mday,info->tm_hour,info->tm_min);
			break;
		case 9:
			printf("九  %d %d:%d ",info->tm_mday,info->tm_hour,info->tm_min);
			break;
		case 10:
			printf("十  %d %d:%d ",info->tm_mday,info->tm_hour,info->tm_min);
			break;
		case 11:
			printf("十一  %d %d:%d ",info->tm_mday,info->tm_hour,info->tm_min);
			break;
		case 12:
			printf("十二  %d %d:%d ",info->tm_mday,info->tm_hour,info->tm_min);
			break;
		default:
			printf("error\n");
			break;
	}
}

头文件:

#ifndef __FILEPERMISION_H__
#define __FILEPERMISION_H__

void get_filepermission(mode_t mode);
char get_filetype(mode_t mode);
void get_usrname(uid_t st_uid);
void get_groupname(gid_t st_gid);
void get_time(time_t *timep);

#endif

运行结果:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值