|
功能:打印出目录信息
代码:
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 } |
|