-
Notifications
You must be signed in to change notification settings - Fork 0
/
seek.c
333 lines (325 loc) · 7.13 KB
/
seek.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
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
#include "headers.h"
void rmv(char *filename)
{
char *my = strrchr(filename, '.');
if (my != NULL)
{
*my = '\0';
}
}
int recursion(char *dir, int direc, int executable, int files, char *file);
void condition_1(char *tokens[])
{
if (!recursion(current_direc, 1, 0, 1, tokens[1]))
{
printf("No match found!\n");
}
directory_hai = 0;
file_hai = 0;
return;
}
void condition_2(char *tokens[], int tk_freq)
{
if (recursion(tokens[tk_freq - 1], 1, 0, 1, tokens[tk_freq - 2]))
{
}
else
{
printf("No match found!\n");
}
directory_hai = 0;
file_hai = 0;
return;
}
void condition_3(char *tokens[], int tk_freq)
{
if (recursion(tokens[tk_freq - 1], 1, 0, 1, tokens[tk_freq - 2]))
{
}
else
{
printf("No match found!\n");
}
directory_hai = 0;
file_hai = 0;
return;
}
void condition_4(char *tokens[], int executable, int direc, int files)
{
if (strcmp("-e", tokens[1]) == 0)
{
int van = 1;
executable = van;
}
else if (strcmp("-d", tokens[1]) == 0)
{
int fg = 1;
direc = fg;
}
else if (strcmp("-f", tokens[1]) == 0)
{
int ty = 1;
files = ty;
}
else
{
printf("Invalid Command!\n");
return;
}
}
void condition_5(char *tokens[], int executable, int direc, int files)
{
if (strcmp("-e", tokens[1]) == 0)
{
int mom = 1;
executable = mom;
}
else if (strcmp("-d", tokens[1]) == 0)
{
int sik = 1;
direc = sik;
}
else if (strcmp("-f", tokens[1]) == 0)
{
int mass = 1;
files = mass;
}
else
{
printf("Invalid Command!\n");
return;
}
if (strcmp("-e", tokens[2]) == 0)
{
int damn = 1;
executable = damn;
}
else if (strcmp("-d", tokens[2]) == 0)
{
int dem = 1;
direc = dem;
}
else if (strcmp("-f", tokens[2]) == 0)
{
int you = 1;
files = you;
}
else
{
printf("Invalid Command!\n");
return;
}
if (direc && files)
{
directory_hai = 0, file_hai = 0;
printf("Flags are invalid !\n");
return;
}
}
void seeks(char *tokens[], int tk_freq)
{
int executable = 0;
int files = 0;
int direc = 0;
if (tk_freq - 2 == 0)
{
if (!recursion(current_direc, 1, 0, 1, tokens[1]))
{
printf("No match found!\n");
}
directory_hai = 0;
file_hai = 0;
return;
}
else if (tk_freq - 3 == 0)
{
if (recursion(tokens[tk_freq - 1], 1, 0, 1, tokens[tk_freq - 2]))
{
}
else
{
printf("No match found!\n");
}
directory_hai = 0;
file_hai = 0;
return;
}
else if (tk_freq - 4 == 0)
{
if (strcmp("-e", tokens[1]) == 0)
{
int wow = 1;
executable = wow;
}
else if (strcmp("-f", tokens[1]) == 0)
{
int wowo = 1;
files = wowo;
}
else if (strcmp("-d", tokens[1]) == 0)
{
int wp = 1;
direc = wp;
}
else
{
printf("Invalid Command!\n");
return;
}
}
else if (tk_freq - 5 == 0)
{
if (strcmp("-e", tokens[1]) == 0)
{
int sp = 1;
executable = sp;
}
else if (strcmp("-f", tokens[1]) == 0)
{
int fifo = 1;
files = fifo;
}
else if (strcmp("-d", tokens[1]) == 0)
{
int upi = 1;
direc = upi;
}
else
{
printf("Invalid Command!\n");
return;
}
if (strcmp("-e", tokens[2]) == 0)
{
int kik = 1;
executable = kik;
}
else if (strcmp("-f", tokens[2]) == 0)
{
int koke = 1;
files = koke;
}
else if (strcmp("-d", tokens[2]) == 0)
{
int sumo = 1;
direc = sumo;
}
else
{
printf("Invalid Command!\n");
return;
}
if (direc && files)
{
directory_hai = 0, file_hai = 0;
printf("Flags are invalid !\n");
return;
}
}
else
{
printf("Invalid Command !\n");
return;
}
if (files == 0 && direc == 0)
{
direc = 1;
files = 1;
}
int c = recursion(tokens[tk_freq - 1], direc, executable, files, tokens[tk_freq - 2]);
if (c == 0)
{
printf("No match found!\n");
}
else if (c == 1 && file_hai && !directory_hai && executable)
{
printf("%s\n", to_change);
FILE *file_descriptor = fopen(to_change, "r");
if (file_descriptor == NULL)
{
if (errno == EACCES)
{
printf("Task Permissions missing!\n");
}
printf("File can't be opened \n");
exit(0);
}
c = fgetc(file_descriptor);
while (c != EOF)
{
printf("%c", c);
c = fgetc(file_descriptor);
}
printf("\n");
fclose(file_descriptor);
}
else if (c == 1 && directory_hai && !file_hai && executable)
{
chdir(to_change);
}
directory_hai = 0, file_hai = 0;
}
int recursion(char *dir, int direc, int executable, int files, char *file)
{
DIR *dh = opendir(dir);
if (!dh)
{
if (errno = ENOENT)
{
perror("Directory doesn't exist");
}
else
{
perror("Unable to read directory");
}
return 0;
}
struct dirent *d;
int sum = 0;
while ((d = readdir(dh)) != NULL)
{
char *temp = (char *)malloc(sizeof(char) * MAX);
if (strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0)
{
continue;
}
strcpy(temp, d->d_name);
rmv(temp);
char path[MAX];
snprintf(path, sizeof(path), "%s/%s", dir, d->d_name);
struct stat statbuf;
if (stat(path, &statbuf) == -1)
{
// perror("Error getting file information");
continue;
}
if (S_ISDIR(statbuf.st_mode))
{
if (strcmp(temp, file) == 0 || strcmp(d->d_name, file) == 0)
{
sum++;
directory_hai = 1;
if (direc)
{
printf("\033[1;32m%s\033[0m", path);
printf("\n");
}
strcpy(to_change, path);
}
sum += recursion(path, direc, executable, files, file);
}
else if (S_ISREG(statbuf.st_mode) && (strcmp(temp, file) == 0 || strcmp(d->d_name, file) == 0))
{
sum++;
file_hai = 1;
if (files)
{
printf("\033[1;34m%s\033[0m", path);
printf("\n");
strcpy(to_change, path);
}
}
}
closedir(dh);
return sum;
}