diff options
Diffstat (limited to 'src/lib/libc/stdlib/realpath.c')
-rw-r--r-- | src/lib/libc/stdlib/realpath.c | 251 |
1 files changed, 152 insertions, 99 deletions
diff --git a/src/lib/libc/stdlib/realpath.c b/src/lib/libc/stdlib/realpath.c index e349b7e068..21af4cf005 100644 --- a/src/lib/libc/stdlib/realpath.c +++ b/src/lib/libc/stdlib/realpath.c | |||
@@ -1,9 +1,6 @@ | |||
1 | /* $OpenBSD: realpath.c,v 1.14 2011/07/24 21:03:00 miod Exp $ */ | ||
1 | /* | 2 | /* |
2 | * Copyright (c) 1994 | 3 | * Copyright (c) 2003 Constantin S. Svintsoff <kostik@iclub.nsu.ru> |
3 | * The Regents of the University of California. All rights reserved. | ||
4 | * | ||
5 | * This code is derived from software contributed to Berkeley by | ||
6 | * Jan-Simon Pendry. | ||
7 | * | 4 | * |
8 | * Redistribution and use in source and binary forms, with or without | 5 | * Redistribution and use in source and binary forms, with or without |
9 | * modification, are permitted provided that the following conditions | 6 | * modification, are permitted provided that the following conditions |
@@ -13,18 +10,14 @@ | |||
13 | * 2. Redistributions in binary form must reproduce the above copyright | 10 | * 2. Redistributions in binary form must reproduce the above copyright |
14 | * notice, this list of conditions and the following disclaimer in the | 11 | * notice, this list of conditions and the following disclaimer in the |
15 | * documentation and/or other materials provided with the distribution. | 12 | * documentation and/or other materials provided with the distribution. |
16 | * 3. All advertising materials mentioning features or use of this software | 13 | * 3. The names of the authors may not be used to endorse or promote |
17 | * must display the following acknowledgement: | 14 | * products derived from this software without specific prior written |
18 | * This product includes software developed by the University of | 15 | * permission. |
19 | * California, Berkeley and its contributors. | ||
20 | * 4. Neither the name of the University nor the names of its contributors | ||
21 | * may be used to endorse or promote products derived from this software | ||
22 | * without specific prior written permission. | ||
23 | * | 16 | * |
24 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
25 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | 18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
26 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
27 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | 20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
28 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | 21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
29 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | 22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
30 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | 23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
@@ -34,126 +27,186 @@ | |||
34 | * SUCH DAMAGE. | 27 | * SUCH DAMAGE. |
35 | */ | 28 | */ |
36 | 29 | ||
37 | #if defined(LIBC_SCCS) && !defined(lint) | ||
38 | /*static char sccsid[] = "from: @(#)realpath.c 8.1 (Berkeley) 2/16/94";*/ | ||
39 | static char *rcsid = "$Id: realpath.c,v 1.1.1.1 1995/10/18 08:42:19 deraadt Exp $"; | ||
40 | #endif /* LIBC_SCCS and not lint */ | ||
41 | |||
42 | #include <sys/param.h> | 30 | #include <sys/param.h> |
43 | #include <sys/stat.h> | 31 | #include <sys/stat.h> |
44 | 32 | ||
45 | #include <errno.h> | 33 | #include <errno.h> |
46 | #include <fcntl.h> | ||
47 | #include <stdlib.h> | 34 | #include <stdlib.h> |
48 | #include <string.h> | 35 | #include <string.h> |
49 | #include <unistd.h> | 36 | #include <unistd.h> |
50 | 37 | ||
51 | /* | 38 | /* |
52 | * char *realpath(const char *path, char resolved_path[MAXPATHLEN]); | 39 | * char *realpath(const char *path, char resolved[PATH_MAX]); |
53 | * | 40 | * |
54 | * Find the real name of path, by removing all ".", ".." and symlink | 41 | * Find the real name of path, by removing all ".", ".." and symlink |
55 | * components. Returns (resolved) on success, or (NULL) on failure, | 42 | * components. Returns (resolved) on success, or (NULL) on failure, |
56 | * in which case the path which caused trouble is left in (resolved). | 43 | * in which case the path which caused trouble is left in (resolved). |
57 | */ | 44 | */ |
58 | char * | 45 | char * |
59 | realpath(path, resolved) | 46 | realpath(const char *path, char *resolved) |
60 | const char *path; | ||
61 | char *resolved; | ||
62 | { | 47 | { |
63 | struct stat sb; | 48 | struct stat sb; |
64 | int fd, n, rootd, serrno; | 49 | char *p, *q, *s; |
65 | char *p, *q, wbuf[MAXPATHLEN]; | 50 | size_t left_len, resolved_len; |
51 | unsigned symlinks; | ||
52 | int serrno, slen, mem_allocated; | ||
53 | char left[PATH_MAX], next_token[PATH_MAX], symlink[PATH_MAX]; | ||
66 | 54 | ||
67 | /* Save the starting point. */ | 55 | if (path[0] == '\0') { |
68 | if ((fd = open(".", O_RDONLY)) < 0) { | 56 | errno = ENOENT; |
69 | (void)strcpy(resolved, "."); | ||
70 | return (NULL); | 57 | return (NULL); |
71 | } | 58 | } |
72 | 59 | ||
73 | /* | 60 | serrno = errno; |
74 | * Find the dirname and basename from the path to be resolved. | 61 | |
75 | * Change directory to the dirname component. | 62 | if (resolved == NULL) { |
76 | * lstat the basename part. | 63 | resolved = malloc(PATH_MAX); |
77 | * if it is a symlink, read in the value and loop. | 64 | if (resolved == NULL) |
78 | * if it is a directory, then change to that directory. | 65 | return (NULL); |
79 | * get the current directory name and append the basename. | 66 | mem_allocated = 1; |
80 | */ | ||
81 | (void)strncpy(resolved, path, MAXPATHLEN - 1); | ||
82 | resolved[MAXPATHLEN - 1] = '\0'; | ||
83 | loop: | ||
84 | q = strrchr(resolved, '/'); | ||
85 | if (q != NULL) { | ||
86 | p = q + 1; | ||
87 | if (q == resolved) | ||
88 | q = "/"; | ||
89 | else { | ||
90 | do { | ||
91 | --q; | ||
92 | } while (q > resolved && *q == '/'); | ||
93 | q[1] = '\0'; | ||
94 | q = resolved; | ||
95 | } | ||
96 | if (chdir(q) < 0) | ||
97 | goto err1; | ||
98 | } else | 67 | } else |
99 | p = resolved; | 68 | mem_allocated = 0; |
100 | 69 | ||
101 | /* Deal with the last component. */ | 70 | symlinks = 0; |
102 | if (lstat(p, &sb) == 0) { | 71 | if (path[0] == '/') { |
103 | if (S_ISLNK(sb.st_mode)) { | 72 | resolved[0] = '/'; |
104 | n = readlink(p, resolved, MAXPATHLEN); | 73 | resolved[1] = '\0'; |
105 | if (n < 0) | 74 | if (path[1] == '\0') |
106 | goto err1; | 75 | return (resolved); |
107 | resolved[n] = '\0'; | 76 | resolved_len = 1; |
108 | goto loop; | 77 | left_len = strlcpy(left, path + 1, sizeof(left)); |
109 | } | 78 | } else { |
110 | if (S_ISDIR(sb.st_mode)) { | 79 | if (getcwd(resolved, PATH_MAX) == NULL) { |
111 | if (chdir(p) < 0) | 80 | if (mem_allocated) |
112 | goto err1; | 81 | free(resolved); |
113 | p = ""; | 82 | else |
83 | strlcpy(resolved, ".", PATH_MAX); | ||
84 | return (NULL); | ||
114 | } | 85 | } |
86 | resolved_len = strlen(resolved); | ||
87 | left_len = strlcpy(left, path, sizeof(left)); | ||
88 | } | ||
89 | if (left_len >= sizeof(left) || resolved_len >= PATH_MAX) { | ||
90 | errno = ENAMETOOLONG; | ||
91 | goto err; | ||
115 | } | 92 | } |
116 | 93 | ||
117 | /* | 94 | /* |
118 | * Save the last component name and get the full pathname of | 95 | * Iterate over path components in `left'. |
119 | * the current directory. | ||
120 | */ | ||
121 | (void)strcpy(wbuf, p); | ||
122 | if (getcwd(resolved, MAXPATHLEN) == 0) | ||
123 | goto err1; | ||
124 | |||
125 | /* | ||
126 | * Join the two strings together, ensuring that the right thing | ||
127 | * happens if the last component is empty, or the dirname is root. | ||
128 | */ | 96 | */ |
129 | if (resolved[0] == '/' && resolved[1] == '\0') | 97 | while (left_len != 0) { |
130 | rootd = 1; | 98 | /* |
131 | else | 99 | * Extract the next path component and adjust `left' |
132 | rootd = 0; | 100 | * and its length. |
101 | */ | ||
102 | p = strchr(left, '/'); | ||
103 | s = p ? p : left + left_len; | ||
104 | if (s - left >= sizeof(next_token)) { | ||
105 | errno = ENAMETOOLONG; | ||
106 | goto err; | ||
107 | } | ||
108 | memcpy(next_token, left, s - left); | ||
109 | next_token[s - left] = '\0'; | ||
110 | left_len -= s - left; | ||
111 | if (p != NULL) | ||
112 | memmove(left, s + 1, left_len + 1); | ||
113 | if (resolved[resolved_len - 1] != '/') { | ||
114 | if (resolved_len + 1 >= PATH_MAX) { | ||
115 | errno = ENAMETOOLONG; | ||
116 | goto err; | ||
117 | } | ||
118 | resolved[resolved_len++] = '/'; | ||
119 | resolved[resolved_len] = '\0'; | ||
120 | } | ||
121 | if (next_token[0] == '\0') | ||
122 | continue; | ||
123 | else if (strcmp(next_token, ".") == 0) | ||
124 | continue; | ||
125 | else if (strcmp(next_token, "..") == 0) { | ||
126 | /* | ||
127 | * Strip the last path component except when we have | ||
128 | * single "/" | ||
129 | */ | ||
130 | if (resolved_len > 1) { | ||
131 | resolved[resolved_len - 1] = '\0'; | ||
132 | q = strrchr(resolved, '/') + 1; | ||
133 | *q = '\0'; | ||
134 | resolved_len = q - resolved; | ||
135 | } | ||
136 | continue; | ||
137 | } | ||
133 | 138 | ||
134 | if (*wbuf) { | 139 | /* |
135 | if (strlen(resolved) + strlen(wbuf) + rootd + 1 > MAXPATHLEN) { | 140 | * Append the next path component and lstat() it. If |
141 | * lstat() fails we still can return successfully if | ||
142 | * there are no more path components left. | ||
143 | */ | ||
144 | resolved_len = strlcat(resolved, next_token, PATH_MAX); | ||
145 | if (resolved_len >= PATH_MAX) { | ||
136 | errno = ENAMETOOLONG; | 146 | errno = ENAMETOOLONG; |
137 | goto err1; | 147 | goto err; |
138 | } | 148 | } |
139 | if (rootd == 0) | 149 | if (lstat(resolved, &sb) != 0) { |
140 | (void)strcat(resolved, "/"); | 150 | if (errno == ENOENT && p == NULL) { |
141 | (void)strcat(resolved, wbuf); | 151 | errno = serrno; |
142 | } | 152 | return (resolved); |
153 | } | ||
154 | goto err; | ||
155 | } | ||
156 | if (S_ISLNK(sb.st_mode)) { | ||
157 | if (symlinks++ > MAXSYMLINKS) { | ||
158 | errno = ELOOP; | ||
159 | goto err; | ||
160 | } | ||
161 | slen = readlink(resolved, symlink, sizeof(symlink) - 1); | ||
162 | if (slen < 0) | ||
163 | goto err; | ||
164 | symlink[slen] = '\0'; | ||
165 | if (symlink[0] == '/') { | ||
166 | resolved[1] = 0; | ||
167 | resolved_len = 1; | ||
168 | } else if (resolved_len > 1) { | ||
169 | /* Strip the last path component. */ | ||
170 | resolved[resolved_len - 1] = '\0'; | ||
171 | q = strrchr(resolved, '/') + 1; | ||
172 | *q = '\0'; | ||
173 | resolved_len = q - resolved; | ||
174 | } | ||
143 | 175 | ||
144 | /* Go back to where we came from. */ | 176 | /* |
145 | if (fchdir(fd) < 0) { | 177 | * If there are any path components left, then |
146 | serrno = errno; | 178 | * append them to symlink. The result is placed |
147 | goto err2; | 179 | * in `left'. |
180 | */ | ||
181 | if (p != NULL) { | ||
182 | if (symlink[slen - 1] != '/') { | ||
183 | if (slen + 1 >= sizeof(symlink)) { | ||
184 | errno = ENAMETOOLONG; | ||
185 | goto err; | ||
186 | } | ||
187 | symlink[slen] = '/'; | ||
188 | symlink[slen + 1] = 0; | ||
189 | } | ||
190 | left_len = strlcat(symlink, left, sizeof(left)); | ||
191 | if (left_len >= sizeof(left)) { | ||
192 | errno = ENAMETOOLONG; | ||
193 | goto err; | ||
194 | } | ||
195 | } | ||
196 | left_len = strlcpy(left, symlink, sizeof(left)); | ||
197 | } | ||
148 | } | 198 | } |
149 | 199 | ||
150 | /* It's okay if the close fails, what's an fd more or less? */ | 200 | /* |
151 | (void)close(fd); | 201 | * Remove trailing slash except when the resolved pathname |
202 | * is a single "/". | ||
203 | */ | ||
204 | if (resolved_len > 1 && resolved[resolved_len - 1] == '/') | ||
205 | resolved[resolved_len - 1] = '\0'; | ||
152 | return (resolved); | 206 | return (resolved); |
153 | 207 | ||
154 | err1: serrno = errno; | 208 | err: |
155 | (void)fchdir(fd); | 209 | if (mem_allocated) |
156 | err2: (void)close(fd); | 210 | free(resolved); |
157 | errno = serrno; | ||
158 | return (NULL); | 211 | return (NULL); |
159 | } | 212 | } |