IO线程进程 day3

#include
char * file_per(mode_t m)
{
 	static char p[10]="";
	char a[4]="rwx";
	for(int i=0;i<9;i++)
	{
		if((m&(0400>>i))==0)
		{
			p[i]= '-';
			continue;
		}
		 p[i] = a[i%3];
	}
	return p;
}
char file_type(mode_t m)
{
	char p[10]="bcd-lsp";
		switch (m & S_IFMT) 
		{
		case S_IFBLK:  return p[0];     
		case S_IFCHR:  return p[1];
		case S_IFDIR:  return p[2];           
		case S_IFREG:  return p[3];
		case S_IFLNK:  return p[4];          
		case S_IFSOCK: return p[5];   
		case S_IFIFO:  return p[6];        
		default:   printf("unknown?\n");
		}
}

int main(int argc,const char *argv[])
{
	DIR *fd=opendir(argv[1]);
	if(NULL==fd)
	{
		ERR_MSG("opendir");
		return -1;
	}
	struct dirent *read=NULL;
	while(1)
	{
		read=readdir(fd);
		if(NULL==read)
			break;
		if('.'!=read->d_name[0])
		{
			struct stat buf;
			char file_name[40]="";
			strcpy(file_name,argv[1]);
			strcat(file_name,read->d_name);
		//	printf("%s\n",file_name);
		    int a=stat(file_name,&buf);
			if(a==-1)
			{ 
				ERR_MSG("stat");
				return -1;
			}
			char type=file_type(buf.st_mode);
		//	printf("%c",type);
			char *per=file_per(buf.st_mode);
		//	printf("%s\n",per);
			struct passwd *pwd=getpwuid(buf.st_uid);
			if(NULL==pwd)
			{
				ERR_MSG("pwd");
				return -1;
			}
		//	printf("%s\n",pwd->pw_name);
			struct group *grp=getgrgid(buf.st_gid);
			if(NULL==grp)

			{
				ERR_MSG("grp");
				return -1;
			}
		//	printf("%s\n",grp->gr_name);
			struct tm *time=localtime(&buf.st_ctime);
			if(NULL==time)
			{
				ERR_MSG("time");
				return -1;
			}
		//	printf("%d月 %2d %d:%-2d\n",time->tm_mon+1,time->tm_mday,time->tm_hour,time->tm_min);
		    printf("%c%s %ld %s %s %6ld %d月 %2d %02d:%02d %-10s\n",type,per,buf.st_nlink,\
					pwd->pw_name,grp->gr_name,buf.st_size,time->tm_mon+1,time->tm_mday,\
					time->tm_hour,time->tm_min,read->d_name);
		}
	}
	closedir(fd);
	return 0;
}

你可能感兴趣的:(linux,运维,服务器)