Import OpenSSH 3.8.1.p1 into base.
[dragonfly.git] / crypto / openssh-3.8.1p1 / openbsd-compat / realpath.c
1 /* OPENBSD ORIGINAL: lib/libc/stdlib/realpath.c */
2
3 /*
4  * Copyright (c) 1994
5  *      The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Jan-Simon Pendry.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34
35 #include "includes.h"
36
37 #if !defined(HAVE_REALPATH) || defined(BROKEN_REALPATH)
38
39 #if defined(LIBC_SCCS) && !defined(lint)
40 static char *rcsid = "$OpenBSD: realpath.c,v 1.10 2003/08/01 21:04:59 millert Exp $";
41 #endif /* LIBC_SCCS and not lint */
42
43 #include <sys/param.h>
44 #include <sys/stat.h>
45
46 #include <errno.h>
47 #include <fcntl.h>
48 #include <stdlib.h>
49 #include <string.h>
50 #include <unistd.h>
51
52 /*
53  * MAXSYMLINKS
54  */
55 #ifndef MAXSYMLINKS
56 #define MAXSYMLINKS 5
57 #endif
58
59 /*
60  * char *realpath(const char *path, char resolved_path[MAXPATHLEN]);
61  *
62  * Find the real name of path, by removing all ".", ".." and symlink
63  * components.  Returns (resolved) on success, or (NULL) on failure,
64  * in which case the path which caused trouble is left in (resolved).
65  */
66 char *
67 realpath(const char *path, char *resolved)
68 {
69         struct stat sb;
70         int fd, n, needslash, serrno = 0;
71         char *p, *q, wbuf[MAXPATHLEN], start[MAXPATHLEN];
72         int symlinks = 0;
73
74         /* Save the starting point. */
75         getcwd(start,MAXPATHLEN);       
76         if ((fd = open(".", O_RDONLY)) < 0) {
77                 (void)strlcpy(resolved, ".", MAXPATHLEN);
78                 return (NULL);
79         }
80         close(fd);
81
82         /* Convert "." -> "" to optimize away a needless lstat() and chdir() */
83         if (path[0] == '.' && path[1] == '\0')
84                 path = "";
85
86         /*
87          * Find the dirname and basename from the path to be resolved.
88          * Change directory to the dirname component.
89          * lstat the basename part.
90          *     if it is a symlink, read in the value and loop.
91          *     if it is a directory, then change to that directory.
92          * get the current directory name and append the basename.
93          */
94         strlcpy(resolved, path, MAXPATHLEN);
95 loop:
96         q = strrchr(resolved, '/');
97         if (q != NULL) {
98                 p = q + 1;
99                 if (q == resolved)
100                         q = "/";
101                 else {
102                         do {
103                                 --q;
104                         } while (q > resolved && *q == '/');
105                         q[1] = '\0';
106                         q = resolved;
107                 }
108                 if (chdir(q) < 0)
109                         goto err1;
110         } else
111                 p = resolved;
112
113         /* Deal with the last component. */
114         if (*p != '\0' && lstat(p, &sb) == 0) {
115                 if (S_ISLNK(sb.st_mode)) {
116                         if (++symlinks > MAXSYMLINKS) {
117                                 serrno = ELOOP;
118                                 goto err1;
119                         }
120                         n = readlink(p, resolved, MAXPATHLEN-1);
121                         if (n < 0)
122                                 goto err1;
123                         resolved[n] = '\0';
124                         goto loop;
125                 }
126                 if (S_ISDIR(sb.st_mode)) {
127                         if (chdir(p) < 0)
128                                 goto err1;
129                         p = "";
130                 }
131         }
132
133         /*
134          * Save the last component name and get the full pathname of
135          * the current directory.
136          */
137         (void)strlcpy(wbuf, p, sizeof wbuf);
138         if (getcwd(resolved, MAXPATHLEN) == 0)
139                 goto err1;
140
141         /*
142          * Join the two strings together, ensuring that the right thing
143          * happens if the last component is empty, or the dirname is root.
144          */
145         if (resolved[0] == '/' && resolved[1] == '\0')
146                 needslash = 0;
147         else
148                 needslash = 1;
149
150         if (*wbuf) {
151                 if (strlen(resolved) + strlen(wbuf) + needslash >= MAXPATHLEN) {
152                         serrno = ENAMETOOLONG;
153                         goto err1;
154                 }
155                 if (needslash)
156                         strlcat(resolved, "/", MAXPATHLEN);
157                 strlcat(resolved, wbuf, MAXPATHLEN);
158         }
159
160         /* Go back to where we came from. */
161         if (chdir(start) < 0) {
162                 serrno = errno;
163                 goto err2;
164         }
165         return (resolved);
166
167 err1:   chdir(start);
168 err2:   errno = serrno;
169         return (NULL);
170 }
171 #endif /* !defined(HAVE_REALPATH) || defined(BROKEN_REALPATH) */