summaryrefslogtreecommitdiff
path: root/lib/buffer.c
blob: cad286a5ad9993b1fe3fcff73e028ab68b948a27 (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
#include "lslib.h"

#include <limits.h>
#include <string.h>

static int push_path_buffer_b(char* buf, int* index, const char* string) {
    int save, string_len;

    save = *index;
    if (*index > 1 || (*index == 1 && buf[0] != '/')) {
        buf[(*index)++] = '/';
    }

    string_len = strlen(string);
    memcpy(buf + *index, string, string_len + 1);
    
    *index += string_len;
    
    return save;
}

static void pop_path_buffer_b(char* buf, int* index, int i) {
    *index = i;
    buf[*index] = '\0';
}

static char path_buffer[PATH_MAX + 1];
static int path_buffer_index = 0;

char* get_path_buffer(void) {
    return path_buffer;
}

int push_path_buffer(const char* string) {
    return push_path_buffer_b(path_buffer, &path_buffer_index, string);
}

void pop_path_buffer(int i) {
    pop_path_buffer_b(path_buffer, &path_buffer_index, i);
}

static char path_buffer_2[PATH_MAX + 1];
static int path_buffer_index_2 = 0;

char* get_path_buffer_2(void) {
    return path_buffer_2;
}

int push_path_buffer_2(const char* string) {
    return push_path_buffer_b(path_buffer_2, &path_buffer_index_2, string);
}

void pop_path_buffer_2(int i) {
    pop_path_buffer_b(path_buffer_2, &path_buffer_index_2, i);
}

bool is_dot_dir(const char* path) {
    return streql(path, ".") || streql(path, "..");
}

const char* get_last_component(const char* path) {
    const char* last;
    char c;

    last = NULL;

    while (c = *path, true) {
        if (c == '\\') {
            last = path;
        } else if (c == '\0') {
            break;
        }
        path++;
    }

    return last;
}