-
Notifications
You must be signed in to change notification settings - Fork 189
/
getrealpath.c
451 lines (367 loc) · 10.9 KB
/
getrealpath.c
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
/* Copyright (c) 2022 Adrian Lopez
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution. */
#include "config.h"
#include "getrealpath.h"
#include "dir.h"
#include "sdirname.h"
#include "sbasename.h"
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#define ISFLAG(a,b) ((a & b) == b)
#ifndef GETREALPATH_MAXSYMLINKS
#define GETREALPATH_MAXSYMLINKS 40
#endif
#define DEFAULT_LINK_ALLOCATION_SIZE 16
/* read link contents into buffer allocated via malloc() */
char *getlink(const char *path, struct stat *s)
{
char *link;
char *buffer;
size_t allocated;
int linksize;
if (s->st_size > 0)
allocated = s->st_size + 1;
else
allocated = DEFAULT_LINK_ALLOCATION_SIZE;
buffer = malloc(allocated);
if (buffer == 0)
return 0;
do
{
link = buffer;
linksize = readlink(path, link, allocated);
if (linksize == -1)
{
free(link);
return 0;
}
if (linksize < allocated)
{
link[linksize] = '\0';
return link;
}
if (stat(path, s) != 0)
{
free(link);
return 0;
}
if (s->st_size != 0)
allocated = s->st_size + 1;
else
allocated *= 2;
} while (buffer = realloc(link, allocated));
free(link);
return 0;
}
/* replace dest[from .. through] with contents of src */
int replacestring(char *dest, size_t from, size_t through, size_t max, const char *src)
{
size_t srclength;
size_t destlength;
size_t moveto;
size_t newlength;
destlength = strlen(dest);
if (through >= destlength || from > through)
return 0;
srclength = strlen(src);
newlength = destlength + srclength - (through - from + 1);
if (newlength > max)
return 0;
memmove(dest + from + srclength, dest + through + 1, destlength - through);
memcpy(dest + from, src, srclength);
return 1;
}
/* print the resolved absolute file name for the specified path */
char *getrealpath(const char *path, unsigned int options)
{
char *scratch;
char *cwd;
char *link;
char *newmem;
char *dirname;
char *basename;
char save;
size_t tail;
size_t next;
size_t pathlength;
size_t linklength;
size_t cwdlength;
size_t allocated;
size_t links;
size_t x;
struct stat st;
struct stat st0;
int pathexists;
/* run stat on unmodified path, for later use */
pathexists = stat(path, &st0) == 0;
if (!pathexists && !ISFLAG(options, GETREALPATH_IGNORE_MISSING_BASENAME))
return 0;
/* optionally ignore the last component if it does not exist */
if (ISFLAG(options, GETREALPATH_IGNORE_MISSING_BASENAME) && !pathexists)
{
dirname = malloc(strlen(path) + 1);
if (dirname == 0)
return 0;
basename = malloc(strlen(path) + 1);
if (basename == 0)
return 0;
sdirname(dirname, path);
sbasename(basename, path);
if (stat(dirname, &st0) != 0)
return 0;
link = getrealpath(dirname, options ^ GETREALPATH_IGNORE_MISSING_BASENAME);
if (link == 0)
{
free(basename);
free(dirname);
return 0;
}
scratch = malloc(strlen(link) + strlen(basename) + 2);
if (scratch == 0)
{
free(basename);
free(dirname);
return 0;
}
strcpy(scratch, link);
strcat(scratch, "/");
strcat(scratch, basename);
if (stat(dirname, &st) != 0)
{
free(link);
free(basename);
free(dirname);
free(scratch);
return 0;
}
free(link);
free(basename);
free(dirname);
if
(
st.st_dev != st0.st_dev ||
st.st_ino != st0.st_ino
)
{
free(scratch);
return 0;
}
return scratch;
}
if (path[0] == '/')
/* if path is an absolute path, copy its contents to scratch buffer */
{
allocated = strlen(path) + 1;
scratch = malloc(allocated);
if (scratch == 0)
return 0;
memcpy(scratch, path, allocated);
}
else
/* if path is a relative path, combine cwd and path into scratch buffer */
{
cwd = getworkingdirectory();
if (cwd == 0)
return 0;
pathlength = strlen(path);
cwdlength = strlen(cwd);
allocated = pathlength + cwdlength + 2;
scratch = malloc(allocated);
if (scratch == 0)
{
free(cwd);
return 0;
}
memcpy(scratch, cwd, cwdlength);
scratch[cwdlength] = '/';
memcpy(scratch + cwdlength + 1, path, pathlength + 1);
free(cwd);
}
tail = 0;
next = 0;
links = 0;
while (scratch[next] != '\0')
{
if (scratch[next] == '/')
/* advance to start of filename */
{
/* keep the first slash */
scratch[tail++] = scratch[next++];
/* skip the rest */
while (scratch[next] == '/')
++next;
continue;
}
else if (scratch[next] == '.')
/* handle filenames beginning with "." */
{
switch (scratch[next + 1])
{
case '/':
/* collapse /./ down to / */
do
++next;
while (scratch[next] == '/');
continue;
case '\0':
/* truncate trailing /. down to / */
++next;
continue;
case '.':
if (scratch[next + 2] == '/')
/* go up one directory from /../ */
{
if (tail > 1)
tail -= 2;
else
tail = 0;
while (scratch[tail] != '/')
--tail;
if (tail == 0)
{
tail = 1;
++next;
}
else
{
next += 2;
}
}
else if (scratch[next + 2] == '\0')
/* go up one directory from trailing /.. */
{
if (tail > 1)
tail -= 2;
else
tail = 0;
while (scratch[tail] != '/')
--tail;
if (tail == 0)
{
tail = 1;
++next;
}
else
{
next += 2;
}
}
else
/* process .. of regular filename beginning with .. */
{
do
scratch[tail++] = scratch[next++];
while (scratch[next] == '.');
}
break;
default:
/* process . of regular filename begining with . */
scratch[tail++] = scratch[next++];
break;
}
}
else
/* process regular filename characters */
{
do
scratch[tail++] = scratch[next++];
while (scratch[next] != '\0' && scratch[next] != '/');
}
save = scratch[tail];
scratch[tail] = '\0';
if (lstat(scratch, &st) != 0)
{
free(scratch);
return 0;
}
if (S_ISLNK(st.st_mode))
{
if (links++ > GETREALPATH_MAXSYMLINKS)
{
free(scratch);
return 0;
}
link = getlink(scratch, &st);
if (link == 0)
{
free(scratch);
return 0;
}
pathlength = strlen(scratch) + strlen(scratch + next);
linklength = strlen(link);
if (pathlength + linklength + 1 > allocated)
{
allocated += pathlength + linklength + 1;
newmem = realloc(scratch, allocated);
if (newmem == 0)
{
free(scratch);
return 0;
}
scratch = newmem;
}
scratch[tail] = save;
if (link[0] == '/')
/* link represents an absolute path */
{
memmove(scratch + tail, scratch + next, strlen(scratch + next) + 1);
replacestring(scratch, 0, tail - 1, allocated, link);
/* start over */
tail = 0;
next = 0;
}
else
/* link represents a relative path */
{
memmove(scratch + tail, scratch + next, strlen(scratch + next) + 1);
x = tail;
while (scratch[x] != '/')
--x;
replacestring(scratch, x + 1, tail - 1, allocated, link);
tail = x;
next = x;
}
free(link);
}
else
{
scratch[tail] = save;
}
}
/* terminate path */
if (tail > 1 && scratch[tail - 1] == '/')
scratch[tail - 1] = '\0';
else
scratch[tail] = '\0';
/* confirm that scratch and path both point to the same file */
if (stat(scratch, &st) != 0)
{
free(scratch);
return 0;
}
if
(
st.st_dev != st0.st_dev ||
st.st_ino != st0.st_ino
)
{
free(scratch);
return 0;
}
return scratch;
}