summaryrefslogtreecommitdiff
path: root/src/kernel/vfs/path.c
blob: 8662f03fb7aab8d9e40e30b50fa45cbc4e277e07 (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
#include <kernel/vfs/path.h>
#include <kernel/panic.h>

bool path_simplify(const char *in, char *out, size_t len) {
	if (len == 0)     return false; // empty paths are invalid
	if (in[0] != '/') return false; // so are relative paths

	int depth = 0;
	int seg_len; // the length of the current path segment

	for (int i = 0; i < len; i += seg_len + 1) {
		// TODO implement assert
		if (in[i] != '/') panic();

		seg_len = 0;
		for (int j = i + 1; j < len; j++) {
			if (in[j] == '/') break;
			seg_len++;
		}

		/* example iteration, illustrated with terrible ASCII art
		 *
		 *          |i=5 |next i = i + seg_len + 1 = 10
		 *          v    v
		 *     /some/path/asdf
		 *           |--|
		 *           seg_len = 4
		 *           (segment starts at i+1) */

		if (seg_len == 0 || (seg_len == 1 && in[i + 1] == '.')) {
			// the segment is // or /./
			// the depth doesn't change
		} else if (seg_len == 2 && in[i + 1] == '.' && in[i + 2] == '.') {
			// the segment is /../
			if (--depth < 0)
				return false;
		} else {
			// normal segment
			depth++;
		}
	}

	return true;
}