要求输入目录的路径后,能够打印出指定路径下所有文件的详细信息,类似ls -l

//主函数
#include "6.h"
#include <dirent.h>
#include <errno.h>

int main(int argc, const char *argv[])
{
	char i[31]="";
  DIR* dir;
    struct dirent* dt=NULL;
    // 1.打开目录
    if ((dir = opendir(argv[1])) == NULL)
	{
        perror("open dir error");
			return -1;
	}
	while((dt=readdir(dir))!= NULL)
	{
		if(dt==NULL)
		{
			if(errno==0)
			{
				printf("1");
			}
			else
			{
				perror("readdir");
				return -1;
			}
		}
        strcpy(i,argv[1]);
		strcat(i,dt->d_name);
		//printf("%s\n",i);
		hfwj(i);


	}
	return 0;
}
//功能函数
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <pwd.h>
#include <grp.h>
#include <time.h>
char *get_file(mode_t m,char pt[10])
{
	char c[4]="rwx";
	int i=0;
	//int t=0400;
	while(i<9)
	{
		if((m & (0400>>i))==0)
		{
			pt[i]='-';
			i++;
			continue;
		}

		pt[i]=c[i%3]; 
		i++;
	}
	return pt;
}
//提取文件类型
char get_wjm(mode_t m)
{
	switch(m&S_IFMT)
	{
	case S_IFSOCK: return('s'); break;
	case S_IFLNK: return('l'); break;
	case S_IFREG: return('-'); break;
	case S_IFDIR: return('d'); break;
	case S_IFBLK: return('b'); break;
	case S_IFCHR: return('c'); break;
	case S_IFIFO: return('p'); break;
	default:return 0;
	}
	}
char *get_ssyhm(uid_t uid,char *pt)
{
	struct passwd *pwd =getpwuid(uid);
	if(pwd == NULL)
	{
		return NULL;
	}
	strcpy(pt,pwd->pw_name);
	return pwd->pw_name;
}
char *get_sszyh(uid_t gid,char *pt)
{
	struct group *grp=getgrgid(gid);
	if(grp == NULL)
	{
		return NULL;
	}
	strcpy(pt,grp->gr_name);
	return pt;
}
void get_time(long int a)
{
	struct tm *info=NULL;
	info= localtime(&a);
	printf("%02d %02d %02d %02d:%02d",\
			info->tm_mon+1,info->tm_mday,\
			info->tm_hour,info->tm_min,info->tm_sec);
}
	int hfwj(char *p)
	{
	
		struct stat buf;
		if(stat(p,&buf)<0)
		{
			perror("stat");
			return -1;
		}
		
        char b=0;
		char c[10]="";
		char name1[20]="";
		char name2[20]="";
		//printf("st_mode :%o\n",buf.st_mode);
        b=get_wjm(buf.st_mode);
	    printf(" %c ",b);
		get_file(buf.st_mode,c);
		printf(" %s ",c);
		get_ssyhm(buf.st_uid,name1);
		printf(" %s ",name1);
    	get_sszyh(buf.st_gid,name2);
		printf(" %s ",name2);
		//printf("st_nlink :%ld\n",buf.st_nlink);
		//printf("st_mode :%d\n",buf.st_uid);	
		//printf("st_mode :%d\n",buf.st_gid);
		printf(" %ld ",buf.st_size);
	//	printf("st_mode :%ld\n",buf.st_ctime);
        get_time(buf.st_ctime);
		printf(" %s\n ",p);
		return 0;
	}
//头文件
#ifndef __6_H__
#define __6_H__
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <pwd.h>
#include <grp.h>
#include <time.h>
char *get_file(mode_t m,char pt[10]);
char get_wjm(mode_t m);
char *get_ssyhm(uid_t uid,char *pt);
char *get_sszyh(uid_t gid,char *pt);
void get_time(long int a);
int hfwj(char *p);
#endif 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值