blob: a954b1b7a997343e83050b8a1dda079126f92074 [file] [log] [blame]
Damien Millere929a432015-10-14 08:25:55 +11001/* $OpenBSD: realpath.c,v 1.16 2013/04/05 12:59:54 kurt 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
Damien Millere929a432015-10-14 08:25:55 +110046/* A slightly modified copy of this file exists in libexec/ld.so */
47
Ben Lindstromb4df15d2000-10-15 00:17:36 +000048/*
Darren Tucker73f671a2005-08-10 21:52:36 +100049 * char *realpath(const char *path, char resolved[PATH_MAX]);
Ben Lindstromb4df15d2000-10-15 00:17:36 +000050 *
51 * Find the real name of path, by removing all ".", ".." and symlink
52 * components. Returns (resolved) on success, or (NULL) on failure,
53 * in which case the path which caused trouble is left in (resolved).
54 */
55char *
Damien Miller7365fe52015-10-14 08:25:09 +110056realpath(const char *path, char *resolved)
Ben Lindstromb4df15d2000-10-15 00:17:36 +000057{
58 struct stat sb;
Darren Tucker73f671a2005-08-10 21:52:36 +100059 char *p, *q, *s;
60 size_t left_len, resolved_len;
61 unsigned symlinks;
Damien Miller7365fe52015-10-14 08:25:09 +110062 int serrno, slen, mem_allocated;
Darren Tucker73f671a2005-08-10 21:52:36 +100063 char left[PATH_MAX], next_token[PATH_MAX], symlink[PATH_MAX];
Ben Lindstromb4df15d2000-10-15 00:17:36 +000064
Damien Miller7365fe52015-10-14 08:25:09 +110065 if (path[0] == '\0') {
66 errno = ENOENT;
67 return (NULL);
68 }
69
Darren Tucker73f671a2005-08-10 21:52:36 +100070 serrno = errno;
Damien Miller7365fe52015-10-14 08:25:09 +110071
72 if (resolved == NULL) {
73 resolved = malloc(PATH_MAX);
74 if (resolved == NULL)
75 return (NULL);
76 mem_allocated = 1;
77 } else
78 mem_allocated = 0;
79
Darren Tucker73f671a2005-08-10 21:52:36 +100080 symlinks = 0;
81 if (path[0] == '/') {
82 resolved[0] = '/';
Darren Tuckerad7646a2005-02-02 10:43:59 +110083 resolved[1] = '\0';
Darren Tucker73f671a2005-08-10 21:52:36 +100084 if (path[1] == '\0')
85 return (resolved);
86 resolved_len = 1;
87 left_len = strlcpy(left, path + 1, sizeof(left));
88 } else {
89 if (getcwd(resolved, PATH_MAX) == NULL) {
Damien Miller7365fe52015-10-14 08:25:09 +110090 if (mem_allocated)
91 free(resolved);
92 else
93 strlcpy(resolved, ".", PATH_MAX);
Darren Tucker73f671a2005-08-10 21:52:36 +100094 return (NULL);
Ben Lindstromb4df15d2000-10-15 00:17:36 +000095 }
Darren Tucker73f671a2005-08-10 21:52:36 +100096 resolved_len = strlen(resolved);
97 left_len = strlcpy(left, path, sizeof(left));
Ben Lindstromb4df15d2000-10-15 00:17:36 +000098 }
Darren Tucker73f671a2005-08-10 21:52:36 +100099 if (left_len >= sizeof(left) || resolved_len >= PATH_MAX) {
Darren Tuckerad7646a2005-02-02 10:43:59 +1100100 errno = ENAMETOOLONG;
Damien Miller7365fe52015-10-14 08:25:09 +1100101 goto err;
Darren Tuckerad7646a2005-02-02 10:43:59 +1100102 }
Ben Lindstromb4df15d2000-10-15 00:17:36 +0000103
104 /*
Darren Tucker73f671a2005-08-10 21:52:36 +1000105 * Iterate over path components in `left'.
Ben Lindstromb4df15d2000-10-15 00:17:36 +0000106 */
Darren Tucker73f671a2005-08-10 21:52:36 +1000107 while (left_len != 0) {
108 /*
109 * Extract the next path component and adjust `left'
110 * and its length.
111 */
112 p = strchr(left, '/');
113 s = p ? p : left + left_len;
Damien Millerd56fd182015-07-20 11:19:51 +1000114 if (s - left >= (ptrdiff_t)sizeof(next_token)) {
Darren Tuckerad7646a2005-02-02 10:43:59 +1100115 errno = ENAMETOOLONG;
Damien Miller7365fe52015-10-14 08:25:09 +1100116 goto err;
Ben Lindstromb4df15d2000-10-15 00:17:36 +0000117 }
Darren Tucker73f671a2005-08-10 21:52:36 +1000118 memcpy(next_token, left, s - left);
119 next_token[s - left] = '\0';
120 left_len -= s - left;
121 if (p != NULL)
122 memmove(left, s + 1, left_len + 1);
123 if (resolved[resolved_len - 1] != '/') {
124 if (resolved_len + 1 >= PATH_MAX) {
Darren Tuckerad7646a2005-02-02 10:43:59 +1100125 errno = ENAMETOOLONG;
Damien Miller7365fe52015-10-14 08:25:09 +1100126 goto err;
Darren Tuckerad7646a2005-02-02 10:43:59 +1100127 }
Darren Tucker73f671a2005-08-10 21:52:36 +1000128 resolved[resolved_len++] = '/';
129 resolved[resolved_len] = '\0';
Darren Tuckerad7646a2005-02-02 10:43:59 +1100130 }
Darren Tucker73f671a2005-08-10 21:52:36 +1000131 if (next_token[0] == '\0')
132 continue;
133 else if (strcmp(next_token, ".") == 0)
134 continue;
135 else if (strcmp(next_token, "..") == 0) {
136 /*
137 * Strip the last path component except when we have
138 * single "/"
139 */
140 if (resolved_len > 1) {
141 resolved[resolved_len - 1] = '\0';
142 q = strrchr(resolved, '/') + 1;
143 *q = '\0';
144 resolved_len = q - resolved;
145 }
146 continue;
147 }
148
149 /*
150 * Append the next path component and lstat() it. If
151 * lstat() fails we still can return successfully if
152 * there are no more path components left.
153 */
154 resolved_len = strlcat(resolved, next_token, PATH_MAX);
155 if (resolved_len >= PATH_MAX) {
Darren Tuckerad7646a2005-02-02 10:43:59 +1100156 errno = ENAMETOOLONG;
Damien Miller7365fe52015-10-14 08:25:09 +1100157 goto err;
Darren Tucker73f671a2005-08-10 21:52:36 +1000158 }
159 if (lstat(resolved, &sb) != 0) {
160 if (errno == ENOENT && p == NULL) {
161 errno = serrno;
162 return (resolved);
163 }
Damien Miller7365fe52015-10-14 08:25:09 +1100164 goto err;
Darren Tucker73f671a2005-08-10 21:52:36 +1000165 }
166 if (S_ISLNK(sb.st_mode)) {
167 if (symlinks++ > MAXSYMLINKS) {
168 errno = ELOOP;
Damien Miller7365fe52015-10-14 08:25:09 +1100169 goto err;
Darren Tucker73f671a2005-08-10 21:52:36 +1000170 }
171 slen = readlink(resolved, symlink, sizeof(symlink) - 1);
172 if (slen < 0)
Damien Miller7365fe52015-10-14 08:25:09 +1100173 goto err;
Darren Tucker73f671a2005-08-10 21:52:36 +1000174 symlink[slen] = '\0';
175 if (symlink[0] == '/') {
176 resolved[1] = 0;
177 resolved_len = 1;
178 } else if (resolved_len > 1) {
179 /* Strip the last path component. */
180 resolved[resolved_len - 1] = '\0';
181 q = strrchr(resolved, '/') + 1;
182 *q = '\0';
183 resolved_len = q - resolved;
184 }
185
186 /*
187 * If there are any path components left, then
188 * append them to symlink. The result is placed
189 * in `left'.
190 */
191 if (p != NULL) {
192 if (symlink[slen - 1] != '/') {
Damien Millerd56fd182015-07-20 11:19:51 +1000193 if (slen + 1 >=
194 (ptrdiff_t)sizeof(symlink)) {
Darren Tucker73f671a2005-08-10 21:52:36 +1000195 errno = ENAMETOOLONG;
Damien Miller7365fe52015-10-14 08:25:09 +1100196 goto err;
Darren Tucker73f671a2005-08-10 21:52:36 +1000197 }
198 symlink[slen] = '/';
199 symlink[slen + 1] = 0;
200 }
Damien Miller5225db62015-10-14 08:25:32 +1100201 left_len = strlcat(symlink, left, sizeof(symlink));
Darren Tucker73f671a2005-08-10 21:52:36 +1000202 if (left_len >= sizeof(left)) {
203 errno = ENAMETOOLONG;
Damien Miller7365fe52015-10-14 08:25:09 +1100204 goto err;
Darren Tucker73f671a2005-08-10 21:52:36 +1000205 }
206 }
207 left_len = strlcpy(left, symlink, sizeof(left));
Darren Tuckerad7646a2005-02-02 10:43:59 +1100208 }
Ben Lindstromb4df15d2000-10-15 00:17:36 +0000209 }
210
Darren Tucker73f671a2005-08-10 21:52:36 +1000211 /*
212 * Remove trailing slash except when the resolved pathname
213 * is a single "/".
214 */
215 if (resolved_len > 1 && resolved[resolved_len - 1] == '/')
216 resolved[resolved_len - 1] = '\0';
Ben Lindstromb4df15d2000-10-15 00:17:36 +0000217 return (resolved);
Damien Miller7365fe52015-10-14 08:25:09 +1100218
219err:
220 if (mem_allocated)
221 free(resolved);
222 return (NULL);
Ben Lindstromb4df15d2000-10-15 00:17:36 +0000223}
224#endif /* !defined(HAVE_REALPATH) || defined(BROKEN_REALPATH) */