]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libc/gen/getcwd.c
awk: Merge upstream 2nd Edition Awk Book
[FreeBSD/FreeBSD.git] / lib / libc / gen / getcwd.c
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1989, 1991, 1993, 1995
5  *      The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31
32 #include <sys/cdefs.h>
33 __SCCSID("@(#)getcwd.c  8.5 (Berkeley) 2/7/95");
34 #include "namespace.h"
35 #include <sys/param.h>
36 #include <sys/stat.h>
37
38 #include <dirent.h>
39 #include <errno.h>
40 #include <fcntl.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <unistd.h>
45 #include "un-namespace.h"
46
47 #include "gen-private.h"
48
49 #define ISDOT(dp) \
50         (dp->d_name[0] == '.' && (dp->d_name[1] == '\0' || \
51             (dp->d_name[1] == '.' && dp->d_name[2] == '\0')))
52
53 extern int __getcwd(char *, size_t);
54
55 char *
56 getcwd(char *pt, size_t size)
57 {
58         struct dirent *dp;
59         DIR *dir = NULL;
60         dev_t dev;
61         ino_t ino;
62         int first;
63         char *bpt;
64         struct stat s;
65         dev_t root_dev;
66         ino_t root_ino;
67         size_t ptsize;
68         int save_errno;
69         char *ept, c;
70         int fd;
71
72         /*
73          * If no buffer specified by the user, allocate one as necessary.
74          * If a buffer is specified, the size has to be non-zero.  The path
75          * is built from the end of the buffer backwards.
76          */
77         if (pt) {
78                 ptsize = 0;
79                 if (!size) {
80                         errno = EINVAL;
81                         return (NULL);
82                 }
83                 if (size == 1) {
84                         errno = ERANGE;
85                         return (NULL);
86                 }
87                 ept = pt + size;
88         } else {
89                 if ((pt = malloc(ptsize = PATH_MAX)) == NULL)
90                         return (NULL);
91                 ept = pt + ptsize;
92         }
93         if (__getcwd(pt, ept - pt) == 0) {
94                 if (*pt != '/') {
95                         bpt = pt;
96                         ept = pt + strlen(pt) - 1;
97                         while (bpt < ept) {
98                                 c = *bpt;
99                                 *bpt++ = *ept;
100                                 *ept-- = c;
101                         }
102                 }
103                 return (pt);
104         }
105         bpt = ept - 1;
106         *bpt = '\0';
107
108         /* Save root values, so know when to stop. */
109         if (stat("/", &s))
110                 goto err;
111         root_dev = s.st_dev;
112         root_ino = s.st_ino;
113
114         errno = 0;                      /* XXX readdir has no error return. */
115
116         for (first = 1;; first = 0) {
117                 /* Stat the current level. */
118                 if (dir != NULL ? _fstat(_dirfd(dir), &s) : lstat(".", &s))
119                         goto err;
120
121                 /* Save current node values. */
122                 ino = s.st_ino;
123                 dev = s.st_dev;
124
125                 /* Check for reaching root. */
126                 if (root_dev == dev && root_ino == ino) {
127                         *--bpt = '/';
128                         /*
129                          * It's unclear that it's a requirement to copy the
130                          * path to the beginning of the buffer, but it's always
131                          * been that way and stuff would probably break.
132                          */
133                         bcopy(bpt, pt, ept - bpt);
134                         if (dir)
135                                 (void) closedir(dir);
136                         return (pt);
137                 }
138
139                 /* Open and stat parent directory. */
140                 fd = _openat(dir != NULL ? _dirfd(dir) : AT_FDCWD,
141                                 "..", O_RDONLY | O_CLOEXEC);
142                 if (fd == -1)
143                         goto err;
144                 if (dir)
145                         (void) closedir(dir);
146                 if (!(dir = fdopendir(fd)) || _fstat(_dirfd(dir), &s)) {
147                         _close(fd);
148                         goto err;
149                 }
150
151                 /*
152                  * If it's a mount point, have to stat each element because
153                  * the inode number in the directory is for the entry in the
154                  * parent directory, not the inode number of the mounted file.
155                  */
156                 save_errno = 0;
157                 if (s.st_dev == dev) {
158                         for (;;) {
159                                 if (!(dp = readdir(dir)))
160                                         goto notfound;
161                                 if (dp->d_fileno == ino)
162                                         break;
163                         }
164                 } else
165                         for (;;) {
166                                 if (!(dp = readdir(dir)))
167                                         goto notfound;
168                                 if (ISDOT(dp))
169                                         continue;
170
171                                 /* Save the first error for later. */
172                                 if (fstatat(_dirfd(dir), dp->d_name, &s,
173                                     AT_SYMLINK_NOFOLLOW)) {
174                                         if (!save_errno)
175                                                 save_errno = errno;
176                                         errno = 0;
177                                         continue;
178                                 }
179                                 if (s.st_dev == dev && s.st_ino == ino)
180                                         break;
181                         }
182
183                 /*
184                  * Check for length of the current name, preceding slash,
185                  * leading slash.
186                  */
187                 while (bpt - pt < dp->d_namlen + (first ? 1 : 2)) {
188                         size_t len, off;
189
190                         if (!ptsize) {
191                                 errno = ERANGE;
192                                 goto err;
193                         }
194                         off = bpt - pt;
195                         len = ept - bpt;
196                         if ((pt = reallocf(pt, ptsize *= 2)) == NULL)
197                                 goto err;
198                         bpt = pt + off;
199                         ept = pt + ptsize;
200                         bcopy(bpt, ept - len, len);
201                         bpt = ept - len;
202                 }
203                 if (!first)
204                         *--bpt = '/';
205                 bpt -= dp->d_namlen;
206                 bcopy(dp->d_name, bpt, dp->d_namlen);
207         }
208
209 notfound:
210         /*
211          * If readdir set errno, use it, not any saved error; otherwise,
212          * didn't find the current directory in its parent directory, set
213          * errno to ENOENT.
214          */
215         if (!errno)
216                 errno = save_errno ? save_errno : ENOENT;
217         /* FALLTHROUGH */
218 err:
219         save_errno = errno;
220
221         if (ptsize)
222                 free(pt);
223         if (dir)
224                 (void) closedir(dir);
225
226         errno = save_errno;
227         return (NULL);
228 }