Linux 扫描目录
时间:2010-11-12 来源:ycdoit
1
2#include<unistd.h>
3#include<stdio.h>
4#include<dirent.h>
5#include<sys/stat.h>
6#include<stdlib.h>
7
8
9void printdir(char *dir , int depth)
10{
11DIR *dp;
12struct dirent * entry;
13struct stat statbuf;
14
15if((dp = opendir(dir))==NULL)
16{
17fprintf(stderr,"cannot open directory: %s\n",dir);
18return;
19}
20chdir(dir);
21while((entry = readdir(dp)) !=NULL )
22{
23lstat(entry->d_name,&statbuf);
24if( S_ISDIR(statbuf.st_mode))
25{
26if(strcmp(".",entry->d_name)==0 || strcmp("..",entry->d_name)==0)
27continue;
28printf("%*s%s/\n",depth,"",entry->d_name);
29printdir(entry->d_name,depth+4);
30}
31
32
33else printf("%*s%s\n",depth,"",entry->d_name);
34}
35chdir("..");
36closedir(dp);
37
38}
39
40int main(int argc,char *argv[])
41{
42char * topdir=".";
43if(argc>=2)
44topdir=argv[1];
45
46printf("Directory scan of %s\n",topdir);
47printdir(topdir,0);
48printf("done.\n");
49
50return 0;
51}
编译链接 gcc -g -o printdir printdir.c 使用方法: ./printdir /usr/local | more |
相关阅读 更多 +