forked from tsiemens/eece-315-proj1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fileGet.c
148 lines (129 loc) · 3.51 KB
/
fileGet.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
/* fileGet.c
*
* EECE 315
* Lab group B5
*
* Functions for retrieving/checking files
*/
#include "definitions.h"
#include "fileGet.h"
/* Checks if directory exists
* @return: false if no directory, true if exists
*/
bool doesDirExist(char* path){
DIR* dir = opendir(path);
if(!dir){
return false;
} else{
return true;
}
}
/* Checks if file exists
* @return: false if file does not exist,
* or path is a directory. true if exists.
*/
bool doesFileExist(char* path){
int status = open(path, O_RDONLY);
if( NEG_ONE == status || doesDirExist(path)){
return false;
} else{
return true;
}
}
/* If path begins with '~', it replaces it with the current absolute
* HOME path.
* @return: A newly allocated string with the full path.
* If path does not start with a ~, returns path, unaltered.
*/
char* tildaToHome(char* path){
char* homeDir = getenv("HOME");
char* fullPath;
char* tempPath;
int pathLength = strlen(path);
if(pathLength && '~' == path[0]){
tempPath = (char*) malloc(pathLength);
strcpy(tempPath, &path[1]);
fullPath = (char*) malloc((strlen(path)+strlen(homeDir)));
strcpy(fullPath, homeDir);
strcat(fullPath, tempPath);
free(tempPath);
return fullPath;
} else{
return path;
}
}
/* If path is in the HOME dir, replaces HOME with '~'
* @return: A newly allocated string, with the abbreviated path
* If path is not in HOME, returns path, unaltered.
*/
char* homeToTilda(char* path){
char* homeDir = getenv("HOME");
int homeLength = strlen(homeDir);
int pathLength = strlen(path);
char* tildaPath;
if(strlen(path) >= homeLength && !strncmp(homeDir, path, homeLength)){
tildaPath = (char*) malloc(pathLength - homeLength +1);
strcpy(tildaPath, &path[homeLength -1]);
tildaPath[0] = '~';
return tildaPath;
} else{
return path;
}
}
/* Get the absolute path of a file. Searches first in PATH.
* If not found in PATH, will check if fileName is already
* an absolute OR relative path.
* Accepts absolute paths with '~' representing HOME
* @param: string of the name or path of a file
* @return: string of absolute path of the file if found. NULL otherwise.
*/
char* getFilePath(char* fileName){
char** paths = getPaths();
char* filePath = NULL;
int i = 0;
filePath = malloc(PATH_MAX*sizeof(char));
while( paths[i] != NULL){
strcpy(filePath, paths[i]);
strcat(filePath, "/");
strcat(filePath, fileName);
if(doesFileExist(filePath)){
return filePath;
}
i++;
}
//File was not found in PATH
strcpy(filePath, fileName);
filePath = tildaToHome(filePath);
filePath = realpath(filePath, NULL);
if( filePath != NULL ){
return filePath;
}
return NULL;
}
/* @return: An array of strings, which are the PATH environment values
* The last element in the array is set to NULL
*/
char** getPaths(){
char* temp = getenv("PATH");
char* pathEnv = (char*)malloc(strlen(temp));
strcpy(pathEnv, temp);
int numOfPaths = 1; //Set to account for
int i;
for(i = (strlen(pathEnv)-1); i > 0; i--){
if(':' == pathEnv[i]){
numOfPaths++;
}
}
char** paths = (char**)calloc(numOfPaths+1, sizeof(char*));
paths[numOfPaths] = NULL;
numOfPaths--;
for(i = (strlen(pathEnv)-1); i > 0; i--){
if(':' == pathEnv[i]){
paths[numOfPaths] = &pathEnv[i+1];
numOfPaths--;
pathEnv[i] = '\0';
}
}
paths[0] = &pathEnv[0];
return paths;
}