9 /* The realpath() implementation from OpenBSD 2.9 (realpath.c 1.4)
10 * Renamed here to bsd_realpath() to avoid library conflicts.
15 * The Regents of the University of California. All rights reserved.
17 * This code is derived from software contributed to Berkeley by
20 * Redistribution and use in source and binary forms, with or without
21 * modification, are permitted provided that the following conditions
23 * 1. Redistributions of source code must retain the above copyright
24 * notice, this list of conditions and the following disclaimer.
25 * 2. Redistributions in binary form must reproduce the above copyright
26 * notice, this list of conditions and the following disclaimer in the
27 * documentation and/or other materials provided with the distribution.
28 * 3. All advertising materials mentioning features or use of this software
29 * must display the following acknowledgement:
30 * This product includes software developed by the University of
31 * California, Berkeley and its contributors.
32 * 4. Neither the name of the University nor the names of its contributors
33 * may be used to endorse or promote products derived from this software
34 * without specific prior written permission.
36 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
37 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
39 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
40 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
41 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
42 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
43 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
44 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
45 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
49 #if defined(LIBC_SCCS) && !defined(lint)
50 static char *rcsid = "$OpenBSD: realpath.c,v 1.4 1998/05/18 09:55:19 deraadt Exp $";
51 #endif /* LIBC_SCCS and not lint */
53 /* OpenBSD system #includes removed since the Perl ones should do. --jhi */
60 * char *realpath(const char *path, char resolved_path[MAXPATHLEN]);
62 * Find the real name of path, by removing all ".", ".." and symlink
63 * components. Returns (resolved) on success, or (NULL) on failure,
64 * in which case the path which caused trouble is left in (resolved).
68 bsd_realpath(path, resolved)
74 return Perl_rmsexpand(aTHX_ (char*)path, resolved, NULL, 0);
78 char *p, *q, wbuf[MAXPATHLEN];
81 /* Save the starting point. */
85 if ((fd = open(".", O_RDONLY)) < 0) {
86 (void)strcpy(resolved, ".");
92 if (getcwd(wd, MAXPATHLEN - 1) == NULL) {
93 (void)strcpy(resolved, ".");
99 * Find the dirname and basename from the path to be resolved.
100 * Change directory to the dirname component.
101 * lstat the basename part.
102 * if it is a symlink, read in the value and loop.
103 * if it is a directory, then change to that directory.
104 * get the current directory name and append the basename.
106 (void)strncpy(resolved, path, MAXPATHLEN - 1);
107 resolved[MAXPATHLEN - 1] = '\0';
109 q = strrchr(resolved, '/');
117 } while (q > resolved && *q == '/');
126 #if defined(HAS_LSTAT) && defined(HAS_READLINK) && defined(HAS_SYMLINK)
127 /* Deal with the last component. */
128 if (lstat(p, &sb) == 0) {
129 if (S_ISLNK(sb.st_mode)) {
130 if (++symlinks > MAXSYMLINKS) {
134 n = readlink(p, resolved, MAXPATHLEN-1);
140 if (S_ISDIR(sb.st_mode)) {
149 * Save the last component name and get the full pathname of
150 * the current directory.
152 (void)strcpy(wbuf, p);
153 if (getcwd(resolved, MAXPATHLEN) == 0)
157 * Join the two strings together, ensuring that the right thing
158 * happens if the last component is empty, or the dirname is root.
160 if (resolved[0] == '/' && resolved[1] == '\0')
166 if (strlen(resolved) + strlen(wbuf) + rootd + 1 > MAXPATHLEN) {
167 errno = ENAMETOOLONG;
171 (void)strcat(resolved, "/");
172 (void)strcat(resolved, wbuf);
175 /* Go back to where we came from. */
177 if (fchdir(fd) < 0) {
188 /* It's okay if the close fails, what's an fd more or less? */
194 err1: serrno = errno;
210 MODULE = Cwd PACKAGE = Cwd
224 abs_path(pathsv=Nullsv)
230 char buf[MAXPATHLEN];
232 path = pathsv ? SvPV_nolen(pathsv) : ".";
234 if (bsd_realpath(path, buf)) {
235 sv_setpvn(TARG, buf, strlen(buf));
240 sv_setsv(TARG, &PL_sv_undef);
243 #ifndef INCOMPLETE_TAINTS