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
|
#include <string.h>
#include <syscall.h>
#include <unistd.h>
char cwd_buf[256];
char* getcwd(char* buf, size_t buf_len) {
if (buf_len > strlen(cwd_buf)) {
strcpy(buf, cwd_buf);
return buf;
} else {
return 0;
}
}
int chdir(const char* path) {
char cwd_buf2[256];
strcpy(cwd_buf2, cwd_buf);
if (!pathncat(cwd_buf2, path, 256)) return -1;
stat_t st;
if (!stat(cwd_buf2, &st)) return -1;
if (!(st.type & FT_DIR)) return -1;
strcpy(cwd_buf, cwd_buf2);
return 0;
}
char* pathncat(char* buf, const char* add, size_t buf_len) {
if (strchr(add, ':')) {
if (strlen(add) < buf_len) {
strcpy(buf, add);
return buf;
} else {
return 0;
}
} else {
char* sep_init = strchr(buf, ':');
if (add[0] == '/') {
if (strlen(add) + (sep_init + 1 - buf) < buf_len) {
strcpy(sep_init + 1, add);
return buf;
} else {
return 0;
}
} else {
//TODO: simplify '..'
char* end = buf + strlen(buf) - 1;
if (*end != '/') end++;
if (end + 1 - buf + strlen(add) < buf_len) {
*end = '/';
strcpy(end + 1, add);
return buf;
} else {
return 0;
}
}
return 0;
}
}
/* vim: set ts=4 sw=4 tw=0 noet :*/
|