fix Cwd.xs for VMS
[p5sagit/p5-mst-13.2.git] / ext / Cwd / Cwd.xs
CommitLineData
0d2079fa 1#include "EXTERN.h"
2#include "perl.h"
3#include "XSUB.h"
4
03d70c89 5/* The realpath() implementation from OpenBSD 2.9 (realpath.c 1.4)
6 * Renamed here to bsd_realpath() to avoid library conflicts.
7 * --jhi 2000-06-20 */
8
9/*
10 * Copyright (c) 1994
11 * The Regents of the University of California. All rights reserved.
12 *
13 * This code is derived from software contributed to Berkeley by
14 * Jan-Simon Pendry.
15 *
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
18 * are met:
19 * 1. Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer.
21 * 2. Redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution.
24 * 3. All advertising materials mentioning features or use of this software
25 * must display the following acknowledgement:
26 * This product includes software developed by the University of
27 * California, Berkeley and its contributors.
28 * 4. Neither the name of the University nor the names of its contributors
29 * may be used to endorse or promote products derived from this software
30 * without specific prior written permission.
31 *
32 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
33 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
34 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
35 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
36 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
40 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
41 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
42 * SUCH DAMAGE.
43 */
44
45#if defined(LIBC_SCCS) && !defined(lint)
46static char *rcsid = "$OpenBSD: realpath.c,v 1.4 1998/05/18 09:55:19 deraadt Exp $";
47#endif /* LIBC_SCCS and not lint */
48
49/* OpenBSD system #includes removed since the Perl ones should do. --jhi */
50
51#ifndef MAXSYMLINKS
52#define MAXSYMLINKS 8
53#endif
54
55/*
56 * char *realpath(const char *path, char resolved_path[MAXPATHLEN]);
57 *
58 * Find the real name of path, by removing all ".", ".." and symlink
59 * components. Returns (resolved) on success, or (NULL) on failure,
60 * in which case the path which caused trouble is left in (resolved).
61 */
62static
63char *
64bsd_realpath(path, resolved)
65 const char *path;
66 char *resolved;
67{
ba9ea07e 68#ifndef VMS
03d70c89 69 struct stat sb;
70 int fd, n, rootd, serrno;
71 char *p, *q, wbuf[MAXPATHLEN];
72 int symlinks = 0;
73
74 /* Save the starting point. */
75 if ((fd = open(".", O_RDONLY)) < 0) {
76 (void)strcpy(resolved, ".");
77 return (NULL);
78 }
79
80 /*
81 * Find the dirname and basename from the path to be resolved.
82 * Change directory to the dirname component.
83 * lstat the basename part.
84 * if it is a symlink, read in the value and loop.
85 * if it is a directory, then change to that directory.
86 * get the current directory name and append the basename.
87 */
88 (void)strncpy(resolved, path, MAXPATHLEN - 1);
89 resolved[MAXPATHLEN - 1] = '\0';
90loop:
91 q = strrchr(resolved, '/');
92 if (q != NULL) {
93 p = q + 1;
94 if (q == resolved)
95 q = "/";
96 else {
97 do {
98 --q;
99 } while (q > resolved && *q == '/');
100 q[1] = '\0';
101 q = resolved;
102 }
103 if (chdir(q) < 0)
104 goto err1;
105 } else
106 p = resolved;
107
108 /* Deal with the last component. */
109 if (lstat(p, &sb) == 0) {
110 if (S_ISLNK(sb.st_mode)) {
111 if (++symlinks > MAXSYMLINKS) {
112 errno = ELOOP;
113 goto err1;
114 }
115 n = readlink(p, resolved, MAXPATHLEN-1);
116 if (n < 0)
117 goto err1;
118 resolved[n] = '\0';
119 goto loop;
120 }
121 if (S_ISDIR(sb.st_mode)) {
122 if (chdir(p) < 0)
123 goto err1;
124 p = "";
125 }
126 }
127
128 /*
129 * Save the last component name and get the full pathname of
130 * the current directory.
131 */
132 (void)strcpy(wbuf, p);
133 if (getcwd(resolved, MAXPATHLEN) == 0)
134 goto err1;
135
136 /*
137 * Join the two strings together, ensuring that the right thing
138 * happens if the last component is empty, or the dirname is root.
139 */
140 if (resolved[0] == '/' && resolved[1] == '\0')
141 rootd = 1;
142 else
143 rootd = 0;
144
145 if (*wbuf) {
146 if (strlen(resolved) + strlen(wbuf) + rootd + 1 > MAXPATHLEN) {
147 errno = ENAMETOOLONG;
148 goto err1;
149 }
150 if (rootd == 0)
151 (void)strcat(resolved, "/");
152 (void)strcat(resolved, wbuf);
153 }
154
155 /* Go back to where we came from. */
156 if (fchdir(fd) < 0) {
157 serrno = errno;
158 goto err2;
159 }
160
161 /* It's okay if the close fails, what's an fd more or less? */
162 (void)close(fd);
163 return (resolved);
164
165err1: serrno = errno;
166 (void)fchdir(fd);
167err2: (void)close(fd);
168 errno = serrno;
169 return (NULL);
ba9ea07e 170
171#else /* it's VMS */
172
173 return Perl_rmsexpand((char*)path, resolved, NULL, 0);
174
175#endif /* ifndef VMS */
03d70c89 176}
177
f22d8e4b 178MODULE = Cwd PACKAGE = Cwd
0d2079fa 179
f22d8e4b 180PROTOTYPES: ENABLE
0d2079fa 181
f22d8e4b 182void
183fastcwd()
184PPCODE:
185{
186 dXSTARG;
187 sv_getcwd(TARG);
188 XSprePUSH; PUSHTARG;
0d2079fa 189}
190
f22d8e4b 191void
03d70c89 192abs_path(pathsv=Nullsv)
193 SV *pathsv
f22d8e4b 194PPCODE:
2ae52c40 195{
f22d8e4b 196 dXSTARG;
197 char *path;
198 STRLEN len;
03d70c89 199 char *buf;
2ae52c40 200
03d70c89 201 New(0, buf, MAXPATHLEN, char);
202 if (buf) {
203 buf[MAXPATHLEN] = 0;
204 if (pathsv)
205 path = SvPV(pathsv, len);
206 else {
207 path = ".";
208 len = 1;
209 }
210
211 if (bsd_realpath(path, buf)) {
212 sv_setpvn(TARG, buf, strlen(buf));
213 SvPOK_only(TARG);
214 }
215 else
216 sv_setsv(TARG, &PL_sv_undef);
217
218 Safefree(buf);
2ae52c40 219 }
03d70c89 220 else
221 sv_setsv(TARG, &PL_sv_undef);
2ae52c40 222
f22d8e4b 223 XSprePUSH; PUSHTARG;
2ae52c40 224}