Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[dragonfly.git] / crypto / openssh / openbsd-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  *
14  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $OpenBSD: getcwd.c,v 1.6 2000/07/19 15:25:13 deraadt Exp $
27  */
28
29 #include "includes.h"
30
31 #if !defined(HAVE_GETCWD)
32
33 #include <sys/param.h>
34 #include <sys/stat.h>
35 #include <errno.h>
36 #include <dirent.h>
37 #include <sys/dir.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include "includes.h"
42
43 #define ISDOT(dp) \
44         (dp->d_name[0] == '.' && (dp->d_name[1] == '\0' || \
45             (dp->d_name[1] == '.' && dp->d_name[2] == '\0')))
46
47 char *
48 getcwd(char *pt,size_t size)
49 {
50         register struct dirent *dp;
51         register DIR *dir = NULL;
52         register dev_t dev;
53         register ino_t ino;
54         register int first;
55         register char *bpt, *bup;
56         struct stat s;
57         dev_t root_dev;
58         ino_t root_ino;
59         size_t ptsize, upsize;
60         int save_errno;
61         char *ept, *eup, *up;
62
63         /*
64          * If no buffer specified by the user, allocate one as necessary.
65          * If a buffer is specified, the size has to be non-zero.  The path
66          * is built from the end of the buffer backwards.
67          */
68         if (pt) {
69                 ptsize = 0;
70                 if (!size) {
71                         errno = EINVAL;
72                         return (NULL);
73                 }
74                 ept = pt + size;
75         } else {
76                 if ((pt = malloc(ptsize = 1024 - 4)) == NULL)
77                         return (NULL);
78                 ept = pt + ptsize;
79         }
80         bpt = ept - 1;
81         *bpt = '\0';
82
83         /*
84          * Allocate bytes (1024 - malloc space) for the string of "../"'s.
85          * Should always be enough (it's 340 levels).  If it's not, allocate
86          * as necessary.  Special * case the first stat, it's ".", not "..".
87          */
88         if ((up = malloc(upsize = 1024 - 4)) == NULL)
89                 goto err;
90         eup = up + MAXPATHLEN;
91         bup = up;
92         up[0] = '.';
93         up[1] = '\0';
94
95         /* Save root values, so know when to stop. */
96         if (stat("/", &s))
97                 goto err;
98         root_dev = s.st_dev;
99         root_ino = s.st_ino;
100
101         errno = 0;                      /* XXX readdir has no error return. */
102
103         for (first = 1;; first = 0) {
104                 /* Stat the current level. */
105                 if (lstat(up, &s))
106                         goto err;
107
108                 /* Save current node values. */
109                 ino = s.st_ino;
110                 dev = s.st_dev;
111
112                 /* Check for reaching root. */
113                 if (root_dev == dev && root_ino == ino) {
114                         *--bpt = '/';
115                         /*
116                          * It's unclear that it's a requirement to copy the
117                          * path to the beginning of the buffer, but it's always
118                          * been that way and stuff would probably break.
119                          */
120                         memmove(pt, bpt, ept - bpt);
121                         free(up);
122                         return (pt);
123                 }
124
125                 /*
126                  * Build pointer to the parent directory, allocating memory
127                  * as necessary.  Max length is 3 for "../", the largest
128                  * possible component name, plus a trailing NULL.
129                  */
130                 if (bup + 3  + MAXNAMLEN + 1 >= eup) {
131                         char *nup;
132
133                         if ((nup = realloc(up, upsize *= 2)) == NULL)
134                                 goto err;
135                         up = nup;
136                         bup = up;
137                         eup = up + upsize;
138                 }
139                 *bup++ = '.';
140                 *bup++ = '.';
141                 *bup = '\0';
142
143                 /* Open and stat parent directory. 
144                  * RACE?? - replaced fstat(dirfd(dir), &s) w/ lstat(up,&s) 
145                  */
146                 if (!(dir = opendir(up)) || lstat(up,&s))
147                         goto err;
148
149                 /* Add trailing slash for next directory. */
150                 *bup++ = '/';
151
152                 /*
153                  * If it's a mount point, have to stat each element because
154                  * the inode number in the directory is for the entry in the
155                  * parent directory, not the inode number of the mounted file.
156                  */
157                 save_errno = 0;
158                 if (s.st_dev == dev) {
159                         for (;;) {
160                                 if (!(dp = readdir(dir)))
161                                         goto notfound;
162                                 if (dp->d_fileno == ino)
163                                         break;
164                         }
165                 } else
166                         for (;;) {
167                                 if (!(dp = readdir(dir)))
168                                         goto notfound;
169                                 if (ISDOT(dp))
170                                         continue;
171                                 memmove(bup, dp->d_name, dp->d_namlen + 1);
172
173                                 /* Save the first error for later. */
174                                 if (lstat(up, &s)) {
175                                         if (!save_errno)
176                                                 save_errno = errno;
177                                         errno = 0;
178                                         continue;
179                                 }
180                                 if (s.st_dev == dev && s.st_ino == ino)
181                                         break;
182                         }
183
184                 /*
185                  * Check for length of the current name, preceding slash,
186                  * leading slash.
187                  */
188                 if (bpt - pt < dp->d_namlen + (first ? 1 : 2)) {
189                         size_t len, off;
190                         char *npt;
191
192                         if (!ptsize) {
193                                 errno = ERANGE;
194                                 goto err;
195                         }
196                         off = bpt - pt;
197                         len = ept - bpt;
198                         if ((npt = realloc(pt, ptsize *= 2)) == NULL)
199                                 goto err;
200                         pt = npt;
201                         bpt = pt + off;
202                         ept = pt + ptsize;
203                         memmove(ept - len, bpt, len);
204                         bpt = ept - len;
205                 }
206                 if (!first)
207                         *--bpt = '/';
208                 bpt -= dp->d_namlen;
209                 memmove(bpt, dp->d_name, dp->d_namlen);
210                 (void)closedir(dir);
211
212                 /* Truncate any file name. */
213                 *bup = '\0';
214         }
215
216 notfound:
217         /*
218          * If readdir set errno, use it, not any saved error; otherwise,
219          * didn't find the current directory in its parent directory, set
220          * errno to ENOENT.
221          */
222         if (!errno)
223                 errno = save_errno ? save_errno : ENOENT;
224         /* FALLTHROUGH */
225 err:
226         if (ptsize)
227                 free(pt);
228         if (up)
229                 free(up);
230         if (dir)
231                 (void)closedir(dir);
232         return (NULL);
233 }
234
235 #endif /* !defined(HAVE_GETCWD) */