Remove local main() definition.
[dragonfly.git] / crypto / openssh / openbsd-compat / realpath.c
1 /*
2  * Copyright (c) 1994
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Jan-Simon Pendry.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
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  * $OpenBSD: realpath.c,v 1.7 2002/05/24 21:22:37 deraadt Exp $
30  */
31
32 #include "includes.h"
33
34 #if !defined(HAVE_REALPATH) || defined(BROKEN_REALPATH)
35
36 #include <sys/param.h>
37 #include <sys/stat.h>
38
39 #include <errno.h>
40 #include <fcntl.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <unistd.h>
44
45 /*
46  * MAXSYMLINKS
47  */
48 #ifndef MAXSYMLINKS
49 #define MAXSYMLINKS 5
50 #endif
51
52 /*
53  * char *realpath(const char *path, char resolved_path[MAXPATHLEN]);
54  *
55  * Find the real name of path, by removing all ".", ".." and symlink
56  * components.  Returns (resolved) on success, or (NULL) on failure,
57  * in which case the path which caused trouble is left in (resolved).
58  */
59 char *
60 realpath(const char *path, char *resolved)
61 {
62         struct stat sb;
63         int fd, n, rootd, serrno = 0;
64         char *p, *q, wbuf[MAXPATHLEN], start[MAXPATHLEN];
65         int symlinks = 0;
66
67         /* Save the starting point. */
68         getcwd(start,MAXPATHLEN);       
69         if ((fd = open(".", O_RDONLY)) < 0) {
70                 (void)strlcpy(resolved, ".", MAXPATHLEN);
71                 return (NULL);
72         }
73         close(fd);
74
75         /* Convert "." -> "" to optimize away a needless lstat() and chdir() */
76         if (path[0] == '.' && path[1] == '\0')
77                 path = "";
78
79         /*
80          * Find the dirname and basename from the path to be resolved.
81          * Change directory to the dirname component.
82          * lstat the basename part.
83          *     if it is a symlink, read in the value and loop.
84          *     if it is a directory, then change to that directory.
85          * get the current directory name and append the basename.
86          */
87         strlcpy(resolved, path, MAXPATHLEN);
88 loop:
89         q = strrchr(resolved, '/');
90         if (q != NULL) {
91                 p = q + 1;
92                 if (q == resolved)
93                         q = "/";
94                 else {
95                         do {
96                                 --q;
97                         } while (q > resolved && *q == '/');
98                         q[1] = '\0';
99                         q = resolved;
100                 }
101                 if (chdir(q) < 0)
102                         goto err1;
103         } else
104                 p = resolved;
105
106         /* Deal with the last component. */
107         if (*p != '\0' && lstat(p, &sb) == 0) {
108                 if (S_ISLNK(sb.st_mode)) {
109                         if (++symlinks > MAXSYMLINKS) {
110                                 serrno = ELOOP;
111                                 goto err1;
112                         }
113                         n = readlink(p, resolved, MAXPATHLEN-1);
114                         if (n < 0)
115                                 goto err1;
116                         resolved[n] = '\0';
117                         goto loop;
118                 }
119                 if (S_ISDIR(sb.st_mode)) {
120                         if (chdir(p) < 0)
121                                 goto err1;
122                         p = "";
123                 }
124         }
125
126         /*
127          * Save the last component name and get the full pathname of
128          * the current directory.
129          */
130         (void)strlcpy(wbuf, p, sizeof wbuf);
131         if (getcwd(resolved, MAXPATHLEN) == 0)
132                 goto err1;
133
134         /*
135          * Join the two strings together, ensuring that the right thing
136          * happens if the last component is empty, or the dirname is root.
137          */
138         if (resolved[0] == '/' && resolved[1] == '\0')
139                 rootd = 1;
140         else
141                 rootd = 0;
142
143         if (*wbuf) {
144                 if (strlen(resolved) + strlen(wbuf) + rootd + 1 > MAXPATHLEN) {
145                         serrno = ENAMETOOLONG;
146                         goto err1;
147                 }
148                 if (rootd == 0)
149                         (void)strcat(resolved, "/");
150                 (void)strcat(resolved, wbuf);
151         }
152
153         /* Go back to where we came from. */
154         if (chdir(start) < 0) {
155                 serrno = errno;
156                 goto err2;
157         }
158         return (resolved);
159
160 err1:   chdir(start);
161 err2:   errno = serrno;
162         return (NULL);
163 }
164 #endif /* !defined(HAVE_REALPATH) || defined(BROKEN_REALPATH) */