欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Linux下如何实现获取当前路径文件属性

程序员文章站 2022-04-29 19:54:54
...

想要实现这样的效果:Linux下如何实现获取当前路径文件属性
readdir获取目录
判断解析权限位
lstat获取具体信息
获取时间

int list_common(void)
{  
	DIR *dir = opendir(".");  
	if(dir==NULL){      
		return 0;
	}
	//遍历  
	struct dirent* dt;  
	struct stat sbuf;  
	while((dt = readdir(dir))!=NULL)    //当前目录{       
		if(lstat(dt->d_name,&sbuf)<0)//获取文件状态,保存到第二个参数中     
		{		           
			continue;     
		}            
		//过滤 .xxx 文件       
		if(dt->d_name[0]=='.'){           
			continue;       
		}       
		//开始解析权限位 drwxr-xr-x      
		char perms[]="----------";
		perms[0]='?';       
		mode_t mode = sbuf.st_mode;       
		switch (mode&S_IFMT)     {       
			case S_IFREG:  
			perms[0]='-';           
			break;       
			case S_IFDIR:       
			perms[0]='d'; 
			break; 
			case S_IFLNK:        
			perms[0]='l';           
			break;
			case S_IFIFO:       
			perms[0]='p'   
			break;     
			case S_IFSOCK:           
			perms[0]='s';           
			break;
			case S_IFCHR:        
			perms[0]='c';
			break;       
			case S_IFBLK:           
			perms[0]='b';          
			break;   
			default:   
			break;       
		}
   
	if(mode&&S_IRUSR)   
	{     
		perms[1]='r';
	}
	if(mode&&S_IWUSR)
	{        
		perms[2]='w';   
	}      
	if(mode&&S_IXUSR)    
	{           
		perms[3]='x';       
	}        
	if(mode&&S_IRGRP)
	{
          	  perms[4]='r';       
	}
	if(mode&&S_IWGRP)   
	{        
		perms[5]='w';    
	}      
	if(mode&&S_IXGRP)     
	{         
		perms[6]='x';   
	}     
	if(mode&&S_IROTH)
	{       
		perms[7]='r';   
	}    
	if(mode&&S_IWOTH)   
	{
		perms[8]='w';       
	}
	if(mode&&S_IXOTH)
	{         
		perms[9]='x';   
	}   
	if(mode&S_ISUID)    
	{
		perms[3]=(perms[3]=='x')?'s':'S';
	}   
	if(mode&S_ISGID)       
	{           
		perms[6]=(perms[6]=='x')?'s':'S';       
	}       
	if(mode&S_ISVTX)       
	{           
		perms[9]=(perms[9]=='x')?'t':'T';       
	}
	//硬连接数  st_nlink 
    
	char buf[1024] = {0};    
	int off = 0;   	
	off += sprintf(buf,"%s",perms);    
	off += sprintf(buf+off,"%3ld %-8d %-8d",sbuf.st_nlink,sbuf.st_uid,sbuf.st_gid);    
	off += sprintf(buf+off,"%8lu ",sbuf.st_size);
	//时间格式     	
	const char *p_date_format = "%b %e %H:%M";    
	struct timeval tv;
	gettimeofday(&tv,NULL);     //获取系统时间   
	time_t local_time = tv.tv_sec;     
	if(sbuf.st_mtime>local_time||(local_time-sbuf.st_mtime)>60*60*24*182)       
	{           
		p_date_format =  "%b  %e  %Y";
	}
	char databuf[64]={0};       
	struct tm*p_tm = localtime(&local_time);      
	strftime(databuf,sizeof(databuf),p_date_format,p_tm);       
	off += sprintf(buf+off,"%s",databuf);               
	//文件名  要判断是否是链接文件            
	if(S_ISLNK(sbuf.st_mode)){           
		char tmp[1024] = {0};         	
		readlink(dt->d_name,tmp,sizeof(tmp));           
		off += sprintf(buf+off,"%s -> %s\r\n",dt->d_name,tmp);      
	}else{           
		off += sprintf(buf+off,"%s\r\n",dt->d_name);
	}
	printf("%s",buf);
        }
	closedir(dir);
	return 1;
}

相关标签: linux编程专栏