Sync usage with man page.
[netbsd-mini2440.git] / gnu / dist / gettext / gettext-tools / lib / canonicalize.c
blobfe0cb6d82c3137f2d1db2f110462e482b507cebe
1 /* Return the canonical absolute name of a given file.
2 Copyright (C) 1996-2003 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
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. */
20 #if HAVE_CONFIG_H
21 # include <config.h>
22 #endif
24 /* Avoid a clash of our rpl_realpath() function with the prototype in
25 <stdlib.h> on Solaris 2.5.1. */
26 #undef realpath
28 #if !HAVE_CANONICALIZE_FILE_NAME || defined _LIBC
30 #include <alloca.h>
32 /* Specification. */
33 #include "canonicalize.h"
35 #include <stddef.h>
36 #include <stdlib.h>
37 #include <string.h>
39 #if HAVE_UNISTD_H || defined _LIBC
40 # include <unistd.h>
41 #endif
43 #include <limits.h>
45 #if HAVE_SYS_PARAM_H || defined _LIBC
46 # include <sys/param.h>
47 #endif
48 #ifndef MAXSYMLINKS
49 # define MAXSYMLINKS 20
50 #endif
52 #include <sys/stat.h>
54 #include <errno.h>
55 #ifndef _LIBC
56 # define __set_errno(e) errno = (e)
57 # ifndef ENAMETOOLONG
58 # define ENAMETOOLONG EINVAL
59 # endif
60 #endif
62 #ifdef _LIBC
63 # include <shlib-compat.h>
64 #else
65 # define SHLIB_COMPAT(lib, introduced, obsoleted) 0
66 # define versioned_symbol(lib, local, symbol, version)
67 # define compat_symbol(lib, local, symbol, version)
68 # define weak_alias(local, symbol)
69 # define __canonicalize_file_name canonicalize_file_name
70 # define __realpath rpl_realpath
71 # include "pathmax.h"
72 # include "allocsa.h"
73 # if HAVE_GETCWD
74 # ifdef VMS
75 /* We want the directory in Unix syntax, not in VMS syntax. */
76 # define __getcwd(buf, max) getcwd (buf, max, 0)
77 # else
78 # define __getcwd getcwd
79 # endif
80 # else
81 # define __getcwd(buf, max) getwd (buf)
82 # endif
83 # define __readlink readlink
84 /* On systems without symbolic links, call stat() instead of lstat(). */
85 # if !defined S_ISNLK && !HAVE_READLINK
86 # define lstat stat
87 # endif
88 #endif
90 /* Return the canonical absolute name of file NAME. A canonical name
91 does not contain any `.', `..' components nor any repeated path
92 separators ('/') or symlinks. All path components must exist. If
93 RESOLVED is null, the result is malloc'd; otherwise, if the
94 canonical name is PATH_MAX chars or more, returns null with `errno'
95 set to ENAMETOOLONG; if the name fits in fewer than PATH_MAX chars,
96 returns the name in RESOLVED. If the name cannot be resolved and
97 RESOLVED is non-NULL, it contains the path of the first component
98 that cannot be resolved. If the path can be resolved, RESOLVED
99 holds the same value as the value returned. */
101 char *
102 __realpath (const char *name, char *resolved)
104 char *rpath, *dest, *extra_buf = NULL;
105 const char *start, *end, *rpath_limit;
106 long int path_max;
107 int num_links = 0;
109 if (name == NULL)
111 /* As per Single Unix Specification V2 we must return an error if
112 either parameter is a null pointer. We extend this to allow
113 the RESOLVED parameter to be NULL in case the we are expected to
114 allocate the room for the return value. */
115 __set_errno (EINVAL);
116 return NULL;
119 if (name[0] == '\0')
121 /* As per Single Unix Specification V2 we must return an error if
122 the name argument points to an empty string. */
123 __set_errno (ENOENT);
124 return NULL;
127 #ifdef PATH_MAX
128 path_max = PATH_MAX;
129 #else
130 path_max = pathconf (name, _PC_PATH_MAX);
131 if (path_max <= 0)
132 path_max = 1024;
133 #endif
135 if (resolved == NULL)
137 rpath = malloc (path_max);
138 if (rpath == NULL)
139 return NULL;
141 else
142 rpath = resolved;
143 rpath_limit = rpath + path_max;
145 if (name[0] != '/')
147 if (!__getcwd (rpath, path_max))
149 rpath[0] = '\0';
150 goto error;
152 dest = strchr (rpath, '\0');
154 else
156 rpath[0] = '/';
157 dest = rpath + 1;
160 for (start = end = name; *start; start = end)
162 #ifdef _LIBC
163 struct stat64 st;
164 #else
165 struct stat st;
166 #endif
167 int n;
169 /* Skip sequence of multiple path-separators. */
170 while (*start == '/')
171 ++start;
173 /* Find end of path component. */
174 for (end = start; *end && *end != '/'; ++end)
175 /* Nothing. */;
177 if (end - start == 0)
178 break;
179 else if (end - start == 1 && start[0] == '.')
180 /* nothing */;
181 else if (end - start == 2 && start[0] == '.' && start[1] == '.')
183 /* Back up to previous component, ignore if at root already. */
184 if (dest > rpath + 1)
185 while ((--dest)[-1] != '/');
187 else
189 size_t new_size;
191 if (dest[-1] != '/')
192 *dest++ = '/';
194 if (dest + (end - start) >= rpath_limit)
196 ptrdiff_t dest_offset = dest - rpath;
197 char *new_rpath;
199 if (resolved)
201 __set_errno (ENAMETOOLONG);
202 if (dest > rpath + 1)
203 dest--;
204 *dest = '\0';
205 goto error;
207 new_size = rpath_limit - rpath;
208 if (end - start + 1 > path_max)
209 new_size += end - start + 1;
210 else
211 new_size += path_max;
212 new_rpath = (char *) realloc (rpath, new_size);
213 if (new_rpath == NULL)
214 goto error;
215 rpath = new_rpath;
216 rpath_limit = rpath + new_size;
218 dest = rpath + dest_offset;
221 #ifdef _LIBC
222 dest = __mempcpy (dest, start, end - start);
223 #else
224 memcpy (dest, start, end - start);
225 dest += end - start;
226 #endif
227 *dest = '\0';
229 #ifdef _LIBC
230 if (__lxstat64 (_STAT_VER, rpath, &st) < 0)
231 #else
232 if (lstat (rpath, &st) < 0)
233 #endif
234 goto error;
236 #ifdef S_ISLNK
237 if (S_ISLNK (st.st_mode))
239 char *buf;
240 size_t len;
242 if (++num_links > MAXSYMLINKS)
244 __set_errno (ELOOP);
245 goto error;
248 buf = allocsa (path_max);
249 if (!buf)
251 errno = ENOMEM;
252 goto error;
255 n = __readlink (rpath, buf, path_max);
256 if (n < 0)
258 int saved_errno = errno;
259 freesa (buf);
260 errno = saved_errno;
261 goto error;
263 buf[n] = '\0';
265 if (!extra_buf)
267 extra_buf = allocsa (path_max);
268 if (!extra_buf)
270 freesa (buf);
271 errno = ENOMEM;
272 goto error;
276 len = strlen (end);
277 if ((long int) (n + len) >= path_max)
279 freesa (buf);
280 __set_errno (ENAMETOOLONG);
281 goto error;
284 /* Careful here, end may be a pointer into extra_buf... */
285 memmove (&extra_buf[n], end, len + 1);
286 name = end = memcpy (extra_buf, buf, n);
288 if (buf[0] == '/')
289 dest = rpath + 1; /* It's an absolute symlink */
290 else
291 /* Back up to previous component, ignore if at root already: */
292 if (dest > rpath + 1)
293 while ((--dest)[-1] != '/');
295 #endif
298 if (dest > rpath + 1 && dest[-1] == '/')
299 --dest;
300 *dest = '\0';
302 if (extra_buf)
303 freesa (extra_buf);
305 return resolved ? memcpy (resolved, rpath, dest - rpath + 1) : rpath;
307 error:
309 int saved_errno = errno;
310 if (extra_buf)
311 freesa (extra_buf);
312 if (resolved)
313 strcpy (resolved, rpath);
314 else
315 free (rpath);
316 errno = saved_errno;
318 return NULL;
320 #ifdef _LIBC
321 versioned_symbol (libc, __realpath, realpath, GLIBC_2_3);
322 #endif
325 #if SHLIB_COMPAT(libc, GLIBC_2_0, GLIBC_2_3)
326 char *
327 __old_realpath (const char *name, char *resolved)
329 if (resolved == NULL)
331 __set_errno (EINVAL);
332 return NULL;
335 return __realpath (name, resolved);
337 compat_symbol (libc, __old_realpath, realpath, GLIBC_2_0);
338 #endif
341 char *
342 __canonicalize_file_name (const char *name)
344 return __realpath (name, NULL);
346 weak_alias (__canonicalize_file_name, canonicalize_file_name)
348 #else
350 /* This declaration is solely to ensure that after preprocessing
351 this file is never empty. */
352 typedef int dummy;
354 #endif