xref: /dragonfly/contrib/bmake/realpath.c (revision 3170ffd7)
1 /* $Id: realpath.c,v 1.2 2010/04/21 17:47:49 sjg Exp $ */
2 /* from: $NetBSD: getcwd.c,v 1.45 2007/10/26 19:48:14 christos Exp $	*/
3 
4 /*
5  * Copyright (c) 1989, 1991, 1993, 1995
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * This code is derived from software contributed to Berkeley by
9  * Jan-Simon Pendry.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 #ifdef HAVE_CONFIG_H
36 # include <config.h>
37 #endif
38 #ifndef HAVE_REALPATH
39 
40 #include <sys/cdefs.h>
41 #include <sys/param.h>
42 #include <sys/stat.h>
43 
44 #include <errno.h>
45 #ifdef HAVE_STDLIB_H
46 # include <stdlib.h>
47 #endif
48 #ifdef HAVE_STRING_H
49 # include <string.h>
50 #endif
51 #ifdef HAVE_UNISTD_H
52 # include <unistd.h>
53 #endif
54 
55 /*
56  * char *realpath(const char *path, char resolved[MAXPATHLEN]);
57  *
58  * Find the real name of path, by removing all ".", ".." and symlink
59  * components.  Returns (resolved) on success, or (NULL) on failure,
60  * in which case the path which caused trouble is left in (resolved).
61  */
62 char *
63 realpath(const char *path, char *resolved)
64 {
65 	struct stat sb;
66 	int idx = 0, n, nlnk = 0;
67 	const char *q;
68 	char *p, wbuf[2][MAXPATHLEN];
69 	size_t len;
70 
71 	if (!path || !resolved || path == resolved)
72 		return (NULL);
73 
74 	/*
75 	 * Build real path one by one with paying an attention to .,
76 	 * .. and symbolic link.
77 	 */
78 
79 	/*
80 	 * `p' is where we'll put a new component with prepending
81 	 * a delimiter.
82 	 */
83 	p = resolved;
84 
85 	if (*path == 0) {
86 		*p = 0;
87 		errno = ENOENT;
88 		return (NULL);
89 	}
90 
91 	/* If relative path, start from current working directory. */
92 	if (*path != '/') {
93 		/* check for resolved pointer to appease coverity */
94 		if (resolved && getcwd(resolved, MAXPATHLEN) == NULL) {
95 			p[0] = '.';
96 			p[1] = 0;
97 			return (NULL);
98 		}
99 		len = strlen(resolved);
100 		if (len > 1)
101 			p += len;
102 	}
103 
104 loop:
105 	/* Skip any slash. */
106 	while (*path == '/')
107 		path++;
108 
109 	if (*path == 0) {
110 		if (p == resolved)
111 			*p++ = '/';
112 		*p = 0;
113 		return (resolved);
114 	}
115 
116 	/* Find the end of this component. */
117 	q = path;
118 	do
119 		q++;
120 	while (*q != '/' && *q != 0);
121 
122 	/* Test . or .. */
123 	if (path[0] == '.') {
124 		if (q - path == 1) {
125 			path = q;
126 			goto loop;
127 		}
128 		if (path[1] == '.' && q - path == 2) {
129 			/* Trim the last component. */
130 			if (p != resolved)
131 				while (*--p != '/')
132 					;
133 			path = q;
134 			goto loop;
135 		}
136 	}
137 
138 	/* Append this component. */
139 	if (p - resolved + 1 + q - path + 1 > MAXPATHLEN) {
140 		errno = ENAMETOOLONG;
141 		if (p == resolved)
142 			*p++ = '/';
143 		*p = 0;
144 		return (NULL);
145 	}
146 	p[0] = '/';
147 	memcpy(&p[1], path,
148 	    /* LINTED We know q > path. */
149 	    q - path);
150 	p[1 + q - path] = 0;
151 
152 	/*
153 	 * If this component is a symlink, toss it and prepend link
154 	 * target to unresolved path.
155 	 */
156 	if (lstat(resolved, &sb) == -1) {
157 		return (NULL);
158 	}
159 	if (S_ISLNK(sb.st_mode)) {
160 		if (nlnk++ >= MAXSYMLINKS) {
161 			errno = ELOOP;
162 			return (NULL);
163 		}
164 		n = readlink(resolved, wbuf[idx], sizeof(wbuf[0]) - 1);
165 		if (n < 0)
166 			return (NULL);
167 		if (n == 0) {
168 			errno = ENOENT;
169 			return (NULL);
170 		}
171 
172 		/* Append unresolved path to link target and switch to it. */
173 		if (n + (len = strlen(q)) + 1 > sizeof(wbuf[0])) {
174 			errno = ENAMETOOLONG;
175 			return (NULL);
176 		}
177 		memcpy(&wbuf[idx][n], q, len + 1);
178 		path = wbuf[idx];
179 		idx ^= 1;
180 
181 		/* If absolute symlink, start from root. */
182 		if (*path == '/')
183 			p = resolved;
184 		goto loop;
185 	}
186 	if (*q == '/' && !S_ISDIR(sb.st_mode)) {
187 		errno = ENOTDIR;
188 		return (NULL);
189 	}
190 
191 	/* Advance both resolved and unresolved path. */
192 	p += 1 + q - path;
193 	path = q;
194 	goto loop;
195 }
196 #endif
197