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