summaryrefslogtreecommitdiff
path: root/userland/libc/stdlib/realpath.c
blob: 86ac4eda49a2aea71e6cea8a63106ca8d2ef89e5 (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
#include <assert.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <tb/sb.h>
#include <tb/sv.h>
#include <unistd.h>

char *realpath(const char *filename, char *resolvedname) {
  if (!filename) {
    return -EINVAL;
  }

  char cwd[256];
  getcwd(cwd, 256);
  strcat(cwd, filename); // FIXME: bounds check

  struct sb string;
  sb_init_capacity(&string, 256);

  struct sv path = C_TO_SV(cwd);

  int ignore = 0;
  int last_was_dotdot = 0;

  int was_root = 0;
  for (;;) {
    ignore = 0;
    was_root = 0;
    struct sv dir = sv_end_split_delim(path, &path, '/');

    if (sv_partial_eq(dir, C_TO_SV("/"))) {
      was_root = 1;
    }

    if (sv_eq(dir, C_TO_SV("/"))) {
      ignore = 1;
    }

    if (sv_eq(dir, C_TO_SV("/."))) {
      ignore = 1;
    }

    if (sv_eq(dir, C_TO_SV("/.."))) {
      last_was_dotdot = 1;
      ignore = 1;
    } else {
      if (last_was_dotdot) {
        ignore = 1;
      }
      last_was_dotdot = 0;
    }

    if (!ignore && !last_was_dotdot) {
      sb_prepend_sv(&string, dir);
    }

    if (sv_isempty(path)) {
      if (was_root && ignore && sb_isempty(&string)) {
        sb_prepend_sv(&string, C_TO_SV("/"));
      }
      break;
    }
  }
  char *result = sv_copy_to_c(SB_TO_SV(string), resolvedname, 256);
  sb_free(&string);
  return result;
}