blob: baf17f1a4d8f15619ef66b746f624ec2b1eaa4c3 [file] [log] [blame]
Ben Lindstromb4df15d2000-10-15 00:17:36 +00001/*
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
30#include "includes.h"
31
32#if !defined(HAVE_REALPATH) || defined(BROKEN_REALPATH)
33
34#if defined(LIBC_SCCS) && !defined(lint)
35static char *rcsid = "$OpenBSD: realpath..c,v 1.4 1998/05/18 09:55:19 deraadt Exp $";
36#endif /* LIBC_SCCS and not lint */
37
38#include <sys/param.h>
39#include <sys/stat.h>
40
41#include <errno.h>
42#include <fcntl.h>
43#include <stdlib.h>
44#include <string.h>
45#include <unistd.h>
46
47/*
48 * S_ISLNK compatibility
49 */
50#ifndef S_ISLNK
51#define S_ISLNK(m) ((m & 0170000) == 0120000)
52#endif
53
54/*
Ben Lindstrom65571522000-11-16 02:46:20 +000055 * MAXSYMLINKS
56 */
57#ifndef MAXSYMLINKS
58#define MAXSYMLINKS 5
59#endif
60
61/*
Ben Lindstromb4df15d2000-10-15 00:17:36 +000062 * char *realpath(const char *path, char resolved_path[MAXPATHLEN]);
63 *
64 * Find the real name of path, by removing all ".", ".." and symlink
65 * components. Returns (resolved) on success, or (NULL) on failure,
66 * in which case the path which caused trouble is left in (resolved).
67 */
68char *
69realpath(const char *path, char *resolved)
70{
71 struct stat sb;
72 int fd, n, rootd, serrno = 0;
73 char *p, *q, wbuf[MAXPATHLEN], start[MAXPATHLEN];
74 int symlinks = 0;
75
76 /* Save the starting point. */
77 getcwd(start,MAXPATHLEN);
78 if ((fd = open(".", O_RDONLY)) < 0) {
79 (void)strcpy(resolved, ".");
80 return (NULL);
81 }
82 close(fd);
83
84 /*
85 * Find the dirname and basename from the path to be resolved.
86 * Change directory to the dirname component.
87 * lstat the basename part.
88 * if it is a symlink, read in the value and loop.
89 * if it is a directory, then change to that directory.
90 * get the current directory name and append the basename.
91 */
92 (void)strncpy(resolved, path, MAXPATHLEN - 1);
93 resolved[MAXPATHLEN - 1] = '\0';
94loop:
95 q = strrchr(resolved, '/');
96 if (q != NULL) {
97 p = q + 1;
98 if (q == resolved)
99 q = "/";
100 else {
101 do {
102 --q;
103 } while (q > resolved && *q == '/');
104 q[1] = '\0';
105 q = resolved;
106 }
107 if (chdir(q) < 0)
108 goto err1;
109 } else
110 p = resolved;
111
112 /* Deal with the last component. */
113 if (lstat(p, &sb) == 0) {
114 if (S_ISLNK(sb.st_mode)) {
115 if (++symlinks > MAXSYMLINKS) {
116 serrno = ELOOP;
117 goto err1;
118 }
119 n = readlink(p, resolved, MAXPATHLEN-1);
120 if (n < 0)
121 goto err1;
122 resolved[n] = '\0';
123 goto loop;
124 }
125 if (S_ISDIR(sb.st_mode)) {
126 if (chdir(p) < 0)
127 goto err1;
128 p = "";
129 }
130 }
131
132 /*
133 * Save the last component name and get the full pathname of
134 * the current directory.
135 */
136 (void)strcpy(wbuf, p);
137 if (getcwd(resolved, MAXPATHLEN) == 0)
138 goto err1;
139
140 /*
141 * Join the two strings together, ensuring that the right thing
142 * happens if the last component is empty, or the dirname is root.
143 */
144 if (resolved[0] == '/' && resolved[1] == '\0')
145 rootd = 1;
146 else
147 rootd = 0;
148
149 if (*wbuf) {
150 if (strlen(resolved) + strlen(wbuf) + rootd + 1 > MAXPATHLEN) {
151 serrno = ENAMETOOLONG;
152 goto err1;
153 }
154 if (rootd == 0)
155 (void)strcat(resolved, "/");
156 (void)strcat(resolved, wbuf);
157 }
158
159 /* Go back to where we came from. */
160 if (chdir(start) < 0) {
161 serrno = errno;
162 goto err2;
163 }
164 return (resolved);
165
166err1: chdir(start);
167err2: errno = serrno;
168 return (NULL);
169}
170#endif /* !defined(HAVE_REALPATH) || defined(BROKEN_REALPATH) */