1 #include <stdio.h>
2 #include <unistd.h>
3 #include <sys/types.h>
4 #include <dirent.h>
5 #include <sys/stat.h>
6 #include <string.h>
7 #include <stdlib.h>
8 #include <errno.h>
9
10 void print_tree(char *filename,int blank);
11
12 int main(int argc, char *argv[])
13 {
14 printf("/home/p/p/SigKill\n");
15 print_tree("/home/p/p/SigKill",2);
16 return 0;
17 }
18
19 void print_tree(char *filename,int blank){
20
21 DIR *dir = opendir(filename);
22 if(dir == NULL)
23 perror("opendir");
24
25 struct dirent *ent;
26 int i=0;
27
28 while((ent=readdir(dir)) != NULL){
29
30 if(!strcmp(ent->d_name,".") ||\
31 !strncmp(ent->d_name,"..",2))
32 continue;
33
34 struct stat st;
35
36 char *name = (char *)malloc(sizeof(char)*1024);
37 memset(name,'\0',sizeof(char)*1024);
38 strcpy(name,filename);
39 strcat(name,"/");
40 strcat(name,ent->d_name);
41
42 int f=stat(name,&st);
43 if(f == -1){
44 switch(errno){
45 case EACCES:puts("EACCES");break;
46 case EBADF:puts("EBADF");break;
47 case EFAULT:puts("EFAULT");break;
48 case ENOENT:puts("ENOENT");break;
49 case ENOMEM:puts("ENOMEM");break;
50 case ENOTDIR:puts("ENOTDIR");break;
51 }
52
53 }
54
55 if(S_ISDIR(st.st_mode)){
56 for(i=0;i<blank;i++)
57 printf(" ");
58 char *name = (char *)malloc(sizeof(char)*1024);
59 memset(name,'\0',sizeof(char)*1024);
60 strcpy(name,filename);
61 strcat(name,"/");
62 strcat(name,ent->d_name);
63 printf("%s\n",ent->d_name);
64 print_tree(name,blank+2);
65 free(name);
66 }
67 else{
68 for(i=0;i<blank;i++)
69 printf(" ");
70 printf("%s\n",ent->d_name);
71 }
72 }
73
74 }
75
|