3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
32 #if !defined(HAVE_REALPATH) || defined(BROKEN_REALPATH)
34 #if defined(LIBC_SCCS) && !defined(lint)
35 static char *rcsid = "$OpenBSD: realpath.c,v 1.5 2001/06/27 00:58:56 lebel Exp $";
36 #endif /* LIBC_SCCS and not lint */
38 #include <sys/param.h>
55 * char *realpath(const char *path, char resolved_path[MAXPATHLEN]);
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).
62 realpath(const char *path, char *resolved)
65 int fd, n, rootd, serrno = 0;
66 char *p, *q, wbuf[MAXPATHLEN], start[MAXPATHLEN];
69 /* Save the starting point. */
70 getcwd(start,MAXPATHLEN);
71 if ((fd = open(".", O_RDONLY)) < 0) {
72 (void)strcpy(resolved, ".");
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.
85 strlcpy(resolved, path, MAXPATHLEN);
87 q = strrchr(resolved, '/');
95 } while (q > resolved && *q == '/');
104 /* Deal with the last component. */
105 if (lstat(p, &sb) == 0) {
106 if (S_ISLNK(sb.st_mode)) {
107 if (++symlinks > MAXSYMLINKS) {
111 n = readlink(p, resolved, MAXPATHLEN-1);
117 if (S_ISDIR(sb.st_mode)) {
125 * Save the last component name and get the full pathname of
126 * the current directory.
128 (void)strcpy(wbuf, p);
129 if (getcwd(resolved, MAXPATHLEN) == 0)
133 * Join the two strings together, ensuring that the right thing
134 * happens if the last component is empty, or the dirname is root.
136 if (resolved[0] == '/' && resolved[1] == '\0')
142 if (strlen(resolved) + strlen(wbuf) + rootd + 1 > MAXPATHLEN) {
143 serrno = ENAMETOOLONG;
147 (void)strcat(resolved, "/");
148 (void)strcat(resolved, wbuf);
151 /* Go back to where we came from. */
152 if (chdir(start) < 0) {
159 err2: errno = serrno;
162 #endif /* !defined(HAVE_REALPATH) || defined(BROKEN_REALPATH) */