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.c237
1 files changed, 134 insertions, 103 deletions
diff --git a/src/lib/libc/stdlib/realpath.c b/src/lib/libc/stdlib/realpath.c
index e349b7e068..4cb847b313 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.13 2005/08/08 08:05:37 espie 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,164 @@
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";*/
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 */
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 */
58char * 45char *
59realpath(path, resolved) 46realpath(const char *path, char resolved[PATH_MAX])
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;
53 char left[PATH_MAX], next_token[PATH_MAX], symlink[PATH_MAX];
66 54
67 /* Save the starting point. */ 55 serrno = errno;
68 if ((fd = open(".", O_RDONLY)) < 0) { 56 symlinks = 0;
69 (void)strcpy(resolved, "."); 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;
70 return (NULL); 74 return (NULL);
71 } 75 }
72 76
73 /* 77 /*
74 * Find the dirname and basename from the path to be resolved. 78 * 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 */ 79 */
81 (void)strncpy(resolved, path, MAXPATHLEN - 1); 80 while (left_len != 0) {
82 resolved[MAXPATHLEN - 1] = '\0'; 81 /*
83loop: 82 * Extract the next path component and adjust `left'
84 q = strrchr(resolved, '/'); 83 * and its length.
85 if (q != NULL) { 84 */
86 p = q + 1; 85 p = strchr(left, '/');
87 if (q == resolved) 86 s = p ? p : left + left_len;
88 q = "/"; 87 if (s - left >= sizeof(next_token)) {
89 else { 88 errno = ENAMETOOLONG;
90 do { 89 return (NULL);
91 --q;
92 } while (q > resolved && *q == '/');
93 q[1] = '\0';
94 q = resolved;
95 } 90 }
96 if (chdir(q) < 0) 91 memcpy(next_token, left, s - left);
97 goto err1; 92 next_token[s - left] = '\0';
98 } else 93 left_len -= s - left;
99 p = resolved; 94 if (p != NULL)
100 95 memmove(left, s + 1, left_len + 1);
101 /* Deal with the last component. */ 96 if (resolved[resolved_len - 1] != '/') {
102 if (lstat(p, &sb) == 0) { 97 if (resolved_len + 1 >= PATH_MAX) {
103 if (S_ISLNK(sb.st_mode)) { 98 errno = ENAMETOOLONG;
104 n = readlink(p, resolved, MAXPATHLEN); 99 return (NULL);
105 if (n < 0) 100 }
106 goto err1; 101 resolved[resolved_len++] = '/';
107 resolved[n] = '\0'; 102 resolved[resolved_len] = '\0';
108 goto loop;
109 } 103 }
110 if (S_ISDIR(sb.st_mode)) { 104 if (next_token[0] == '\0')
111 if (chdir(p) < 0) 105 continue;
112 goto err1; 106 else if (strcmp(next_token, ".") == 0)
113 p = ""; 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;
114 } 120 }
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 121
125 /* 122 /*
126 * Join the two strings together, ensuring that the right thing 123 * Append the next path component and lstat() it. If
127 * happens if the last component is empty, or the dirname is root. 124 * lstat() fails we still can return successfully if
128 */ 125 * there are no more path components left.
129 if (resolved[0] == '/' && resolved[1] == '\0') 126 */
130 rootd = 1; 127 resolved_len = strlcat(resolved, next_token, PATH_MAX);
131 else 128 if (resolved_len >= PATH_MAX) {
132 rootd = 0;
133
134 if (*wbuf) {
135 if (strlen(resolved) + strlen(wbuf) + rootd + 1 > MAXPATHLEN) {
136 errno = ENAMETOOLONG; 129 errno = ENAMETOOLONG;
137 goto err1; 130 return (NULL);
138 } 131 }
139 if (rootd == 0) 132 if (lstat(resolved, &sb) != 0) {
140 (void)strcat(resolved, "/"); 133 if (errno == ENOENT && p == NULL) {
141 (void)strcat(resolved, wbuf); 134 errno = serrno;
142 } 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 }
143 158
144 /* Go back to where we came from. */ 159 /*
145 if (fchdir(fd) < 0) { 160 * If there are any path components left, then
146 serrno = errno; 161 * append them to symlink. The result is placed
147 goto err2; 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 }
148 } 181 }
149 182
150 /* It's okay if the close fails, what's an fd more or less? */ 183 /*
151 (void)close(fd); 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';
152 return (resolved); 189 return (resolved);
153
154err1: serrno = errno;
155 (void)fchdir(fd);
156err2: (void)close(fd);
157 errno = serrno;
158 return (NULL);
159} 190}