Commit | Line | Data |
0d2079fa |
1 | #include "EXTERN.h" |
2 | #include "perl.h" |
3 | #include "XSUB.h" |
4 | |
c70498c1 |
5 | #ifdef I_UNISTD |
6 | # include <unistd.h> |
7 | #endif |
8 | |
03d70c89 |
9 | /* The realpath() implementation from OpenBSD 2.9 (realpath.c 1.4) |
10 | * Renamed here to bsd_realpath() to avoid library conflicts. |
11 | * --jhi 2000-06-20 */ |
12 | |
13 | /* |
14 | * Copyright (c) 1994 |
15 | * The Regents of the University of California. All rights reserved. |
16 | * |
17 | * This code is derived from software contributed to Berkeley by |
18 | * Jan-Simon Pendry. |
19 | * |
20 | * Redistribution and use in source and binary forms, with or without |
21 | * modification, are permitted provided that the following conditions |
22 | * are met: |
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. |
35 | * |
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 |
46 | * SUCH DAMAGE. |
47 | */ |
48 | |
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 */ |
52 | |
53 | /* OpenBSD system #includes removed since the Perl ones should do. --jhi */ |
54 | |
55 | #ifndef MAXSYMLINKS |
56 | #define MAXSYMLINKS 8 |
57 | #endif |
58 | |
59 | /* |
60 | * char *realpath(const char *path, char resolved_path[MAXPATHLEN]); |
61 | * |
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). |
65 | */ |
66 | static |
67 | char * |
68 | bsd_realpath(path, resolved) |
69 | const char *path; |
70 | char *resolved; |
71 | { |
e3d944f4 |
72 | #ifdef VMS |
73 | return Perl_rmsexpand((char*)path, resolved, NULL, 0); |
74 | #else |
03d70c89 |
75 | struct stat sb; |
76 | int fd, n, rootd, serrno; |
77 | char *p, *q, wbuf[MAXPATHLEN]; |
78 | int symlinks = 0; |
79 | |
80 | /* Save the starting point. */ |
e3d944f4 |
81 | #ifdef HAS_FCHDIR |
03d70c89 |
82 | if ((fd = open(".", O_RDONLY)) < 0) { |
83 | (void)strcpy(resolved, "."); |
84 | return (NULL); |
85 | } |
e3d944f4 |
86 | #else |
87 | char wd[MAXPATHLEN]; |
88 | |
89 | if (getcwd(wd, MAXPATHLEN - 1) == NULL) { |
90 | (void)strcpy(resolved, "."); |
91 | return (NULL); |
92 | } |
93 | #endif |
03d70c89 |
94 | |
95 | /* |
96 | * Find the dirname and basename from the path to be resolved. |
97 | * Change directory to the dirname component. |
98 | * lstat the basename part. |
99 | * if it is a symlink, read in the value and loop. |
100 | * if it is a directory, then change to that directory. |
101 | * get the current directory name and append the basename. |
102 | */ |
103 | (void)strncpy(resolved, path, MAXPATHLEN - 1); |
104 | resolved[MAXPATHLEN - 1] = '\0'; |
105 | loop: |
106 | q = strrchr(resolved, '/'); |
107 | if (q != NULL) { |
108 | p = q + 1; |
109 | if (q == resolved) |
110 | q = "/"; |
111 | else { |
112 | do { |
113 | --q; |
114 | } while (q > resolved && *q == '/'); |
115 | q[1] = '\0'; |
116 | q = resolved; |
117 | } |
118 | if (chdir(q) < 0) |
119 | goto err1; |
120 | } else |
121 | p = resolved; |
122 | |
e3d944f4 |
123 | #ifdef HAS_LSTAT |
03d70c89 |
124 | /* Deal with the last component. */ |
125 | if (lstat(p, &sb) == 0) { |
126 | if (S_ISLNK(sb.st_mode)) { |
127 | if (++symlinks > MAXSYMLINKS) { |
128 | errno = ELOOP; |
129 | goto err1; |
130 | } |
131 | n = readlink(p, resolved, MAXPATHLEN-1); |
132 | if (n < 0) |
133 | goto err1; |
134 | resolved[n] = '\0'; |
135 | goto loop; |
136 | } |
137 | if (S_ISDIR(sb.st_mode)) { |
138 | if (chdir(p) < 0) |
139 | goto err1; |
140 | p = ""; |
141 | } |
142 | } |
e3d944f4 |
143 | #endif |
03d70c89 |
144 | |
145 | /* |
146 | * Save the last component name and get the full pathname of |
147 | * the current directory. |
148 | */ |
149 | (void)strcpy(wbuf, p); |
150 | if (getcwd(resolved, MAXPATHLEN) == 0) |
151 | goto err1; |
152 | |
153 | /* |
154 | * Join the two strings together, ensuring that the right thing |
155 | * happens if the last component is empty, or the dirname is root. |
156 | */ |
157 | if (resolved[0] == '/' && resolved[1] == '\0') |
158 | rootd = 1; |
159 | else |
160 | rootd = 0; |
161 | |
162 | if (*wbuf) { |
163 | if (strlen(resolved) + strlen(wbuf) + rootd + 1 > MAXPATHLEN) { |
164 | errno = ENAMETOOLONG; |
165 | goto err1; |
166 | } |
167 | if (rootd == 0) |
168 | (void)strcat(resolved, "/"); |
169 | (void)strcat(resolved, wbuf); |
170 | } |
171 | |
172 | /* Go back to where we came from. */ |
e3d944f4 |
173 | #ifdef HAS_FCHDIR |
03d70c89 |
174 | if (fchdir(fd) < 0) { |
175 | serrno = errno; |
176 | goto err2; |
177 | } |
e3d944f4 |
178 | #else |
179 | if (chdir(wd) < 0) { |
180 | serrno = errno; |
181 | goto err2; |
182 | } |
183 | #endif |
03d70c89 |
184 | |
185 | /* It's okay if the close fails, what's an fd more or less? */ |
9cad6237 |
186 | #ifdef HAS_FCHDIR |
03d70c89 |
187 | (void)close(fd); |
9cad6237 |
188 | #endif |
03d70c89 |
189 | return (resolved); |
190 | |
191 | err1: serrno = errno; |
29ab7413 |
192 | #ifdef HAS_FCHDIR |
03d70c89 |
193 | (void)fchdir(fd); |
29ab7413 |
194 | #else |
195 | (void)chdir(wd); |
196 | #endif |
89423764 |
197 | |
198 | err2: |
199 | #ifdef HAS_FCHDIR |
200 | (void)close(fd); |
201 | #endif |
03d70c89 |
202 | errno = serrno; |
203 | return (NULL); |
e3d944f4 |
204 | #endif |
03d70c89 |
205 | } |
206 | |
f22d8e4b |
207 | MODULE = Cwd PACKAGE = Cwd |
0d2079fa |
208 | |
f22d8e4b |
209 | PROTOTYPES: ENABLE |
0d2079fa |
210 | |
f22d8e4b |
211 | void |
212 | fastcwd() |
213 | PPCODE: |
214 | { |
215 | dXSTARG; |
89423764 |
216 | getcwd_sv(TARG); |
f22d8e4b |
217 | XSprePUSH; PUSHTARG; |
0d2079fa |
218 | } |
219 | |
f22d8e4b |
220 | void |
03d70c89 |
221 | abs_path(pathsv=Nullsv) |
222 | SV *pathsv |
f22d8e4b |
223 | PPCODE: |
2ae52c40 |
224 | { |
f22d8e4b |
225 | dXSTARG; |
226 | char *path; |
227 | STRLEN len; |
00536bfc |
228 | char buf[MAXPATHLEN]; |
03d70c89 |
229 | |
00536bfc |
230 | if (pathsv) |
231 | path = SvPV(pathsv, len); |
232 | else { |
233 | path = "."; |
234 | len = 1; |
235 | } |
03d70c89 |
236 | |
00536bfc |
237 | if (bsd_realpath(path, buf)) { |
238 | sv_setpvn(TARG, buf, strlen(buf)); |
239 | SvPOK_only(TARG); |
2ae52c40 |
240 | } |
03d70c89 |
241 | else |
00536bfc |
242 | sv_setsv(TARG, &PL_sv_undef); |
2ae52c40 |
243 | |
f22d8e4b |
244 | XSprePUSH; PUSHTARG; |
2ae52c40 |
245 | } |