01 #include <unistd.h>
02 #include <stdio.h>
03 #include <dirent.h>
04 #include <string.h>
05 #include <sys/stat.h>
06 #include <stdlib.h>
07
08
09 void printdir(char *dir, int depth)
10 {
11 DIR *dp;
12 struct dirent *entry;
13 struct stat statbuf;
14
15 if( (dp = opendir(dir)) == NULL ) {
16 fprintf(stderr, "cannot open directory: %s\n", dir);
17 return;
18 }
19
20 chdir(dir);
21
22 while((entry = readdir(dp)) != NULL) {
23 lstat(entry->d_name, &statbuf);
24 if(S_ISDIR(statbuf.st_mode)) {
25 /* Found a directory, but ignore . and .. */
26 if(strcmp(".",entry->d_name) == 0 ||
27 strcmp("..",entry->d_name) == 0 )
28 continue;
29
30 printf("%*s%s/\n",depth, "", entry->d_name);
31
32 /* Recurse at a new indent level */
33
34 printdir(entry->d_name,depth+4);
35 }
36
37 else printf("%*s%s\n",depth,"",entry->d_name);
38 }
39
40 chdir("..");
41 closedir(dp);
42 }
43
44
45
46 int main()
47 {
48 printf("Directory scan of /root/C:\n");
49 printdir("/root/C",0);
50 printf("done.\n");
51
52 exit(0);
53
54 }
01 int main(int argc, char *argv[])
02 {
03 char *topdir = ".";
04 if(argc >= 2)
05 topdir = argv[1];
06
07 printf("Directory scan of %s:\\n",topdir);
08 printdir("tipdir",0);
09 printf("done.\\n");
10
11 exit(0);
12
13 }
欢迎光临 曲径通幽论坛 (http://www.groad.net/bbs/) | Powered by Discuz! X3.2 |