-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPath.c
More file actions
34 lines (27 loc) · 746 Bytes
/
Copy pathPath.c
File metadata and controls
34 lines (27 loc) · 746 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
#include "mocc.h"
char *Path_join(const char *dir, const char *rel_path) {
assert(dir);
assert(rel_path);
size_t dir_len = strlen(dir);
size_t rel_path_len = strlen(rel_path);
char *path = malloc(sizeof(char) * (dir_len + 1 + rel_path_len + 1));
if (dir_len == 0) {
strcpy(path, rel_path);
} else if (dir[dir_len - 1] == '/') {
strcpy(path, dir);
strcat(path, rel_path);
} else {
strcpy(path, dir);
strcat(path, "/");
strcat(path, rel_path);
}
return path;
}
char *Path_dir(const char *path) {
assert(path);
size_t len = strlen(path);
while (len > 0 && path[len - 1] != '/') {
len = len - 1;
}
return strndup(path, len);
}