/[path-sandbox]/trunk/libsandbox/canonicalize.c
Gentoo

Contents of /trunk/libsandbox/canonicalize.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 315 - (show annotations) (download) (as text)
Sun Jul 9 20:38:41 2006 UTC (12 years, 5 months ago) by azarah
File MIME type: text/x-csrc
File size: 4858 byte(s)
Start moving libsandbox only related stuff out of libsbutil.

1 /* Return the canonical absolute name of a given file.
2 Copyright (C) 1996-2001, 2002 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
5 The GNU C Library 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.
9
10 The GNU C 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.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
19
20 /*
21 * $Header$
22 */
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <unistd.h>
28 #include <limits.h>
29 #include <sys/param.h>
30 #include <sys/stat.h>
31 #include <errno.h>
32 #include <stddef.h>
33
34 #include "config.h"
35 #include "localdecls.h"
36
37 #include "sbutil.h"
38 #include "libsandbox.h"
39
40 #ifndef __set_errno
41 # define __set_errno(val) errno = (val)
42 #endif
43
44 /* Return the canonical absolute name of file NAME. A canonical name
45 does not contain any `.', `..' components nor any repeated path
46 separators ('/') or symlinks. All path components must exist. If
47 RESOLVED is null, the result is malloc'd; otherwise, if the
48 canonical name is SB_PATH_MAX chars or more, returns null with `errno'
49 set to ENAMETOOLONG; if the name fits in fewer than SB_PATH_MAX chars,
50 returns the name in RESOLVED. If the name cannot be resolved and
51 RESOLVED is non-NULL, it contains the path of the first component
52 that cannot be resolved. If the path can be resolved, RESOLVED
53 holds the same value as the value returned. */
54
55 /* Modified: 19 Aug 2002; Martin Schlemmer <azarah@gentoo.org>
56 *
57 * Cleaned up unneeded stuff, and change so that it will not
58 * resolve symlinks. Also prepended a 'e' to functions that
59 * I did not rip out.
60 *
61 */
62
63 char *
64 erealpath(const char *name, char *resolved)
65 {
66 char *rpath, *dest;
67 const char *start, *end, *rpath_limit;
68 long int path_max;
69
70 if (name == NULL) {
71 /* As per Single Unix Specification V2 we must return an error if
72 either parameter is a null pointer. We extend this to allow
73 the RESOLVED parameter to be NULL in case the we are expected to
74 allocate the room for the return value. */
75 __set_errno(EINVAL);
76 return NULL;
77 }
78
79 if (name[0] == '\0') {
80 /* As per Single Unix Specification V2 we must return an error if
81 the name argument points to an empty string. */
82 __set_errno(ENOENT);
83 return NULL;
84 }
85 #ifdef SB_PATH_MAX
86 path_max = SB_PATH_MAX;
87 #else
88 path_max = pathconf(name, _PC_PATH_MAX);
89 if (path_max <= 0)
90 path_max = 1024;
91 #endif
92
93 if (resolved == NULL) {
94 rpath = malloc(path_max);
95 if (rpath == NULL)
96 return NULL;
97 } else
98 rpath = resolved;
99 rpath_limit = rpath + path_max;
100
101 if (name[0] != '/') {
102 if (!egetcwd(rpath, path_max)) {
103 rpath[0] = '\0';
104 goto error;
105 }
106 dest = strchr(rpath, '\0');
107 } else {
108 rpath[0] = '/';
109 dest = rpath + 1;
110 }
111
112 for (start = end = name; *start; start = end) {
113 /* Skip sequence of multiple path-separators. */
114 while (*start == '/')
115 ++start;
116
117 /* Find end of path component. */
118 for (end = start; *end && *end != '/'; ++end)
119 /* Nothing. */ ;
120
121 if (end - start == 0)
122 break;
123 else if (end - start == 1 && start[0] == '.')
124 /* nothing */ ;
125 else if (end - start == 2 && start[0] == '.' && start[1] == '.') {
126 /* Back up to previous component, ignore if at root already. */
127 if (dest > rpath + 1)
128 while ((--dest)[-1] != '/') ;
129 } else {
130 size_t new_size;
131
132 if (dest[-1] != '/')
133 *dest++ = '/';
134
135 if (dest + (end - start) >= rpath_limit) {
136 ptrdiff_t dest_offset = dest - rpath;
137 char *new_rpath;
138
139 if (resolved) {
140 __set_errno(ENAMETOOLONG);
141 if (dest > rpath + 1)
142 dest--;
143 *dest = '\0';
144 goto error;
145 }
146 new_size = rpath_limit - rpath;
147 if (end - start + 1 > path_max)
148 new_size += end - start + 1;
149 else
150 new_size += path_max;
151 new_rpath = (char *) realloc(rpath, new_size);
152 if (new_rpath == NULL)
153 goto error;
154 rpath = new_rpath;
155 rpath_limit = rpath + new_size;
156
157 dest = rpath + dest_offset;
158 }
159
160 memcpy(dest, start, end - start);
161 dest += end - start;
162 *dest = '\0';
163 }
164 }
165 #if 1
166 if (dest > rpath + 1 && dest[-1] == '/')
167 --dest;
168 #endif
169 *dest = '\0';
170
171 return resolved ? rpath : memcpy(resolved, rpath, dest - rpath + 1);
172
173 error:
174 if (resolved)
175 snprintf(resolved, path_max, "%s", rpath);
176 else
177 free(rpath);
178 return NULL;
179 }
180
181 // vim:noexpandtab noai:cindent ai

Properties

Name Value
svn:eol-style native
svn:keywords Author Date Id Revision

  ViewVC Help
Powered by ViewVC 1.1.20