diff options
Diffstat (limited to 'src/lib/libc/stdlib/realpath.c')
-rw-r--r-- | src/lib/libc/stdlib/realpath.c | 198 |
1 files changed, 198 insertions, 0 deletions
diff --git a/src/lib/libc/stdlib/realpath.c b/src/lib/libc/stdlib/realpath.c new file mode 100644 index 0000000000..62e06b00be --- /dev/null +++ b/src/lib/libc/stdlib/realpath.c | |||
@@ -0,0 +1,198 @@ | |||
1 | /* | ||
2 | * Copyright (c) 2003 Constantin S. Svintsoff <kostik@iclub.nsu.ru> | ||
3 | * | ||
4 | * Redistribution and use in source and binary forms, with or without | ||
5 | * modification, are permitted provided that the following conditions | ||
6 | * are met: | ||
7 | * 1. Redistributions of source code must retain the above copyright | ||
8 | * notice, this list of conditions and the following disclaimer. | ||
9 | * 2. Redistributions in binary form must reproduce the above copyright | ||
10 | * notice, this list of conditions and the following disclaimer in the | ||
11 | * documentation and/or other materials provided with the distribution. | ||
12 | * 3. The names of the authors may not be used to endorse or promote | ||
13 | * products derived from this software without specific prior written | ||
14 | * permission. | ||
15 | * | ||
16 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND | ||
17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | ||
20 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
21 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
22 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
23 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
24 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
25 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
26 | * SUCH DAMAGE. | ||
27 | */ | ||
28 | |||
29 | #if 0 | ||
30 | #include <sys/cdefs.h> | ||
31 | __FBSDID("$FreeBSD: /usr/local/www/cvsroot/FreeBSD/src/lib/libc/stdlib/realpath.c,v 1.9.2.1 2003/05/22 17:11:44 fjoe Exp $"); | ||
32 | #endif | ||
33 | |||
34 | #if defined(LIBC_SCCS) && !defined(lint) | ||
35 | static char *rcsid = "$OpenBSD: realpath.c,v 1.12 2005/03/29 19:34:14 brad Exp $"; | ||
36 | #endif /* LIBC_SCCS and not lint */ | ||
37 | |||
38 | #include <sys/param.h> | ||
39 | #include <sys/stat.h> | ||
40 | |||
41 | #include <errno.h> | ||
42 | #include <stdlib.h> | ||
43 | #include <string.h> | ||
44 | #include <unistd.h> | ||
45 | |||
46 | /* | ||
47 | * char *realpath(const char *path, char resolved[PATH_MAX]); | ||
48 | * | ||
49 | * Find the real name of path, by removing all ".", ".." and symlink | ||
50 | * components. Returns (resolved) on success, or (NULL) on failure, | ||
51 | * in which case the path which caused trouble is left in (resolved). | ||
52 | */ | ||
53 | char * | ||
54 | realpath(const char *path, char resolved[PATH_MAX]) | ||
55 | { | ||
56 | struct stat sb; | ||
57 | char *p, *q, *s; | ||
58 | size_t left_len, resolved_len; | ||
59 | unsigned symlinks; | ||
60 | int serrno, slen; | ||
61 | char left[PATH_MAX], next_token[PATH_MAX], symlink[PATH_MAX]; | ||
62 | |||
63 | serrno = errno; | ||
64 | symlinks = 0; | ||
65 | if (path[0] == '/') { | ||
66 | resolved[0] = '/'; | ||
67 | resolved[1] = '\0'; | ||
68 | if (path[1] == '\0') | ||
69 | return (resolved); | ||
70 | resolved_len = 1; | ||
71 | left_len = strlcpy(left, path + 1, sizeof(left)); | ||
72 | } else { | ||
73 | if (getcwd(resolved, PATH_MAX) == NULL) { | ||
74 | strlcpy(resolved, ".", PATH_MAX); | ||
75 | return (NULL); | ||
76 | } | ||
77 | resolved_len = strlen(resolved); | ||
78 | left_len = strlcpy(left, path, sizeof(left)); | ||
79 | } | ||
80 | if (left_len >= sizeof(left) || resolved_len >= PATH_MAX) { | ||
81 | errno = ENAMETOOLONG; | ||
82 | return (NULL); | ||
83 | } | ||
84 | |||
85 | /* | ||
86 | * Iterate over path components in `left'. | ||
87 | */ | ||
88 | while (left_len != 0) { | ||
89 | /* | ||
90 | * Extract the next path component and adjust `left' | ||
91 | * and its length. | ||
92 | */ | ||
93 | p = strchr(left, '/'); | ||
94 | s = p ? p : left + left_len; | ||
95 | if (s - left >= sizeof(next_token)) { | ||
96 | errno = ENAMETOOLONG; | ||
97 | return (NULL); | ||
98 | } | ||
99 | memcpy(next_token, left, s - left); | ||
100 | next_token[s - left] = '\0'; | ||
101 | left_len -= s - left; | ||
102 | if (p != NULL) | ||
103 | memmove(left, s + 1, left_len + 1); | ||
104 | if (resolved[resolved_len - 1] != '/') { | ||
105 | if (resolved_len + 1 >= PATH_MAX) { | ||
106 | errno = ENAMETOOLONG; | ||
107 | return (NULL); | ||
108 | } | ||
109 | resolved[resolved_len++] = '/'; | ||
110 | resolved[resolved_len] = '\0'; | ||
111 | } | ||
112 | if (next_token[0] == '\0') | ||
113 | continue; | ||
114 | else if (strcmp(next_token, ".") == 0) | ||
115 | continue; | ||
116 | else if (strcmp(next_token, "..") == 0) { | ||
117 | /* | ||
118 | * Strip the last path component except when we have | ||
119 | * single "/" | ||
120 | */ | ||
121 | if (resolved_len > 1) { | ||
122 | resolved[resolved_len - 1] = '\0'; | ||
123 | q = strrchr(resolved, '/') + 1; | ||
124 | *q = '\0'; | ||
125 | resolved_len = q - resolved; | ||
126 | } | ||
127 | continue; | ||
128 | } | ||
129 | |||
130 | /* | ||
131 | * Append the next path component and lstat() it. If | ||
132 | * lstat() fails we still can return successfully if | ||
133 | * there are no more path components left. | ||
134 | */ | ||
135 | resolved_len = strlcat(resolved, next_token, PATH_MAX); | ||
136 | if (resolved_len >= PATH_MAX) { | ||
137 | errno = ENAMETOOLONG; | ||
138 | return (NULL); | ||
139 | } | ||
140 | if (lstat(resolved, &sb) != 0) { | ||
141 | if (errno == ENOENT && p == NULL) { | ||
142 | errno = serrno; | ||
143 | return (resolved); | ||
144 | } | ||
145 | return (NULL); | ||
146 | } | ||
147 | if (S_ISLNK(sb.st_mode)) { | ||
148 | if (symlinks++ > MAXSYMLINKS) { | ||
149 | errno = ELOOP; | ||
150 | return (NULL); | ||
151 | } | ||
152 | slen = readlink(resolved, symlink, sizeof(symlink) - 1); | ||
153 | if (slen < 0) | ||
154 | return (NULL); | ||
155 | symlink[slen] = '\0'; | ||
156 | if (symlink[0] == '/') { | ||
157 | resolved[1] = 0; | ||
158 | resolved_len = 1; | ||
159 | } else if (resolved_len > 1) { | ||
160 | /* Strip the last path component. */ | ||
161 | resolved[resolved_len - 1] = '\0'; | ||
162 | q = strrchr(resolved, '/') + 1; | ||
163 | *q = '\0'; | ||
164 | resolved_len = q - resolved; | ||
165 | } | ||
166 | |||
167 | /* | ||
168 | * If there are any path components left, then | ||
169 | * append them to symlink. The result is placed | ||
170 | * in `left'. | ||
171 | */ | ||
172 | if (p != NULL) { | ||
173 | if (symlink[slen - 1] != '/') { | ||
174 | if (slen + 1 >= sizeof(symlink)) { | ||
175 | errno = ENAMETOOLONG; | ||
176 | return (NULL); | ||
177 | } | ||
178 | symlink[slen] = '/'; | ||
179 | symlink[slen + 1] = 0; | ||
180 | } | ||
181 | left_len = strlcat(symlink, left, sizeof(left)); | ||
182 | if (left_len >= sizeof(left)) { | ||
183 | errno = ENAMETOOLONG; | ||
184 | return (NULL); | ||
185 | } | ||
186 | } | ||
187 | left_len = strlcpy(left, symlink, sizeof(left)); | ||
188 | } | ||
189 | } | ||
190 | |||
191 | /* | ||
192 | * Remove trailing slash except when the resolved pathname | ||
193 | * is a single "/". | ||
194 | */ | ||
195 | if (resolved_len > 1 && resolved[resolved_len - 1] == '/') | ||
196 | resolved[resolved_len - 1] = '\0'; | ||
197 | return (resolved); | ||
198 | } | ||