-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.cpp
58 lines (44 loc) · 1.03 KB
/
search.cpp
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
/******************************
Created By: Aishwary Dewangan
Course: M. Tech. CSIS
Roll No.: 2018202016
GitLab Handle: aish_2018202016
******************************/
#include "include/search.h"
int flag;
void traverse(const char *dir, const char *name) {
DIR *dp;
struct dirent *entry;
struct stat statbuf;
if((dp = opendir(dir)) == NULL) {
printStatus("Error: Unable to open directory");
return;
}
chdir(dir);
while((entry = readdir(dp)) != NULL) {
lstat(entry->d_name, &statbuf);
if(S_ISDIR(statbuf.st_mode)) {
if(strcmp(".",entry->d_name) == 0 || strcmp("..",entry->d_name) == 0)
continue;
if(strcmp(entry->d_name, name) == 0) {
flag = 1;
}
traverse(entry->d_name, name);
}
else {
if(strcmp(entry->d_name, name) == 0) {
flag = 1;
}
}
}
chdir("..");
closedir(dp);
}
void search(const char *dir, const char *name) {
flag = 0;
traverse(dir, name);
if(flag == 0)
printStatus("Error: File/Directory not found");
if(flag == 1)
printStatus("Success: File/Directory found");
}