-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptions.c
More file actions
71 lines (57 loc) · 2.02 KB
/
Copy pathoptions.c
File metadata and controls
71 lines (57 loc) · 2.02 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
#include "options.h"
/* **************************************************************************
Scans command line arguments provided and performs actions as needed
**************************************************************************
*/
void parseOptions(int argc, char const *argv[]) {
int i;
for (i = 1; i < argc; i++) {
if ((int)argv[i][0] == '-' && strlen(argv[i]) > 1 && argc > i+1) {
switch (tolower((int)argv[i][1])) {
case ADD_FLAG:
addPathToTracked(argv[++i]);
break;
case REMOVE_FLAG:
removePathFromTracked(argv[++i]);
break;
}
} else {
printf("Ambiguous arg '%s' passed\n", argv[i]);
}
}
}
/* **************************************************************************
Adds path to the paths file specified in macros.h
- Notes: will make sure the path is a valid directory on the local machine
**************************************************************************
*/
void addPathToTracked(char const *path) {
FILE *pathFile = NULL;
if (access(path, F_OK) == -1) {
printf("Invlaid path '%s' passed\n", path);
return;
}
pathFile = fopen(PATH_FILE, "a");
fprintf(pathFile, "\n%s", path);
printf("Success adding path '%s'\n", path);
fclose(pathFile);
}
/* **************************************************************************
Removes path from paths file specified in macros.h
**************************************************************************
*/
void removePathFromTracked(char const *path) {
char * filePath = NULL;
size_t len = 0;
ssize_t read = 0;
FILE *pathFile = fopen(PATH_FILE, "r");
while ((read = getline(&filePath, &len, pathFile)) != -1) {
if (filePath[read-1] == '\n') {
filePath[read-1] = '\0';
}
}
if (filePath)
free(filePath);
fclose(pathFile);
fclose(pathFile);
}