xref: /freebsd/lib/libc/stdlib/realpath.c (revision f9218d3d4fd34f082473b3a021c6d4d109fb47cf)
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)
38 static char sccsid[] = "@(#)realpath.c	8.1 (Berkeley) 2/16/94";
39 #endif /* LIBC_SCCS and not lint */
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD$");
42 
43 #include "namespace.h"
44 #include <sys/param.h>
45 #include <sys/stat.h>
46 
47 #include <errno.h>
48 #include <fcntl.h>
49 #include <stdlib.h>
50 #include <string.h>
51 #include <unistd.h>
52 #include "un-namespace.h"
53 
54 /*
55  * char *realpath(const char *path, char resolved_path[PATH_MAX]);
56  *
57  * Find the real name of path, by removing all ".", ".." and symlink
58  * components.  Returns (resolved) on success, or (NULL) on failure,
59  * in which case the path which caused trouble is left in (resolved).
60  */
61 char *
62 realpath(path, resolved)
63 	const char *path;
64 	char *resolved;
65 {
66 	struct stat sb;
67 	int fd, n, rootd, serrno;
68 	char *p, *q, wbuf[PATH_MAX];
69 	int symlinks = 0;
70 
71 	/* Save the starting point. */
72 	if ((fd = _open(".", O_RDONLY)) < 0) {
73 		(void)strcpy(resolved, ".");
74 		return (NULL);
75 	}
76 
77 	/*
78 	 * Find the dirname and basename from the path to be resolved.
79 	 * Change directory to the dirname component.
80 	 * lstat the basename part.
81 	 *     if it is a symlink, read in the value and loop.
82 	 *     if it is a directory, then change to that directory.
83 	 * get the current directory name and append the basename.
84 	 */
85 	(void)strlcpy(resolved, path, PATH_MAX);
86 loop:
87 	q = strrchr(resolved, '/');
88 	if (q != NULL) {
89 		p = q + 1;
90 		if (q == resolved)
91 			q = "/";
92 		else {
93 			do {
94 				--q;
95 			} while (q > resolved && *q == '/');
96 			q[1] = '\0';
97 			q = resolved;
98 		}
99 		if (chdir(q) < 0)
100 			goto err1;
101 	} else
102 		p = resolved;
103 
104 	/* Deal with the last component. */
105 	if (*p != '\0' && lstat(p, &sb) == 0) {
106 		if (S_ISLNK(sb.st_mode)) {
107 			if (++symlinks > MAXSYMLINKS) {
108 				errno = ELOOP;
109 				goto err1;
110 			}
111 			n = readlink(p, resolved, PATH_MAX - 1);
112 			if (n < 0)
113 				goto err1;
114 			resolved[n] = '\0';
115 			goto loop;
116 		}
117 		if (S_ISDIR(sb.st_mode)) {
118 			if (chdir(p) < 0)
119 				goto err1;
120 			p = "";
121 		}
122 	}
123 
124 	/*
125 	 * Save the last component name and get the full pathname of
126 	 * the current directory.
127 	 */
128 	(void)strcpy(wbuf, p);
129 	if (getcwd(resolved, PATH_MAX) == 0)
130 		goto err1;
131 
132 	/*
133 	 * Join the two strings together, ensuring that the right thing
134 	 * happens if the last component is empty, or the dirname is root.
135 	 */
136 	if (resolved[0] == '/' && resolved[1] == '\0')
137 		rootd = 1;
138 	else
139 		rootd = 0;
140 
141 	if (*wbuf) {
142 		if (strlen(resolved) + strlen(wbuf) + rootd + 1 > PATH_MAX) {
143 			errno = ENAMETOOLONG;
144 			goto err1;
145 		}
146 		if (rootd == 0)
147 			(void)strcat(resolved, "/");
148 		(void)strcat(resolved, wbuf);
149 	}
150 
151 	/* Go back to where we came from. */
152 	if (fchdir(fd) < 0) {
153 		serrno = errno;
154 		goto err2;
155 	}
156 
157 	/* It's okay if the close fails, what's an fd more or less? */
158 	(void)_close(fd);
159 	return (resolved);
160 
161 err1:	serrno = errno;
162 	(void)fchdir(fd);
163 err2:	(void)_close(fd);
164 	errno = serrno;
165 	return (NULL);
166 }
167