-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdir_search.c
95 lines (79 loc) · 2.53 KB
/
dir_search.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <stdio.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdlib.h>
#include <memory.h>
#include <errno.h>
int depth = 2;
void find(char *str, char* cmp, int i){
if(i > depth)
return;
DIR* a;
a = opendir(str);
if(a == NULL){
if(errno == EACCES){
printf("Permission denied %s\n", str);
} else if(errno == ENOTDIR){
printf("Is not a directory %s\n", str);
} else if(errno == EBADF){
printf("fd is not a valid file descriptor opened for reading %s\n", str);
} else if(errno == EMFILE){
printf("The per-process limit on the number of open file descriptors has been reached %s\n", str);
} else if(errno == ENFILE){
printf("The system-wide limit on the total number of open files has been reached %s\n", str);
} else if(errno == ENOENT){
printf("Directory does not exist, or name is an empty string(more common it is link to other directory %s\n", str);
} else if(errno == ENOMEM){
printf("Insufficient memory to complete the operation %s\n", str);
} else{
printf("Can't read\n");
}
return;
}
char* str2 = malloc(400*sizeof(char));
char* cmp2 = malloc(400*sizeof(char));
struct dirent* b;
struct stat buf;
while((b = readdir(a)) != NULL){
strcpy(str2, str);
strcat(str2, "/");
strcat(str2, b->d_name);
//printf("in directory: %s\n", str2);
stat(str2, &buf);
if(S_ISDIR(buf.st_mode)){
if((strcmp(b->d_name, ".") == 0) || (strcmp(b->d_name, "..") == 0))
continue;
//printf("Started finding in %s\n\n", str2);
find(str2, cmp, i+1);
} else if(S_ISLNK(buf.st_mode)) {
continue;
} else {
strcpy(cmp2, str);
strcat(cmp2, "/");
strcat(cmp2, cmp);
if(strcmp(cmp2, str2) == 0)
printf("FOUND: %s\n", cmp2);
}
memset(str2, (int)'\0', 400);
memset(cmp2, (int)'\0', 400);
}
free(str2);
free(cmp2);
closedir(a);
}
int main() {
char* str = malloc(100* sizeof(char));
char* cmp = malloc(20* sizeof(char));
printf("Write the depth of search\n");
scanf("%d", &depth);
printf("Where:\n");
scanf("%s", str);
printf("What:\n");
scanf("%s", cmp);
if(str[strlen(str)-1] == '/')
str[strlen(str)-1] = '\0';
find(str, cmp, 1);
return 0;
}