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