日期:2014-05-16 浏览次数:20797 次
#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<dirent.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void print_dir(char *dir,int depth)
{
DIR *dp=opendir(dir);
struct dirent *dirent;
struct stat stat_buf;
if(dp==NULL)
{
fprintf(stderr,"opendir function is error\n");
exit(EXIT_FAILURE);
}
chdir(dir);
while((dirent=readdir(dp))!=NULL)
{
lstat(dirent->d_name,&stat_buf);
if(S_ISDIR(stat_buf.st_mode))
{
if(strcmp(".",dirent->d_name)==0||
strcmp("..",dirent->d_name)==0)
continue;
printf("%*s%s\n",depth," ",dirent->d_name);
print_dir(dirent->d_name,depth+4);
}
else
printf("%*s%s\n",depth," ",dirent->d_name);
}
//chdir("..");如果去掉这里
closedir(dp);
}
int main(int argc,char *argv[])
{
if(argc==1)
print_dir(".",0);
else
{
while(--argc)
{
printf("%s:\n",argv[argc]);
print_dir(argv[argc],0);
}
}
exit(EXIT_SUCCESS);
}