prepare to upload
[debian/sudo] / compat / 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 #endif /* HAVE_STRING_H */
48 #ifdef HAVE_STRINGS_H
49 # include <strings.h>
50 #endif /* HAVE_STRINGS_H */
51 #if defined(HAVE_MALLOC_H) && !defined(STDC_HEADERS)
52 # include <malloc.h>
53 #endif /* HAVE_MALLOC_H && !STDC_HEADERS */
54 #ifdef HAVE_UNISTD_H
55 # include <unistd.h>
56 #endif /* HAVE_UNISTD_H */
57 #ifdef HAVE_DIRENT_H
58 # include <dirent.h>
59 # define NAMLEN(dirent) strlen((dirent)->d_name)
60 #else
61 # define dirent direct
62 # define NAMLEN(dirent) (dirent)->d_namlen
63 # ifdef HAVE_SYS_NDIR_H
64 #  include <sys/ndir.h>
65 # endif
66 # ifdef HAVE_SYS_DIR_H
67 #  include <sys/dir.h>
68 # endif
69 # ifdef HAVE_NDIR_H
70 #  include <ndir.h>
71 # endif
72 #endif
73
74 #include "missing.h"
75
76 #define ISDOT(dp) \
77         (dp->d_name[0] == '.' && (dp->d_name[1] == '\0' || \
78             (dp->d_name[1] == '.' && dp->d_name[2] == '\0')))
79
80 char *
81 getcwd(char *pt, size_t size)
82 {
83         struct dirent *dp;
84         DIR *dir = NULL;
85         dev_t dev;
86         ino_t ino;
87         int first;
88         char *bpt, *bup;
89         struct stat s;
90         dev_t root_dev;
91         ino_t root_ino;
92         size_t ptsize, upsize;
93         int save_errno;
94         char *ept, *eup, *up;
95
96         /*
97          * If no buffer specified by the user, allocate one as necessary.
98          * If a buffer is specified, the size has to be non-zero.  The path
99          * is built from the end of the buffer backwards.
100          */
101         if (pt) {
102                 ptsize = 0;
103                 if (!size) {
104                         errno = EINVAL;
105                         return NULL;
106                 }
107                 ept = pt + size;
108         } else {
109                 if ((pt = malloc(ptsize = 1024 - 4)) == NULL)
110                         return NULL;
111                 ept = pt + ptsize;
112         }
113         bpt = ept - 1;
114         *bpt = '\0';
115
116         /*
117          * Allocate bytes (1024 - malloc space) for the string of "../"'s.
118          * Should always be enough (it's 340 levels).  If it's not, allocate
119          * as necessary.  Special * case the first stat, it's ".", not "..".
120          */
121         if ((up = malloc(upsize = 1024 - 4)) == NULL)
122                 goto err;
123         eup = up + PATH_MAX;
124         bup = up;
125         up[0] = '.';
126         up[1] = '\0';
127
128         /* Save root values, so know when to stop. */
129         if (stat("/", &s))
130                 goto err;
131         root_dev = s.st_dev;
132         root_ino = s.st_ino;
133
134         errno = 0;                      /* XXX readdir has no error return. */
135
136         for (first = 1;; first = 0) {
137                 /* Stat the current level. */
138                 if (lstat(up, &s))
139                         goto err;
140
141                 /* Save current node values. */
142                 ino = s.st_ino;
143                 dev = s.st_dev;
144
145                 /* Check for reaching root. */
146                 if (root_dev == dev && root_ino == ino) {
147                         *--bpt = '/';
148                         /*
149                          * It's unclear that it's a requirement to copy the
150                          * path to the beginning of the buffer, but it's always
151                          * been that way and stuff would probably break.
152                          */
153                         bcopy(bpt, pt, ept - bpt);
154                         free(up);
155                         return pt;
156                 }
157
158                 /*
159                  * Build pointer to the parent directory, allocating memory
160                  * as necessary.  Max length is 3 for "../", the largest
161                  * possible component name, plus a trailing NULL.
162                  */
163                 if (bup + 3  + MAXNAMLEN + 1 >= eup) {
164                         char *nup;
165
166                         if ((nup = realloc(up, upsize *= 2)) == NULL)
167                                 goto err;
168                         up = nup;
169                         bup = up;
170                         eup = up + upsize;
171                 }
172                 *bup++ = '.';
173                 *bup++ = '.';
174                 *bup = '\0';
175
176                 /* Open and stat parent directory. */
177                 if (!(dir = opendir(up)) || fstat(dirfd(dir), &s))
178                         goto err;
179
180                 /* Add trailing slash for next directory. */
181                 *bup++ = '/';
182
183                 /*
184                  * If it's a mount point, have to stat each element because
185                  * the inode number in the directory is for the entry in the
186                  * parent directory, not the inode number of the mounted file.
187                  */
188                 save_errno = 0;
189                 if (s.st_dev == dev) {
190                         for (;;) {
191                                 if (!(dp = readdir(dir)))
192                                         goto notfound;
193                                 if (dp->d_fileno == ino)
194                                         break;
195                         }
196                 } else
197                         for (;;) {
198                                 if (!(dp = readdir(dir)))
199                                         goto notfound;
200                                 if (ISDOT(dp))
201                                         continue;
202                                 bcopy(dp->d_name, bup, NAMLEN(dp) + 1);
203
204                                 /* Save the first error for later. */
205                                 if (lstat(up, &s)) {
206                                         if (!save_errno)
207                                                 save_errno = errno;
208                                         errno = 0;
209                                         continue;
210                                 }
211                                 if (s.st_dev == dev && s.st_ino == ino)
212                                         break;
213                         }
214
215                 /*
216                  * Check for length of the current name, preceding slash,
217                  * leading slash.
218                  */
219                 if (bpt - pt <= NAMLEN(dp) + (first ? 1 : 2)) {
220                         size_t len, off;
221                         char *npt;
222
223                         if (!ptsize) {
224                                 errno = ERANGE;
225                                 goto err;
226                         }
227                         off = bpt - pt;
228                         len = ept - bpt;
229                         if ((npt = realloc(pt, ptsize *= 2)) == NULL)
230                                 goto err;
231                         pt = npt;
232                         bpt = pt + off;
233                         ept = pt + ptsize;
234                         bcopy(bpt, ept - len, len);
235                         bpt = ept - len;
236                 }
237                 if (!first)
238                         *--bpt = '/';
239                 bpt -= NAMLEN(dp);
240                 bcopy(dp->d_name, bpt, NAMLEN(dp));
241                 (void)closedir(dir);
242
243                 /* Truncate any file name. */
244                 *bup = '\0';
245         }
246
247 notfound:
248         /*
249          * If readdir set errno, use it, not any saved error; otherwise,
250          * didn't find the current directory in its parent directory, set
251          * errno to ENOENT.
252          */
253         if (!errno)
254                 errno = save_errno ? save_errno : ENOENT;
255         /* FALLTHROUGH */
256 err:
257         if (ptsize)
258                 free(pt);
259         if (up)
260                 free(up);
261         if (dir)
262                 (void)closedir(dir);
263         return NULL;
264 }