realpath.c revision 27449
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 * 3. All advertising materials mentioning features or use of this software
17 *    must display the following acknowledgement:
18 *	This product includes software developed by the University of
19 *	California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 *    may be used to endorse or promote products derived from this software
22 *    without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 */
36
37#if defined(LIBC_SCCS) && !defined(lint)
38static char sccsid[] = "@(#)realpath.c	8.1 (Berkeley) 2/16/94";
39#endif /* LIBC_SCCS and not lint */
40
41#include <sys/param.h>
42#include <sys/stat.h>
43
44#include <errno.h>
45#include <fcntl.h>
46#include <stdlib.h>
47#include <string.h>
48#include <unistd.h>
49
50/*
51 * char *realpath(const char *path, char resolved_path[MAXPATHLEN]);
52 *
53 * Find the real name of path, by removing all ".", ".." and symlink
54 * components.  Returns (resolved) on success, or (NULL) on failure,
55 * in which case the path which caused trouble is left in (resolved).
56 */
57char *
58realpath(path, resolved)
59	const char *path;
60	char *resolved;
61{
62	struct stat sb;
63	int fd, n, rootd, serrno;
64	char *p, *q, wbuf[MAXPATHLEN];
65      int symlinks = 0;
66
67	/* Save the starting point. */
68	if ((fd = open(".", O_RDONLY)) < 0) {
69		(void)strcpy(resolved, ".");
70		return (NULL);
71	}
72
73	/*
74	 * Find the dirname and basename from the path to be resolved.
75	 * Change directory to the dirname component.
76	 * lstat the basename part.
77	 *     if it is a symlink, read in the value and loop.
78	 *     if it is a directory, then change to that directory.
79	 * get the current directory name and append the basename.
80	 */
81	(void)strncpy(resolved, path, MAXPATHLEN - 1);
82	resolved[MAXPATHLEN - 1] = '\0';
83loop:
84	q = strrchr(resolved, '/');
85	if (q != NULL) {
86		p = q + 1;
87		if (q == resolved)
88			q = "/";
89		else {
90			do {
91				--q;
92			} while (q > resolved && *q == '/');
93			q[1] = '\0';
94			q = resolved;
95		}
96		if (chdir(q) < 0)
97			goto err1;
98	} else
99		p = resolved;
100
101	/* Deal with the last component. */
102	if (*p != '\0' && lstat(p, &sb) == 0) {
103		if (S_ISLNK(sb.st_mode)) {
104                      if (++symlinks > MAXSYMLINKS) {
105                              errno = ELOOP;
106                              goto err1;
107                      }
108			n = readlink(p, resolved, MAXPATHLEN);
109			if (n < 0)
110				goto err1;
111			resolved[n] = '\0';
112			goto loop;
113		}
114		if (S_ISDIR(sb.st_mode)) {
115			if (chdir(p) < 0)
116				goto err1;
117			p = "";
118		}
119	}
120
121	/*
122	 * Save the last component name and get the full pathname of
123	 * the current directory.
124	 */
125	(void)strcpy(wbuf, p);
126	if (getcwd(resolved, MAXPATHLEN) == 0)
127		goto err1;
128
129	/*
130	 * Join the two strings together, ensuring that the right thing
131	 * happens if the last component is empty, or the dirname is root.
132	 */
133	if (resolved[0] == '/' && resolved[1] == '\0')
134		rootd = 1;
135	else
136		rootd = 0;
137
138	if (*wbuf) {
139		if (strlen(resolved) + strlen(wbuf) + rootd + 1 > MAXPATHLEN) {
140			errno = ENAMETOOLONG;
141			goto err1;
142		}
143		if (rootd == 0)
144			(void)strcat(resolved, "/");
145		(void)strcat(resolved, wbuf);
146	}
147
148	/* Go back to where we came from. */
149	if (fchdir(fd) < 0) {
150		serrno = errno;
151		goto err2;
152	}
153
154	/* It's okay if the close fails, what's an fd more or less? */
155	(void)close(fd);
156	return (resolved);
157
158err1:	serrno = errno;
159	(void)fchdir(fd);
160err2:	(void)close(fd);
161	errno = serrno;
162	return (NULL);
163}
164