1 #define PERL_NO_GET_CONTEXT
6 #define NEED_my_strlcpy
7 #define NEED_my_strlcat
14 /* The realpath() implementation from OpenBSD 3.9 to 4.2 (realpath.c 1.13)
15 * Renamed here to bsd_realpath() to avoid library conflicts.
19 * http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/2004-11/msg00979.html
20 * for the details of why the BSD license is compatible with the
21 * AL/GPL standard perl license.
25 * Copyright (c) 2003 Constantin S. Svintsoff <kostik@iclub.nsu.ru>
27 * Redistribution and use in source and binary forms, with or without
28 * modification, are permitted provided that the following conditions
30 * 1. Redistributions of source code must retain the above copyright
31 * notice, this list of conditions and the following disclaimer.
32 * 2. Redistributions in binary form must reproduce the above copyright
33 * notice, this list of conditions and the following disclaimer in the
34 * documentation and/or other materials provided with the distribution.
35 * 3. The names of the authors may not be used to endorse or promote
36 * products derived from this software without specific prior written
39 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS "AS IS" AND
40 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
42 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
43 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
44 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
45 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
46 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
47 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
48 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52 /* OpenBSD system #includes removed since the Perl ones should do. --jhi */
60 * char *realpath(const char *path, char resolved[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(const char *path, char resolved[MAXPATHLEN])
71 size_t left_len, resolved_len;
74 char left[MAXPATHLEN], next_token[MAXPATHLEN];
84 left_len = my_strlcpy(left, path + 1, sizeof(left));
86 if (getcwd(resolved, MAXPATHLEN) == NULL) {
87 my_strlcpy(resolved, ".", MAXPATHLEN);
90 resolved_len = strlen(resolved);
91 left_len = my_strlcpy(left, path, sizeof(left));
93 if (left_len >= sizeof(left) || resolved_len >= MAXPATHLEN) {
99 * Iterate over path components in 'left'.
101 while (left_len != 0) {
103 * Extract the next path component and adjust 'left'
106 p = strchr(left, '/');
107 s = p ? p : left + left_len;
108 if ((STRLEN)(s - left) >= (STRLEN)sizeof(next_token)) {
109 errno = ENAMETOOLONG;
112 memcpy(next_token, left, s - left);
113 next_token[s - left] = '\0';
114 left_len -= s - left;
116 memmove(left, s + 1, left_len + 1);
117 if (resolved[resolved_len - 1] != '/') {
118 if (resolved_len + 1 >= MAXPATHLEN) {
119 errno = ENAMETOOLONG;
122 resolved[resolved_len++] = '/';
123 resolved[resolved_len] = '\0';
125 if (next_token[0] == '\0')
127 else if (strcmp(next_token, ".") == 0)
129 else if (strcmp(next_token, "..") == 0) {
131 * Strip the last path component except when we have
134 if (resolved_len > 1) {
135 resolved[resolved_len - 1] = '\0';
136 q = strrchr(resolved, '/') + 1;
138 resolved_len = q - resolved;
144 * Append the next path component and lstat() it. If
145 * lstat() fails we still can return successfully if
146 * there are no more path components left.
148 resolved_len = my_strlcat(resolved, next_token, MAXPATHLEN);
149 if (resolved_len >= MAXPATHLEN) {
150 errno = ENAMETOOLONG;
153 #if defined(HAS_LSTAT) && defined(HAS_READLINK) && defined(HAS_SYMLINK)
156 if (lstat(resolved, &sb) != 0) {
157 if (errno == ENOENT && p == NULL) {
163 if (S_ISLNK(sb.st_mode)) {
165 char symlink[MAXPATHLEN];
167 if (symlinks++ > MAXSYMLINKS) {
171 slen = readlink(resolved, symlink, sizeof(symlink) - 1);
174 symlink[slen] = '\0';
175 if (symlink[0] == '/') {
178 } else if (resolved_len > 1) {
179 /* Strip the last path component. */
180 resolved[resolved_len - 1] = '\0';
181 q = strrchr(resolved, '/') + 1;
183 resolved_len = q - resolved;
187 * If there are any path components left, then
188 * append them to symlink. The result is placed
192 if (symlink[slen - 1] != '/') {
193 if ((STRLEN)(slen + 1) >= (STRLEN)sizeof(symlink)) {
194 errno = ENAMETOOLONG;
198 symlink[slen + 1] = 0;
200 left_len = my_strlcat(symlink, left, sizeof(left));
201 if (left_len >= sizeof(left)) {
202 errno = ENAMETOOLONG;
206 left_len = my_strlcpy(left, symlink, sizeof(left));
213 * Remove trailing slash except when the resolved pathname
216 if (resolved_len > 1 && resolved[resolved_len - 1] == '/')
217 resolved[resolved_len - 1] = '\0';
222 #ifndef SV_CWD_RETURN_UNDEF
223 #define SV_CWD_RETURN_UNDEF \
224 sv_setsv(sv, &PL_sv_undef); \
228 #ifndef OPpENTERSUB_HASTARG
229 #define OPpENTERSUB_HASTARG 32 /* Called from OP tree. */
233 #define dXSTARG SV * targ = ((PL_op->op_private & OPpENTERSUB_HASTARG) \
234 ? PAD_SV(PL_op->op_targ) : sv_newmortal())
238 #define XSprePUSH (sp = PL_stack_base + ax - 1)
242 #define SV_CWD_ISDOT(dp) \
243 (dp->d_name[0] == '.' && (dp->d_name[1] == '\0' || \
244 (dp->d_name[1] == '.' && dp->d_name[2] == '\0')))
248 /* Taken from perl 5.8's util.c */
249 #define getcwd_sv(a) Perl_getcwd_sv(aTHX_ a)
250 int Perl_getcwd_sv(pTHX_ SV *sv)
254 #ifndef INCOMPLETE_TAINTS
260 char buf[MAXPATHLEN];
262 /* Some getcwd()s automatically allocate a buffer of the given
263 * size from the heap if they are given a NULL buffer pointer.
264 * The problem is that this behaviour is not portable. */
265 if (getcwd(buf, sizeof(buf) - 1)) {
266 STRLEN len = strlen(buf);
267 sv_setpvn(sv, buf, len);
271 sv_setsv(sv, &PL_sv_undef);
279 int orig_cdev, orig_cino, cdev, cino, odev, oino, tdev, tino;
280 int namelen, pathlen=0;
284 (void)SvUPGRADE(sv, SVt_PV);
286 if (PerlLIO_lstat(".", &statbuf) < 0) {
290 orig_cdev = statbuf.st_dev;
291 orig_cino = statbuf.st_ino;
299 if (PerlDir_chdir("..") < 0) {
302 if (PerlLIO_stat(".", &statbuf) < 0) {
306 cdev = statbuf.st_dev;
307 cino = statbuf.st_ino;
309 if (odev == cdev && oino == cino) {
312 if (!(dir = PerlDir_open("."))) {
316 while ((dp = PerlDir_read(dir)) != NULL) {
318 namelen = dp->d_namlen;
320 namelen = strlen(dp->d_name);
323 if (SV_CWD_ISDOT(dp)) {
327 if (PerlLIO_lstat(dp->d_name, &statbuf) < 0) {
331 tdev = statbuf.st_dev;
332 tino = statbuf.st_ino;
333 if (tino == oino && tdev == odev) {
342 if (pathlen + namelen + 1 >= MAXPATHLEN) {
346 SvGROW(sv, pathlen + namelen + 1);
350 Move(SvPVX(sv), SvPVX(sv) + namelen + 1, pathlen, char);
353 /* prepend current directory to the front */
355 Move(dp->d_name, SvPVX(sv)+1, namelen, char);
356 pathlen += (namelen + 1);
361 if (PerlDir_close(dir) < 0) {
368 SvCUR_set(sv, pathlen);
372 if (PerlDir_chdir(SvPVX(sv)) < 0) {
376 if (PerlLIO_stat(".", &statbuf) < 0) {
380 cdev = statbuf.st_dev;
381 cino = statbuf.st_ino;
383 if (cdev != orig_cdev || cino != orig_cino) {
384 Perl_croak(aTHX_ "Unstable directory path, "
385 "current directory changed unexpectedly");
399 #if defined(START_MY_CXT) && defined(MY_CXT_CLONE)
400 # define USE_MY_CXT 1
402 # define USE_MY_CXT 0
406 # define MY_CXT_KEY "Cwd::_guts"XS_VERSION
408 SV *empty_string_sv, *slash_string_sv;
411 # define dUSE_MY_CXT dMY_CXT
412 # define EMPTY_STRING_SV MY_CXT.empty_string_sv
413 # define SLASH_STRING_SV MY_CXT.slash_string_sv
414 # define POPULATE_MY_CXT do { \
415 MY_CXT.empty_string_sv = newSVpvs(""); \
416 MY_CXT.slash_string_sv = newSVpvs("/"); \
419 # define dUSE_MY_CXT dNOOP
420 # define EMPTY_STRING_SV sv_2mortal(newSVpvs(""))
421 # define SLASH_STRING_SV sv_2mortal(newSVpvs("/"))
424 #define invocant_is_unix(i) THX_invocant_is_unix(aTHX_ i)
427 THX_invocant_is_unix(pTHX_ SV *invocant)
430 * This is used to enable optimisations that avoid method calls
431 * by knowing how they would resolve. False negatives, disabling
432 * the optimisation where it would actually behave correctly, are
435 return SvPOK(invocant) && SvCUR(invocant) == 16 &&
436 !memcmp(SvPVX(invocant), "File::Spec::Unix", 16);
439 #define unix_canonpath(p) THX_unix_canonpath(aTHX_ p)
442 THX_unix_canonpath(pTHX_ SV *path)
445 char const *p, *pe, *q;
450 if(!SvOK(path)) return &PL_sv_undef;
451 p = SvPV_nomg(path, plen);
452 if(plen == 0) return newSVpvs("");
454 retval = newSV(plen);
456 if(SvUTF8(path)) SvUTF8_on(retval);
459 if(DOUBLE_SLASHES_SPECIAL && p[0] == '/' && p[1] == '/' && p[2] != '/') {
460 q = (const char *) memchr(p+2, '/', pe-(p+2));
468 * The transformations performed here are:
469 * . squeeze multiple slashes
470 * . eliminate "." segments, except one if that's all there is
471 * . eliminate leading ".." segments
472 * . eliminate trailing slash, unless it's all there is
477 do { p++; } while(p[0] == '/');
478 if(p[0] == '.' && p[1] == '.' && (p+2 == pe || p[2] == '/')) {
480 /* advance past second "." next time round loop */
481 } else if(p[0] == '.' && (p+1 == pe || p[1] == '/')) {
482 /* advance past "." next time round loop */
487 } else if(p[0] == '.' && p[1] == '/') {
490 do { p++; } while(p[0] == '/');
491 } while(p[0] == '.' && p[1] == '/');
492 if(p == pe) *o++ = '.';
494 if(p == pe) goto end;
496 q = (const char *) memchr(p, '/', pe-p);
502 if(p == pe) goto end;
504 do { p++; } while(p[0] == '/');
505 if(p == pe) goto end;
506 if(p[0] != '.') break;
507 if(p+1 == pe) goto end;
508 if(p[1] != '/') break;
516 SvCUR_set(retval, o - SvPVX(retval));
520 MODULE = Cwd PACKAGE = Cwd
537 PERL_UNUSED_VAR(items);
538 { MY_CXT_CLONE; POPULATE_MY_CXT; }
549 /* fastcwd takes zero parameters: */
550 if (ix == 1 && items != 0)
551 croak_xs_usage(cv, "");
554 #ifndef INCOMPLETE_TAINTS
560 abs_path(pathsv=Nullsv)
565 char *const path = pathsv ? SvPV_nolen(pathsv) : (char *)".";
566 char buf[MAXPATHLEN];
570 Perl_rmsexpand(aTHX_ path, buf, NULL, 0)
572 bsd_realpath(path, buf)
575 sv_setpv_mg(TARG, buf);
580 sv_setsv(TARG, &PL_sv_undef);
582 XSprePUSH; PUSHs(TARG);
583 #ifndef INCOMPLETE_TAINTS
588 #if defined(WIN32) && !defined(UNDER_CE)
599 /* Drive 0 is the current drive, 1 is A:, 2 is B:, 3 is C: and so on. */
601 (items == 1 && (!SvOK(ST(0)) || (SvPOK(ST(0)) && !SvCUR(ST(0))))))
603 else if (items == 1 && SvPOK(ST(0)) && SvCUR(ST(0)) &&
604 isALPHA(SvPVX(ST(0))[0]))
605 drive = toUPPER(SvPVX(ST(0))[0]) - 'A' + 1;
607 croak("Usage: getdcwd(DRIVE)");
609 New(0,dir,MAXPATHLEN,char);
610 if (_getdcwd(drive, dir, MAXPATHLEN)) {
611 sv_setpv_mg(TARG, dir);
615 sv_setsv(TARG, &PL_sv_undef);
619 XSprePUSH; PUSHs(TARG);
620 #ifndef INCOMPLETE_TAINTS
627 MODULE = Cwd PACKAGE = File::Spec::Unix
630 canonpath(SV *self, SV *path = &PL_sv_undef, ...)
632 PERL_UNUSED_VAR(self);
633 RETVAL = unix_canonpath(path);
638 _fn_canonpath(SV *path = &PL_sv_undef, ...)
640 RETVAL = unix_canonpath(path);
645 catdir(SV *self, ...)
651 ST(items) = EMPTY_STRING_SV;
652 joined = sv_newmortal();
653 do_join(joined, SLASH_STRING_SV, &ST(0), &ST(items));
654 if(invocant_is_unix(self)) {
655 RETVAL = unix_canonpath(joined);
663 call_method("canonpath", G_SCALAR);
667 SvREFCNT_inc(RETVAL);
679 ST(items) = EMPTY_STRING_SV;
680 joined = sv_newmortal();
681 do_join(joined, SLASH_STRING_SV, &ST(-1), &ST(items));
682 RETVAL = unix_canonpath(joined);
687 catfile(SV *self, ...)
691 if(invocant_is_unix(self)) {
693 RETVAL = &PL_sv_undef;
695 SV *file = unix_canonpath(ST(items-1));
699 SV *dir = sv_newmortal();
701 ST(items-1) = EMPTY_STRING_SV;
702 do_join(dir, SLASH_STRING_SV, &ST(0), &ST(items-1));
703 RETVAL = unix_canonpath(dir);
704 if(SvCUR(RETVAL) == 0 || SvPVX(RETVAL)[SvCUR(RETVAL)-1] != '/')
705 sv_catsv(RETVAL, SLASH_STRING_SV);
706 sv_catsv(RETVAL, file);
715 PUSHs(items == 1 ? &PL_sv_undef : ST(items-1));
717 call_method("canonpath", G_SCALAR);
722 RETVAL = SvREFCNT_inc(file);
731 call_method("catdir", G_SCALAR);
736 need_slash = len == 0 || pv[len-1] != '/';
737 RETVAL = newSVsv(dir);
738 if(need_slash) sv_catsv(RETVAL, SLASH_STRING_SV);
739 sv_catsv(RETVAL, file);
751 RETVAL = &PL_sv_undef;
753 SV *file = unix_canonpath(ST(items-1));
757 SV *dir = sv_newmortal();
759 ST(items-1) = EMPTY_STRING_SV;
760 do_join(dir, SLASH_STRING_SV, &ST(-1), &ST(items-1));
761 RETVAL = unix_canonpath(dir);
762 if(SvCUR(RETVAL) == 0 || SvPVX(RETVAL)[SvCUR(RETVAL)-1] != '/')
763 sv_catsv(RETVAL, SLASH_STRING_SV);
764 sv_catsv(RETVAL, file);