blob: 6646330cab97fb6bae62898c6ff68092e0335103 [file] [log] [blame]
Damien Miller7365fe52015-10-14 08:25:09 +11001/* $OpenBSD: realpath.c,v 1.14 2011/07/24 21:03:00 miod Exp $ */
Ben Lindstromb4df15d2000-10-15 00:17:36 +00002/*
Darren Tucker73f671a2005-08-10 21:52:36 +10003 * Copyright (c) 2003 Constantin S. Svintsoff <kostik@iclub.nsu.ru>
Ben Lindstromb4df15d2000-10-15 00:17:36 +00004 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
Darren Tucker73f671a2005-08-10 21:52:36 +100013 * 3. The names of the authors may not be used to endorse or promote
14 * products derived from this software without specific prior written
15 * permission.
Ben Lindstromb4df15d2000-10-15 00:17:36 +000016 *
Darren Tucker73f671a2005-08-10 21:52:36 +100017 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
Ben Lindstromb4df15d2000-10-15 00:17:36 +000018 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
Darren Tucker73f671a2005-08-10 21:52:36 +100020 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
Ben Lindstromb4df15d2000-10-15 00:17:36 +000021 * 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
Darren Tucker7f24a0e2005-11-10 16:18:56 +110030/* OPENBSD ORIGINAL: lib/libc/stdlib/realpath.c */
31
Ben Lindstromb4df15d2000-10-15 00:17:36 +000032#include "includes.h"
33
34#if !defined(HAVE_REALPATH) || defined(BROKEN_REALPATH)
35
Damien Millerd56fd182015-07-20 11:19:51 +100036#include <sys/types.h>
Ben Lindstromb4df15d2000-10-15 00:17:36 +000037#include <sys/param.h>
38#include <sys/stat.h>
39
40#include <errno.h>
Ben Lindstromb4df15d2000-10-15 00:17:36 +000041#include <stdlib.h>
Damien Millerd56fd182015-07-20 11:19:51 +100042#include <stddef.h>
Ben Lindstromb4df15d2000-10-15 00:17:36 +000043#include <string.h>
44#include <unistd.h>
45
46/*
Darren Tucker73f671a2005-08-10 21:52:36 +100047 * char *realpath(const char *path, char resolved[PATH_MAX]);
Ben Lindstromb4df15d2000-10-15 00:17:36 +000048 *
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 */
53char *
Damien Miller7365fe52015-10-14 08:25:09 +110054realpath(const char *path, char *resolved)
Ben Lindstromb4df15d2000-10-15 00:17:36 +000055{
56 struct stat sb;
Darren Tucker73f671a2005-08-10 21:52:36 +100057 char *p, *q, *s;
58 size_t left_len, resolved_len;
59 unsigned symlinks;
Damien Miller7365fe52015-10-14 08:25:09 +110060 int serrno, slen, mem_allocated;
Darren Tucker73f671a2005-08-10 21:52:36 +100061 char left[PATH_MAX], next_token[PATH_MAX], symlink[PATH_MAX];
Ben Lindstromb4df15d2000-10-15 00:17:36 +000062
Damien Miller7365fe52015-10-14 08:25:09 +110063 if (path[0] == '\0') {
64 errno = ENOENT;
65 return (NULL);
66 }
67
Darren Tucker73f671a2005-08-10 21:52:36 +100068 serrno = errno;
Damien Miller7365fe52015-10-14 08:25:09 +110069
70 if (resolved == NULL) {
71 resolved = malloc(PATH_MAX);
72 if (resolved == NULL)
73 return (NULL);
74 mem_allocated = 1;
75 } else
76 mem_allocated = 0;
77
Darren Tucker73f671a2005-08-10 21:52:36 +100078 symlinks = 0;
79 if (path[0] == '/') {
80 resolved[0] = '/';
Darren Tuckerad7646a2005-02-02 10:43:59 +110081 resolved[1] = '\0';
Darren Tucker73f671a2005-08-10 21:52:36 +100082 if (path[1] == '\0')
83 return (resolved);
84 resolved_len = 1;
85 left_len = strlcpy(left, path + 1, sizeof(left));
86 } else {
87 if (getcwd(resolved, PATH_MAX) == NULL) {
Damien Miller7365fe52015-10-14 08:25:09 +110088 if (mem_allocated)
89 free(resolved);
90 else
91 strlcpy(resolved, ".", PATH_MAX);
Darren Tucker73f671a2005-08-10 21:52:36 +100092 return (NULL);
Ben Lindstromb4df15d2000-10-15 00:17:36 +000093 }
Darren Tucker73f671a2005-08-10 21:52:36 +100094 resolved_len = strlen(resolved);
95 left_len = strlcpy(left, path, sizeof(left));
Ben Lindstromb4df15d2000-10-15 00:17:36 +000096 }
Darren Tucker73f671a2005-08-10 21:52:36 +100097 if (left_len >= sizeof(left) || resolved_len >= PATH_MAX) {
Darren Tuckerad7646a2005-02-02 10:43:59 +110098 errno = ENAMETOOLONG;
Damien Miller7365fe52015-10-14 08:25:09 +110099 goto err;
Darren Tuckerad7646a2005-02-02 10:43:59 +1100100 }
Ben Lindstromb4df15d2000-10-15 00:17:36 +0000101
102 /*
Darren Tucker73f671a2005-08-10 21:52:36 +1000103 * Iterate over path components in `left'.
Ben Lindstromb4df15d2000-10-15 00:17:36 +0000104 */
Darren Tucker73f671a2005-08-10 21:52:36 +1000105 while (left_len != 0) {
106 /*
107 * Extract the next path component and adjust `left'
108 * and its length.
109 */
110 p = strchr(left, '/');
111 s = p ? p : left + left_len;
Damien Millerd56fd182015-07-20 11:19:51 +1000112 if (s - left >= (ptrdiff_t)sizeof(next_token)) {
Darren Tuckerad7646a2005-02-02 10:43:59 +1100113 errno = ENAMETOOLONG;
Damien Miller7365fe52015-10-14 08:25:09 +1100114 goto err;
Ben Lindstromb4df15d2000-10-15 00:17:36 +0000115 }
Darren Tucker73f671a2005-08-10 21:52:36 +1000116 memcpy(next_token, left, s - left);
117 next_token[s - left] = '\0';
118 left_len -= s - left;
119 if (p != NULL)
120 memmove(left, s + 1, left_len + 1);
121 if (resolved[resolved_len - 1] != '/') {
122 if (resolved_len + 1 >= PATH_MAX) {
Darren Tuckerad7646a2005-02-02 10:43:59 +1100123 errno = ENAMETOOLONG;
Damien Miller7365fe52015-10-14 08:25:09 +1100124 goto err;
Darren Tuckerad7646a2005-02-02 10:43:59 +1100125 }
Darren Tucker73f671a2005-08-10 21:52:36 +1000126 resolved[resolved_len++] = '/';
127 resolved[resolved_len] = '\0';
Darren Tuckerad7646a2005-02-02 10:43:59 +1100128 }
Darren Tucker73f671a2005-08-10 21:52:36 +1000129 if (next_token[0] == '\0')
130 continue;
131 else if (strcmp(next_token, ".") == 0)
132 continue;
133 else if (strcmp(next_token, "..") == 0) {
134 /*
135 * Strip the last path component except when we have
136 * single "/"
137 */
138 if (resolved_len > 1) {
139 resolved[resolved_len - 1] = '\0';
140 q = strrchr(resolved, '/') + 1;
141 *q = '\0';
142 resolved_len = q - resolved;
143 }
144 continue;
145 }
146
147 /*
148 * Append the next path component and lstat() it. If
149 * lstat() fails we still can return successfully if
150 * there are no more path components left.
151 */
152 resolved_len = strlcat(resolved, next_token, PATH_MAX);
153 if (resolved_len >= PATH_MAX) {
Darren Tuckerad7646a2005-02-02 10:43:59 +1100154 errno = ENAMETOOLONG;
Damien Miller7365fe52015-10-14 08:25:09 +1100155 goto err;
Darren Tucker73f671a2005-08-10 21:52:36 +1000156 }
157 if (lstat(resolved, &sb) != 0) {
158 if (errno == ENOENT && p == NULL) {
159 errno = serrno;
160 return (resolved);
161 }
Damien Miller7365fe52015-10-14 08:25:09 +1100162 goto err;
Darren Tucker73f671a2005-08-10 21:52:36 +1000163 }
164 if (S_ISLNK(sb.st_mode)) {
165 if (symlinks++ > MAXSYMLINKS) {
166 errno = ELOOP;
Damien Miller7365fe52015-10-14 08:25:09 +1100167 goto err;
Darren Tucker73f671a2005-08-10 21:52:36 +1000168 }
169 slen = readlink(resolved, symlink, sizeof(symlink) - 1);
170 if (slen < 0)
Damien Miller7365fe52015-10-14 08:25:09 +1100171 goto err;
Darren Tucker73f671a2005-08-10 21:52:36 +1000172 symlink[slen] = '\0';
173 if (symlink[0] == '/') {
174 resolved[1] = 0;
175 resolved_len = 1;
176 } else if (resolved_len > 1) {
177 /* Strip the last path component. */
178 resolved[resolved_len - 1] = '\0';
179 q = strrchr(resolved, '/') + 1;
180 *q = '\0';
181 resolved_len = q - resolved;
182 }
183
184 /*
185 * If there are any path components left, then
186 * append them to symlink. The result is placed
187 * in `left'.
188 */
189 if (p != NULL) {
190 if (symlink[slen - 1] != '/') {
Damien Millerd56fd182015-07-20 11:19:51 +1000191 if (slen + 1 >=
192 (ptrdiff_t)sizeof(symlink)) {
Darren Tucker73f671a2005-08-10 21:52:36 +1000193 errno = ENAMETOOLONG;
Damien Miller7365fe52015-10-14 08:25:09 +1100194 goto err;
Darren Tucker73f671a2005-08-10 21:52:36 +1000195 }
196 symlink[slen] = '/';
197 symlink[slen + 1] = 0;
198 }
199 left_len = strlcat(symlink, left, sizeof(left));
200 if (left_len >= sizeof(left)) {
201 errno = ENAMETOOLONG;
Damien Miller7365fe52015-10-14 08:25:09 +1100202 goto err;
Darren Tucker73f671a2005-08-10 21:52:36 +1000203 }
204 }
205 left_len = strlcpy(left, symlink, sizeof(left));
Darren Tuckerad7646a2005-02-02 10:43:59 +1100206 }
Ben Lindstromb4df15d2000-10-15 00:17:36 +0000207 }
208
Darren Tucker73f671a2005-08-10 21:52:36 +1000209 /*
210 * Remove trailing slash except when the resolved pathname
211 * is a single "/".
212 */
213 if (resolved_len > 1 && resolved[resolved_len - 1] == '/')
214 resolved[resolved_len - 1] = '\0';
Ben Lindstromb4df15d2000-10-15 00:17:36 +0000215 return (resolved);
Damien Miller7365fe52015-10-14 08:25:09 +1100216
217err:
218 if (mem_allocated)
219 free(resolved);
220 return (NULL);
Ben Lindstromb4df15d2000-10-15 00:17:36 +0000221}
222#endif /* !defined(HAVE_REALPATH) || defined(BROKEN_REALPATH) */