Cwd.xs

filter: Statementstmt 0.0% 0 / 226 Branchbran 0.0% 0 / 190 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 0.0% 0 / 416 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/lib/File/Spec/Functions.pm ↑ Summary
1 /*
2 * ex: set ts=8 sts=4 sw=4 et:
3 */
4  
5 #define PERL_NO_GET_CONTEXT
6  
7 #include "EXTERN.h"
8 #include "perl.h"
9 #include "XSUB.h"
10 #ifndef NO_PPPORT_H
11 # define NEED_croak_xs_usage
12 # define NEED_sv_2pv_flags
13 # define NEED_my_strlcpy
14 # define NEED_my_strlcat
15 # include "ppport.h"
16 #endif
17  
18 #ifdef I_UNISTD
19 # include <unistd.h>
20 #endif
21  
22 /* For special handling of os390 sysplexed systems */
23 #define SYSNAME "$SYSNAME"
24 #define SYSNAME_LEN (sizeof(SYSNAME) - 1)
25  
26 /* The realpath() implementation from OpenBSD 3.9 to 4.2 (realpath.c 1.13)
27 * Renamed here to bsd_realpath() to avoid library conflicts.
28 */
29  
30 /* See
31 * http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/2004-11/msg00979.html
32 * for the details of why the BSD license is compatible with the
33 * AL/GPL standard perl license.
34 */
35  
36 /*
37 * Copyright (c) 2003 Constantin S. Svintsoff <kostik@iclub.nsu.ru>
38 *
39 * Redistribution and use in source and binary forms, with or without
40 * modification, are permitted provided that the following conditions
41 * are met:
42 * 1. Redistributions of source code must retain the above copyright
43 * notice, this list of conditions and the following disclaimer.
44 * 2. Redistributions in binary form must reproduce the above copyright
45 * notice, this list of conditions and the following disclaimer in the
46 * documentation and/or other materials provided with the distribution.
47 * 3. The names of the authors may not be used to endorse or promote
48 * products derived from this software without specific prior written
49 * permission.
50 *
51 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS "AS IS" AND
52 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
53 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
54 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
55 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
56 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
57 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
59 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
60 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61 * SUCH DAMAGE.
62 */
63  
64 /* OpenBSD system #includes removed since the Perl ones should do. --jhi */
65  
66 #ifndef MAXSYMLINKS
67 #define MAXSYMLINKS 8
68 #endif
69  
70 #ifndef VMS
71 /*
72 * char *realpath(const char *path, char resolved[MAXPATHLEN]);
73 *
74 * Find the real name of path, by removing all ".", ".." and symlink
75 * components. Returns (resolved) on success, or (NULL) on failure,
76 * in which case the path which caused trouble is left in (resolved).
77 */
78 static
79 char *
80 0 bsd_realpath(const char *path, char resolved[MAXPATHLEN])
81 {
82 char *p, *q, *s;
83 size_t remaining_len, resolved_len;
84 unsigned symlinks;
85 int serrno;
86 char remaining[MAXPATHLEN], next_token[MAXPATHLEN];
87  
88 0 serrno = errno;
89 0 symlinks = 0;
90 0 ▶ if (path[0] == '/') {
Branch: 1 branch
BranchTrueFalseTotal
if (path[0] == '/') { 0 0 0
91 0 resolved[0] = '/';
92 0 resolved[1] = '\0';
93 0 ▶ if (path[1] == '\0')
Branch: 1 branch
BranchTrueFalseTotal
if (path[1] == '\0') 0 0 0
94 0 return (resolved);
95 0 resolved_len = 1;
96 0 remaining_len = my_strlcpy(remaining, path + 1, sizeof(remaining));
97 } else {
98 0 ▶ if (getcwd(resolved, MAXPATHLEN) == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (getcwd(resolved, MAXPATHLEN) == NULL) { 0 0 0
99 0 my_strlcpy(resolved, ".", MAXPATHLEN);
100 0 return (NULL);
101 }
102 0 resolved_len = strlen(resolved);
103 0 remaining_len = my_strlcpy(remaining, path, sizeof(remaining));
104 }
105 0 ▶ if (remaining_len >= sizeof(remaining) || resolved_len >= MAXPATHLEN) {
Branch: 2 branches
BranchTrueFalseTotal
if (remaining_len >= sizeof(remaining) || resolved_len >= MAXPATHLEN) { 0 0 0
if (remaining_len >= sizeof(remaining) || resolved_len >= MAXPATHLEN) { 0 0 0
106 0 errno = ENAMETOOLONG;
107 0 return (NULL);
108 }
109  
110 /*
111 * Iterate over path components in 'remaining'.
112 */
113 0 ▶ while (remaining_len != 0) {
Branch: 1 branch
BranchTrueFalseTotal
while (remaining_len != 0) { 0 0 0
114  
115 /*
116 * Extract the next path component and adjust 'remaining'
117 * and its length.
118 */
119  
120 0 p = strchr(remaining, '/');
121 0 ▶ s = p ? p : remaining + remaining_len;
Branch: 1 branch
BranchTrueFalseTotal
s = p ? p : remaining + remaining_len; 0 0 0
122 0 ▶ if ((STRLEN)(s - remaining) >= (STRLEN)sizeof(next_token)) {
Branch: 1 branch
BranchTrueFalseTotal
if ((STRLEN)(s - remaining) >= (STRLEN)sizeof(next_token)) { 0 0 0
123 0 errno = ENAMETOOLONG;
124 0 return (NULL);
125 }
126 0 memcpy(next_token, remaining, s - remaining);
127 0 next_token[s - remaining] = '\0';
128 0 remaining_len -= s - remaining;
129 0 ▶ if (p != NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (p != NULL) 0 0 0
130 0 memmove(remaining, s + 1, remaining_len + 1);
131 0 ▶ if (resolved[resolved_len - 1] != '/') {
Branch: 1 branch
BranchTrueFalseTotal
if (resolved[resolved_len - 1] != '/') { 0 0 0
132 0 ▶ if (resolved_len + 1 >= MAXPATHLEN) {
Branch: 1 branch
BranchTrueFalseTotal
if (resolved_len + 1 >= MAXPATHLEN) { 0 0 0
133 0 errno = ENAMETOOLONG;
134 0 return (NULL);
135 }
136 0 resolved[resolved_len++] = '/';
137 0 resolved[resolved_len] = '\0';
138 }
139 0 ▶ if (next_token[0] == '\0')
Branch: 1 branch
BranchTrueFalseTotal
if (next_token[0] == '\0') 0 0 0
140 0 continue;
141 0 ▶ else if (strEQ(next_token, "."))
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(next_token, ".")) 0 0 0
142 0 continue;
143 0 ▶ else if (strEQ(next_token, "..")) {
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(next_token, "..")) { 0 0 0
144 /*
145 * Strip the last path component except when we have
146 * single "/"
147 */
148 0 ▶ if (resolved_len > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (resolved_len > 1) { 0 0 0
149 0 resolved[resolved_len - 1] = '\0';
150 0 q = strrchr(resolved, '/') + 1;
151 0 *q = '\0';
152 0 resolved_len = q - resolved;
153 }
154 0 continue;
155 }
156  
157 /*
158 * Append the next path component and lstat() it. If
159 * lstat() fails we still can return successfully if
160 * there are no more path components left.
161 */
162 0 resolved_len = my_strlcat(resolved, next_token, MAXPATHLEN);
163 0 ▶ if (resolved_len >= MAXPATHLEN) {
Branch: 1 branch
BranchTrueFalseTotal
if (resolved_len >= MAXPATHLEN) { 0 0 0
164 0 errno = ENAMETOOLONG;
165 0 return (NULL);
166 }
167 #if defined(HAS_LSTAT) && defined(HAS_READLINK) && defined(HAS_SYMLINK)
168 {
169 struct stat sb;
170 0 ▶ if (lstat(resolved, &sb) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (lstat(resolved, &sb) != 0) { 0 0 0
171 0 ▶ if (errno == ENOENT && p == NULL) {
Branch: 2 branches
BranchTrueFalseTotal
if (errno == ENOENT && p == NULL) { 0 0 0
if (errno == ENOENT && p == NULL) { 0 0 0
172 0 errno = serrno;
173 0 return (resolved);
174 }
175 0 return (NULL);
176 }
177 0 ▶ if (S_ISLNK(sb.st_mode)) {
Branch: 1 branch
BranchTrueFalseTotal
if (S_ISLNK(sb.st_mode)) { 0 0 0
178 int slen;
179 char symlink[MAXPATHLEN];
180  
181 0 ▶ if (symlinks++ > MAXSYMLINKS) {
Branch: 1 branch
BranchTrueFalseTotal
if (symlinks++ > MAXSYMLINKS) { 0 0 0
182 0 errno = ELOOP;
183 0 return (NULL);
184 }
185 0 slen = readlink(resolved, symlink, sizeof(symlink) - 1);
186 0 ▶ if (slen < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (slen < 0) 0 0 0
187 0 return (NULL);
188 0 symlink[slen] = '\0';
189 # ifdef EBCDIC /* XXX Probably this should be only os390 */
190 /* Replace all instances of $SYSNAME/foo simply by /foo */
191 if (slen > SYSNAME_LEN + strlen(next_token)
192 && strnEQ(symlink, SYSNAME, SYSNAME_LEN)
193 && *(symlink + SYSNAME_LEN) == '/'
194 && strEQ(symlink + SYSNAME_LEN + 1, next_token))
195 {
196 goto not_symlink;
197 }
198 # endif
199 0 ▶ if (symlink[0] == '/') {
Branch: 1 branch
BranchTrueFalseTotal
if (symlink[0] == '/') { 0 0 0
200 0 resolved[1] = 0;
201 0 resolved_len = 1;
202 0 ▶ } else if (resolved_len > 1) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (resolved_len > 1) { 0 0 0
203 /* Strip the last path component. */
204 0 resolved[resolved_len - 1] = '\0';
205 0 q = strrchr(resolved, '/') + 1;
206 0 *q = '\0';
207 0 resolved_len = q - resolved;
208 }
209  
210 /*
211 * If there are any path components left, then
212 * append them to symlink. The result is placed
213 * in 'remaining'.
214 */
215 0 ▶ if (p != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (p != NULL) { 0 0 0
216 0 ▶ if (symlink[slen - 1] != '/') {
Branch: 1 branch
BranchTrueFalseTotal
if (symlink[slen - 1] != '/') { 0 0 0
217 0 ▶ if ((STRLEN)(slen + 1) >= (STRLEN)sizeof(symlink)) {
Branch: 1 branch
BranchTrueFalseTotal
if ((STRLEN)(slen + 1) >= (STRLEN)sizeof(symlink)) { 0 0 0
218 0 errno = ENAMETOOLONG;
219 0 return (NULL);
220 }
221 0 symlink[slen] = '/';
222 0 symlink[slen + 1] = 0;
223 }
224 0 remaining_len = my_strlcat(symlink, remaining, sizeof(symlink));
225 0 ▶ if (remaining_len >= sizeof(remaining)) {
Branch: 1 branch
BranchTrueFalseTotal
if (remaining_len >= sizeof(remaining)) { 0 0 0
226 0 errno = ENAMETOOLONG;
227 0 return (NULL);
228 }
229 }
230 0 remaining_len = my_strlcpy(remaining, symlink, sizeof(remaining));
231 }
232 # ifdef EBCDIC
233 not_symlink: ;
234 # endif
235 }
236 #endif
237 }
238  
239 /*
240 * Remove trailing slash except when the resolved pathname
241 * is a single "/".
242 */
243 0 ▶ if (resolved_len > 1 && resolved[resolved_len - 1] == '/')
Branch: 2 branches
BranchTrueFalseTotal
if (resolved_len > 1 && resolved[resolved_len - 1] == '/') 0 0 0
if (resolved_len > 1 && resolved[resolved_len - 1] == '/') 0 0 0
244 0 resolved[resolved_len - 1] = '\0';
245 0 return (resolved);
246 }
247 #endif
248  
249 #ifndef SV_CWD_RETURN_UNDEF
250 #define SV_CWD_RETURN_UNDEF \
251 sv_setsv(sv, &PL_sv_undef); \
252 return FALSE
253 #endif
254  
255 #ifndef OPpENTERSUB_HASTARG
256 #define OPpENTERSUB_HASTARG 32 /* Called from OP tree. */
257 #endif
258  
259 #ifndef dXSTARG
260 #define dXSTARG SV * targ = ((PL_op->op_private & OPpENTERSUB_HASTARG) \
261 ? PAD_SV(PL_op->op_targ) : sv_newmortal())
262 #endif
263  
264 #ifndef XSprePUSH
265 #define XSprePUSH (sp = PL_stack_base + ax - 1)
266 #endif
267  
268 #ifndef SV_CWD_ISDOT
269 #define SV_CWD_ISDOT(dp) \
270 (dp->d_name[0] == '.' && (dp->d_name[1] == '\0' || \
271 (dp->d_name[1] == '.' && dp->d_name[2] == '\0')))
272 #endif
273  
274 #ifndef getcwd_sv
275 /* Taken from perl 5.8's util.c */
276 #define getcwd_sv(a) Perl_getcwd_sv(aTHX_ a)
277 int Perl_getcwd_sv(pTHX_ SV *sv)
278 {
279 #ifndef PERL_MICRO
280  
281 SvTAINTED_on(sv);
282  
283 #ifdef HAS_GETCWD
284 {
285 char buf[MAXPATHLEN];
286  
287 /* Some getcwd()s automatically allocate a buffer of the given
288 * size from the heap if they are given a NULL buffer pointer.
289 * The problem is that this behaviour is not portable. */
290 if (getcwd(buf, sizeof(buf) - 1)) {
291 STRLEN len = strlen(buf);
292 sv_setpvn(sv, buf, len);
293 return TRUE;
294 }
295 else {
296 sv_setsv(sv, &PL_sv_undef);
297 return FALSE;
298 }
299 }
300  
301 #else
302 {
303 Stat_t statbuf;
304 int orig_cdev, orig_cino, cdev, cino, odev, oino, tdev, tino;
305 int namelen, pathlen=0;
306 DIR *dir;
307 Direntry_t *dp;
308  
309 (void)SvUPGRADE(sv, SVt_PV);
310  
311 if (PerlLIO_lstat(".", &statbuf) < 0) {
312 SV_CWD_RETURN_UNDEF;
313 }
314  
315 orig_cdev = statbuf.st_dev;
316 orig_cino = statbuf.st_ino;
317 cdev = orig_cdev;
318 cino = orig_cino;
319  
320 for (;;) {
321 odev = cdev;
322 oino = cino;
323  
324 if (PerlDir_chdir("..") < 0) {
325 SV_CWD_RETURN_UNDEF;
326 }
327 if (PerlLIO_stat(".", &statbuf) < 0) {
328 SV_CWD_RETURN_UNDEF;
329 }
330  
331 cdev = statbuf.st_dev;
332 cino = statbuf.st_ino;
333  
334 if (odev == cdev && oino == cino) {
335 break;
336 }
337 if (!(dir = PerlDir_open("."))) {
338 SV_CWD_RETURN_UNDEF;
339 }
340  
341 while ((dp = PerlDir_read(dir)) != NULL) {
342 #ifdef DIRNAMLEN
343 namelen = dp->d_namlen;
344 #else
345 namelen = strlen(dp->d_name);
346 #endif
347 /* skip . and .. */
348 if (SV_CWD_ISDOT(dp)) {
349 continue;
350 }
351  
352 if (PerlLIO_lstat(dp->d_name, &statbuf) < 0) {
353 SV_CWD_RETURN_UNDEF;
354 }
355  
356 tdev = statbuf.st_dev;
357 tino = statbuf.st_ino;
358 if (tino == oino && tdev == odev) {
359 break;
360 }
361 }
362  
363 if (!dp) {
364 SV_CWD_RETURN_UNDEF;
365 }
366  
367 if (pathlen + namelen + 1 >= MAXPATHLEN) {
368 SV_CWD_RETURN_UNDEF;
369 }
370  
371 SvGROW(sv, pathlen + namelen + 1);
372  
373 if (pathlen) {
374 /* shift down */
375 Move(SvPVX(sv), SvPVX(sv) + namelen + 1, pathlen, char);
376 }
377  
378 /* prepend current directory to the front */
379 *SvPVX(sv) = '/';
380 Move(dp->d_name, SvPVX(sv)+1, namelen, char);
381 pathlen += (namelen + 1);
382  
383 #ifdef VOID_CLOSEDIR
384 PerlDir_close(dir);
385 #else
386 if (PerlDir_close(dir) < 0) {
387 SV_CWD_RETURN_UNDEF;
388 }
389 #endif
390 }
391  
392 if (pathlen) {
393 SvCUR_set(sv, pathlen);
394 *SvEND(sv) = '\0';
395 SvPOK_only(sv);
396  
397 if (PerlDir_chdir(SvPVX(sv)) < 0) {
398 SV_CWD_RETURN_UNDEF;
399 }
400 }
401 if (PerlLIO_stat(".", &statbuf) < 0) {
402 SV_CWD_RETURN_UNDEF;
403 }
404  
405 cdev = statbuf.st_dev;
406 cino = statbuf.st_ino;
407  
408 if (cdev != orig_cdev || cino != orig_cino) {
409 Perl_croak(aTHX_ "Unstable directory path, "
410 "current directory changed unexpectedly");
411 }
412  
413 return TRUE;
414 }
415 #endif
416  
417 #else
418 return FALSE;
419 #endif
420 }
421  
422 #endif
423  
424 #if defined(START_MY_CXT) && defined(MY_CXT_CLONE)
425 # define USE_MY_CXT 1
426 #else
427 # define USE_MY_CXT 0
428 #endif
429  
430 #if USE_MY_CXT
431 # define MY_CXT_KEY "Cwd::_guts" XS_VERSION
432 typedef struct {
433 SV *empty_string_sv, *slash_string_sv;
434 } my_cxt_t;
435 START_MY_CXT
436 # define dUSE_MY_CXT dMY_CXT
437 # define EMPTY_STRING_SV MY_CXT.empty_string_sv
438 # define SLASH_STRING_SV MY_CXT.slash_string_sv
439 # define POPULATE_MY_CXT do { \
440 MY_CXT.empty_string_sv = newSVpvs(""); \
441 MY_CXT.slash_string_sv = newSVpvs("/"); \
442 } while(0)
443 #else
444 # define dUSE_MY_CXT dNOOP
445 # define EMPTY_STRING_SV sv_2mortal(newSVpvs(""))
446 # define SLASH_STRING_SV sv_2mortal(newSVpvs("/"))
447 #endif
448  
449 #define invocant_is_unix(i) THX_invocant_is_unix(aTHX_ i)
450 static
451 bool
452 0 THX_invocant_is_unix(pTHX_ SV *invocant)
453 {
454 /*
455 * This is used to enable optimisations that avoid method calls
456 * by knowing how they would resolve. False negatives, disabling
457 * the optimisation where it would actually behave correctly, are
458 * acceptable.
459 */
460 0 ▶ return SvPOK(invocant) && SvCUR(invocant) == 16 &&
Branch: 2 branches
BranchTrueFalseTotal
return SvPOK(invocant) && SvCUR(invocant) == 16 && 0 0 0
return SvPOK(invocant) && SvCUR(invocant) == 16 && 0 0 0
461 0 ▶ !memcmp(SvPVX(invocant), "File::Spec::Unix", 16);
Branch: 1 branch
BranchTrueFalseTotal
!memcmp(SvPVX(invocant), "File::Spec::Unix", 16); 0 0 0
462 }
463  
464 #define unix_canonpath(p) THX_unix_canonpath(aTHX_ p)
465 static
466 SV *
467 0 THX_unix_canonpath(pTHX_ SV *path)
468 {
469 SV *retval;
470 char const *p, *pe, *q;
471 STRLEN l;
472 char *o;
473 STRLEN plen;
474 0 SvGETMAGIC(path);
475 0 ▶ if(!SvOK(path)) return &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
if(!SvOK(path)) return &PL_sv_undef; 0 0 0
476 0 p = SvPV_nomg(path, plen);
477 0 ▶ if(plen == 0) return newSVpvs("");
Branch: 1 branch
BranchTrueFalseTotal
if(plen == 0) return newSVpvs(""); 0 0 0
478 0 pe = p + plen;
479 0 retval = newSV(plen);
480 #ifdef SvUTF8
481 0 ▶ if(SvUTF8(path)) SvUTF8_on(retval);
Branch: 1 branch
BranchTrueFalseTotal
if(SvUTF8(path)) SvUTF8_on(retval); 0 0 0
482 #endif
483 0 o = SvPVX(retval);
484 if(DOUBLE_SLASHES_SPECIAL && p[0] == '/' && p[1] == '/' && p[2] != '/') {
485 q = (const char *) memchr(p+2, '/', pe-(p+2));
486 if(!q) q = pe;
487 l = q - p;
488 memcpy(o, p, l);
489 p = q;
490 o += l;
491 }
492 /*
493 * The transformations performed here are:
494 * . squeeze multiple slashes
495 * . eliminate "." segments, except one if that's all there is
496 * . eliminate leading ".." segments
497 * . eliminate trailing slash, unless it's all there is
498 */
499 0 ▶ if(p[0] == '/') {
Branch: 1 branch
BranchTrueFalseTotal
if(p[0] == '/') { 0 0 0
500 0 *o++ = '/';
501 0 while(1) {
502 0 ▶ do { p++; } while(p[0] == '/');
Branch: 1 branch
BranchTrueFalseTotal
do { p++; } while(p[0] == '/'); 0 0 0
503 0 ▶ if(p[0] == '.' && p[1] == '.' && (p+2 == pe || p[2] == '/')) {
Branch: 4 branches
BranchTrueFalseTotal
if(p[0] == '.' && p[1] == '.' && (p+2 == pe || p[2] == '/')) { 0 0 0
if(p[0] == '.' && p[1] == '.' && (p+2 == pe || p[2] == '/')) { 0 0 0
if(p[0] == '.' && p[1] == '.' && (p+2 == pe || p[2] == '/')) { 0 0 0
if(p[0] == '.' && p[1] == '.' && (p+2 == pe || p[2] == '/')) { 0 0 0
504 0 p++;
505 /* advance past second "." next time round loop */
506 0 ▶ } else if(p[0] == '.' && (p+1 == pe || p[1] == '/')) {
Branch: 3 branches
BranchTrueFalseTotal
} else if(p[0] == '.' && (p+1 == pe || p[1] == '/')) { 0 0 0
} else if(p[0] == '.' && (p+1 == pe || p[1] == '/')) { 0 0 0
} else if(p[0] == '.' && (p+1 == pe || p[1] == '/')) { 0 0 0
507 /* advance past "." next time round loop */
508 } else {
509 break;
510 }
511 }
512 0 ▶ } else if(p[0] == '.' && p[1] == '/') {
Branch: 2 branches
BranchTrueFalseTotal
} else if(p[0] == '.' && p[1] == '/') { 0 0 0
} else if(p[0] == '.' && p[1] == '/') { 0 0 0
513 do {
514 0 p++;
515 0 ▶ do { p++; } while(p[0] == '/');
Branch: 1 branch
BranchTrueFalseTotal
do { p++; } while(p[0] == '/'); 0 0 0
516 0 ▶ } while(p[0] == '.' && p[1] == '/');
Branch: 2 branches
BranchTrueFalseTotal
} while(p[0] == '.' && p[1] == '/'); 0 0 0
} while(p[0] == '.' && p[1] == '/'); 0 0 0
517 0 ▶ if(p == pe) *o++ = '.';
Branch: 1 branch
BranchTrueFalseTotal
if(p == pe) *o++ = '.'; 0 0 0
518 }
519 0 ▶ if(p == pe) goto end;
Branch: 1 branch
BranchTrueFalseTotal
if(p == pe) goto end; 0 0 0
520 while(1) {
521 0 q = (const char *) memchr(p, '/', pe-p);
522 0 ▶ if(!q) q = pe;
Branch: 1 branch
BranchTrueFalseTotal
if(!q) q = pe; 0 0 0
523 0 l = q - p;
524 0 memcpy(o, p, l);
525 0 p = q;
526 0 o += l;
527 0 ▶ if(p == pe) goto end;
Branch: 1 branch
BranchTrueFalseTotal
if(p == pe) goto end; 0 0 0
528 0 while(1) {
529 0 ▶ do { p++; } while(p[0] == '/');
Branch: 1 branch
BranchTrueFalseTotal
do { p++; } while(p[0] == '/'); 0 0 0
530 0 ▶ if(p == pe) goto end;
Branch: 1 branch
BranchTrueFalseTotal
if(p == pe) goto end; 0 0 0
531 0 ▶ if(p[0] != '.') break;
Branch: 1 branch
BranchTrueFalseTotal
if(p[0] != '.') break; 0 0 0
532 0 ▶ if(p+1 == pe) goto end;
Branch: 1 branch
BranchTrueFalseTotal
if(p+1 == pe) goto end; 0 0 0
533 0 ▶ if(p[1] != '/') break;
Branch: 1 branch
BranchTrueFalseTotal
if(p[1] != '/') break; 0 0 0
534 0 p++;
535 }
536 0 *o++ = '/';
537 }
538 0 end: ;
539 0 *o = 0;
540 0 SvPOK_on(retval);
541 0 SvCUR_set(retval, o - SvPVX(retval));
542 0 ▶ SvTAINT(retval);
Branch: 2 branches
BranchTrueFalseTotal
SvTAINT(retval); 0 0 0
SvTAINT(retval); 0 0 0
543 0 return retval;
544 }
545  
546 MODULE = Cwd PACKAGE = Cwd
547  
548 PROTOTYPES: DISABLE
549  
550 BOOT:
551 #if USE_MY_CXT
552 {
553 MY_CXT_INIT;
554 0 POPULATE_MY_CXT;
555 }
556 #endif
557  
558 #if USE_MY_CXT
559  
560 void
561 CLONE(...)
562 CODE:
563 PERL_UNUSED_VAR(items);
564 0 { MY_CXT_CLONE; POPULATE_MY_CXT; }
565  
566 #endif
567  
568 void
569 getcwd(...)
570 ALIAS:
571 fastcwd=1
572 PPCODE:
573 {
574 0 ▶ dXSTARG;
Branch: 1 branch
BranchTrueFalseTotal
dXSTARG; 0 0 0
575 /* fastcwd takes zero parameters: */
576 0 ▶ if (ix == 1 && items != 0)
Branch: 2 branches
BranchTrueFalseTotal
if (ix == 1 && items != 0) 0 0 0
if (ix == 1 && items != 0) 0 0 0
577 0 croak_xs_usage(cv, "");
578 0 getcwd_sv(TARG);
579 0 ▶ XSprePUSH; PUSHTARG;
Branch: 1 branch
BranchTrueFalseTotal
XSprePUSH; PUSHTARG; 0 0 0
580 0 ▶ SvTAINTED_on(TARG);
Branch: 1 branch
BranchTrueFalseTotal
SvTAINTED_on(TARG); 0 0 0
581 }
582  
583 void
584 abs_path(pathsv=Nullsv)
585 SV *pathsv
586 PPCODE:
587 {
588 0 ▶ dXSTARG;
Branch: 1 branch
BranchTrueFalseTotal
dXSTARG; 0 0 0
589 0 ▶ char *const path = pathsv ? SvPV_nolen(pathsv) : (char *)".";
Branch: 1 branch
BranchTrueFalseTotal
char *const path = pathsv ? SvPV_nolen(pathsv) : (char *)"."; 0 0 0
590 char buf[MAXPATHLEN];
591  
592 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ( 0 0 0
593 #ifdef VMS
594 Perl_rmsexpand(aTHX_ path, buf, NULL, 0)
595 #else
596 0 bsd_realpath(path, buf)
597 #endif
598 ) {
599 0 sv_setpv_mg(TARG, buf);
600 0 SvPOK_only(TARG);
601 0 ▶ SvTAINTED_on(TARG);
Branch: 1 branch
BranchTrueFalseTotal
SvTAINTED_on(TARG); 0 0 0
602 }
603 else
604 0 sv_setsv(TARG, &PL_sv_undef);
605  
606 0 XSprePUSH; PUSHs(TARG);
607 0 ▶ SvTAINTED_on(TARG);
Branch: 1 branch
BranchTrueFalseTotal
SvTAINTED_on(TARG); 0 0 0
608 }
609  
610 #if defined(WIN32) && !defined(UNDER_CE)
611  
612 void
613 getdcwd(...)
614 PROTOTYPE: ENABLE
615 PPCODE:
616 {
617 dXSTARG;
618 int drive;
619 char *dir;
620  
621 /* Drive 0 is the current drive, 1 is A:, 2 is B:, 3 is C: and so on. */
622 if ( items == 0 ||
623 (items == 1 && (!SvOK(ST(0)) || (SvPOK(ST(0)) && !SvCUR(ST(0))))))
624 drive = 0;
625 else if (items == 1 && SvPOK(ST(0)) && SvCUR(ST(0)) &&
626 isALPHA(SvPVX(ST(0))[0]))
627 drive = toUPPER(SvPVX(ST(0))[0]) - 'A' + 1;
628 else
629 croak("Usage: getdcwd(DRIVE)");
630  
631 New(0,dir,MAXPATHLEN,char);
632 if (_getdcwd(drive, dir, MAXPATHLEN)) {
633 sv_setpv_mg(TARG, dir);
634 SvPOK_only(TARG);
635 }
636 else
637 sv_setsv(TARG, &PL_sv_undef);
638  
639 Safefree(dir);
640  
641 XSprePUSH; PUSHs(TARG);
642 SvTAINTED_on(TARG);
643 }
644  
645 #endif
646  
647 MODULE = Cwd PACKAGE = File::Spec::Unix
648  
649 SV *
650 canonpath(SV *self, SV *path = &PL_sv_undef, ...)
651 CODE:
652 PERL_UNUSED_VAR(self);
653 0 RETVAL = unix_canonpath(path);
654 OUTPUT:
655 RETVAL
656  
657 SV *
658 _fn_canonpath(SV *path = &PL_sv_undef, ...)
659 CODE:
660 0 RETVAL = unix_canonpath(path);
661 OUTPUT:
662 RETVAL
663  
664 SV *
665 catdir(SV *self, ...)
666 PREINIT:
667 dUSE_MY_CXT;
668 SV *joined;
669 CODE:
670 0 ▶ EXTEND(SP, items+1);
Branch: 2 branches
BranchTrueFalseTotal
EXTEND(SP, items+1); 0 0 0
EXTEND(SP, items+1); 0 0 0
671 0 ST(items) = EMPTY_STRING_SV;
672 0 joined = sv_newmortal();
673 0 do_join(joined, SLASH_STRING_SV, &ST(0), &ST(items));
674 0 ▶ if(invocant_is_unix(self)) {
Branch: 1 branch
BranchTrueFalseTotal
if(invocant_is_unix(self)) { 0 0 0
675 0 RETVAL = unix_canonpath(joined);
676 } else {
677 0 ENTER;
678 0 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 0 0
679 0 ▶ EXTEND(SP, 2);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 2); 0 0 0
680 0 PUSHs(self);
681 0 PUSHs(joined);
682 0 PUTBACK;
683 0 call_method("canonpath", G_SCALAR);
684 0 SPAGAIN;
685 0 RETVAL = POPs;
686 0 LEAVE;
687 0 SvREFCNT_inc(RETVAL);
688 }
689 OUTPUT:
690 RETVAL
691  
692 SV *
693 _fn_catdir(...)
694 PREINIT:
695 dUSE_MY_CXT;
696 SV *joined;
697 CODE:
698 0 ▶ EXTEND(SP, items+1);
Branch: 2 branches
BranchTrueFalseTotal
EXTEND(SP, items+1); 0 0 0
EXTEND(SP, items+1); 0 0 0
699 0 ST(items) = EMPTY_STRING_SV;
700 0 joined = sv_newmortal();
701 0 do_join(joined, SLASH_STRING_SV, &ST(-1), &ST(items));
702 0 RETVAL = unix_canonpath(joined);
703 OUTPUT:
704 RETVAL
705  
706 SV *
707 catfile(SV *self, ...)
708 PREINIT:
709 dUSE_MY_CXT;
710 CODE:
711 0 ▶ if(invocant_is_unix(self)) {
Branch: 1 branch
BranchTrueFalseTotal
if(invocant_is_unix(self)) { 0 0 0
712 0 ▶ if(items == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if(items == 1) { 0 0 0
713 0 RETVAL = &PL_sv_undef;
714 } else {
715 0 SV *file = unix_canonpath(ST(items-1));
716 0 ▶ if(items == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if(items == 2) { 0 0 0
717 0 RETVAL = file;
718 } else {
719 0 SV *dir = sv_newmortal();
720 0 sv_2mortal(file);
721 0 ST(items-1) = EMPTY_STRING_SV;
722 0 do_join(dir, SLASH_STRING_SV, &ST(0), &ST(items-1));
723 0 RETVAL = unix_canonpath(dir);
724 0 ▶ if(SvCUR(RETVAL) == 0 || SvPVX(RETVAL)[SvCUR(RETVAL)-1] != '/')
Branch: 2 branches
BranchTrueFalseTotal
if(SvCUR(RETVAL) == 0 || SvPVX(RETVAL)[SvCUR(RETVAL)-1] != '/') 0 0 0
if(SvCUR(RETVAL) == 0 || SvPVX(RETVAL)[SvCUR(RETVAL)-1] != '/') 0 0 0
725 0 sv_catsv(RETVAL, SLASH_STRING_SV);
726 0 sv_catsv(RETVAL, file);
727 }
728 }
729 } else {
730 SV *file, *dir;
731 0 ENTER;
732 0 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 0 0
733 0 ▶ EXTEND(SP, 2);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 2); 0 0 0
734 0 PUSHs(self);
735 0 ▶ PUSHs(items == 1 ? &PL_sv_undef : ST(items-1));
Branch: 1 branch
BranchTrueFalseTotal
PUSHs(items == 1 ? &PL_sv_undef : ST(items-1)); 0 0 0
736 0 PUTBACK;
737 0 call_method("canonpath", G_SCALAR);
738 0 SPAGAIN;
739 0 file = POPs;
740 0 LEAVE;
741 0 ▶ if(items <= 2) {
Branch: 1 branch
BranchTrueFalseTotal
if(items <= 2) { 0 0 0
742 0 RETVAL = SvREFCNT_inc(file);
743 } else {
744 char const *pv;
745 STRLEN len;
746 bool need_slash;
747 0 SP--;
748 0 ENTER;
749 0 ▶ PUSHMARK(&ST(-1));
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(&ST(-1)); 0 0 0
750 0 PUTBACK;
751 0 call_method("catdir", G_SCALAR);
752 0 SPAGAIN;
753 0 dir = POPs;
754 0 LEAVE;
755 0 pv = SvPV(dir, len);
756 0 ▶ need_slash = len == 0 || pv[len-1] != '/';
Branch: 2 branches
BranchTrueFalseTotal
need_slash = len == 0 || pv[len-1] != '/'; 0 0 0
need_slash = len == 0 || pv[len-1] != '/'; 0 0 0
757 0 RETVAL = newSVsv(dir);
758 0 ▶ if(need_slash) sv_catsv(RETVAL, SLASH_STRING_SV);
Branch: 1 branch
BranchTrueFalseTotal
if(need_slash) sv_catsv(RETVAL, SLASH_STRING_SV); 0 0 0
759 0 sv_catsv(RETVAL, file);
760 }
761 }
762 OUTPUT:
763 RETVAL
764  
765 SV *
766 _fn_catfile(...)
767 PREINIT:
768 dUSE_MY_CXT;
769 CODE:
770 0 ▶ if(items == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if(items == 0) { 0 0 0
771 0 RETVAL = &PL_sv_undef;
772 } else {
773 0 SV *file = unix_canonpath(ST(items-1));
774 0 ▶ if(items == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if(items == 1) { 0 0 0
775 0 RETVAL = file;
776 } else {
777 0 SV *dir = sv_newmortal();
778 0 sv_2mortal(file);
779 0 ST(items-1) = EMPTY_STRING_SV;
780 0 do_join(dir, SLASH_STRING_SV, &ST(-1), &ST(items-1));
781 0 RETVAL = unix_canonpath(dir);
782 0 ▶ if(SvCUR(RETVAL) == 0 || SvPVX(RETVAL)[SvCUR(RETVAL)-1] != '/')
Branch: 2 branches
BranchTrueFalseTotal
if(SvCUR(RETVAL) == 0 || SvPVX(RETVAL)[SvCUR(RETVAL)-1] != '/') 0 0 0
if(SvCUR(RETVAL) == 0 || SvPVX(RETVAL)[SvCUR(RETVAL)-1] != '/') 0 0 0
783 0 sv_catsv(RETVAL, SLASH_STRING_SV);
784 0 sv_catsv(RETVAL, file);
785 }
786 }
787 OUTPUT:
788 RETVAL
« blib/lib/File/Spec/Functions.pm ↑ Summary