IO进程线程day3

#include 
void get_per(mode_t m);
void get_type(mode_t m);
void file_msg(struct stat buf);
int main(int argc, const char *argv[])
{
	char filepath[30];
	printf("please enter filepath:");
	scanf("%s",filepath);
	DIR *pd = opendir(filepath);
	if(NULL == pd){
		ERR_MSG("opendir");
		return -1;
	}
	char file_name[20];
	struct dirent *rp = NULL;
	while(1){
		rp = readdir(pd);
		if(NULL == rp){
			break;
		}
		char file_path[100];
		bzero(file_path,sizeof(file_path));
		strcpy(file_path,filepath);
		strcpy(file_name,rp->d_name);
		strcat(file_path,file_name);
		if('.' != rp->d_name[0]){
			struct stat buf;
			if(stat(filepath, &buf) < 0){
				ERR_MSG("stat");
				return -1;
			}

			file_msg(buf);
			printf(" %s\n",rp->d_name);
		}
	}
	closedir(pd);
}
void get_per(mode_t m)
{
	int i = 0400;
	while(i){
		if(m & i)
			putchar('r');
		else
			putchar('-');
		if(m & i>>1)
			putchar('w');
		else 
			putchar('-');
		if(m & i>>2)
			putchar('x');
		else
			putchar('-');
		i = i>>3;
	}
}
void get_type(mode_t m)
{
	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("unknown?\n");  break;
	}

}
void file_msg(struct stat buf)
{
	//文件的类型及权限
	get_type(buf.st_mode);
	get_per(buf.st_mode);
	//文件的硬链接个数
	printf("%2ld",buf.st_nlink);
	//文件所属用户的id号
	struct passwd *pwd = getpwuid(buf.st_uid);
	if(NULL == pwd){
		perror("getpwuid");
		return;
	}
	printf(" %s",pwd->pw_name);
	struct group *grp = getgrgid(buf.st_gid);
		if(NULL == grp){
			perror("getgrgid");
			return;
		}
	printf(" %s",grp->gr_name);
	//文件大小
	printf(" %6ld",buf.st_size);
	//时间
	struct tm *t = localtime(&buf.st_mtime);
	printf("%2d月%2d %02d:%02d",t->tm_mon+1,t->tm_mday,t->tm_hour,t->tm_min);
}

IO进程线程day3_第1张图片

你可能感兴趣的:(c#)