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

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





//文件的权限

char *get_filepermission(mode_t mode,char p[10])
{
	int a=0400;
	char b[]="xrw";
	for(int i=1;i<=9;i++)
	{
		if((mode&a)!=0)
		{
		//	putchar(b[i%3]);
			p[i-1]=b[i%3];
		}
		else
		{
		//	putchar('-');
			p[i-1]='-';
		}
		a=a>>1;
	}
//	puts("");
	return p;
}



//文件的类型

char get_fileType(mode_t mode)
{
	if(S_ISREG(mode))
	{
		return '-';	
	}
	else if(S_ISDIR(mode))
	{
		return 'd';
	}
	else if(S_ISCHR(mode))
	{
		return 'c';
	}
	else if(S_ISBLK(mode))
	{
		return 'b';
	}
	else if(S_ISFIFO(mode))
	{
		return 'p';
	}	
	else if(S_ISLNK(mode))
	{
		return 'l';
	}	
	else if(S_ISSOCK(mode))
	{
		return 's';
	}	
}

//文件的所属用户


void getp(mode_t mode)
{
	struct passwd *pass=getpwuid(mode);
	if(NULL==pass)
	{
		perror("pass");
		return ;
	}
	printf(" %s",pass->pw_name);
}

	//文件的所属组用户


void getg(mode_t mode)
{
	struct group *pass=getgrgid(mode);
	if(NULL==pass)
	{
		perror("pass");
		return ;
	}
	printf(" %s",pass->gr_name);
}

	//文件的时间

void loca(time_t mode)
{
	struct tm *p=localtime(&mode);
	printf(" %d %d %d:%d",p->tm_mon+1,p->tm_mday,\
						p->tm_hour,p->tm_min);

}


功能函数文件^

#ifndef __STAFUN_H__
#define __STAFUN_H__

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



char *get_filepermission(mode_t mode,char p[10]);
char get_fileType(mode_t mode);
void getp(mode_t mode);
void getg(mode_t mode);
void loca(time_t mode);



#endif

声明文件

#include<stdio.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<unistd.h>
#include<fcntl.h>
#include<grp.h>
#include<pwd.h>
#include<time.h>
#include"stafun.h"
#include<dirent.h>
#include<errno.h>
/*char *get_filepermission(mode_t mode,char p[10])
{
	int a=0400;
	char b[]="xrw";
	for(int i=1;i<=9;i++)
	{
		if((mode&a)!=0)
		{
		//	putchar(b[i%3]);
			p[i-1]=b[i%3];
		}
		else
		{
		//	putchar('-');
			p[i-1]='-';
		}
		a=a>>1;
	}
//	puts("");
	return p;
}
char get_fileType(mode_t mode)
{
	if(S_ISREG(mode))
	{
		return '-';	
	}
	else if(S_ISDIR(mode))
	{
		return 'd';
	}
	else if(S_ISCHR(mode))
	{
		return 'c';
	}
	else if(S_ISBLK(mode))
	{
		return 'b';
	}
	else if(S_ISFIFO(mode))
	{
		return 'p';
	}	
	else if(S_ISLNK(mode))
	{
		return 'l';
	}	
	else if(S_ISSOCK(mode))
	{
		return 's';
	}	
}
void getp(mode_t mode)
{
	struct passwd *pass=getpwuid(mode);
	if(NULL==pass)
	{
		perror("pass");
		return ;
	}
	printf(" %s",pass->pw_name);
}
void getg(mode_t mode)
{
	struct group *pass=getgrgid(mode);
	if(NULL==pass)
	{
		perror("pass");
		return ;
	}
	printf(" %s",pass->gr_name);
}
void loca(time_t mode)
{
	struct tm *p=localtime(&mode);
	printf(" %d %d %d:%d",p->tm_mon+1,p->tm_mday,\
						p->tm_hour,p->tm_min);

}
*/
int main(int argc, const char *argv[])
{

	char *p[100]={NULL};
	int i=0;
	int len=0;
	DIR *dp=opendir(argv[1]);
	if(NULL==dp)
	{
		perror("opendir");
		return -1;
	}
	struct dirent *pr=NULL;
	//	int n=0;
	while(1)
	{
		pr=readdir(dp);
		if(NULL==pr)
		{
			if(errno!=0)
			{
				perror("reddir");
				return -1;
			}
			else
			{
				printf("读取完毕\n");
				break;
			}
		}
		//	n=strcmp(pr->d_name,"..");
		if(pr->d_name[0]=='.')
		{
			continue;
		}
		printf("%s\n",pr->d_name);

		p[i]=pr->d_name;
		i++;
		len++;
	}




//	int  i=0;
//	int len =3;
	struct stat buf;
	for(i=0;i<len;i++)
	{
		if(stat(p[i],&buf)<0)
		{
			perror("seat");
			return -1;
		}
		//文件的类型及权限
		//	printf("mode:%o\n",buf.st_mode);
		char n=get_fileType(buf.st_mode);
		putchar(n);
		char arr[10]="";
		char *p1= get_filepermission(buf.st_mode,arr);
		//	puts(p);
		printf("%s",p1);

		//文件的硬链接数
		printf(" %ld",buf.st_nlink);

		//文件的所属用户
		//	printf("uid:%d\n",buf.st_uid);
		getp(buf.st_uid);

		//文件的所属组用户
		//	printf("gid:%d\n",buf.st_gid);
		getg(buf.st_gid);

		//文件的大小
		printf("  %ld",buf.st_size);
		//文件的时间
		//	printf("time:%ld\n",buf.st_ctime);
		loca(buf.st_ctime);
		//文件名
		printf(" %s\n",p[i]);
	}
	return 0;
}


man函数^

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值