]>
Commit | Line | Data |
---|---|---|
d74671e4 | 1 | /* OPENBSD ORIGINAL: lib/libc/stdlib/realpath.c */ |
2 | ||
443172c4 | 3 | /* |
4 | * Copyright (c) 1994 | |
5 | * The Regents of the University of California. All rights reserved. | |
6 | * | |
7 | * This code is derived from software contributed to Berkeley by | |
8 | * Jan-Simon Pendry. | |
9 | * | |
10 | * Redistribution and use in source and binary forms, with or without | |
11 | * modification, are permitted provided that the following conditions | |
12 | * are met: | |
13 | * 1. Redistributions of source code must retain the above copyright | |
14 | * notice, this list of conditions and the following disclaimer. | |
15 | * 2. Redistributions in binary form must reproduce the above copyright | |
16 | * notice, this list of conditions and the following disclaimer in the | |
17 | * documentation and/or other materials provided with the distribution. | |
3e67f7df | 18 | * 3. Neither the name of the University nor the names of its contributors |
19 | * may be used to endorse or promote products derived from this software | |
20 | * without specific prior written permission. | |
443172c4 | 21 | * |
22 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
23 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
24 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
25 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
26 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
27 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
28 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
30 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
31 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
32 | * SUCH DAMAGE. | |
33 | */ | |
34 | ||
35 | #include "includes.h" | |
36 | ||
37 | #if !defined(HAVE_REALPATH) || defined(BROKEN_REALPATH) | |
38 | ||
39 | #if defined(LIBC_SCCS) && !defined(lint) | |
667abcc6 | 40 | static char *rcsid = "$OpenBSD: realpath.c,v 1.11 2004/11/30 15:12:59 millert Exp $"; |
443172c4 | 41 | #endif /* LIBC_SCCS and not lint */ |
42 | ||
43 | #include <sys/param.h> | |
44 | #include <sys/stat.h> | |
45 | ||
46 | #include <errno.h> | |
47 | #include <fcntl.h> | |
48 | #include <stdlib.h> | |
49 | #include <string.h> | |
50 | #include <unistd.h> | |
51 | ||
c2207f11 | 52 | /* |
53 | * MAXSYMLINKS | |
54 | */ | |
55 | #ifndef MAXSYMLINKS | |
56 | #define MAXSYMLINKS 5 | |
57 | #endif | |
58 | ||
443172c4 | 59 | /* |
60 | * char *realpath(const char *path, char resolved_path[MAXPATHLEN]); | |
61 | * | |
62 | * Find the real name of path, by removing all ".", ".." and symlink | |
63 | * components. Returns (resolved) on success, or (NULL) on failure, | |
64 | * in which case the path which caused trouble is left in (resolved). | |
65 | */ | |
66 | char * | |
67 | realpath(const char *path, char *resolved) | |
68 | { | |
69 | struct stat sb; | |
667abcc6 | 70 | int fd, n, needslash, serrno; |
71 | char *p, *q, wbuf[MAXPATHLEN]; | |
443172c4 | 72 | int symlinks = 0; |
73 | ||
74 | /* Save the starting point. */ | |
667abcc6 | 75 | #ifndef HAVE_FCHDIR |
76 | char start[MAXPATHLEN]; | |
77 | /* this is potentially racy but without fchdir we have no option */ | |
78 | if (getcwd(start, sizeof(start)) == NULL) { | |
79 | resolved[0] = '.'; | |
80 | resolved[1] = '\0'; | |
81 | return (NULL); | |
82 | } | |
83 | #endif | |
443172c4 | 84 | if ((fd = open(".", O_RDONLY)) < 0) { |
667abcc6 | 85 | resolved[0] = '.'; |
86 | resolved[1] = '\0'; | |
443172c4 | 87 | return (NULL); |
88 | } | |
443172c4 | 89 | |
1656cbed | 90 | /* Convert "." -> "" to optimize away a needless lstat() and chdir() */ |
91 | if (path[0] == '.' && path[1] == '\0') | |
92 | path = ""; | |
93 | ||
443172c4 | 94 | /* |
95 | * Find the dirname and basename from the path to be resolved. | |
96 | * Change directory to the dirname component. | |
97 | * lstat the basename part. | |
98 | * if it is a symlink, read in the value and loop. | |
99 | * if it is a directory, then change to that directory. | |
100 | * get the current directory name and append the basename. | |
101 | */ | |
667abcc6 | 102 | if (strlcpy(resolved, path, MAXPATHLEN) >= MAXPATHLEN) { |
103 | serrno = ENAMETOOLONG; | |
104 | goto err2; | |
105 | } | |
443172c4 | 106 | loop: |
107 | q = strrchr(resolved, '/'); | |
108 | if (q != NULL) { | |
109 | p = q + 1; | |
110 | if (q == resolved) | |
111 | q = "/"; | |
112 | else { | |
113 | do { | |
114 | --q; | |
115 | } while (q > resolved && *q == '/'); | |
116 | q[1] = '\0'; | |
117 | q = resolved; | |
118 | } | |
119 | if (chdir(q) < 0) | |
120 | goto err1; | |
121 | } else | |
122 | p = resolved; | |
123 | ||
124 | /* Deal with the last component. */ | |
1656cbed | 125 | if (*p != '\0' && lstat(p, &sb) == 0) { |
443172c4 | 126 | if (S_ISLNK(sb.st_mode)) { |
127 | if (++symlinks > MAXSYMLINKS) { | |
667abcc6 | 128 | errno = ELOOP; |
443172c4 | 129 | goto err1; |
130 | } | |
667abcc6 | 131 | if ((n = readlink(p, resolved, MAXPATHLEN-1)) < 0) |
443172c4 | 132 | goto err1; |
133 | resolved[n] = '\0'; | |
134 | goto loop; | |
135 | } | |
136 | if (S_ISDIR(sb.st_mode)) { | |
137 | if (chdir(p) < 0) | |
138 | goto err1; | |
139 | p = ""; | |
140 | } | |
141 | } | |
142 | ||
143 | /* | |
144 | * Save the last component name and get the full pathname of | |
145 | * the current directory. | |
146 | */ | |
667abcc6 | 147 | if (strlcpy(wbuf, p, sizeof(wbuf)) >= sizeof(wbuf)) { |
148 | errno = ENAMETOOLONG; | |
149 | goto err1; | |
150 | } | |
151 | if (getcwd(resolved, MAXPATHLEN) == NULL) | |
443172c4 | 152 | goto err1; |
153 | ||
154 | /* | |
155 | * Join the two strings together, ensuring that the right thing | |
156 | * happens if the last component is empty, or the dirname is root. | |
157 | */ | |
158 | if (resolved[0] == '/' && resolved[1] == '\0') | |
510a42ce | 159 | needslash = 0; |
443172c4 | 160 | else |
510a42ce | 161 | needslash = 1; |
443172c4 | 162 | |
163 | if (*wbuf) { | |
510a42ce | 164 | if (strlen(resolved) + strlen(wbuf) + needslash >= MAXPATHLEN) { |
667abcc6 | 165 | errno = ENAMETOOLONG; |
166 | goto err1; | |
167 | } | |
168 | if (needslash) { | |
169 | if (strlcat(resolved, "/", MAXPATHLEN) >= MAXPATHLEN) { | |
170 | errno = ENAMETOOLONG; | |
171 | goto err1; | |
172 | } | |
173 | } | |
174 | if (strlcat(resolved, wbuf, MAXPATHLEN) >= MAXPATHLEN) { | |
175 | errno = ENAMETOOLONG; | |
443172c4 | 176 | goto err1; |
177 | } | |
443172c4 | 178 | } |
179 | ||
180 | /* Go back to where we came from. */ | |
667abcc6 | 181 | #ifdef HAVE_FCHDIR |
182 | if (fchdir(fd) < 0) { | |
183 | #else | |
443172c4 | 184 | if (chdir(start) < 0) { |
667abcc6 | 185 | #endif |
443172c4 | 186 | serrno = errno; |
187 | goto err2; | |
188 | } | |
667abcc6 | 189 | |
190 | /* It's okay if the close fails, what's an fd more or less? */ | |
191 | (void)close(fd); | |
443172c4 | 192 | return (resolved); |
193 | ||
667abcc6 | 194 | err1: serrno = errno; |
195 | #ifdef HAVE_FCHDIR | |
196 | (void)fchdir(fd); | |
197 | #else | |
198 | chdir(start); | |
199 | #endif | |
200 | err2: (void)close(fd); | |
201 | errno = serrno; | |
443172c4 | 202 | return (NULL); |
203 | } | |
204 | #endif /* !defined(HAVE_REALPATH) || defined(BROKEN_REALPATH) */ |