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