-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch.cpp
62 lines (55 loc) · 1.72 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
59
60
61
62
/********************************************************************************/
/* Name: Priyendu Mori */
/* Roll no: 2018201103 */
/********************************************************************************/
#include "header.h"
#include "macro.h"
vector<string> results, printresults;
extern string home;
extern struct winsize w;
/*
this function searches for a file or directory from current directory
recursively in a DFS manner and stores it
*/
void search(string path, string pattern){
string fullpath;
DIR *dir;
struct stat stat_dir, stat_entry;
struct dirent *entry;
stat(path.c_str(), &stat_dir);
if (S_ISDIR(stat_dir.st_mode) == 0) {
commands(true);
}
if ((dir = opendir(path.c_str())) == NULL) {
commands(true);
}
while ((entry = readdir(dir)) != NULL) {
if (!strcmp(entry->d_name, ".") || !strcmp(entry->d_name, ".."))
continue;
fullpath=path;
fullpath.append("/").append(entry->d_name);
if(entry->d_name == pattern){
results.push_back(fullpath);
string temp=fullpath;
temp.replace( temp.find( home ), home.size(), "" );
printresults.push_back(temp);
}
stat(fullpath.c_str(), &stat_entry);
if (S_ISDIR(stat_entry.st_mode) != 0) {
search(fullpath, pattern);
continue;
}
}
closedir(dir);
}
/*
this function displays the results of the search
*/
void displaySearchResults(){
for(auto i:printresults){
cout<<i<<endl;
}
pos(w.ws_row,0);
cout<<"Normal mode";
pos(0,0);
}