File: normfile.c

package info (click to toggle)
dv4l 1.0-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 576 kB
  • sloc: ansic: 7,541; makefile: 382; sh: 260
file content (288 lines) | stat: -rw-r--r-- 4,771 bytes parent folder | download | duplicates (4)
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/* Copyright (C) 2008 Free Software Foundation, Inc.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; version 2.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 * Author: Wolfgang Beck <bewo at users.berlios.de> 2008
 */

#include <limits.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/*
 * Unfortunately, realpath(3) requires that the path exists.
 * As we are faking files, we need to replicate its
 * functionality, at least the part that doesn't deal with links
 */

typedef enum { Null, Lim, Dot1, Dot2, DName, Fin } state_t;

typedef struct {
    state_t nf_st;
    const char *nf_buf;
} normfile_t;

#ifdef UNIT_TEST
static void st_print(state_t st)
{
    const char *s;

    switch(st) {
	case Null: s = "Null"; break;
	case Lim: s = "Lim"; break;
	case Dot1: s = "Dot1"; break;
	case Dot2: s = "Dot2"; break;
	case DName: s = "DName"; break;
	case Fin: s = "Fin"; break;
    }

    printf("%s\n", s);
}
#endif

state_t lim_ev(const normfile_t *nf, const char **cp, char **dp)
{
    const char *c;
    char *d;
    state_t st;

    c = *cp;
    d = *dp;
    switch(nf->nf_st) {
	case Lim:
	    ++c;
	    st = Lim;
	    break;
	case Dot2:
	    /* remove last dir elem from d */
	    while(d >= nf->nf_buf && *d != '/') --d;
	    if(d != nf->nf_buf) --d;
	    if(*d != '/') ++d;
	    ++c;
	    st = Lim;
	    break;
	case Dot1:
	    ++c;
	    st = Lim;
	    break;
	case DName:
	default:
	    ++c;
	    st = Lim;
	    break;

    }
    *cp = c;
    *dp = d;

    return st;
}

state_t dot_ev(const normfile_t *nf, const char **cp, char **dp)
{
    const char *c;
    char *d;
    state_t st;

    c = *cp;
    d = *dp;
    switch(nf->nf_st) {
	case Null:
	    st = Dot1;
	    ++c;
	    break;
	case Dot1:
	    st = Dot2;
	    ++c;
	    break;
	case Dot2:
	    *d = '.';
	    ++d;
	    *d = '.';
	    ++d;
	    *d = *c;
	    ++c;
	    ++d;
	    st = DName;
	    break;
	case Fin:
	case Lim:
	    ++c;
	    st = Dot1;
	    break;
	case DName:
	default:
	    *d = *c;
	    ++c;
	    ++d;
	    st = DName;
	    break;
    }
    *cp = c;
    *dp = d;

    return st;
}

state_t char_ev(const normfile_t *nf, const char **cp, char **dp)
{
    const char *c;
    char *d;
    state_t st;

    c = *cp;
    d = *dp;
    switch(nf->nf_st) {
	case Fin:
	case Lim:
	    *d = '/';
	    ++d;
	    *d = *c;
	    ++c;
	    ++d;
	    st = DName;
	    break;
	case Dot2:
	    *d = '/';
	    ++d;
	    *d = '.';
	    ++d;
	    *d = '.';
	    ++d;
	    *d = *c;
	    ++c;
	    ++d;
	    st = DName;
	    break;
	case Dot1:
	    *d = '/';
	    ++d;
	    *d = '.';
	    ++d;
	    *d = *c;
	    ++d;
	    ++c;
	    st = DName;
	    break;
	case Null:
	case DName:
	    *d = *c;
	    ++d;
	    ++c;
	    st = DName;
	    break;
    }
    *cp = c;
    *dp = d;

    return st;
}

state_t null_ev(const normfile_t *nf, const char **cp, char **dp)
{
    const char *c;
    char *d;
    state_t st;

    c = *cp;
    d = *dp;
    switch(nf->nf_st) {
	case Fin:
	case Null:
	case Dot1:
	case DName:
	    *d = *c;
	    st = Fin;
	    break;
	case Lim:
	    ++d;
	    *d = *c;
	    st = Fin;
	    break;
	case Dot2:
	    while(d != nf->nf_buf && *d != '/') --d;
	    if(d == nf->nf_buf) {
		++d;
	    }
	    *d = *c;
	    st = Fin;
	    break;
    }

    *cp = c;
    *dp = d;

    return st;
}
char *normalize(const char *path, char *normpath)
{
    const char *c;
    char *d;
    state_t st;
    normfile_t nf;
    char buf[PATH_MAX];

    if(realpath(path, normpath) != NULL) {
	return normpath;
    }

    nf.nf_st = Null;
    nf.nf_buf = normpath;

    c = path;
    if(*c != '/') {
	getcwd(buf, PATH_MAX);
	d = buf + strlen(buf);
	*d = '/';
	++d;
	strcat(d, path);
	c = buf;
    }
    d = normpath;
    st = Null;
    while(st != Fin) {
	switch(*c) {
	    case '/':
		st = lim_ev(&nf, &c, &d);
		break;
	    case '.':
		st = dot_ev(&nf, &c, &d);
		break;
	    case '\0':
		st = null_ev(&nf, &c, &d);
		break;
	    default:
		st = char_ev(&nf, &c, &d);
		break;
	}
	nf.nf_st = st;
    }

    return normpath;
}

#ifdef UNIT_TEST
int main(int argc, char **argv)
{
    char buf[PATH_MAX];
    int rv;

    rv = normalize(argv[1], buf);
    printf("<%s> -> <%s>\n", argv[1], buf);

    return rv;
}
#endif