xref: /original-bsd/lib/libc/gen/getcwd.c (revision c3e32dec)
1 /*
2  * Copyright (c) 1989, 1991, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #if defined(LIBC_SCCS) && !defined(lint)
9 static char sccsid[] = "@(#)getcwd.c	8.1 (Berkeley) 06/04/93";
10 #endif /* LIBC_SCCS and not lint */
11 
12 #include <sys/param.h>
13 #include <sys/stat.h>
14 
15 #include <errno.h>
16 #include <dirent.h>
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <unistd.h>
21 
22 #define	ISDOT(dp) \
23 	(dp->d_name[0] == '.' && (dp->d_name[1] == '\0' || \
24 	    dp->d_name[1] == '.' && dp->d_name[2] == '\0'))
25 
26 char *
27 getcwd(pt, size)
28 	char *pt;
29 	size_t size;
30 {
31 	register struct dirent *dp;
32 	register DIR *dir;
33 	register dev_t dev;
34 	register ino_t ino;
35 	register int first;
36 	register char *bpt, *bup;
37 	struct stat s;
38 	dev_t root_dev;
39 	ino_t root_ino;
40 	size_t ptsize, upsize;
41 	int save_errno;
42 	char *ept, *eup, *up;
43 
44 	/*
45 	 * If no buffer specified by the user, allocate one as necessary.
46 	 * If a buffer is specified, the size has to be non-zero.  The path
47 	 * is built from the end of the buffer backwards.
48 	 */
49 	if (pt) {
50 		ptsize = 0;
51 		if (!size) {
52 			errno = EINVAL;
53 			return (NULL);
54 		}
55 		ept = pt + size;
56 	} else {
57 		if ((pt = malloc(ptsize = 1024 - 4)) == NULL)
58 			return (NULL);
59 		ept = pt + ptsize;
60 	}
61 	bpt = ept - 1;
62 	*bpt = '\0';
63 
64 	/*
65 	 * Allocate bytes (1024 - malloc space) for the string of "../"'s.
66 	 * Should always be enough (it's 340 levels).  If it's not, allocate
67 	 * as necessary.  Special * case the first stat, it's ".", not "..".
68 	 */
69 	if ((up = malloc(upsize = 1024 - 4)) == NULL)
70 		goto err;
71 	eup = up + MAXPATHLEN;
72 	bup = up;
73 	up[0] = '.';
74 	up[1] = '\0';
75 
76 	/* Save root values, so know when to stop. */
77 	if (stat("/", &s))
78 		goto err;
79 	root_dev = s.st_dev;
80 	root_ino = s.st_ino;
81 
82 	errno = 0;			/* XXX readdir has no error return. */
83 
84 	for (first = 1;; first = 0) {
85 		/* Stat the current level. */
86 		if (lstat(up, &s))
87 			goto err;
88 
89 		/* Save current node values. */
90 		ino = s.st_ino;
91 		dev = s.st_dev;
92 
93 		/* Check for reaching root. */
94 		if (root_dev == dev && root_ino == ino) {
95 			*--bpt = '/';
96 			/*
97 			 * It's unclear that it's a requirement to copy the
98 			 * path to the beginning of the buffer, but it's always
99 			 * been that way and stuff would probably break.
100 			 */
101 			(void)bcopy(bpt, pt, ept - bpt);
102 			free(up);
103 			return (pt);
104 		}
105 
106 		/*
107 		 * Build pointer to the parent directory, allocating memory
108 		 * as necessary.  Max length is 3 for "../", the largest
109 		 * possible component name, plus a trailing NULL.
110 		 */
111 		if (bup + 3  + MAXNAMLEN + 1 >= eup) {
112 			if ((up = realloc(up, upsize *= 2)) == NULL)
113 				goto err;
114 			bup = up;
115 			eup = up + upsize;
116 		}
117 		*bup++ = '.';
118 		*bup++ = '.';
119 		*bup = '\0';
120 
121 		/* Open and stat parent directory. */
122 		if (!(dir = opendir(up)) || fstat(dirfd(dir), &s))
123 			goto err;
124 
125 		/* Add trailing slash for next directory. */
126 		*bup++ = '/';
127 
128 		/*
129 		 * If it's a mount point, have to stat each element because
130 		 * the inode number in the directory is for the entry in the
131 		 * parent directory, not the inode number of the mounted file.
132 		 */
133 		save_errno = 0;
134 		if (s.st_dev == dev) {
135 			for (;;) {
136 				if (!(dp = readdir(dir)))
137 					goto notfound;
138 				if (dp->d_fileno == ino)
139 					break;
140 			}
141 		} else
142 			for (;;) {
143 				if (!(dp = readdir(dir)))
144 					goto notfound;
145 				if (ISDOT(dp))
146 					continue;
147 				bcopy(dp->d_name, bup, dp->d_namlen + 1);
148 
149 				/* Save the first error for later. */
150 				if (lstat(up, &s)) {
151 					if (!save_errno)
152 						save_errno = errno;
153 					errno = 0;
154 					continue;
155 				}
156 				if (s.st_dev == dev && s.st_ino == ino)
157 					break;
158 			}
159 
160 		/*
161 		 * Check for length of the current name, preceding slash,
162 		 * leading slash.
163 		 */
164 		if (bpt - pt <= dp->d_namlen + (first ? 1 : 2)) {
165 			size_t len, off;
166 
167 			if (!ptsize) {
168 				errno = ERANGE;
169 				goto err;
170 			}
171 			off = bpt - pt;
172 			len = ept - bpt;
173 			if ((pt = realloc(pt, ptsize *= 2)) == NULL)
174 				goto err;
175 			bpt = pt + off;
176 			ept = pt + ptsize;
177 			(void)bcopy(bpt, ept - len, len);
178 			bpt = ept - len;
179 		}
180 		if (!first)
181 			*--bpt = '/';
182 		bpt -= dp->d_namlen;
183 		bcopy(dp->d_name, bpt, dp->d_namlen);
184 		(void)closedir(dir);
185 
186 		/* Truncate any file name. */
187 		*bup = '\0';
188 	}
189 
190 notfound:
191 	/*
192 	 * If readdir set errno, use it, not any saved error; otherwise,
193 	 * didn't find the current directory in its parent directory, set
194 	 * errno to ENOENT.
195 	 */
196 	if (!errno)
197 		errno = save_errno ? save_errno : ENOENT;
198 	/* FALLTHROUGH */
199 err:
200 	if (ptsize)
201 		free(pt);
202 	free(up);
203 	return (NULL);
204 }
205