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