Repository navigation
Expand file tree
/
Copy pathrealpath.c
More file actions
223 lines (187 loc) · 5 KB
File metadata and controls
223 lines (187 loc) · 5 KB
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
/* realpath() - resolve absolute path Author: Erik van der Kouwe
* 4 December 2009
*
* Based on this specification:
* http://www.opengroup.org/onlinepubs/000095399/functions/realpath.html
*/
#include <errno.h>
#include <limits.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
static char *append_path_component(char *path, const char *component,
size_t component_length);
static char *process_path_component(const char *component,
size_t component_length, char *resolved_name, int last_part, int max_depth);
static char *realpath_recurse(const char *file_name, char *resolved_name,
int max_depth);
static char *remove_last_path_component(char *path);
static char *append_path_component(char *path, const char *component,
size_t component_length)
{
size_t path_length, slash_length;
/* insert or remove a slash? */
path_length = strlen(path);
slash_length =
((path[path_length - 1] == '/') ? 0 : 1) +
((component[0] == '/') ? 0 : 1) - 1;
/* check whether this fits */
if (path_length + slash_length + component_length >= PATH_MAX)
{
errno = ENAMETOOLONG;
return NULL;
}
/* insert slash if needed */
if (slash_length > 0)
path[path_length] = '/';
/* copy the bytes */
memcpy(path + path_length + slash_length, component, component_length);
path[path_length + slash_length + component_length] = 0;
return path;
}
static char *process_path_component(const char *component,
size_t component_length, char *resolved_name, int last_part, int max_depth)
{
char readlink_buffer[PATH_MAX + 1];
ssize_t readlink_buffer_length;
struct stat stat_buffer;
/* handle zero-length components */
if (!component_length)
{
if (last_part)
return resolved_name;
else
{
errno = ENOENT;
return NULL;
}
}
/* ignore current directory components */
if (component_length == 1 && component[0] == '.')
return resolved_name;
/* process parent directory components */
if (component_length == 2 && component[0] == '.' && component[1] == '.')
return remove_last_path_component(resolved_name);
/* not a special case, so just add the component */
if (!append_path_component(resolved_name, component, component_length))
return NULL;
/* stat partially resolved file */
if (lstat(resolved_name, &stat_buffer) < 0)
{
if (last_part && errno == ENOENT)
return resolved_name;
else
return NULL;
}
if (S_ISLNK(stat_buffer.st_mode))
{
/* resolve symbolic link */
readlink_buffer_length = readlink(resolved_name,
readlink_buffer,
sizeof(readlink_buffer) - 1);
if (readlink_buffer_length < 0)
return NULL;
readlink_buffer[readlink_buffer_length] = 0;
/* recurse to resolve path in link */
remove_last_path_component(resolved_name);
if (!realpath_recurse(readlink_buffer, resolved_name,
max_depth - 1))
return NULL;
/* stat symlink target */
if (lstat(resolved_name, &stat_buffer) < 0)
{
if (last_part && errno == ENOENT)
return resolved_name;
else
return NULL;
}
}
/* non-directories may appear only as the last component */
if (!last_part && !S_ISDIR(stat_buffer.st_mode))
{
errno = ENOTDIR;
return NULL;
}
return resolved_name;
}
static char *realpath_recurse(const char *file_name, char *resolved_name,
int max_depth)
{
const char *file_name_component;
/* avoid infinite recursion */
if (max_depth <= 0)
{
errno = ELOOP;
return NULL;
}
/* relative to root or to current? */
if (file_name[0] == '/')
{
/* relative to root */
resolved_name[0] = '/';
resolved_name[1] = '\0';
file_name++;
}
/* process the path component by component */
while (*file_name)
{
/* extract a slash-delimited component */
file_name_component = file_name;
while (*file_name && *file_name != '/')
file_name++;
/* check length of component */
if (file_name - file_name_component > PATH_MAX)
{
errno = ENAMETOOLONG;
return NULL;
}
/* add the component to the current result */
if (!process_path_component(
file_name_component,
file_name - file_name_component,
resolved_name,
!*file_name,
max_depth))
return NULL;
/* skip the slash(es) */
while (*file_name == '/')
file_name++;
}
return resolved_name;
}
static char *remove_last_path_component(char *path)
{
char *current, *slash;
/* find the last slash */
slash = NULL;
for (current = path; *current; current++)
if (*current == '/')
slash = current;
/* truncate after the last slash, but do not remove the root */
if (slash > path)
*slash = 0;
else if (slash == path)
slash[1] = 0;
return path;
}
char *realpath(const char *file_name, char *resolved_name)
{
/* check parameters */
if (!file_name || !resolved_name)
{
errno = EINVAL;
return NULL;
}
if (strlen(file_name) > PATH_MAX)
{
errno = ENAMETOOLONG;
return NULL;
}
/* basis to resolve against: root or CWD */
if (file_name[0] == '/')
*resolved_name = 0;
else if (!getcwd(resolved_name, PATH_MAX))
return NULL;
/* do the actual work */
return realpath_recurse(file_name, resolved_name, SYMLOOP_MAX);
}