aboutsummaryrefslogtreecommitdiff
path: root/crealpath.c
blob: 9403911e38c545ab1e59f3e4f31f04b5d90aa9c5 (plain) (blame)
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#define _POSIX_C_SOURCE 200809L

#include "crealpath.h"
#include <unistd.h>
#include <errno.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

static char *alloc_cwd(void)
{
    size_t len = 1;
    char *p = NULL;

    for (;;)
    {
        char *const pp = realloc(p, len);

        if (!pp)
        {
            fprintf(stderr, "%s: realloc(3): %s\n", __func__, strerror(errno));
            break;
        }

        p = pp;

        if (!getcwd(pp, len))
        {
            if (errno != ERANGE)
            {
                fprintf(stderr, "%s: getcwd(3): %s\n",
                    __func__, strerror(errno));
                break;
            }
            else
                len++;
        }
        else
            return p;
    }

    free(p);
    return NULL;
}

static char *get_parent(char *p)
{
    char *last;
    size_t len;

    if (!p || !(last = strrchr(p, '/')) || strlen(last) < strlen("/a"))
    {
        fprintf(stderr, "%s: parent folder expected\n", __func__);
        return NULL;
    }

    len = last == p ? strlen("/") : last - p;

    if (!(p = realloc(p, len + 1)))
    {
        fprintf(stderr, "%s: realloc(3): %s\n", __func__, strerror(errno));
        return NULL;
    }

    p[len] = '\0';
    return p;
}

static char *get_component(const char **const path)
{
    char *ret;

    while (**path == '/')
        (*path)++;

    const char *const next = strchr(*path, '/');
    const size_t len = next ? next - *path : strlen(*path);

    if (!(ret = malloc(len + 1)))
    {
        fprintf(stderr, "%s: malloc(3): %s\n", __func__, strerror(errno));
        return NULL;
    }

    memcpy(ret, *path, len);
    ret[len] = '\0';
    *path += len;
    return ret;
}

static char *resolve_component(const char **const path, char *p)
{
    char *const c = get_component(path);

    if (!*c)
    {
        if (!p && !(p = strdup("/")))
        {
            fprintf(stderr, "%s: strdup(3): %s\n", __func__, strerror(errno));
            goto end;
        }

        goto end;
    }
    else if (!strcmp(c, "..") && !(p = get_parent(p)))
    {
        fprintf(stderr, "%s: get_parent failed\n", __func__);
        goto end;
    }
    else if (strcmp(c, "."))
    {
        const size_t len = p ? strlen(p) : 0, clen = strlen(c);

        if (!(p = realloc(p, len + strlen("/") + clen + 1)))
        {
            fprintf(stderr, "%s: realloc(3): %s\n",
                __func__, strerror(errno));
            goto end;
        }
        else
        {
            p[len] = '/';
            memcpy(&p[len + 1], c, clen);
            p[len + 1 + clen] = '\0';
        }
    }

end:
    free(c);
    return p;
}

static char *resolve(const char *path, char *p)
{
    while (*path)
        if (!(p = resolve_component(&path, p)))
        {
            fprintf(stderr, "%s: resolve_component failed\n", __func__);
            return NULL;
        }

    return p;
}

char *crealpath(const char *const path)
{
    char *p = NULL, *ret = NULL;

    if (!*path)
    {
        fprintf(stderr, "%s: expected non-empty path\n", __func__);
        goto end;
    }
    else if (*path != '/' && !(p = alloc_cwd()))
    {
        fprintf(stderr, "%s: alloc_cwd failed\n", __func__);
        goto end;
    }
    else if (!(p = resolve(path, p)))
    {
        fprintf(stderr, "%s: resolve failed\n", __func__);
        goto end;
    }

    ret = p;

end:
    if (!ret)
        free(p);

    return ret;
}