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