getcwd.c revision 1.5.4.1 1 /* $NetBSD: getcwd.c,v 1.5.4.1 1996/09/16 18:40:24 jtc Exp $ */
2
3 /*
4 * Copyright (c) 1989, 1991, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #if defined(LIBC_SCCS) && !defined(lint)
37 #if 0
38 static char sccsid[] = "@(#)getcwd.c 8.1 (Berkeley) 6/4/93";
39 #else
40 static char rcsid[] = "$NetBSD: getcwd.c,v 1.5.4.1 1996/09/16 18:40:24 jtc Exp $";
41 #endif
42 #endif /* LIBC_SCCS and not lint */
43
44 #include "namespace.h"
45 #include <sys/param.h>
46 #include <sys/stat.h>
47
48 #include <errno.h>
49 #include <dirent.h>
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <string.h>
53 #include <unistd.h>
54
55 #define ISDOT(dp) \
56 (dp->d_name[0] == '.' && (dp->d_name[1] == '\0' || \
57 dp->d_name[1] == '.' && dp->d_name[2] == '\0'))
58
59 char *
60 getcwd(pt, size)
61 char *pt;
62 size_t size;
63 {
64 register struct dirent *dp;
65 register DIR *dir;
66 register dev_t dev;
67 register ino_t ino;
68 register int first;
69 register char *bpt, *bup;
70 struct stat s;
71 dev_t root_dev;
72 ino_t root_ino;
73 size_t ptsize, upsize;
74 int save_errno;
75 char *ept, *eup, *up;
76
77 /*
78 * If no buffer specified by the user, allocate one as necessary.
79 * If a buffer is specified, the size has to be non-zero. The path
80 * is built from the end of the buffer backwards.
81 */
82 if (pt) {
83 ptsize = 0;
84 if (!size) {
85 errno = EINVAL;
86 return (NULL);
87 }
88 ept = pt + size;
89 } else {
90 if ((pt = malloc(ptsize = 1024 - 4)) == NULL)
91 return (NULL);
92 ept = pt + ptsize;
93 }
94 bpt = ept - 1;
95 *bpt = '\0';
96
97 /*
98 * Allocate bytes (1024 - malloc space) for the string of "../"'s.
99 * Should always be enough (it's 340 levels). If it's not, allocate
100 * as necessary. Special * case the first stat, it's ".", not "..".
101 */
102 if ((up = malloc(upsize = 1024 - 4)) == NULL)
103 goto err;
104 eup = up + MAXPATHLEN;
105 bup = up;
106 up[0] = '.';
107 up[1] = '\0';
108
109 /* Save root values, so know when to stop. */
110 if (stat("/", &s))
111 goto err;
112 root_dev = s.st_dev;
113 root_ino = s.st_ino;
114
115 errno = 0; /* XXX readdir has no error return. */
116
117 for (first = 1;; first = 0) {
118 /* Stat the current level. */
119 if (lstat(up, &s))
120 goto err;
121
122 /* Save current node values. */
123 ino = s.st_ino;
124 dev = s.st_dev;
125
126 /* Check for reaching root. */
127 if (root_dev == dev && root_ino == ino) {
128 *--bpt = '/';
129 /*
130 * It's unclear that it's a requirement to copy the
131 * path to the beginning of the buffer, but it's always
132 * been that way and stuff would probably break.
133 */
134 bcopy(bpt, pt, ept - bpt);
135 free(up);
136 return (pt);
137 }
138
139 /*
140 * Build pointer to the parent directory, allocating memory
141 * as necessary. Max length is 3 for "../", the largest
142 * possible component name, plus a trailing NULL.
143 */
144 if (bup + 3 + MAXNAMLEN + 1 >= eup) {
145 if ((up = realloc(up, upsize *= 2)) == NULL)
146 goto err;
147 bup = up;
148 eup = up + upsize;
149 }
150 *bup++ = '.';
151 *bup++ = '.';
152 *bup = '\0';
153
154 /* Open and stat parent directory. */
155 if (!(dir = opendir(up)) || fstat(dirfd(dir), &s))
156 goto err;
157
158 /* Add trailing slash for next directory. */
159 *bup++ = '/';
160
161 /*
162 * If it's a mount point, have to stat each element because
163 * the inode number in the directory is for the entry in the
164 * parent directory, not the inode number of the mounted file.
165 */
166 save_errno = 0;
167 if (s.st_dev == dev) {
168 for (;;) {
169 if (!(dp = readdir(dir)))
170 goto notfound;
171 if (dp->d_fileno == ino)
172 break;
173 }
174 } else
175 for (;;) {
176 if (!(dp = readdir(dir)))
177 goto notfound;
178 if (ISDOT(dp))
179 continue;
180 bcopy(dp->d_name, bup, dp->d_namlen + 1);
181
182 /* Save the first error for later. */
183 if (lstat(up, &s)) {
184 if (!save_errno)
185 save_errno = errno;
186 errno = 0;
187 continue;
188 }
189 if (s.st_dev == dev && s.st_ino == ino)
190 break;
191 }
192
193 /*
194 * Check for length of the current name, preceding slash,
195 * leading slash.
196 */
197 if (bpt - pt <= dp->d_namlen + (first ? 1 : 2)) {
198 size_t len, off;
199
200 if (!ptsize) {
201 errno = ERANGE;
202 goto err;
203 }
204 off = bpt - pt;
205 len = ept - bpt;
206 if ((pt = realloc(pt, ptsize *= 2)) == NULL)
207 goto err;
208 bpt = pt + off;
209 ept = pt + ptsize;
210 bcopy(bpt, ept - len, len);
211 bpt = ept - len;
212 }
213 if (!first)
214 *--bpt = '/';
215 bpt -= dp->d_namlen;
216 bcopy(dp->d_name, bpt, dp->d_namlen);
217 (void)closedir(dir);
218
219 /* Truncate any file name. */
220 *bup = '\0';
221 }
222
223 notfound:
224 /*
225 * If readdir set errno, use it, not any saved error; otherwise,
226 * didn't find the current directory in its parent directory, set
227 * errno to ENOENT.
228 */
229 if (!errno)
230 errno = save_errno ? save_errno : ENOENT;
231 /* FALLTHROUGH */
232 err:
233 if (ptsize)
234 free(pt);
235 free(up);
236 return (NULL);
237 }
238