荔园在线
荔园之美,在春之萌芽,在夏之绽放,在秋之收获,在冬之沉淀
[回到开始]
[上一篇][下一篇]
发信人: beike (***睡务局***), 信区: Linux
标 题: show_dir
发信站: 荔园晨风BBS站 (Tue Apr 2 22:20:08 2002), 转信
//**有两个东东我看不懂,斑竹帮忙解析一下。顺便帮忙调试一下
#include <unistd.h>
#include <stdio.h>
#include <dirent.h>
#include <string.h>
#include <sys/stat.h>
void show_dir(char *dir,int depth)
{
DIR *dp;
struct dirent *entry;
struct stat statbuf;
if((dp=opendir(dir))==NULL){
fprintf(stderr,"cannot open directory: %s\n",dir);
return;
}
chdir(dir);
while((entry=readdir(dp))!=NULL){
stat(entry->d_name,&statbuf); //stat有什么作用?
if(S_ISDIR(statbuf,st_mode)){ //S_ISDIR的作用是什么?
if(strcmp(".",entry->d_name)==0||
strcmp("..",entry->d_name)==0)
continue;
printf("%*s%s/\n",depth,"",entry->d_name);
show_dir(entry->d_name,depth+4);
}
else printf("%*s%s\n",depth,".",entry->d_name);
}
chdir("..");
closedir(dp);
}
int main()
{
printf("show directory:\n");
show_dir("/home/init",0);
printf("done.");
exit(0);
}
--
※ 来源:·荔园晨风BBS站 bbs.szu.edu.cn·[FROM: 211.66.99.53]
[回到开始]
[上一篇][下一篇]
荔园在线首页 友情链接:深圳大学 深大招生 荔园晨风BBS S-Term软件 网络书店