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