xref: /freebsd/lib/libc/stdlib/realpath.c (revision 17ee9d00bc1ae1e598c38f25826f861e4bc6c3ce)
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 
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  */
57 char *
58 realpath(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 
66 	/* Save the starting point. */
67 	if ((fd = open(".", O_RDONLY)) < 0) {
68 		(void)strcpy(resolved, ".");
69 		return (NULL);
70 	}
71 
72 	/*
73 	 * Find the dirname and basename from the path to be resolved.
74 	 * Change directory to the dirname component.
75 	 * lstat the basename part.
76 	 *     if it is a symlink, read in the value and loop.
77 	 *     if it is a directory, then change to that directory.
78 	 * get the current directory name and append the basename.
79 	 */
80 	(void)strncpy(resolved, path, MAXPATHLEN - 1);
81 	resolved[MAXPATHLEN - 1] = '\0';
82 loop:
83 	q = strrchr(resolved, '/');
84 	if (q != NULL) {
85 		p = q + 1;
86 		if (q == resolved)
87 			q = "/";
88 		else {
89 			do {
90 				--q;
91 			} while (q > resolved && *q == '/');
92 			q[1] = '\0';
93 			q = resolved;
94 		}
95 		if (chdir(q) < 0)
96 			goto err1;
97 	} else
98 		p = resolved;
99 
100 	/* Deal with the last component. */
101 	if (lstat(p, &sb) == 0) {
102 		if (S_ISLNK(sb.st_mode)) {
103 			n = readlink(p, resolved, MAXPATHLEN);
104 			if (n < 0)
105 				goto err1;
106 			resolved[n] = '\0';
107 			goto loop;
108 		}
109 		if (S_ISDIR(sb.st_mode)) {
110 			if (chdir(p) < 0)
111 				goto err1;
112 			p = "";
113 		}
114 	}
115 
116 	/*
117 	 * Save the last component name and get the full pathname of
118 	 * the current directory.
119 	 */
120 	(void)strcpy(wbuf, p);
121 	if (getcwd(resolved, MAXPATHLEN) == 0)
122 		goto err1;
123 
124 	/*
125 	 * Join the two strings together, ensuring that the right thing
126 	 * happens if the last component is empty, or the dirname is root.
127 	 */
128 	if (resolved[0] == '/' && resolved[1] == '\0')
129 		rootd = 1;
130 	else
131 		rootd = 0;
132 
133 	if (*wbuf) {
134 		if (strlen(resolved) + strlen(wbuf) + rootd + 1 > MAXPATHLEN) {
135 			errno = ENAMETOOLONG;
136 			goto err1;
137 		}
138 		if (rootd == 0)
139 			(void)strcat(resolved, "/");
140 		(void)strcat(resolved, wbuf);
141 	}
142 
143 	/* Go back to where we came from. */
144 	if (fchdir(fd) < 0) {
145 		serrno = errno;
146 		goto err2;
147 	}
148 
149 	/* It's okay if the close fails, what's an fd more or less? */
150 	(void)close(fd);
151 	return (resolved);
152 
153 err1:	serrno = errno;
154 	(void)fchdir(fd);
155 err2:	(void)close(fd);
156 	errno = serrno;
157 	return (NULL);
158 }
159