-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathseek.c
184 lines (169 loc) · 5.06 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
#include "headers.h"
void search(const char *target, const char *directory, int isDirFlag, int isFileFlag, int isExecuteFlag, int topLevelSearch)
{
if (directory[0] == '~')
{
char expanded_path[DEF_SIZE];
snprintf(expanded_path, sizeof(expanded_path), "%s%s", global_home, directory + 1);
directory = expanded_path;
}
DIR *dir = opendir(directory);
if (dir == NULL)
{
printError("opendir");
return;
}
int matchFound = 0;
struct dirent *entry;
while ((entry = readdir(dir)) != NULL)
{
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
{
continue;
}
char entryPath[DEF_SIZE];
snprintf(entryPath, sizeof(entryPath), "%s/%s", directory, entry->d_name);
struct stat entryStat;
if (stat(entryPath, &entryStat) == -1)
{
printError("stat");
continue;
}
if (S_ISDIR(entryStat.st_mode) && (isDirFlag || (!isFileFlag && !isDirFlag)))
{
if (strcmp(entry->d_name, target) == 0 || strstr(entry->d_name, target) != NULL)
{
matchFound = 1;
printf("%s/%s\n", directory, entry->d_name);
if (isExecuteFlag)
{
if (access(entryPath, X_OK) != 0)
{
printError("Missing permissions for task!");
closedir(dir);
return;
}
if (chdir(entryPath) == -1)
{
printError("chdir");
closedir(dir);
return;
}
printf("Changed current working directory to %s\n", entryPath);
}
search(target, entryPath, isDirFlag, isFileFlag, isExecuteFlag, 0);
}
}
else if (S_ISREG(entryStat.st_mode) && (isFileFlag || (!isFileFlag && !isDirFlag)))
{
if (strstr(entry->d_name, target) != NULL)
{
matchFound = 1; // Mark match found
printf("%s/%s\n", directory, entry->d_name);
if (isExecuteFlag)
{
if (access(entryPath, R_OK) != 0)
{
printError("Missing permissions for task!");
closedir(dir);
return;
}
FILE *file = fopen(entryPath, "r");
if (file == NULL)
{
printError("fopen");
}
else
{
char buffer[1024];
printf(DARK_GREY_COLOR);
while (fgets(buffer, sizeof(buffer), file) != NULL)
{
printf("%s", buffer);
}
printf(RESET_COLOR);
printf("\n");
fclose(file);
}
}
}
}
}
closedir(dir);
if (topLevelSearch && !matchFound && isDirFlag)
{
printError("No match found!\n");
}
}
// Main seek function
void seek(Command *cmd)
{
if (cmd->argc < 2)
{
printError("Usage: seek <flags> <search> <target_directory>\n");
return;
}
int isDirFlag = 0;
int isFileFlag = 0;
int isExecuteFlag = 0;
char *target = NULL;
char *directory = "."; // Initialize with the current directory
int hasInvalidFlags = 0;
for (int i = 1; i < cmd->argc; i++)
{
if (strcmp(cmd->argv[i], "-d") == 0)
{
if (cmd->argc == 2) {
hasInvalidFlags = 1;
break;
}
if (isFileFlag || isExecuteFlag)
{
hasInvalidFlags = 1;
break;
}
isDirFlag = 1;
}
else if (strcmp(cmd->argv[i], "-f") == 0)
{
if (isDirFlag)
{
hasInvalidFlags = 1;
break;
}
isFileFlag = 1;
}
else if (strcmp(cmd->argv[i], "-e") == 0)
{
if ((isFileFlag) || isExecuteFlag)
{
hasInvalidFlags = 1;
break;
}
isExecuteFlag = 1;
}
else if (target == NULL)
{
target = cmd->argv[i];
}
else if (i == cmd->argc - 1)
{
directory = cmd->argv[i];
}
else
{
printError("Invalid usage!\n");
return;
}
}
if (hasInvalidFlags)
{
printError("Invalid flags!\n");
return;
}
if ((isFileFlag && isExecuteFlag) || (isExecuteFlag && isFileFlag))
{
search(target, directory, isDirFlag, isFileFlag, isExecuteFlag, 1);
}
search(target, directory, isDirFlag, isFileFlag, isExecuteFlag, 1);
}