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