-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjob.c
More file actions
338 lines (325 loc) · 9.49 KB
/
Copy pathjob.c
File metadata and controls
338 lines (325 loc) · 9.49 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
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
334
335
336
337
338
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <ctype.h>
#include <glob.h>
#include <dirent.h>
#include <fnmatch.h>
#include <string.h>
#include <sys/stat.h>
#include "job.h"
#include "arraylist.h"
#include "builtins.h"
/**
* @brief This function checks if the program exists in the given path
*
* @param pathToCheck The path to check in.
* @param bareName The bare name of the program
* @param lenOfBareName The len of the bare name
* @return char* The fullpath, or NULL if not found
*/
char* my_acess(char* pathToCheck, char* bareName, size_t lenOfBareName){
size_t lenOfpath1PlusBareName = strlen(pathToCheck) + 1 + lenOfBareName; //len w/o terminator
char* path1PlusBareName = malloc(lenOfpath1PlusBareName + 1);
snprintf(path1PlusBareName, lenOfpath1PlusBareName + 1, "%s/%s", pathToCheck, bareName);
if(access(path1PlusBareName, F_OK) == 0){
return path1PlusBareName;
}
else{
free(path1PlusBareName);
return NULL;
}
}
/**
* @brief This utility function checks if a
* directory entry is . or .. or hiden(starts with .)
* Makes sure we do not check for wildcard expansion with hidden files, or . or ..
*
* @param name The name of the file
* @return int 1 if it is, 0 if it is not
*/
int isItSelfOrParrentDirOrHidden(char* name){
if(strcmp(name, ".") == 0 || strcmp(name, "..") == 0){
return 1;
}
if(name[0] == '.'){
return 1;
}
return 0;
}
/**
* @brief This function handles wildcards. Up to the client to insure that
* the function is only called after detecting a proper *.
*
* @param arg The wildcard pattern to expand
* @param argList The arraylist to add to
* @return int 0 if no matches found. 1 if matches found
*/
int handleWildcards(char* arg, arraylist_t* argList) {
char* slash = strchr(arg, '/');
char* dir_part;
char* file_part;
if (slash) {
*slash = '\0';
dir_part = arg;
file_part = slash + 1;
}
else {
dir_part = ".";
file_part = arg;
}
DIR *dir = opendir(dir_part);
if (dir == NULL) {
return 0;
}
struct dirent *entry;
int found = 0;
while ((entry = readdir(dir)) != NULL) {
if (isItSelfOrParrentDirOrHidden(entry->d_name)) {
continue;
}
struct stat stats;
char* temp;
int sizeofpathcpy = strlen(entry->d_name);
//printf("Size of %lu\n", strlen(entry->d_name));
char* pathCopy = malloc(sizeof(char) * (sizeofpathcpy + 1));
strcpy(pathCopy, entry->d_name);
//char* pathCopy = strdup(entry->d_name);
int malloced = 0;
if (dir_part[0] != '.') {
temp = malloc(sizeof(char) * (strlen(dir_part) + strlen(pathCopy) + 2));
malloced = 1;
strcpy(temp, dir_part);
strcat(temp, "/");
strcat(temp, pathCopy);
}
else {
temp = entry->d_name;
}
if(stat(temp, &stats) == 0){
if (malloced) free(temp);
if (!S_ISREG(stats.st_mode)) {
free(pathCopy);
continue;
}
} else {
if (malloced) free(temp);
}
if (fnmatch(file_part, entry->d_name, 0) == 0) {
if (dir_part[0] != '.') {
char* newPath = malloc(sizeof(char) * (strlen(dir_part) + strlen(pathCopy) + 2));
strcpy(newPath, dir_part);
strcat(newPath, "/");
strcat(newPath, pathCopy);
al_push(argList, newPath);
free(pathCopy);
}
else {
al_push(argList, pathCopy);
}
found = 1;
} else {
free(pathCopy);
}
}
closedir(dir);
if (!found) {
return 0;
}
return 1;
}
/**
* @brief Make sure a * happens after the last / in the path name
*
* @param name The wildcard pattern
* @return int 1 if true, 0 if false
*/
int isRegexInLastPath(char *name){
char* locOfLastSlash = strrchr(name, '/');
char* locOfFirstAshtrik = strchr(name, '*');
if(locOfFirstAshtrik == NULL){
// if no *
return 0;
}
if (locOfLastSlash == NULL) {
// if no / and at this point, the name must have a *
return 1;
}
if (locOfFirstAshtrik > locOfLastSlash) {
// the name contains a * and a slash
// if the astrik occurs after the slash
return 1;
} else {
return 0;
}
}
/**
* @brief This takes in the string job cmd, and returns a Job Struct pointer
* Example Inputs/Outputs:
* Input1:
* "foo < bar baz"
* Output1:
* ptr to Job:
* execPath = foo
* args = {foo, baz}
* inputReDirectPath = "baz"
* outputReDirectPath = "\0" (NULL)
*
*
* Input2:
* "foo bar > baz "
* Output2:
* ptr to Job:
* execPath = foo
* args = {foo, bar}
* inputReDirectPath = "\0" (NULL)
* outputReDirectPath = "baz"
*
* Input3:
* " quux *.txt > spam"
* Output3:
* ptr to Job:
* execPath = quux
* args = {quux, a.txt,...(all .txt files in current dir)}
* inputReDirectPath = "spam"
* outputReDirectPath = "\0" (NULL)
*
* There will be no pipes, or conditinals in the input string
* @param jobCmds
* @return Job* A job if succesfull or NULL if parsing error
*/
Job *makeJob(char* jobCmd){
Job* job = malloc(sizeof(Job));
job->inputReDirectPath = malloc(sizeof(char) * 256);
job->outputReDirectPath = malloc(sizeof(char) * 256);
job->inputReDirectPath[0] = '\0';
job->outputReDirectPath[0] = '\0';
if(strlen(jobCmd) == 0) return NULL;
char* endOfBareName = strpbrk(jobCmd, " \t\r"); //get a ptr to first ' ' or '\r' or '\t' in jobCmd
if(endOfBareName == NULL) endOfBareName = jobCmd + strlen(jobCmd);
size_t lenOfBareName = endOfBareName - jobCmd; //len w/o terminator
char* bareName = malloc((lenOfBareName + 1) * sizeof(char)); //malloc lenOfBareName + terminator
strncpy(bareName, jobCmd, lenOfBareName); //cpy barename
bareName[lenOfBareName] = '\0'; //append terminator
arraylist_t* argList = al_create(1);
if(strchr(bareName, '/')){
job->execPath = bareName;
}
else if(isBuiltIn(bareName)){
job->execPath = bareName;
}
else{
for(int i = 0; i < NUMOFPATHS; i++){
job->execPath = my_acess(paths[i], bareName, lenOfBareName);
if(job->execPath != NULL) break;
}
free(bareName);
}
int i = 0;
//printf("jobCmd: |%s|\n", jobCmd);
while (jobCmd[i] != '\0'){
if(isspace(jobCmd[i])){
i++;
continue;
}
else if(jobCmd[i] == '<'){
i++;
while(isspace(jobCmd[i]) || jobCmd[i] == '<' || jobCmd[i] == '>'){
if(jobCmd[i] == '>') return NULL;
i++;
}
int j = 0;
if(jobCmd[i] == '\0'){
return NULL;
}
while(!isspace(jobCmd[i]) && jobCmd[i] != '\0'){
job->inputReDirectPath[j] = jobCmd[i];
i++;
j++;
}
job->inputReDirectPath[j] = '\0';
}
else if(jobCmd[i] == '>'){
i++;
while(isspace(jobCmd[i]) || jobCmd[i] == '>' || jobCmd[i] == '<'){
if(jobCmd[i] == '<') return NULL;
i++;
}
if(jobCmd[i] == '\0'){
return NULL;
}
int j = 0;
while(!isspace(jobCmd[i]) && jobCmd[i] != '\0'){
job->outputReDirectPath[j] = jobCmd[i];
i++;
j++;
}
job->outputReDirectPath[j] = '\0';
}
else{
char* currArg = malloc(sizeof(char)*256);
int j = 0;
while(!isspace(jobCmd[i]) && jobCmd[i] != '\0'){
currArg[j] = jobCmd[i];
i++;
j++;
}
currArg[j] = '\0';
//printf("|%s|\n", currArg);
if(isRegexInLastPath(currArg)) {
if(!handleWildcards(currArg, argList)) {
al_push(argList, currArg);
}
else {
free(currArg);
}
} else {
al_push(argList, currArg);
}
}
}
job->numOfArgs = al_length(argList);
job->args = malloc(sizeof(char*) * (job->numOfArgs + 1)); //+1 bc execv needs NULL terminated array
for(int i = 0; i < job->numOfArgs; i++){
char* arg;
al_pop(argList, &arg);
job->args[i] = malloc((sizeof(char) * strlen(arg)) + 1);
strcpy(job->args[i], arg);
free(arg);
}
job->args[job->numOfArgs] = NULL;
al_destroy(argList);
return job;
}
/**
* @brief This free a given Job
*
* @param job The job to free
*/
void freeJob(Job* job){
free(job->execPath);
for(int i = 0; i < job->numOfArgs+1; i++){
free(job->args[i]);
}
free(job->args);
free(job->inputReDirectPath);
free(job->outputReDirectPath);
free(job);
}
/**
* @brief This prints a given Job
*
* @param job The job to print
*/
void printJob(Job* job){
printf("Job: \n");
printf("\tExec: %s\n", job->execPath);
printf("\tArgs: ");
for(int i = 0; i < job->numOfArgs+1; i++){
printf("|%s|", job->args[i]);
}
printf("\n");
printf("\tInRe: %s\n", job->inputReDirectPath);
printf("\tOuRe: %s\n", job->outputReDirectPath);
}