summaryrefslogtreecommitdiff
path: root/lib/buffer.c
blob: 2bee94b29c9c80c07b08fb11ba1a7765a785626e (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
#include "buffer.h"
#include "convert.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, "..");
}