3 * c-indentation-style: bsd
5 * indent-tabs-mode: nil
8 * ex: set ts=8 sts=4 sw=4 et:
11 #define PERL_NO_GET_CONTEXT
16 #define NEED_my_strlcpy
17 #define NEED_my_strlcat
24 /* The realpath() implementation from OpenBSD 3.9 to 4.2 (realpath.c 1.13)
25 * Renamed here to bsd_realpath() to avoid library conflicts.
29 * http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/2004-11/msg00979.html
30 * for the details of why the BSD license is compatible with the
31 * AL/GPL standard perl license.
35 * Copyright (c) 2003 Constantin S. Svintsoff <kostik@iclub.nsu.ru>
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
40 * 1. Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in the
44 * documentation and/or other materials provided with the distribution.
45 * 3. The names of the authors may not be used to endorse or promote
46 * products derived from this software without specific prior written
49 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS "AS IS" AND
50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62 /* OpenBSD system #includes removed since the Perl ones should do. --jhi */
70 * char *realpath(const char *path, char resolved[MAXPATHLEN]);
72 * Find the real name of path, by removing all ".", ".." and symlink
73 * components. Returns (resolved) on success, or (NULL) on failure,
74 * in which case the path which caused trouble is left in (resolved).
78 bsd_realpath(const char *path, char resolved[MAXPATHLEN])
81 size_t left_len, resolved_len;
84 char left[MAXPATHLEN], next_token[MAXPATHLEN];
94 left_len = my_strlcpy(left, path + 1, sizeof(left));
96 if (getcwd(resolved, MAXPATHLEN) == NULL) {
97 my_strlcpy(resolved, ".", MAXPATHLEN);
100 resolved_len = strlen(resolved);
101 left_len = my_strlcpy(left, path, sizeof(left));
103 if (left_len >= sizeof(left) || resolved_len >= MAXPATHLEN) {
104 errno = ENAMETOOLONG;
109 * Iterate over path components in 'left'.
111 while (left_len != 0) {
113 * Extract the next path component and adjust 'left'
116 p = strchr(left, '/');
117 s = p ? p : left + left_len;
118 if ((STRLEN)(s - left) >= (STRLEN)sizeof(next_token)) {
119 errno = ENAMETOOLONG;
122 memcpy(next_token, left, s - left);
123 next_token[s - left] = '\0';
124 left_len -= s - left;
126 memmove(left, s + 1, left_len + 1);
127 if (resolved[resolved_len - 1] != '/') {
128 if (resolved_len + 1 >= MAXPATHLEN) {
129 errno = ENAMETOOLONG;
132 resolved[resolved_len++] = '/';
133 resolved[resolved_len] = '\0';
135 if (next_token[0] == '\0')
137 else if (strcmp(next_token, ".") == 0)
139 else if (strcmp(next_token, "..") == 0) {
141 * Strip the last path component except when we have
144 if (resolved_len > 1) {
145 resolved[resolved_len - 1] = '\0';
146 q = strrchr(resolved, '/') + 1;
148 resolved_len = q - resolved;
154 * Append the next path component and lstat() it. If
155 * lstat() fails we still can return successfully if
156 * there are no more path components left.
158 resolved_len = my_strlcat(resolved, next_token, MAXPATHLEN);
159 if (resolved_len >= MAXPATHLEN) {
160 errno = ENAMETOOLONG;
163 #if defined(HAS_LSTAT) && defined(HAS_READLINK) && defined(HAS_SYMLINK)
166 if (lstat(resolved, &sb) != 0) {
167 if (errno == ENOENT && p == NULL) {
173 if (S_ISLNK(sb.st_mode)) {
175 char symlink[MAXPATHLEN];
177 if (symlinks++ > MAXSYMLINKS) {
181 slen = readlink(resolved, symlink, sizeof(symlink) - 1);
184 symlink[slen] = '\0';
185 if (symlink[0] == '/') {
188 } else if (resolved_len > 1) {
189 /* Strip the last path component. */
190 resolved[resolved_len - 1] = '\0';
191 q = strrchr(resolved, '/') + 1;
193 resolved_len = q - resolved;
197 * If there are any path components left, then
198 * append them to symlink. The result is placed
202 if (symlink[slen - 1] != '/') {
203 if ((STRLEN)(slen + 1) >= (STRLEN)sizeof(symlink)) {
204 errno = ENAMETOOLONG;
208 symlink[slen + 1] = 0;
210 left_len = my_strlcat(symlink, left, sizeof(symlink));
211 if (left_len >= sizeof(left)) {
212 errno = ENAMETOOLONG;
216 left_len = my_strlcpy(left, symlink, sizeof(left));
223 * Remove trailing slash except when the resolved pathname
226 if (resolved_len > 1 && resolved[resolved_len - 1] == '/')
227 resolved[resolved_len - 1] = '\0';
232 #ifndef SV_CWD_RETURN_UNDEF
233 #define SV_CWD_RETURN_UNDEF \
234 sv_setsv(sv, &PL_sv_undef); \
238 #ifndef OPpENTERSUB_HASTARG
239 #define OPpENTERSUB_HASTARG 32 /* Called from OP tree. */
243 #define dXSTARG SV * targ = ((PL_op->op_private & OPpENTERSUB_HASTARG) \
244 ? PAD_SV(PL_op->op_targ) : sv_newmortal())
248 #define XSprePUSH (sp = PL_stack_base + ax - 1)
252 #define SV_CWD_ISDOT(dp) \
253 (dp->d_name[0] == '.' && (dp->d_name[1] == '\0' || \
254 (dp->d_name[1] == '.' && dp->d_name[2] == '\0')))
258 /* Taken from perl 5.8's util.c */
259 #define getcwd_sv(a) Perl_getcwd_sv(aTHX_ a)
260 int Perl_getcwd_sv(pTHX_ SV *sv)
268 char buf[MAXPATHLEN];
270 /* Some getcwd()s automatically allocate a buffer of the given
271 * size from the heap if they are given a NULL buffer pointer.
272 * The problem is that this behaviour is not portable. */
273 if (getcwd(buf, sizeof(buf) - 1)) {
274 STRLEN len = strlen(buf);
275 sv_setpvn(sv, buf, len);
279 sv_setsv(sv, &PL_sv_undef);
287 int orig_cdev, orig_cino, cdev, cino, odev, oino, tdev, tino;
288 int namelen, pathlen=0;
292 (void)SvUPGRADE(sv, SVt_PV);
294 if (PerlLIO_lstat(".", &statbuf) < 0) {
298 orig_cdev = statbuf.st_dev;
299 orig_cino = statbuf.st_ino;
307 if (PerlDir_chdir("..") < 0) {
310 if (PerlLIO_stat(".", &statbuf) < 0) {
314 cdev = statbuf.st_dev;
315 cino = statbuf.st_ino;
317 if (odev == cdev && oino == cino) {
320 if (!(dir = PerlDir_open("."))) {
324 while ((dp = PerlDir_read(dir)) != NULL) {
326 namelen = dp->d_namlen;
328 namelen = strlen(dp->d_name);
331 if (SV_CWD_ISDOT(dp)) {
335 if (PerlLIO_lstat(dp->d_name, &statbuf) < 0) {
339 tdev = statbuf.st_dev;
340 tino = statbuf.st_ino;
341 if (tino == oino && tdev == odev) {
350 if (pathlen + namelen + 1 >= MAXPATHLEN) {
354 SvGROW(sv, pathlen + namelen + 1);
358 Move(SvPVX(sv), SvPVX(sv) + namelen + 1, pathlen, char);
361 /* prepend current directory to the front */
363 Move(dp->d_name, SvPVX(sv)+1, namelen, char);
364 pathlen += (namelen + 1);
369 if (PerlDir_close(dir) < 0) {
376 SvCUR_set(sv, pathlen);
380 if (PerlDir_chdir(SvPVX(sv)) < 0) {
384 if (PerlLIO_stat(".", &statbuf) < 0) {
388 cdev = statbuf.st_dev;
389 cino = statbuf.st_ino;
391 if (cdev != orig_cdev || cino != orig_cino) {
392 Perl_croak(aTHX_ "Unstable directory path, "
393 "current directory changed unexpectedly");
407 #if defined(START_MY_CXT) && defined(MY_CXT_CLONE)
408 # define USE_MY_CXT 1
410 # define USE_MY_CXT 0
414 # define MY_CXT_KEY "Cwd::_guts"XS_VERSION
416 SV *empty_string_sv, *slash_string_sv;
419 # define dUSE_MY_CXT dMY_CXT
420 # define EMPTY_STRING_SV MY_CXT.empty_string_sv
421 # define SLASH_STRING_SV MY_CXT.slash_string_sv
422 # define POPULATE_MY_CXT do { \
423 MY_CXT.empty_string_sv = newSVpvs(""); \
424 MY_CXT.slash_string_sv = newSVpvs("/"); \
427 # define dUSE_MY_CXT dNOOP
428 # define EMPTY_STRING_SV sv_2mortal(newSVpvs(""))
429 # define SLASH_STRING_SV sv_2mortal(newSVpvs("/"))
432 #define invocant_is_unix(i) THX_invocant_is_unix(aTHX_ i)
435 THX_invocant_is_unix(pTHX_ SV *invocant)
438 * This is used to enable optimisations that avoid method calls
439 * by knowing how they would resolve. False negatives, disabling
440 * the optimisation where it would actually behave correctly, are
443 return SvPOK(invocant) && SvCUR(invocant) == 16 &&
444 !memcmp(SvPVX(invocant), "File::Spec::Unix", 16);
447 #define unix_canonpath(p) THX_unix_canonpath(aTHX_ p)
450 THX_unix_canonpath(pTHX_ SV *path)
453 char const *p, *pe, *q;
458 if(!SvOK(path)) return &PL_sv_undef;
459 p = SvPV_nomg(path, plen);
460 if(plen == 0) return newSVpvs("");
462 retval = newSV(plen);
464 if(SvUTF8(path)) SvUTF8_on(retval);
467 if(DOUBLE_SLASHES_SPECIAL && p[0] == '/' && p[1] == '/' && p[2] != '/') {
468 q = (const char *) memchr(p+2, '/', pe-(p+2));
476 * The transformations performed here are:
477 * . squeeze multiple slashes
478 * . eliminate "." segments, except one if that's all there is
479 * . eliminate leading ".." segments
480 * . eliminate trailing slash, unless it's all there is
485 do { p++; } while(p[0] == '/');
486 if(p[0] == '.' && p[1] == '.' && (p+2 == pe || p[2] == '/')) {
488 /* advance past second "." next time round loop */
489 } else if(p[0] == '.' && (p+1 == pe || p[1] == '/')) {
490 /* advance past "." next time round loop */
495 } else if(p[0] == '.' && p[1] == '/') {
498 do { p++; } while(p[0] == '/');
499 } while(p[0] == '.' && p[1] == '/');
500 if(p == pe) *o++ = '.';
502 if(p == pe) goto end;
504 q = (const char *) memchr(p, '/', pe-p);
510 if(p == pe) goto end;
512 do { p++; } while(p[0] == '/');
513 if(p == pe) goto end;
514 if(p[0] != '.') break;
515 if(p+1 == pe) goto end;
516 if(p[1] != '/') break;
524 SvCUR_set(retval, o - SvPVX(retval));
528 MODULE = Cwd PACKAGE = Cwd
545 PERL_UNUSED_VAR(items);
546 { MY_CXT_CLONE; POPULATE_MY_CXT; }
557 /* fastcwd takes zero parameters: */
558 if (ix == 1 && items != 0)
559 croak_xs_usage(cv, "");
566 abs_path(pathsv=Nullsv)
571 char *const path = pathsv ? SvPV_nolen(pathsv) : (char *)".";
572 char buf[MAXPATHLEN];
576 Perl_rmsexpand(aTHX_ path, buf, NULL, 0)
578 bsd_realpath(path, buf)
581 sv_setpv_mg(TARG, buf);
586 sv_setsv(TARG, &PL_sv_undef);
588 XSprePUSH; PUSHs(TARG);
592 #if defined(WIN32) && !defined(UNDER_CE)
603 /* Drive 0 is the current drive, 1 is A:, 2 is B:, 3 is C: and so on. */
605 (items == 1 && (!SvOK(ST(0)) || (SvPOK(ST(0)) && !SvCUR(ST(0))))))
607 else if (items == 1 && SvPOK(ST(0)) && SvCUR(ST(0)) &&
608 isALPHA(SvPVX(ST(0))[0]))
609 drive = toUPPER(SvPVX(ST(0))[0]) - 'A' + 1;
611 croak("Usage: getdcwd(DRIVE)");
613 New(0,dir,MAXPATHLEN,char);
614 if (_getdcwd(drive, dir, MAXPATHLEN)) {
615 sv_setpv_mg(TARG, dir);
619 sv_setsv(TARG, &PL_sv_undef);
623 XSprePUSH; PUSHs(TARG);
629 MODULE = Cwd PACKAGE = File::Spec::Unix
632 canonpath(SV *self, SV *path = &PL_sv_undef, ...)
634 PERL_UNUSED_VAR(self);
635 RETVAL = unix_canonpath(path);
640 _fn_canonpath(SV *path = &PL_sv_undef, ...)
642 RETVAL = unix_canonpath(path);
647 catdir(SV *self, ...)
653 ST(items) = EMPTY_STRING_SV;
654 joined = sv_newmortal();
655 do_join(joined, SLASH_STRING_SV, &ST(0), &ST(items));
656 if(invocant_is_unix(self)) {
657 RETVAL = unix_canonpath(joined);
665 call_method("canonpath", G_SCALAR);
669 SvREFCNT_inc(RETVAL);
681 ST(items) = EMPTY_STRING_SV;
682 joined = sv_newmortal();
683 do_join(joined, SLASH_STRING_SV, &ST(-1), &ST(items));
684 RETVAL = unix_canonpath(joined);
689 catfile(SV *self, ...)
693 if(invocant_is_unix(self)) {
695 RETVAL = &PL_sv_undef;
697 SV *file = unix_canonpath(ST(items-1));
701 SV *dir = sv_newmortal();
703 ST(items-1) = EMPTY_STRING_SV;
704 do_join(dir, SLASH_STRING_SV, &ST(0), &ST(items-1));
705 RETVAL = unix_canonpath(dir);
706 if(SvCUR(RETVAL) == 0 || SvPVX(RETVAL)[SvCUR(RETVAL)-1] != '/')
707 sv_catsv(RETVAL, SLASH_STRING_SV);
708 sv_catsv(RETVAL, file);
717 PUSHs(items == 1 ? &PL_sv_undef : ST(items-1));
719 call_method("canonpath", G_SCALAR);
724 RETVAL = SvREFCNT_inc(file);
733 call_method("catdir", G_SCALAR);
738 need_slash = len == 0 || pv[len-1] != '/';
739 RETVAL = newSVsv(dir);
740 if(need_slash) sv_catsv(RETVAL, SLASH_STRING_SV);
741 sv_catsv(RETVAL, file);
753 RETVAL = &PL_sv_undef;
755 SV *file = unix_canonpath(ST(items-1));
759 SV *dir = sv_newmortal();
761 ST(items-1) = EMPTY_STRING_SV;
762 do_join(dir, SLASH_STRING_SV, &ST(-1), &ST(items-1));
763 RETVAL = unix_canonpath(dir);
764 if(SvCUR(RETVAL) == 0 || SvPVX(RETVAL)[SvCUR(RETVAL)-1] != '/')
765 sv_catsv(RETVAL, SLASH_STRING_SV);
766 sv_catsv(RETVAL, file);