xref: /openbsd/lib/libc/stdlib/realpath.c (revision 1821443c)
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. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 #if defined(LIBC_SCCS) && !defined(lint)
34 static char *rcsid = "$OpenBSD: realpath.c,v 1.10 2003/08/01 21:04:59 millert Exp $";
35 #endif /* LIBC_SCCS and not lint */
36 
37 #include <sys/param.h>
38 #include <sys/stat.h>
39 
40 #include <errno.h>
41 #include <fcntl.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <unistd.h>
45 
46 /*
47  * char *realpath(const char *path, char resolved_path[MAXPATHLEN]);
48  *
49  * Find the real name of path, by removing all ".", ".." and symlink
50  * components.  Returns (resolved) on success, or (NULL) on failure,
51  * in which case the path which caused trouble is left in (resolved).
52  */
53 char *
54 realpath(path, resolved)
55 	const char *path;
56 	char *resolved;
57 {
58 	struct stat sb;
59 	int fd, n, needslash, serrno;
60 	char *p, *q, wbuf[MAXPATHLEN];
61 	int symlinks = 0;
62 
63 	/* Save the starting point. */
64 	if ((fd = open(".", O_RDONLY)) < 0) {
65 		(void)strlcpy(resolved, ".", MAXPATHLEN);
66 		return (NULL);
67 	}
68 
69 	/* Convert "." -> "" to optimize away a needless lstat() and chdir() */
70 	if (path[0] == '.' && path[1] == '\0')
71 		path = "";
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 	strlcpy(resolved, path, MAXPATHLEN);
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 (*p != '\0' && lstat(p, &sb) == 0) {
102 		if (S_ISLNK(sb.st_mode)) {
103 			if (++symlinks > MAXSYMLINKS) {
104 				errno = ELOOP;
105 				goto err1;
106 			}
107 			n = readlink(p, resolved, MAXPATHLEN-1);
108 			if (n < 0)
109 				goto err1;
110 			resolved[n] = '\0';
111 			goto loop;
112 		}
113 		if (S_ISDIR(sb.st_mode)) {
114 			if (chdir(p) < 0)
115 				goto err1;
116 			p = "";
117 		}
118 	}
119 
120 	/*
121 	 * Save the last component name and get the full pathname of
122 	 * the current directory.
123 	 */
124 	(void)strlcpy(wbuf, p, sizeof wbuf);
125 	if (getcwd(resolved, MAXPATHLEN) == 0)
126 		goto err1;
127 
128 	/*
129 	 * Join the two strings together, ensuring that the right thing
130 	 * happens if the last component is empty, or the dirname is root.
131 	 */
132 	if (resolved[0] == '/' && resolved[1] == '\0')
133 		needslash = 0;
134 	else
135 		needslash = 1;
136 
137 	if (*wbuf) {
138 		if (strlen(resolved) + strlen(wbuf) + needslash >= MAXPATHLEN) {
139 			errno = ENAMETOOLONG;
140 			goto err1;
141 		}
142 		if (needslash)
143 			strlcat(resolved, "/", MAXPATHLEN);
144 		strlcat(resolved, wbuf, MAXPATHLEN);
145 	}
146 
147 	/* Go back to where we came from. */
148 	if (fchdir(fd) < 0) {
149 		serrno = errno;
150 		goto err2;
151 	}
152 
153 	/* It's okay if the close fails, what's an fd more or less? */
154 	(void)close(fd);
155 	return (resolved);
156 
157 err1:	serrno = errno;
158 	(void)fchdir(fd);
159 err2:	(void)close(fd);
160 	errno = serrno;
161 	return (NULL);
162 }
163