-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpath_handle.c
More file actions
49 lines (39 loc) · 894 Bytes
/
path_handle.c
File metadata and controls
49 lines (39 loc) · 894 Bytes
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
#include "path_handle.h"
char* searchPathForFile(char* fn){
//If the filename begins with a '/', check just fn and return
if(fn[0] == '/' || (fn[0] == '.' && fn[1] == '/'))
{
if(doesFileExist(fn))
return fn;
else
return NULL;
}
//Otherwise we check the full PATH
int charInd = 0;
char* env_path = strdup(PATH);
char* token;
char* currPath;
//Split the string based on delim
token = strtok(env_path, ":");
while(token != NULL)
{
//Create full pathname
currPath = strdup(token);
strcat(currPath, "/");
strcat(currPath, fn);
//perform file check
int f_exists = doesFileExist(currPath);
if(f_exists)
return currPath;
token = strtok(NULL, ":");
}
return NULL;
}
int doesFileExist(char* pathname){
struct stat s;
//printf("Checking path: %s\n", pathname);
if(stat(pathname, &s) == 0 && S_ISREG(s.st_mode))
return 1;
else
return 0;
}