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