realpath.c revision 98937
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 *
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
27 * SUCH DAMAGE.
28 */
29
30#include "includes.h"
31
32#if !defined(HAVE_REALPATH) || defined(BROKEN_REALPATH)
33
34#if defined(LIBC_SCCS) && !defined(lint)
35static char *rcsid = "$OpenBSD: realpath.c,v 1.6 2002/01/12 16:24:35 millert Exp $";
36#endif /* LIBC_SCCS and not lint */
37
38#include <sys/param.h>
39#include <sys/stat.h>
40
41#include <errno.h>
42#include <fcntl.h>
43#include <stdlib.h>
44#include <string.h>
45#include <unistd.h>
46
47/*
48 * MAXSYMLINKS
49 */
50#ifndef MAXSYMLINKS
51#define MAXSYMLINKS 5
52#endif
53
54/*
55 * char *realpath(const char *path, char resolved_path[MAXPATHLEN]);
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 */
61char *
62realpath(const char *path, char *resolved)
63{
64	struct stat sb;
65	int fd, n, rootd, serrno = 0;
66	char *p, *q, wbuf[MAXPATHLEN], start[MAXPATHLEN];
67	int symlinks = 0;
68
69	/* Save the starting point. */
70	getcwd(start,MAXPATHLEN);
71	if ((fd = open(".", O_RDONLY)) < 0) {
72		(void)strcpy(resolved, ".");
73		return (NULL);
74	}
75	close(fd);
76
77	/* Convert "." -> "" to optimize away a needless lstat() and chdir() */
78	if (path[0] == '.' && path[1] == '\0')
79		path = "";
80
81	/*
82	 * Find the dirname and basename from the path to be resolved.
83	 * Change directory to the dirname component.
84	 * lstat the basename part.
85	 *     if it is a symlink, read in the value and loop.
86	 *     if it is a directory, then change to that directory.
87	 * get the current directory name and append the basename.
88	 */
89	strlcpy(resolved, path, MAXPATHLEN);
90loop:
91	q = strrchr(resolved, '/');
92	if (q != NULL) {
93		p = q + 1;
94		if (q == resolved)
95			q = "/";
96		else {
97			do {
98				--q;
99			} while (q > resolved && *q == '/');
100			q[1] = '\0';
101			q = resolved;
102		}
103		if (chdir(q) < 0)
104			goto err1;
105	} else
106		p = resolved;
107
108	/* Deal with the last component. */
109	if (*p != '\0' && lstat(p, &sb) == 0) {
110		if (S_ISLNK(sb.st_mode)) {
111			if (++symlinks > MAXSYMLINKS) {
112				serrno = ELOOP;
113				goto err1;
114			}
115			n = readlink(p, resolved, MAXPATHLEN-1);
116			if (n < 0)
117				goto err1;
118			resolved[n] = '\0';
119			goto loop;
120		}
121		if (S_ISDIR(sb.st_mode)) {
122			if (chdir(p) < 0)
123				goto err1;
124			p = "";
125		}
126	}
127
128	/*
129	 * Save the last component name and get the full pathname of
130	 * the current directory.
131	 */
132	(void)strcpy(wbuf, p);
133	if (getcwd(resolved, MAXPATHLEN) == 0)
134		goto err1;
135
136	/*
137	 * Join the two strings together, ensuring that the right thing
138	 * happens if the last component is empty, or the dirname is root.
139	 */
140	if (resolved[0] == '/' && resolved[1] == '\0')
141		rootd = 1;
142	else
143		rootd = 0;
144
145	if (*wbuf) {
146		if (strlen(resolved) + strlen(wbuf) + rootd + 1 > MAXPATHLEN) {
147			serrno = ENAMETOOLONG;
148			goto err1;
149		}
150		if (rootd == 0)
151			(void)strcat(resolved, "/");
152		(void)strcat(resolved, wbuf);
153	}
154
155	/* Go back to where we came from. */
156	if (chdir(start) < 0) {
157		serrno = errno;
158		goto err2;
159	}
160	return (resolved);
161
162err1:	chdir(start);
163err2:	errno = serrno;
164	return (NULL);
165}
166#endif /* !defined(HAVE_REALPATH) || defined(BROKEN_REALPATH) */
167