Imported Debian patch 1.6.9p11-1
[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 #define ISDOT(dp) \
78         (dp->d_name[0] == '.' && (dp->d_name[1] == '\0' || \
79             (dp->d_name[1] == '.' && dp->d_name[2] == '\0')))
80
81 #ifndef lint
82 __unused static const char rcsid[] = "$Sudo: getcwd.c,v 1.25.2.1 2007/06/12 00:56:42 millert Exp $";
83 #endif /* lint */
84
85 char *
86 getcwd(pt, size)
87         char *pt;
88         size_t size;
89 {
90         struct dirent *dp;
91         DIR *dir = NULL;
92         dev_t dev;
93         ino_t ino;
94         int first;
95         char *bpt, *bup;
96         struct stat s;
97         dev_t root_dev;
98         ino_t root_ino;
99         size_t ptsize, upsize;
100         int save_errno;
101         char *ept, *eup, *up;
102
103         /*
104          * If no buffer specified by the user, allocate one as necessary.
105          * If a buffer is specified, the size has to be non-zero.  The path
106          * is built from the end of the buffer backwards.
107          */
108         if (pt) {
109                 ptsize = 0;
110                 if (!size) {
111                         errno = EINVAL;
112                         return (NULL);
113                 }
114                 ept = pt + size;
115         } else {
116                 if ((pt = malloc(ptsize = 1024 - 4)) == NULL)
117                         return (NULL);
118                 ept = pt + ptsize;
119         }
120         bpt = ept - 1;
121         *bpt = '\0';
122
123         /*
124          * Allocate bytes (1024 - malloc space) for the string of "../"'s.
125          * Should always be enough (it's 340 levels).  If it's not, allocate
126          * as necessary.  Special * case the first stat, it's ".", not "..".
127          */
128         if ((up = malloc(upsize = 1024 - 4)) == NULL)
129                 goto err;
130         eup = up + PATH_MAX;
131         bup = up;
132         up[0] = '.';
133         up[1] = '\0';
134
135         /* Save root values, so know when to stop. */
136         if (stat("/", &s))
137                 goto err;
138         root_dev = s.st_dev;
139         root_ino = s.st_ino;
140
141         errno = 0;                      /* XXX readdir has no error return. */
142
143         for (first = 1;; first = 0) {
144                 /* Stat the current level. */
145                 if (lstat(up, &s))
146                         goto err;
147
148                 /* Save current node values. */
149                 ino = s.st_ino;
150                 dev = s.st_dev;
151
152                 /* Check for reaching root. */
153                 if (root_dev == dev && root_ino == ino) {
154                         *--bpt = '/';
155                         /*
156                          * It's unclear that it's a requirement to copy the
157                          * path to the beginning of the buffer, but it's always
158                          * been that way and stuff would probably break.
159                          */
160                         bcopy(bpt, pt, ept - bpt);
161                         free(up);
162                         return (pt);
163                 }
164
165                 /*
166                  * Build pointer to the parent directory, allocating memory
167                  * as necessary.  Max length is 3 for "../", the largest
168                  * possible component name, plus a trailing NULL.
169                  */
170                 if (bup + 3  + MAXNAMLEN + 1 >= eup) {
171                         char *nup;
172
173                         if ((nup = realloc(up, upsize *= 2)) == NULL)
174                                 goto err;
175                         up = nup;
176                         bup = up;
177                         eup = up + upsize;
178                 }
179                 *bup++ = '.';
180                 *bup++ = '.';
181                 *bup = '\0';
182
183                 /* Open and stat parent directory. */
184                 if (!(dir = opendir(up)) || fstat(dirfd(dir), &s))
185                         goto err;
186
187                 /* Add trailing slash for next directory. */
188                 *bup++ = '/';
189
190                 /*
191                  * If it's a mount point, have to stat each element because
192                  * the inode number in the directory is for the entry in the
193                  * parent directory, not the inode number of the mounted file.
194                  */
195                 save_errno = 0;
196                 if (s.st_dev == dev) {
197                         for (;;) {
198                                 if (!(dp = readdir(dir)))
199                                         goto notfound;
200                                 if (dp->d_fileno == ino)
201                                         break;
202                         }
203                 } else
204                         for (;;) {
205                                 if (!(dp = readdir(dir)))
206                                         goto notfound;
207                                 if (ISDOT(dp))
208                                         continue;
209                                 bcopy(dp->d_name, bup, NAMLEN(dp) + 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 <= NAMLEN(dp) + (first ? 1 : 2)) {
227                         size_t len, off;
228                         char *npt;
229
230                         if (!ptsize) {
231                                 errno = ERANGE;
232                                 goto err;
233                         }
234                         off = bpt - pt;
235                         len = ept - bpt;
236                         if ((npt = realloc(pt, ptsize *= 2)) == NULL)
237                                 goto err;
238                         pt = npt;
239                         bpt = pt + off;
240                         ept = pt + ptsize;
241                         bcopy(bpt, ept - len, len);
242                         bpt = ept - len;
243                 }
244                 if (!first)
245                         *--bpt = '/';
246                 bpt -= NAMLEN(dp);
247                 bcopy(dp->d_name, bpt, NAMLEN(dp));
248                 (void)closedir(dir);
249
250                 /* Truncate any file name. */
251                 *bup = '\0';
252         }
253
254 notfound:
255         /*
256          * If readdir set errno, use it, not any saved error; otherwise,
257          * didn't find the current directory in its parent directory, set
258          * errno to ENOENT.
259          */
260         if (!errno)
261                 errno = save_errno ? save_errno : ENOENT;
262         /* FALLTHROUGH */
263 err:
264         if (ptsize)
265                 free(pt);
266         if (up)
267                 free(up);
268         if (dir)
269                 (void)closedir(dir);
270         return (NULL);
271 }