按树的形状列出目录中内容 (程序清单4-7)
2012-03-12 13:51
429 查看
结果:/home/suqin/apuetest
[b]代码:未考虑通用性和文件目录访问权限问题
[/b]
[b]代码:未考虑通用性和文件目录访问权限问题
[/b]
#include"apue.h" #include<dirent.h> unsigned int total,reg,dir,chr,blk,fifo,lnk,sock; int traverse(char *pathname,int dep) { DIR *dp; struct dirent *dirp; int depth=dep,i; char tmp[500],tmp2[500]; strcpy(tmp,pathname); struct stat buf; if( (dp=opendir(pathname))==NULL) err_sys("opendir %s error!",tmp); while( (dirp=readdir(dp))!=NULL) { if(strcmp(dirp->d_name,".")==0) continue; if(strcmp(dirp->d_name,"..")==0) continue; strcpy(tmp2,tmp); strcat(tmp2,"/"); strcat(tmp2,dirp->d_name); for(i=0;i<depth;i++) printf("| "); printf("|----%s\n",dirp->d_name); if(lstat(tmp2,&buf)<0){ err_ret("lstat error:%s",tmp2); continue;} if(S_ISREG(buf.st_mode)) {reg++;continue;} else if(S_ISCHR(buf.st_mode)) {chr++;continue;} else if(S_ISBLK(buf.st_mode)) {blk++;continue;} else if(S_ISFIFO(buf.st_mode)) {fifo++;continue;} else if(S_ISLNK(buf.st_mode)) {lnk++;continue;} else if(S_ISSOCK(buf.st_mode)) {sock++;continue;} else if(S_ISDIR(buf.st_mode)) { dir++; traverse(tmp2,depth+1); continue; } else printf("No such file style!\n"); } return 0; } int main(int argc,char *argv[]) { if(argc!=2) err_sys("argc!=2"); printf("%s\n",argv[1]); traverse(argv[1],0); printf("\n reg=%d\n dir=%d\n chr=%d\n blk=%d\n fifo=%d\n sock=%d\n",reg,dir,chr,blk,fifo,sock); }
相关文章推荐
- 第一章 列出一个目录中所有文件 程序清单1-1
- unix 环境高级编成 程序清单1-1 列出一个目录中所有的文件
- win7配置iis 出现:HTTP 错误 403.14 - Forbidden Web 服务器被配置为不列出此目录的内容
- HTTP 错误 403.14 - Forbidden Web 服务器被配置为不列出此目录的内容
- HTTP 错误 403.14 - Forbidden Web 服务器被配置为不列出此目录的内容
- 解决【HTTP 错误 403.14 - Forbidden Web 服务器被配置为不列出此目录的内容】
- GUI初探--列出指定目录的内容
- 解决【HTTP 错误 403.14 - Forbidden Web 服务器被配置为不列出此目录的内容】
- HTTP 错误 403.14 - Forbidden Web 服务器被配置为不列出此目录的内容
- Mac程序开发基础:得到一个目录内的内容
- .net 发布mvc报错:403.14-Forbidden Web 服务器被配置为不列出此目录的内容
- iis7 发布mvc 遇到的HTTP错误 403.14-Forbidden Web 服务器被配置为不列出此目录的内容
- Java基础---Java---IO流-----File 类、递归、删除一个带内容的目录、列出指定目录下文件夹、FilenameFilte
- iis7 发布mvc 遇到的HTTP错误 403.14-Forbidden Web 服务器被配置为不列出此目录的内容
- Linux tree - 以树状图列出目录的内容
- iis7 发布mvc3 遇到的HTTP错误 403.14-Forbidden Web 服务器被配置为不列出此目录的内容及Login on failed for "IIS APPPOOL\ASP.NET v4.0"问题
- iis 6发布webservice服务报错:HTTP 错误 403.14 - Forbidden Web 服务器被配置为不列出此目录的内容。
- 发布mvc报错:403.14-Forbidden Web 服务器被配置为不列出此目录的内容
- PHP 实现列出目录的内容
- IIS7错误:“Web服务器被配置为不列出此目录的内容”的解决办法