-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpath.c
141 lines (118 loc) · 3.19 KB
/
path.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
#include <sys/stat.h>
#include <aul/common.h>
#include <aul/string.h>
#include <kernel.h>
static char * pathlist[PATH_MAXPATHS] = {0};
static char pathbuf[PATH_BUFSIZE] = {0};
static size_t pathlist_next = 0;
static char * pathbuf_next = pathbuf;
static inline bool file_exists(const char * path)
{
struct stat buf;
int i = stat(path, &buf);
return i == 0 && S_ISREG(buf.st_mode);
}
static inline bool dir_exists(const char * path)
{
struct stat buf;
int i = stat(path, &buf);
return i == 0 && S_ISDIR(buf.st_mode);
}
bool path_set(const char * newpath, exception_t ** err)
{
// Sanity check
if (exception_check(err))
{
return false;
}
memset(pathlist, 0, sizeof(pathlist));
memset(pathbuf, 0, sizeof(pathbuf));
pathbuf_next = pathbuf;
pathlist_next = 0;
if (newpath == NULL)
{
return true;
}
return path_append(newpath, err);
}
bool path_append(const char * path, exception_t ** err)
{
// Sanity check
if (exception_check(err) || path == NULL)
{
return false;
}
const char * start = path;
ssize_t length = strlen(path);
while (length > 0)
{
// Get the length of the next path segment
char * end = strchr(start, ':');
ssize_t plen = (end == NULL)? length : end - start;
// Make sure there is enough room in the path list
if (pathlist_next >= PATH_MAXPATHS)
{
// Not enough room!
exception_set(err, ENOMEM, "Could not append path, not enough room! Consider increasing PATH_MAXPATHS (currently %d)", PATH_MAXPATHS);
return false;
}
// Make sure there is enough space in the path buffer
if ((plen + 1) > (PATH_BUFSIZE - (pathbuf_next - pathbuf)))
{
// Not enough room!
exception_set(err, ENOMEM, "Could not append path, not enough room! Consider increasing PATH_BUFSIZE (currently %d)", PATH_BUFSIZE);
return false;
}
// Copy the segment into the path buffer
memcpy(pathbuf_next, start, plen);
pathbuf_next[plen++] = '\0';
// Update the pointers
pathlist[pathlist_next++] = pathbuf_next;
pathbuf_next += plen;
length -= plen;
start += plen;
}
return true;
}
// Returns the directory path prefix where the file can be found
const char * path_resolve(const char * name, ptype_t type)
{
string_t file = string_new("%s", name);
if (type & P_MODULE)
{
if (!strsuffix(file.string, ".mo"))
{
// We are looking for a module, add an '.mo' to the name
string_append(&file, ".mo");
}
type = P_FILE;
}
if (unlikely(file.string[0] == '/'))
{
// Absolute path
if (((type & P_FILE) && file_exists(file.string)) || ((type & P_DIRECTORY) && dir_exists(file.string)))
{
// Return an empty string to denote that no prefix is required
return "";
}
}
else
{
for (size_t i = 0; pathlist[i] != NULL && i < pathlist_next; i++)
{
// Relative path
string_t test = path_join(pathlist[i], file.string);
if (((type & P_FILE) && file_exists(test.string)) || ((type & P_DIRECTORY) && dir_exists(test.string)))
{
// Return an empty string to denote that no prefix is required
return pathlist[i];
}
}
}
return NULL;
}
string_t path_join(const char * prefix, const char * file)
{
bool slash_needed = !strprefix(file, "/") && !strsuffix(prefix, "/");
return string_new("%s%s%s", prefix, (slash_needed)? "/" : "", file);
}