]>
Commit | Line | Data |
---|---|---|
e9ce8d39 | 1 | /* |
9385eb3d | 2 | * Copyright (c) 1989, 1991, 1993, 1995 |
e9ce8d39 A |
3 | * The Regents of the University of California. All rights reserved. |
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. | |
e9ce8d39 A |
13 | * 4. Neither the name of the University nor the names of its contributors |
14 | * may be used to endorse or promote products derived from this software | |
15 | * without specific prior written permission. | |
16 | * | |
17 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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 | ||
70ad1dc8 A |
30 | #pragma clang diagnostic push |
31 | #pragma clang diagnostic ignored "-Wstrict-prototypes" | |
32 | ||
9385eb3d A |
33 | #if defined(LIBC_SCCS) && !defined(lint) |
34 | static char sccsid[] = "@(#)getcwd.c 8.5 (Berkeley) 2/7/95"; | |
35 | #endif /* LIBC_SCCS and not lint */ | |
36 | #include <sys/cdefs.h> | |
1f2f436a | 37 | __FBSDID("$FreeBSD: src/lib/libc/gen/getcwd.c,v 1.29 2007/01/09 00:27:53 imp Exp $"); |
e9ce8d39 | 38 | |
9385eb3d | 39 | #include "namespace.h" |
e9ce8d39 A |
40 | #include <sys/param.h> |
41 | #include <sys/stat.h> | |
42 | ||
43 | #include <dirent.h> | |
44 | #include <errno.h> | |
45 | #include <fcntl.h> | |
46 | #include <stdio.h> | |
47 | #include <stdlib.h> | |
48 | #include <string.h> | |
49 | #include <unistd.h> | |
9385eb3d | 50 | #include "un-namespace.h" |
e9ce8d39 A |
51 | |
52 | #define ISDOT(dp) \ | |
53 | (dp->d_name[0] == '.' && (dp->d_name[1] == '\0' || \ | |
9385eb3d A |
54 | (dp->d_name[1] == '.' && dp->d_name[2] == '\0'))) |
55 | ||
ad3c9f2a A |
56 | /* |
57 | * If __getcwd() ever becomes a syscall, we can remove this workaround. | |
58 | * The problem here is that fcntl() assumes a buffer of size MAXPATHLEN, | |
59 | * if size is less than MAXPATHLEN, we need to use a temporary buffer | |
60 | * and see if it fits. We also have to assume that open() or fcntl() | |
61 | * don't fail with errno=ERANGE. | |
62 | */ | |
63 | static inline int | |
64 | __getcwd(char *buf, size_t size) | |
65 | { | |
66 | int fd, err, save; | |
67 | struct stat dot, pt; | |
68 | char *b; | |
e9ce8d39 | 69 | |
ad3c9f2a A |
70 | if ((fd = open(".", O_RDONLY)) < 0) |
71 | return -1; | |
72 | if (fstat(fd, &dot) < 0) { | |
73 | save = errno; | |
74 | close(fd); | |
75 | errno = save; | |
76 | return -1; | |
77 | } | |
78 | /* check that the device and inode are non-zero, otherwise punt */ | |
79 | if (dot.st_dev == 0 || dot.st_ino == 0) { | |
80 | close(fd); | |
81 | errno = EINVAL; | |
82 | return -1; | |
83 | } | |
84 | if (size < MAXPATHLEN) { | |
85 | /* the hard case; allocate a buffer of size MAXPATHLEN to use */ | |
86 | b = (char *)alloca(MAXPATHLEN); | |
87 | if (b == NULL) { | |
88 | close(fd); | |
89 | errno = ENOMEM; /* make sure it isn't ERANGE */ | |
90 | return -1; | |
91 | } | |
92 | } else | |
93 | b = buf; | |
94 | ||
95 | err = fcntl(fd, F_GETPATH, b); | |
96 | if (err) { | |
97 | save = errno; | |
98 | close(fd); | |
99 | errno = save; | |
100 | return err; | |
101 | } | |
102 | close(fd); | |
103 | /* | |
104 | * now double-check that the path returned by fcntl() has the same | |
105 | * device and inode number as '.'. | |
106 | */ | |
107 | if (stat(b, &pt) < 0) | |
108 | return -1; | |
109 | /* | |
110 | * Since dot.st_dev and dot.st_ino are non-zero, we don't need to | |
111 | * separately test for pt.st_dev and pt.st_ino being non-zero, because | |
112 | * they have to match | |
113 | */ | |
114 | if (dot.st_dev != pt.st_dev || dot.st_ino != pt.st_ino) { | |
115 | errno = EINVAL; | |
116 | return -1; | |
117 | } | |
118 | /* | |
119 | * For the case where we allocated a buffer, check that it can fit | |
120 | * in the real buffer, and copy it over. | |
121 | */ | |
122 | if (size < MAXPATHLEN) { | |
123 | if (strlen(b) >= size) { | |
124 | errno = ERANGE; | |
125 | return -1; | |
126 | } | |
127 | strcpy(buf, b); | |
128 | } | |
129 | return 0; | |
130 | } | |
131 | ||
132 | __private_extern__ char * | |
133 | __private_getcwd(pt, size, usegetpath) | |
e9ce8d39 A |
134 | char *pt; |
135 | size_t size; | |
ad3c9f2a | 136 | int usegetpath; |
e9ce8d39 | 137 | { |
9385eb3d A |
138 | struct dirent *dp; |
139 | DIR *dir = NULL; | |
e9ce8d39 A |
140 | dev_t dev; |
141 | ino_t ino; | |
9385eb3d A |
142 | int first; |
143 | char *bpt, *bup; | |
e9ce8d39 A |
144 | struct stat s; |
145 | dev_t root_dev; | |
146 | ino_t root_ino; | |
147 | size_t ptsize, upsize; | |
148 | int save_errno; | |
6465356a | 149 | char *ept, *eup, *up; |
e9ce8d39 A |
150 | |
151 | /* | |
152 | * If no buffer specified by the user, allocate one as necessary. | |
153 | * If a buffer is specified, the size has to be non-zero. The path | |
154 | * is built from the end of the buffer backwards. | |
155 | */ | |
156 | if (pt) { | |
157 | ptsize = 0; | |
158 | if (!size) { | |
159 | errno = EINVAL; | |
160 | return (NULL); | |
161 | } | |
9385eb3d A |
162 | if (size == 1) { |
163 | errno = ERANGE; | |
164 | return (NULL); | |
165 | } | |
e9ce8d39 A |
166 | ept = pt + size; |
167 | } else { | |
ad3c9f2a | 168 | if ((pt = malloc(ptsize = MAXPATHLEN)) == NULL) |
e9ce8d39 A |
169 | return (NULL); |
170 | ept = pt + ptsize; | |
171 | } | |
ad3c9f2a A |
172 | if (usegetpath) { |
173 | if (__getcwd(pt, ept - pt) == 0) { | |
174 | return (pt); | |
175 | } else if (errno == ERANGE) /* failed because buffer too small */ | |
176 | return NULL; | |
9385eb3d | 177 | } |
e9ce8d39 A |
178 | bpt = ept - 1; |
179 | *bpt = '\0'; | |
180 | ||
181 | /* | |
ad3c9f2a | 182 | * Allocate MAXPATHLEN bytes for the string of "../"'s. |
1f2f436a | 183 | * Should always be enough. If it's not, allocate |
e9ce8d39 A |
184 | * as necessary. Special case the first stat, it's ".", not "..". |
185 | */ | |
ad3c9f2a | 186 | if ((up = malloc(upsize = MAXPATHLEN)) == NULL) |
e9ce8d39 | 187 | goto err; |
ad3c9f2a | 188 | eup = up + MAXPATHLEN; |
e9ce8d39 A |
189 | bup = up; |
190 | up[0] = '.'; | |
191 | up[1] = '\0'; | |
192 | ||
193 | /* Save root values, so know when to stop. */ | |
194 | if (stat("/", &s)) | |
195 | goto err; | |
196 | root_dev = s.st_dev; | |
197 | root_ino = s.st_ino; | |
198 | ||
199 | errno = 0; /* XXX readdir has no error return. */ | |
200 | ||
201 | for (first = 1;; first = 0) { | |
202 | /* Stat the current level. */ | |
203 | if (lstat(up, &s)) | |
204 | goto err; | |
205 | ||
206 | /* Save current node values. */ | |
207 | ino = s.st_ino; | |
208 | dev = s.st_dev; | |
209 | ||
210 | /* Check for reaching root. */ | |
211 | if (root_dev == dev && root_ino == ino) { | |
212 | *--bpt = '/'; | |
213 | /* | |
214 | * It's unclear that it's a requirement to copy the | |
215 | * path to the beginning of the buffer, but it's always | |
216 | * been that way and stuff would probably break. | |
217 | */ | |
9385eb3d | 218 | bcopy(bpt, pt, ept - bpt); |
e9ce8d39 A |
219 | free(up); |
220 | return (pt); | |
221 | } | |
222 | ||
223 | /* | |
224 | * Build pointer to the parent directory, allocating memory | |
225 | * as necessary. Max length is 3 for "../", the largest | |
9385eb3d | 226 | * possible component name, plus a trailing NUL. |
e9ce8d39 | 227 | */ |
1f2f436a | 228 | while (bup + 3 + MAXNAMLEN + 1 >= eup) { |
9385eb3d | 229 | if ((up = reallocf(up, upsize *= 2)) == NULL) |
e9ce8d39 A |
230 | goto err; |
231 | bup = up; | |
232 | eup = up + upsize; | |
233 | } | |
234 | *bup++ = '.'; | |
235 | *bup++ = '.'; | |
236 | *bup = '\0'; | |
237 | ||
238 | /* Open and stat parent directory. */ | |
9385eb3d | 239 | if (!(dir = opendir(up)) || _fstat(dirfd(dir), &s)) |
e9ce8d39 A |
240 | goto err; |
241 | ||
242 | /* Add trailing slash for next directory. */ | |
243 | *bup++ = '/'; | |
9385eb3d | 244 | *bup = '\0'; |
e9ce8d39 A |
245 | |
246 | /* | |
247 | * If it's a mount point, have to stat each element because | |
248 | * the inode number in the directory is for the entry in the | |
249 | * parent directory, not the inode number of the mounted file. | |
250 | */ | |
251 | save_errno = 0; | |
252 | if (s.st_dev == dev) { | |
253 | for (;;) { | |
254 | if (!(dp = readdir(dir))) | |
255 | goto notfound; | |
256 | if (dp->d_fileno == ino) | |
257 | break; | |
258 | } | |
259 | } else | |
260 | for (;;) { | |
261 | if (!(dp = readdir(dir))) | |
262 | goto notfound; | |
263 | if (ISDOT(dp)) | |
264 | continue; | |
265 | bcopy(dp->d_name, bup, dp->d_namlen + 1); | |
266 | ||
267 | /* Save the first error for later. */ | |
268 | if (lstat(up, &s)) { | |
269 | if (!save_errno) | |
270 | save_errno = errno; | |
271 | errno = 0; | |
272 | continue; | |
273 | } | |
274 | if (s.st_dev == dev && s.st_ino == ino) | |
275 | break; | |
276 | } | |
277 | ||
278 | /* | |
279 | * Check for length of the current name, preceding slash, | |
280 | * leading slash. | |
281 | */ | |
1f2f436a | 282 | while (bpt - pt < dp->d_namlen + (first ? 1 : 2)) { |
e9ce8d39 A |
283 | size_t len, off; |
284 | ||
285 | if (!ptsize) { | |
286 | errno = ERANGE; | |
287 | goto err; | |
288 | } | |
289 | off = bpt - pt; | |
290 | len = ept - bpt; | |
9385eb3d | 291 | if ((pt = reallocf(pt, ptsize *= 2)) == NULL) |
e9ce8d39 A |
292 | goto err; |
293 | bpt = pt + off; | |
294 | ept = pt + ptsize; | |
9385eb3d | 295 | bcopy(bpt, ept - len, len); |
e9ce8d39 A |
296 | bpt = ept - len; |
297 | } | |
298 | if (!first) | |
299 | *--bpt = '/'; | |
300 | bpt -= dp->d_namlen; | |
301 | bcopy(dp->d_name, bpt, dp->d_namlen); | |
9385eb3d A |
302 | (void) closedir(dir); |
303 | dir = NULL; | |
e9ce8d39 A |
304 | |
305 | /* Truncate any file name. */ | |
306 | *bup = '\0'; | |
307 | } | |
308 | ||
309 | notfound: | |
310 | /* | |
311 | * If readdir set errno, use it, not any saved error; otherwise, | |
312 | * didn't find the current directory in its parent directory, set | |
313 | * errno to ENOENT. | |
314 | */ | |
315 | if (!errno) | |
316 | errno = save_errno ? save_errno : ENOENT; | |
317 | /* FALLTHROUGH */ | |
318 | err: | |
9385eb3d A |
319 | save_errno = errno; |
320 | ||
e9ce8d39 A |
321 | if (ptsize) |
322 | free(pt); | |
9385eb3d A |
323 | if (dir) |
324 | (void) closedir(dir); | |
e9ce8d39 | 325 | free(up); |
9385eb3d A |
326 | |
327 | errno = save_errno; | |
e9ce8d39 A |
328 | return (NULL); |
329 | } | |
ad3c9f2a A |
330 | |
331 | char * | |
332 | getcwd(pt, size) | |
333 | char *pt; | |
334 | size_t size; | |
335 | { | |
336 | return __private_getcwd(pt, size, 1); | |
337 | } | |
70ad1dc8 | 338 | #pragma clang diagnostic pop |