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