~ruther/CTU-FEE-B0B35APO-Semestral-project

ref: e8073d35e16c95715b66cec97021333d16f95911 CTU-FEE-B0B35APO-Semestral-project/file-browser/src/path.c -rw-r--r-- 703 bytes
e8073d35 — František Boháček fix: some file access function types 4 years ago
                                                                                
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
#include "path.h"
#include <stdlib.h>
#include <string.h>

char *path_join(char *base, char *relative) {
  size_t base_len = strlen(base);
  size_t relative_len = strlen(relative);

  while (base[base_len - 1] == '/' && base_len > 0) {
    base_len--;
  }

  while (relative[0] == '/' && relative_len > 0) {
    relative_len--;
    relative++;
  }

  size_t new_len = base_len + relative_len + 1; // length of the string
  char *out = malloc((new_len + 1) * sizeof(out)); // length of the string plus one for \0
  if (out == NULL) {
    return NULL;
  }
  out[new_len] = '\0';
  out[base_len] = '/';

  memcpy(out, base, base_len);
  memcpy(out + base_len + 1, relative, relative_len);

  return out;
}