diff options
author | dzwdz | 2023-08-14 18:51:07 +0200 |
---|---|---|
committer | dzwdz | 2023-08-14 18:51:07 +0200 |
commit | 642b5fb0007b64c77d186fcb018d571152ee1d47 (patch) | |
tree | 1c466461f3602d306be309a053edae558ef2568e /src/libk/path.c | |
parent | 8050069c57b729c18c19b1a03ab6e4bf63b4735e (diff) |
reorganization: first steps
Diffstat (limited to 'src/libk/path.c')
-rw-r--r-- | src/libk/path.c | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/src/libk/path.c b/src/libk/path.c new file mode 100644 index 0000000..4e3077b --- /dev/null +++ b/src/libk/path.c @@ -0,0 +1,53 @@ +#include <assert.h> +#include <camellia/path.h> +#include <shared/mem.h> +#include <stdbool.h> + +size_t path_simplify(const char *in, char *out, size_t len) { + if (len == 0) return 0; /* empty paths are invalid */ + if (in[0] != '/') return 0; /* so are relative paths */ + + int seg_len; + int out_pos = 0; + bool directory = false; + + for (size_t i = 0; i < len; i += seg_len + 1) { + assert(in[i] == '/'); + + seg_len = 0; + directory = false; + for (size_t j = i + 1; j < len; j++) { + if (in[j] == '/') { + directory = true; + break; + } + seg_len++; + } + + /* |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] == '.')) { + /* // or /./ */ + directory = true; + } else if (seg_len == 2 && in[i + 1] == '.' && in[i + 2] == '.') { + /* /../ */ + directory = true; + /* try to backtrack to the last slash */ + while (--out_pos >= 0 && out[out_pos] != '/'); + if (out_pos < 0) return 0; + } else { + /* a normal segment, e.g. /asdf/ */ + out[out_pos] = '/'; + memcpy(&out[out_pos + 1], &in[i + 1], seg_len); + out_pos += seg_len + 1; + } + } + + if (directory) out[out_pos++] = '/'; + assert(0 < out_pos && (size_t)out_pos <= len); + return out_pos; +} |