x86_64: refresh ULPs
[uclibc-ng.git] / utils / chroot_realpath.c
blobf17a2184f9a3f87ff95cec9c74413311edab8798
1 /*
2 * chroot_realpath.c -- resolve pathname as if inside chroot
3 * Based on realpath.c Copyright (C) 1993 Rick Sladkey <jrs@world.std.com>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; see the file COPYING.LIB. If not,
17 * see <http://www.gnu.org/licenses/>.
19 * 2005/09/12: Dan Howell (modified from realpath.c to emulate chroot)
22 #include "porting.h"
24 #define MAX_READLINKS 32
26 char *chroot_realpath(const char *root, const char *path,
27 char resolved_path[]);
29 char *chroot_realpath(const char *root, const char *path,
30 char resolved_path[])
32 char copy_path[PATH_MAX];
33 char link_path[PATH_MAX];
34 char got_path[PATH_MAX];
35 char *got_path_root = got_path;
36 char *new_path = got_path;
37 char *max_path;
38 int readlinks = 0;
39 int n;
40 int chroot_len;
42 /* Trivial case. */
43 if (root == NULL || *root == '\0' ||
44 (*root == '/' && root[1] == '\0')) {
45 strcpy(resolved_path, path);
46 return resolved_path;
49 chroot_len = strlen(root);
51 if (chroot_len + strlen(path) >= PATH_MAX - 3) {
52 errno = ENAMETOOLONG;
53 return NULL;
56 /* Make a copy of the source path since we may need to modify it. */
57 strcpy(copy_path, path);
58 path = copy_path;
59 max_path = copy_path + PATH_MAX - chroot_len - 3;
61 /* Start with the chroot path. */
62 strcpy(new_path, root);
63 new_path += chroot_len;
64 while (*new_path == '/' && new_path > got_path)
65 new_path--;
66 got_path_root = new_path;
67 *new_path++ = '/';
69 /* Expand each slash-separated pathname component. */
70 while (*path != '\0') {
71 /* Ignore stray "/". */
72 if (*path == '/') {
73 path++;
74 continue;
76 if (*path == '.') {
77 /* Ignore ".". */
78 if (path[1] == '\0' || path[1] == '/') {
79 path++;
80 continue;
82 if (path[1] == '.') {
83 if (path[2] == '\0' || path[2] == '/') {
84 path += 2;
85 /* Ignore ".." at root. */
86 if (new_path == got_path_root + 1)
87 continue;
88 /* Handle ".." by backing up. */
89 while ((--new_path)[-1] != '/') ;
90 continue;
94 /* Safely copy the next pathname component. */
95 while (*path != '\0' && *path != '/') {
96 if (path > max_path) {
97 errno = ENAMETOOLONG;
98 return NULL;
100 *new_path++ = *path++;
102 if (*path == '\0')
103 /* Don't follow symlink for last pathname component. */
104 break;
105 #ifdef S_IFLNK
106 /* Protect against infinite loops. */
107 if (readlinks++ > MAX_READLINKS) {
108 errno = ELOOP;
109 return NULL;
111 /* See if latest pathname component is a symlink. */
112 *new_path = '\0';
113 n = readlink(got_path, link_path, PATH_MAX - 1);
114 if (n < 0) {
115 /* EINVAL means the file exists but isn't a symlink. */
116 if (errno != EINVAL) {
117 /* Make sure it's null terminated. */
118 *new_path = '\0';
119 strcpy(resolved_path, got_path);
120 return NULL;
122 } else {
123 /* Note: readlink doesn't add the null byte. */
124 link_path[n] = '\0';
125 if (*link_path == '/')
126 /* Start over for an absolute symlink. */
127 new_path = got_path_root;
128 else
129 /* Otherwise back up over this component. */
130 while (*(--new_path) != '/') ;
131 /* Safe sex check. */
132 if (strlen(path) + n >= PATH_MAX - 2) {
133 errno = ENAMETOOLONG;
134 return NULL;
136 /* Insert symlink contents into path. */
137 strcat(link_path, path);
138 strcpy(copy_path, link_path);
139 path = copy_path;
141 #endif /* S_IFLNK */
142 *new_path++ = '/';
144 /* Delete trailing slash but don't whomp a lone slash. */
145 if (new_path != got_path + 1 && new_path[-1] == '/')
146 new_path--;
147 /* Make sure it's null terminated. */
148 *new_path = '\0';
149 strcpy(resolved_path, got_path);
150 return resolved_path;