The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1 | /* |
Elliott Hughes | ff590ca | 2010-11-04 17:53:06 -0700 | [diff] [blame] | 2 | * Copyright (c) 2003 Constantin S. Svintsoff <kostik@iclub.nsu.ru> |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 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. |
Elliott Hughes | ff590ca | 2010-11-04 17:53:06 -0700 | [diff] [blame] | 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. |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 15 | * |
Elliott Hughes | ff590ca | 2010-11-04 17:53:06 -0700 | [diff] [blame] | 16 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
Elliott Hughes | ff590ca | 2010-11-04 17:53:06 -0700 | [diff] [blame] | 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 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 | |
Ben Cheng | 21eab51 | 2012-03-13 23:04:57 -0700 | [diff] [blame] | 29 | #if defined(LIBC_SCCS) && !defined(lint) |
| 30 | static char sccsid[] = "@(#)realpath.c 8.1 (Berkeley) 2/16/94"; |
| 31 | #endif /* LIBC_SCCS and not lint */ |
| 32 | #include <sys/cdefs.h> |
| 33 | __FBSDID("$FreeBSD: release/9.0.0/lib/libc/stdlib/realpath.c 217144 2011-01-08 11:04:30Z kib $"); |
| 34 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 35 | #include <sys/param.h> |
| 36 | #include <sys/stat.h> |
| 37 | |
| 38 | #include <errno.h> |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 39 | #include <stdlib.h> |
| 40 | #include <string.h> |
| 41 | #include <unistd.h> |
| 42 | |
| 43 | /* |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 44 | * Find the real name of path, by removing all ".", ".." and symlink |
| 45 | * components. Returns (resolved) on success, or (NULL) on failure, |
| 46 | * in which case the path which caused trouble is left in (resolved). |
| 47 | */ |
| 48 | char * |
Ben Cheng | 21eab51 | 2012-03-13 23:04:57 -0700 | [diff] [blame] | 49 | realpath(const char * __restrict path, char * __restrict resolved) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 50 | { |
| 51 | struct stat sb; |
Elliott Hughes | ff590ca | 2010-11-04 17:53:06 -0700 | [diff] [blame] | 52 | char *p, *q, *s; |
| 53 | size_t left_len, resolved_len; |
| 54 | unsigned symlinks; |
Ben Cheng | 21eab51 | 2012-03-13 23:04:57 -0700 | [diff] [blame] | 55 | int m, serrno, slen; |
Elliott Hughes | ff590ca | 2010-11-04 17:53:06 -0700 | [diff] [blame] | 56 | char left[PATH_MAX], next_token[PATH_MAX], symlink[PATH_MAX]; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 57 | |
Ben Cheng | 21eab51 | 2012-03-13 23:04:57 -0700 | [diff] [blame] | 58 | if (path == NULL) { |
| 59 | errno = EINVAL; |
| 60 | return (NULL); |
| 61 | } |
| 62 | if (path[0] == '\0') { |
| 63 | errno = ENOENT; |
| 64 | return (NULL); |
| 65 | } |
Elliott Hughes | ff590ca | 2010-11-04 17:53:06 -0700 | [diff] [blame] | 66 | serrno = errno; |
Ben Cheng | 21eab51 | 2012-03-13 23:04:57 -0700 | [diff] [blame] | 67 | if (resolved == NULL) { |
| 68 | resolved = malloc(PATH_MAX); |
| 69 | if (resolved == NULL) |
| 70 | return (NULL); |
| 71 | m = 1; |
| 72 | } else |
| 73 | m = 0; |
Elliott Hughes | ff590ca | 2010-11-04 17:53:06 -0700 | [diff] [blame] | 74 | symlinks = 0; |
| 75 | if (path[0] == '/') { |
| 76 | resolved[0] = '/'; |
| 77 | resolved[1] = '\0'; |
| 78 | if (path[1] == '\0') |
| 79 | return (resolved); |
| 80 | resolved_len = 1; |
| 81 | left_len = strlcpy(left, path + 1, sizeof(left)); |
| 82 | } else { |
| 83 | if (getcwd(resolved, PATH_MAX) == NULL) { |
Ben Cheng | 21eab51 | 2012-03-13 23:04:57 -0700 | [diff] [blame] | 84 | if (m) |
| 85 | free(resolved); |
| 86 | else { |
| 87 | resolved[0] = '.'; |
| 88 | resolved[1] = '\0'; |
| 89 | } |
Elliott Hughes | ff590ca | 2010-11-04 17:53:06 -0700 | [diff] [blame] | 90 | return (NULL); |
| 91 | } |
| 92 | resolved_len = strlen(resolved); |
| 93 | left_len = strlcpy(left, path, sizeof(left)); |
| 94 | } |
| 95 | if (left_len >= sizeof(left) || resolved_len >= PATH_MAX) { |
Ben Cheng | 21eab51 | 2012-03-13 23:04:57 -0700 | [diff] [blame] | 96 | if (m) |
| 97 | free(resolved); |
Elliott Hughes | ff590ca | 2010-11-04 17:53:06 -0700 | [diff] [blame] | 98 | errno = ENAMETOOLONG; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 99 | return (NULL); |
| 100 | } |
| 101 | |
| 102 | /* |
Elliott Hughes | ff590ca | 2010-11-04 17:53:06 -0700 | [diff] [blame] | 103 | * Iterate over path components in `left'. |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 104 | */ |
Elliott Hughes | ff590ca | 2010-11-04 17:53:06 -0700 | [diff] [blame] | 105 | while (left_len != 0) { |
| 106 | /* |
| 107 | * Extract the next path component and adjust `left' |
| 108 | * and its length. |
| 109 | */ |
| 110 | p = strchr(left, '/'); |
| 111 | s = p ? p : left + left_len; |
| 112 | if (s - left >= sizeof(next_token)) { |
Ben Cheng | 21eab51 | 2012-03-13 23:04:57 -0700 | [diff] [blame] | 113 | if (m) |
| 114 | free(resolved); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 115 | errno = ENAMETOOLONG; |
Elliott Hughes | ff590ca | 2010-11-04 17:53:06 -0700 | [diff] [blame] | 116 | return (NULL); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 117 | } |
Elliott Hughes | ff590ca | 2010-11-04 17:53:06 -0700 | [diff] [blame] | 118 | memcpy(next_token, left, s - left); |
| 119 | next_token[s - left] = '\0'; |
| 120 | left_len -= s - left; |
| 121 | if (p != NULL) |
| 122 | memmove(left, s + 1, left_len + 1); |
| 123 | if (resolved[resolved_len - 1] != '/') { |
| 124 | if (resolved_len + 1 >= PATH_MAX) { |
Ben Cheng | 21eab51 | 2012-03-13 23:04:57 -0700 | [diff] [blame] | 125 | if (m) |
| 126 | free(resolved); |
Elliott Hughes | ff590ca | 2010-11-04 17:53:06 -0700 | [diff] [blame] | 127 | errno = ENAMETOOLONG; |
| 128 | return (NULL); |
| 129 | } |
| 130 | resolved[resolved_len++] = '/'; |
| 131 | resolved[resolved_len] = '\0'; |
| 132 | } |
| 133 | if (next_token[0] == '\0') |
| 134 | continue; |
| 135 | else if (strcmp(next_token, ".") == 0) |
| 136 | continue; |
| 137 | else if (strcmp(next_token, "..") == 0) { |
| 138 | /* |
| 139 | * Strip the last path component except when we have |
| 140 | * single "/" |
| 141 | */ |
| 142 | if (resolved_len > 1) { |
| 143 | resolved[resolved_len - 1] = '\0'; |
| 144 | q = strrchr(resolved, '/') + 1; |
| 145 | *q = '\0'; |
| 146 | resolved_len = q - resolved; |
| 147 | } |
| 148 | continue; |
| 149 | } |
| 150 | |
| 151 | /* |
| 152 | * Append the next path component and lstat() it. If |
| 153 | * lstat() fails we still can return successfully if |
| 154 | * there are no more path components left. |
| 155 | */ |
| 156 | resolved_len = strlcat(resolved, next_token, PATH_MAX); |
| 157 | if (resolved_len >= PATH_MAX) { |
Ben Cheng | 21eab51 | 2012-03-13 23:04:57 -0700 | [diff] [blame] | 158 | if (m) |
| 159 | free(resolved); |
Elliott Hughes | ff590ca | 2010-11-04 17:53:06 -0700 | [diff] [blame] | 160 | errno = ENAMETOOLONG; |
| 161 | return (NULL); |
| 162 | } |
| 163 | if (lstat(resolved, &sb) != 0) { |
| 164 | if (errno == ENOENT && p == NULL) { |
| 165 | errno = serrno; |
| 166 | return (resolved); |
| 167 | } |
Ben Cheng | 21eab51 | 2012-03-13 23:04:57 -0700 | [diff] [blame] | 168 | if (m) |
| 169 | free(resolved); |
Elliott Hughes | ff590ca | 2010-11-04 17:53:06 -0700 | [diff] [blame] | 170 | return (NULL); |
| 171 | } |
| 172 | if (S_ISLNK(sb.st_mode)) { |
| 173 | if (symlinks++ > MAXSYMLINKS) { |
Ben Cheng | 21eab51 | 2012-03-13 23:04:57 -0700 | [diff] [blame] | 174 | if (m) |
| 175 | free(resolved); |
Elliott Hughes | ff590ca | 2010-11-04 17:53:06 -0700 | [diff] [blame] | 176 | errno = ELOOP; |
| 177 | return (NULL); |
| 178 | } |
| 179 | slen = readlink(resolved, symlink, sizeof(symlink) - 1); |
Ben Cheng | 21eab51 | 2012-03-13 23:04:57 -0700 | [diff] [blame] | 180 | if (slen < 0) { |
| 181 | if (m) |
| 182 | free(resolved); |
Elliott Hughes | ff590ca | 2010-11-04 17:53:06 -0700 | [diff] [blame] | 183 | return (NULL); |
Ben Cheng | 21eab51 | 2012-03-13 23:04:57 -0700 | [diff] [blame] | 184 | } |
Elliott Hughes | ff590ca | 2010-11-04 17:53:06 -0700 | [diff] [blame] | 185 | symlink[slen] = '\0'; |
| 186 | if (symlink[0] == '/') { |
| 187 | resolved[1] = 0; |
| 188 | resolved_len = 1; |
| 189 | } else if (resolved_len > 1) { |
| 190 | /* Strip the last path component. */ |
| 191 | resolved[resolved_len - 1] = '\0'; |
| 192 | q = strrchr(resolved, '/') + 1; |
| 193 | *q = '\0'; |
| 194 | resolved_len = q - resolved; |
| 195 | } |
| 196 | |
| 197 | /* |
| 198 | * If there are any path components left, then |
| 199 | * append them to symlink. The result is placed |
| 200 | * in `left'. |
| 201 | */ |
| 202 | if (p != NULL) { |
| 203 | if (symlink[slen - 1] != '/') { |
| 204 | if (slen + 1 >= sizeof(symlink)) { |
Ben Cheng | 21eab51 | 2012-03-13 23:04:57 -0700 | [diff] [blame] | 205 | if (m) |
| 206 | free(resolved); |
Elliott Hughes | ff590ca | 2010-11-04 17:53:06 -0700 | [diff] [blame] | 207 | errno = ENAMETOOLONG; |
| 208 | return (NULL); |
| 209 | } |
| 210 | symlink[slen] = '/'; |
| 211 | symlink[slen + 1] = 0; |
| 212 | } |
| 213 | left_len = strlcat(symlink, left, sizeof(left)); |
| 214 | if (left_len >= sizeof(left)) { |
Ben Cheng | 21eab51 | 2012-03-13 23:04:57 -0700 | [diff] [blame] | 215 | if (m) |
| 216 | free(resolved); |
Elliott Hughes | ff590ca | 2010-11-04 17:53:06 -0700 | [diff] [blame] | 217 | errno = ENAMETOOLONG; |
| 218 | return (NULL); |
| 219 | } |
| 220 | } |
| 221 | left_len = strlcpy(left, symlink, sizeof(left)); |
| 222 | } |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 223 | } |
| 224 | |
Elliott Hughes | ff590ca | 2010-11-04 17:53:06 -0700 | [diff] [blame] | 225 | /* |
| 226 | * Remove trailing slash except when the resolved pathname |
| 227 | * is a single "/". |
| 228 | */ |
| 229 | if (resolved_len > 1 && resolved[resolved_len - 1] == '/') |
| 230 | resolved[resolved_len - 1] = '\0'; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 231 | return (resolved); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 232 | } |