summaryrefslogtreecommitdiff
path: root/src/libc/unistd/cwd.c
blob: 7689d5699242d507deff79c296707b41f76bf558 (plain)
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
#include <camellia.h>
#include <camellia/path.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

static char *cwd = NULL, *cwd2 = NULL;
static size_t cwdcapacity = 0;

static const char *getrealcwd(void) {
	if (cwd) return cwd;
	if (__initialcwd) return __initialcwd;
	return "/";
}

int chdir(const char *path) {
	hid_t h;
	char *tmp;
	size_t len = absolutepath(NULL, path, 0) + 1; /* +1 for the trailing slash */
	if (cwdcapacity < len) {
		cwdcapacity = len;
		if (cwd) {
			cwd = realloc(cwd, len);
			cwd2 = realloc(cwd2, len);
		} else {
			size_t initlen = strlen(__initialcwd) + 1;
			if (len < initlen)
				len = initlen;
			cwd = malloc(initlen);
			cwd2 = malloc(initlen);
			memcpy(cwd, __initialcwd, initlen);
		}
	}
	absolutepath(cwd2, path, cwdcapacity);
	len = strlen(cwd2);
	if (cwd2[len - 1] != '/') {
		cwd2[len] = '/';
		cwd2[len + 1] = '\0';
	}

	/* check if exists */
	h = camellia_open(cwd2, OPEN_READ);
	if (h < 0) return errno = ENOENT, -1;
	close(h);

	tmp  = cwd;
	cwd  = cwd2;
	cwd2 = tmp;
	return 0;
}

char *getcwd(char *buf, size_t capacity) {
	const char *realcwd = getrealcwd();
	size_t len = strlen(realcwd) + 1;
	if (capacity < len) {
		errno = capacity == 0 ? EINVAL : ERANGE;
		return NULL;
	}
	memcpy(buf, realcwd, len);
	return buf;
}

size_t absolutepath(char *out, const char *in, size_t size) {
	const char *realcwd = getrealcwd();
	size_t len, pos = 0;
	if (!in) return strlen(realcwd) + 1;

	if (!(in[0] == '/')) {
		len = strlen(realcwd);
		if (pos + len <= size && out != realcwd)
			memcpy(out + pos, realcwd, len);
		pos += len;

		if (realcwd[len - 1] != '/') {
			if (pos + 1 <= size) out[pos] = '/';
			pos++;
		}
	}

	len = strlen(in);
	if (pos + len <= size)
		memcpy(out + pos, in, len);
	pos += len;

	if (pos <= size) {
		pos = path_simplify(out, out, pos);
		if (pos == 0) return 0;
	}

	if (pos + 1 <= size) out[pos] = '\0';
	pos++;

	return pos;
}