IOday3作业

 IOday3作业_第1张图片

IOday3作业_第2张图片

#include 
int get_filePerrmison(mode_t mode)//获取文件权限
{
	char per[] ="rwx";
	for(int i=0;i<9;i++)
	{
		if((mode&(0400>>i))==0)
		{
			putchar('-');
			continue;
		}
		putchar(per[i%3]);
	}

}
int get_fileType(mode_t m) //获取文件类型
{
	switch(m&S_IFMT)
	{
	case S_IFSOCK: putchar('s');break;
	case S_IFLNK: putchar('l');break;
	case S_IFREG: putchar('-');break;
	case S_IFBLK: putchar('b');break;
	case S_IFDIR: putchar('d');break;
	case S_IFCHR: putchar('c');break;
	case S_IFIFO: putchar('p');break;

	}

}




int main(int argc, const char *argv[])
{
	printf("请输入目录名:\n");
	char arr[50]="";
	scanf("%s",arr);
	char tmp[50]="";
	strcpy(tmp,arr);

	DIR* dirp=opendir(arr);
    if(NULL==dirp)
    {
        ERR_MSG("opendir");
		return -1;
    }
    struct dirent*rp=NULL;//读取路径下目录
    while(1)
    {
		bzero(arr,sizeof(arr));
		strcpy(arr,tmp);
		rp =readdir(dirp);
        if(NULL==rp)
        {
            if(0==errno)
            {
               
                break;
            }
            else
            {
                ERR_MSG("readdir");
            }
		}
		if('.'==rp->d_name[0])
		{
			continue;
		}
		struct stat buf;
		
		if(stat(strcat(arr,rp->d_name),&buf)<0)
		{
			ERR_MSG("open");
			return -1;
		}
	
		struct tm* t;
		t=localtime(&buf.st_ctime);

		get_fileType(buf.st_mode);//提取文件类型
		get_filePerrmison(buf.st_mode);//提取文件权限
		printf(" %5lu",buf.st_nlink);//打印硬链接个数
		struct passwd *pwd=getpwuid(buf.st_uid);
		if(NULL==pwd)
		{
			ERR_MSG("getpwuid");
			return -1;
		}
		int m=sizeof(pwd->pw_name);
		printf(" %-*s",m,pwd->pw_name);
		struct group *grp=getgrgid(buf.st_gid);
		if(NULL==grp)
		{
			ERR_MSG("getgrgid");
			return -1;
		}
		m=sizeof(grp->gr_name);
		printf(" %-*s",m,grp->gr_name);
		m=sizeof(buf.st_size)-1;
		printf("%*ld",m,buf.st_size);
		char *month_ch[]={"一","二","三","四","五","六","七","八","九","十","十一","十二"};
		printf(" %s月",month_ch[t->tm_mon]);
		printf(" %02d %02d:%02d",t->tm_mday,t->tm_hour,t->tm_min);
		printf(" %s",rp->d_name);
		putchar(10);
	}
	return 0;

}        

你可能感兴趣的:(c++,数据结构,c语言)