-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathseek.c
More file actions
269 lines (250 loc) · 12.3 KB
/
seek.c
File metadata and controls
269 lines (250 loc) · 12.3 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#include "headers.h"
// Searches for a given file/directory recursively
int find_file_dir(char** argument_tokens, int no_of_arguments, int ip) {
char base_dir[MAX_LEN] = {0}; // stores the path to base dir
char file_name[MAX_LEN] = {0}; // stores the name of the file to be searched
// flags
int d = 0;
int f = 0;
int e = 0;
int base_dir_flag = 0; // 1 if base directory path (relative to cwd) is provided, 0 otherwise
int valid_flags = 1; // 1 if flags provided are all valid, 0 otherwise
int file_name_flag = 0; // 1 if file name is provided, 0 otherwise
int success = 1;
if (no_of_arguments == 0) {
// file/directory name to be searched should be provided
fprintf(stderr, "\033[1;31mseek: missing arguments\033[1;0m\n");
return 0;
} else {
for (int i = 1; i <= no_of_arguments; i++) {
char* curr_argument = argument_tokens[i];
if (curr_argument[0] == '-') { // if argument starts with a '-' it is considered to be a flag
if (base_dir_flag == 1) { // if base directory is already provided
// flags should be provided before providing the path to the base directory
fprintf(stderr, "\033[1;31mseek: Invalid Arguments\033[1;0m\n");
return 0;
} else {
// checking for flags present
if (strcmp(curr_argument, "-d") == 0) {
d = 1;
} else if (strcmp(curr_argument, "-f") == 0) {
f = 1;
} else if (strcmp(curr_argument, "-e") == 0) {
e = 1;
} else if (strcmp(curr_argument, "-de") == 0 || strcmp(curr_argument, "-ed") == 0) {
d = 1;
e = 1;
} else if (strcmp(curr_argument, "-fe") == 0 || strcmp(curr_argument, "-ef") == 0) {
f = 1;
e = 1;
} else {
// if any other flag is provided other than the seven mentioned
fprintf(stderr, "\033[1;31mseek: Invalid Flag\033[1;0m\n");
return 0;
}
}
} else if (curr_argument[0] == '.') { // if path relative to current directory is provided
// assuming the relative path always starts with a .
base_dir_flag = 1; // base_directory path provided
if (ip == 0) {
strcpy(base_dir, curr_argument);
} else if (ip == 1) { // if input is to be taken from a file
char inp_buff[999999] = {0};
if (read(STDIN_FILENO, inp_buff, 999998) < 0) {
fprintf(stderr, "\033[1;31mError in reading\033[1;0m\n");
return 0;
} else {
// removing the last endline character
if (inp_buff[strlen(inp_buff) - 1] == '\n') {
inp_buff[strlen(inp_buff) - 1] = '\0';
}
strcpy(base_dir, inp_buff);
}
}
} else if (curr_argument[0] == '/') { // absolute path is provided
strcpy(base_dir, curr_argument);
base_dir_flag = 1;
} else {
if (file_name_flag == 1) {
fprintf(stderr, "\033[1;31mseek: invalid arguments\033[1;0m\n");
return 0;
} else {
file_name_flag = 1;
strcpy(file_name, curr_argument);
}
}
}
}
if (valid_flags == 1 && file_name_flag == 1) {
if (d == 1 && f == 1) {
// both d and f flags cannot be provided simultaneously
fprintf(stderr, "\033[1;31mInvalid flags\033[1;0m\n");
return 0;
} else {
// iterating through all the files in the path
linked_list_head paths = create_linked_list_head();
char* path_to_base_dir;
if (base_dir_flag) {
if (base_dir[0] == '/') {
path_to_base_dir = (char*) calloc(MAX_LEN, sizeof(char));
strcpy(path_to_base_dir, base_dir);
} else {
path_to_base_dir = generate_new_path(base_dir);
}
} else {
path_to_base_dir = (char*) calloc(MAX_LEN, sizeof(char));
strcpy(path_to_base_dir, cwd);
}
// seeking in each file/folder recursively
seek(path_to_base_dir, file_name, paths);
if (paths->number_of_nodes == 0) {
printf("No match found!\n");
} else {
int no_of_files = 0;
int no_of_dir = 0;
// iterating through the list and calculating the number of files and directories
linked_list_node temp = paths->first;
while (temp != NULL) {
struct stat dir_stat_temp;
lstat(temp->path, &dir_stat_temp);
if (S_ISDIR(dir_stat_temp.st_mode) != 0) {
no_of_dir++;
} else {
no_of_files++;
}
temp = temp->next;
}
if (e) {
if ((d && (no_of_dir > 1)) || (f && (no_of_files > 1))) {
// do nothing
} else {
if (d) {
linked_list_node trav = paths->first;
while (trav != NULL) {
struct stat dir_stat;
lstat(trav->path, &dir_stat);
if (S_ISDIR(dir_stat.st_mode) != 0) { // checking if it's a dir
if (dir_stat.st_mode & S_IXUSR) {
printf("\033[1;34m%s\033[1;0m\n", relative_path(trav->path, path_to_base_dir));
strcpy(prev_dir, cwd);
strcpy(cwd, trav->path);
} else {
fprintf(stderr, "\033[1;31mMissing permissions for task!\033[1;0m\n");
}
}
trav = trav->next;
}
} else if (f) {
linked_list_node trav = paths->first;
while (trav != NULL) {
struct stat dir_stat;
lstat(trav->path, &dir_stat);
if (S_ISDIR(dir_stat.st_mode) == 0) { // checking if it's a file
if (dir_stat.st_mode & S_IRUSR) {
printf("\033[1;32m%s\033[1;0m\n", relative_path(trav->path, path_to_base_dir));
char buffer[100000];
int fd = open(trav->path, O_RDONLY);
if (fd < 0) {
fprintf(stderr, "\033[1;31mopen: Could not open file for reading\033[1;0m\n");
return 0;
} else {
if (read(fd, buffer, 100000 - 1) < 0) {
fprintf(stderr, "\033[1;31mread: could not read data\033[1;0m\n");
close(fd);
return 0;
} else {
printf("%s\n", buffer);
close(fd);
}
}
} else {
printf("Missing permissions for task!\n");
}
}
trav = trav->next;
}
} else {
if (paths->number_of_nodes > 1) {
// do nothing
} else {
struct stat dir_stat;
lstat(paths->first->path, &dir_stat);
if (S_ISDIR(dir_stat.st_mode) == 0) { // checking if it's a file
if (dir_stat.st_mode & S_IRUSR) {
printf("\033[1;32m%s\033[1;0m\n", relative_path(paths->first->path, path_to_base_dir));
char buffer[100000];
int fd = open(paths->first->path, O_RDONLY);
if (fd < 0) {
fprintf(stderr, "\033[1;31mopen: Could not open file for reading\033[1;0m\n");
return 0;
} else {
if (read(fd, buffer, 100000 - 1) < 0) {
fprintf(stderr, "\033[1;31mread: could not read data\033[1;0m\n");
close(fd);
return 0;
} else {
printf("%s\n", buffer);
close(fd);
}
}
} else {
printf("Missing permissions for task!\n");
}
} else {
if (dir_stat.st_mode & S_IXUSR) {
printf("\033[1;34m%s\033[1;0m\n", relative_path(paths->first->path, path_to_base_dir));
strcpy(prev_dir, cwd);
strcpy(cwd, paths->first->path);
} else {
fprintf(stderr, "\033[1;31mMissing permissions for task!\033[1;0m\n");
}
}
}
}
}
} else {
if (f) {
traverse_and_print(paths, 1, 0, path_to_base_dir);
} else if (d) {
traverse_and_print(paths, 0, 1, path_to_base_dir);
} else {
traverse_and_print(paths, 1, 1, path_to_base_dir);
}
}
}
free(path_to_base_dir);
free_linked_list(paths);
}
}
return 1;
}
void seek(char* path_to_base_dir, char* file_name, linked_list_head paths) {
struct stat dir_stat;
stat(path_to_base_dir, &dir_stat);
if (S_ISDIR(dir_stat.st_mode) == 0) {
return;
}
DIR *dr;
struct dirent *en;
dr = opendir(path_to_base_dir);
if (dr) {
while ((en = readdir(dr)) != NULL) {
if (en->d_name[0] != '.') {
char* next_file_path = (char*) calloc(MAX_LEN, sizeof(char));
strcpy(next_file_path, path_to_base_dir);
update_path(next_file_path, en->d_name);
seek(next_file_path, file_name, paths);
char* new_file_name = remove_extension(file_name);
char* curr_file_name = remove_extension(en->d_name);
if (strcmp(new_file_name, curr_file_name) == 0) {
insert_in_linked_list(paths, next_file_path);
}
free(curr_file_name);
free(next_file_path);
free(new_file_name);
}
}
closedir(dr);
}
return;
}