C++DAY28

#include "head.h"

char* get_Per(mode_t m,char ptr[])
{
	char per[]="rwx";
	
	int i;
	for(i=0;i<9;i++)
	{
		if((m & (0400>>i))==0)
		{
		//	printf("-");
			ptr[i]='-';
			continue;
		}
		else 
			//能运行到当前位置,则代表结果不为0,则需要打印 r w x
		{
		//	putchar(per[i%3]);
			ptr[i]=per[i%3];
		/*	if(i==0||i==3 ||i==6)
				printf("r");
			else if(i==1||i==4||i==7)
				printf("w");
			else
				printf("x");*/
		}
	}
//	putchar(10);
	return ptr;

}
void get_type(mode_t m)   //mode_t m = buf.st_mode
{
	switch (m & S_IFMT) {
	case S_IFBLK:  printf("b");          break;
	case S_IFCHR:  printf("c");          break;
	case S_IFDIR:  printf("d");          break;
	case S_IFIFO:  printf("p");          break;
	case S_IFLNK:  printf("l");          break;
	case S_IFREG:  printf("-");          break;
	case S_IFSOCK: printf("s");          break;
	default:       printf("error\n"); 	 break;
	}
	return ; 
}
int file_msg(struct stat buf)
{
	char str[9]="";
	get_Per(buf.st_mode,str);
	get_type(buf.st_mode);
	printf("%s ",str);
	printf("%2ld",buf.st_nlink);//文件的硬链接数
//	printf("uid:%d\n",buf.st_uid);//文件所属用户的id号
	struct passwd* pwd = getpwuid(buf.st_uid);
	if(NULL == pwd)
	{
		perror("pwd\n");
		return -1;
	}
	printf("%s",pwd->pw_name);
	putchar(' ');
//	printf("gid:%d\n",buf.st_gid);
	struct group* grp = getgrgid(buf.st_gid);
	if(NULL == grp)
	{
		perror("grp\n");
		return -1;
	}
	printf("%s",grp->gr_name);
	printf(" %-2ld",buf.st_size);//文件大小
	time_t t1;
	struct tm* info = localtime(&buf.st_mtime);
	printf(" %02d %d %02d:%02d ",info->tm_mon+1,info->tm_mday,info->tm_hour,info->tm_min);

}
int main(int argc, const char *argv[])
{
	DIR* dp=opendir("./");
	if(NULL == dp)
	{
		perror("opendir");
		return -1;
	}
	struct dirent * rp =NULL;
	while(1)
	{
		rp = readdir(dp);
		if(NULL == rp)
		{
			break;
		}
		else if('.' != rp->d_name[0])
		{
			struct stat buf;
			if(stat(rp->d_name,&buf)<0)
			{
				ERR_MSG("stat");
				return -1;
			}
			file_msg(buf);
			printf("%s\n",rp->d_name);
		}
	}
	closedir(dp);
	return 0;
}

C++DAY28_第1张图片 

 

你可能感兴趣的:(c++,开发语言)