按树的形状列出目录中内容 (程序清单4-7)
来源:互联网 发布:凤凰炒股大赛软件 编辑:程序博客网 时间:2024/06/06 19:49
结果:/home/suqin/apuetest
代码:未考虑通用性和文件目录访问权限问题
#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);}
- 按树的形状列出目录中内容 (程序清单4-7)
- unix 环境高级编成 程序清单1-1 列出一个目录中所有的文件
- 禁止apache和tomcat列出目录清单的方法
- 禁止apache和tomcat列出目录清单的方法
- php列出"目录下文件清单"的函数
- PHP 实现列出目录的内容
- JAVA GUI(列出指定目录内容)
- PHP列出目录内容
- (17)列出目录内容
- GUI列出目录内容
- Java列出数额的清单-DOS小程序
- 列出文件内容的程序-java实现
- 列出当前目录下的目录树
- ~分析apue程序清单4-7 递归降序遍历目录层次结构,并按文件类型计数 。
- 禁止Apache列出目录内容
- 禁止Apache列出目录内容
- 禁止Apache列出目录内容
- 禁止Apache列出目录内容
- PHP 数组 Array
- PHP 字符串处理
- Java编程中“为了性能”尽量要做到的一些地方
- 一个sql 错误 :数字溢出
- NSDate的常用用法
- 按树的形状列出目录中内容 (程序清单4-7)
- oracle10.2.0.1下载链接
- 新手如何学习一门新的语言
- 大端法、小端法、网络字节序(转)
- CentOS 6.0 网卡识别配置问题
- C# Listbox的使用
- Android---快速拨话程序(涵盖appWidget、Tab、联系人、多线程、自定义ListView使用)
- Android学习笔记
- PHP 时间戳 timestamp