2 * ex: set ts=8 sts=4 sw=4 et:
5 #define PERL_NO_GET_CONTEXT
10 #define NEED_my_strlcpy
11 #define NEED_my_strlcat
18 /* For special handling of os390 sysplexed systems */
19 #define SYSNAME "$SYSNAME"
20 #define SYSNAME_LEN (sizeof(SYSNAME) - 1)
22 /* The realpath() implementation from OpenBSD 3.9 to 4.2 (realpath.c 1.13)
23 * Renamed here to bsd_realpath() to avoid library conflicts.
27 * http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/2004-11/msg00979.html
28 * for the details of why the BSD license is compatible with the
29 * AL/GPL standard perl license.
33 * Copyright (c) 2003 Constantin S. Svintsoff <kostik@iclub.nsu.ru>
35 * Redistribution and use in source and binary forms, with or without
36 * modification, are permitted provided that the following conditions
38 * 1. Redistributions of source code must retain the above copyright
39 * notice, this list of conditions and the following disclaimer.
40 * 2. Redistributions in binary form must reproduce the above copyright
41 * notice, this list of conditions and the following disclaimer in the
42 * documentation and/or other materials provided with the distribution.
43 * 3. The names of the authors may not be used to endorse or promote
44 * products derived from this software without specific prior written
47 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS "AS IS" AND
48 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
51 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
52 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
53 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
54 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
55 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
56 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60 /* OpenBSD system #includes removed since the Perl ones should do. --jhi */
68 * char *realpath(const char *path, char resolved[MAXPATHLEN]);
70 * Find the real name of path, by removing all ".", ".." and symlink
71 * components. Returns (resolved) on success, or (NULL) on failure,
72 * in which case the path which caused trouble is left in (resolved).
76 bsd_realpath(const char *path, char resolved[MAXPATHLEN])
79 size_t remaining_len, resolved_len;
82 char remaining[MAXPATHLEN], next_token[MAXPATHLEN];
92 remaining_len = my_strlcpy(remaining, path + 1, sizeof(remaining));
94 if (getcwd(resolved, MAXPATHLEN) == NULL) {
95 my_strlcpy(resolved, ".", MAXPATHLEN);
98 resolved_len = strlen(resolved);
99 remaining_len = my_strlcpy(remaining, path, sizeof(remaining));
101 if (remaining_len >= sizeof(remaining) || resolved_len >= MAXPATHLEN) {
102 errno = ENAMETOOLONG;
107 * Iterate over path components in 'remaining'.
109 while (remaining_len != 0) {
112 * Extract the next path component and adjust 'remaining'
116 p = strchr(remaining, '/');
117 s = p ? p : remaining + remaining_len;
118 if ((STRLEN)(s - remaining) >= (STRLEN)sizeof(next_token)) {
119 errno = ENAMETOOLONG;
122 memcpy(next_token, remaining, s - remaining);
123 next_token[s - remaining] = '\0';
124 remaining_len -= s - remaining;
126 memmove(remaining, s + 1, remaining_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 # ifdef EBCDIC /* XXX Probably this should be only os390 */
186 /* Replace all instances of $SYSNAME/foo simply by /foo */
187 if (slen > SYSNAME_LEN + strlen(next_token)
188 && strnEQ(symlink, SYSNAME, SYSNAME_LEN)
189 && *(symlink + SYSNAME_LEN) == '/'
190 && strEQ(symlink + SYSNAME_LEN + 1, next_token))
195 if (symlink[0] == '/') {
198 } else if (resolved_len > 1) {
199 /* Strip the last path component. */
200 resolved[resolved_len - 1] = '\0';
201 q = strrchr(resolved, '/') + 1;
203 resolved_len = q - resolved;
207 * If there are any path components left, then
208 * append them to symlink. The result is placed
212 if (symlink[slen - 1] != '/') {
213 if ((STRLEN)(slen + 1) >= (STRLEN)sizeof(symlink)) {
214 errno = ENAMETOOLONG;
218 symlink[slen + 1] = 0;
220 remaining_len = my_strlcat(symlink, remaining, sizeof(symlink));
221 if (remaining_len >= sizeof(remaining)) {
222 errno = ENAMETOOLONG;
226 remaining_len = my_strlcpy(remaining, symlink, sizeof(remaining));
236 * Remove trailing slash except when the resolved pathname
239 if (resolved_len > 1 && resolved[resolved_len - 1] == '/')
240 resolved[resolved_len - 1] = '\0';
245 #ifndef SV_CWD_RETURN_UNDEF
246 #define SV_CWD_RETURN_UNDEF \
247 sv_setsv(sv, &PL_sv_undef); \
251 #ifndef OPpENTERSUB_HASTARG
252 #define OPpENTERSUB_HASTARG 32 /* Called from OP tree. */
256 #define dXSTARG SV * targ = ((PL_op->op_private & OPpENTERSUB_HASTARG) \
257 ? PAD_SV(PL_op->op_targ) : sv_newmortal())
261 #define XSprePUSH (sp = PL_stack_base + ax - 1)
265 #define SV_CWD_ISDOT(dp) \
266 (dp->d_name[0] == '.' && (dp->d_name[1] == '\0' || \
267 (dp->d_name[1] == '.' && dp->d_name[2] == '\0')))
271 /* Taken from perl 5.8's util.c */
272 #define getcwd_sv(a) Perl_getcwd_sv(aTHX_ a)
273 int Perl_getcwd_sv(pTHX_ SV *sv)
281 char buf[MAXPATHLEN];
283 /* Some getcwd()s automatically allocate a buffer of the given
284 * size from the heap if they are given a NULL buffer pointer.
285 * The problem is that this behaviour is not portable. */
286 if (getcwd(buf, sizeof(buf) - 1)) {
287 STRLEN len = strlen(buf);
288 sv_setpvn(sv, buf, len);
292 sv_setsv(sv, &PL_sv_undef);
300 int orig_cdev, orig_cino, cdev, cino, odev, oino, tdev, tino;
301 int namelen, pathlen=0;
305 (void)SvUPGRADE(sv, SVt_PV);
307 if (PerlLIO_lstat(".", &statbuf) < 0) {
311 orig_cdev = statbuf.st_dev;
312 orig_cino = statbuf.st_ino;
320 if (PerlDir_chdir("..") < 0) {
323 if (PerlLIO_stat(".", &statbuf) < 0) {
327 cdev = statbuf.st_dev;
328 cino = statbuf.st_ino;
330 if (odev == cdev && oino == cino) {
333 if (!(dir = PerlDir_open("."))) {
337 while ((dp = PerlDir_read(dir)) != NULL) {
339 namelen = dp->d_namlen;
341 namelen = strlen(dp->d_name);
344 if (SV_CWD_ISDOT(dp)) {
348 if (PerlLIO_lstat(dp->d_name, &statbuf) < 0) {
352 tdev = statbuf.st_dev;
353 tino = statbuf.st_ino;
354 if (tino == oino && tdev == odev) {
363 if (pathlen + namelen + 1 >= MAXPATHLEN) {
367 SvGROW(sv, pathlen + namelen + 1);
371 Move(SvPVX(sv), SvPVX(sv) + namelen + 1, pathlen, char);
374 /* prepend current directory to the front */
376 Move(dp->d_name, SvPVX(sv)+1, namelen, char);
377 pathlen += (namelen + 1);
382 if (PerlDir_close(dir) < 0) {
389 SvCUR_set(sv, pathlen);
393 if (PerlDir_chdir(SvPVX(sv)) < 0) {
397 if (PerlLIO_stat(".", &statbuf) < 0) {
401 cdev = statbuf.st_dev;
402 cino = statbuf.st_ino;
404 if (cdev != orig_cdev || cino != orig_cino) {
405 Perl_croak(aTHX_ "Unstable directory path, "
406 "current directory changed unexpectedly");
420 #if defined(START_MY_CXT) && defined(MY_CXT_CLONE)
421 # define USE_MY_CXT 1
423 # define USE_MY_CXT 0
427 # define MY_CXT_KEY "Cwd::_guts" XS_VERSION
429 SV *empty_string_sv, *slash_string_sv;
432 # define dUSE_MY_CXT dMY_CXT
433 # define EMPTY_STRING_SV MY_CXT.empty_string_sv
434 # define SLASH_STRING_SV MY_CXT.slash_string_sv
435 # define POPULATE_MY_CXT do { \
436 MY_CXT.empty_string_sv = newSVpvs(""); \
437 MY_CXT.slash_string_sv = newSVpvs("/"); \
440 # define dUSE_MY_CXT dNOOP
441 # define EMPTY_STRING_SV sv_2mortal(newSVpvs(""))
442 # define SLASH_STRING_SV sv_2mortal(newSVpvs("/"))
445 #define invocant_is_unix(i) THX_invocant_is_unix(aTHX_ i)
448 THX_invocant_is_unix(pTHX_ SV *invocant)
451 * This is used to enable optimisations that avoid method calls
452 * by knowing how they would resolve. False negatives, disabling
453 * the optimisation where it would actually behave correctly, are
456 return SvPOK(invocant) && SvCUR(invocant) == 16 &&
457 !memcmp(SvPVX(invocant), "File::Spec::Unix", 16);
460 #define unix_canonpath(p) THX_unix_canonpath(aTHX_ p)
463 THX_unix_canonpath(pTHX_ SV *path)
466 char const *p, *pe, *q;
471 if(!SvOK(path)) return &PL_sv_undef;
472 p = SvPV_nomg(path, plen);
473 if(plen == 0) return newSVpvs("");
475 retval = newSV(plen);
477 if(SvUTF8(path)) SvUTF8_on(retval);
480 if(DOUBLE_SLASHES_SPECIAL && p[0] == '/' && p[1] == '/' && p[2] != '/') {
481 q = (const char *) memchr(p+2, '/', pe-(p+2));
489 * The transformations performed here are:
490 * . squeeze multiple slashes
491 * . eliminate "." segments, except one if that's all there is
492 * . eliminate leading ".." segments
493 * . eliminate trailing slash, unless it's all there is
498 do { p++; } while(p[0] == '/');
499 if(p[0] == '.' && p[1] == '.' && (p+2 == pe || p[2] == '/')) {
501 /* advance past second "." next time round loop */
502 } else if(p[0] == '.' && (p+1 == pe || p[1] == '/')) {
503 /* advance past "." next time round loop */
508 } else if(p[0] == '.' && p[1] == '/') {
511 do { p++; } while(p[0] == '/');
512 } while(p[0] == '.' && p[1] == '/');
513 if(p == pe) *o++ = '.';
515 if(p == pe) goto end;
517 q = (const char *) memchr(p, '/', pe-p);
523 if(p == pe) goto end;
525 do { p++; } while(p[0] == '/');
526 if(p == pe) goto end;
527 if(p[0] != '.') break;
528 if(p+1 == pe) goto end;
529 if(p[1] != '/') break;
537 SvCUR_set(retval, o - SvPVX(retval));
542 MODULE = Cwd PACKAGE = Cwd
559 PERL_UNUSED_VAR(items);
560 { MY_CXT_CLONE; POPULATE_MY_CXT; }
571 /* fastcwd takes zero parameters: */
572 if (ix == 1 && items != 0)
573 croak_xs_usage(cv, "");
580 abs_path(pathsv=Nullsv)
585 char *const path = pathsv ? SvPV_nolen(pathsv) : (char *)".";
586 char buf[MAXPATHLEN];
590 Perl_rmsexpand(aTHX_ path, buf, NULL, 0)
592 bsd_realpath(path, buf)
595 sv_setpv_mg(TARG, buf);
600 sv_setsv(TARG, &PL_sv_undef);
602 XSprePUSH; PUSHs(TARG);
606 #if defined(WIN32) && !defined(UNDER_CE)
617 /* Drive 0 is the current drive, 1 is A:, 2 is B:, 3 is C: and so on. */
619 (items == 1 && (!SvOK(ST(0)) || (SvPOK(ST(0)) && !SvCUR(ST(0))))))
621 else if (items == 1 && SvPOK(ST(0)) && SvCUR(ST(0)) &&
622 isALPHA(SvPVX(ST(0))[0]))
623 drive = toUPPER(SvPVX(ST(0))[0]) - 'A' + 1;
625 croak("Usage: getdcwd(DRIVE)");
627 New(0,dir,MAXPATHLEN,char);
628 if (_getdcwd(drive, dir, MAXPATHLEN)) {
629 sv_setpv_mg(TARG, dir);
633 sv_setsv(TARG, &PL_sv_undef);
637 XSprePUSH; PUSHs(TARG);
643 MODULE = Cwd PACKAGE = File::Spec::Unix
646 canonpath(SV *self, SV *path = &PL_sv_undef, ...)
648 PERL_UNUSED_VAR(self);
649 RETVAL = unix_canonpath(path);
654 _fn_canonpath(SV *path = &PL_sv_undef, ...)
656 RETVAL = unix_canonpath(path);
661 catdir(SV *self, ...)
667 ST(items) = EMPTY_STRING_SV;
668 joined = sv_newmortal();
669 do_join(joined, SLASH_STRING_SV, &ST(0), &ST(items));
670 if(invocant_is_unix(self)) {
671 RETVAL = unix_canonpath(joined);
679 call_method("canonpath", G_SCALAR);
683 SvREFCNT_inc(RETVAL);
695 ST(items) = EMPTY_STRING_SV;
696 joined = sv_newmortal();
697 do_join(joined, SLASH_STRING_SV, &ST(-1), &ST(items));
698 RETVAL = unix_canonpath(joined);
703 catfile(SV *self, ...)
707 if(invocant_is_unix(self)) {
709 RETVAL = &PL_sv_undef;
711 SV *file = unix_canonpath(ST(items-1));
715 SV *dir = sv_newmortal();
717 ST(items-1) = EMPTY_STRING_SV;
718 do_join(dir, SLASH_STRING_SV, &ST(0), &ST(items-1));
719 RETVAL = unix_canonpath(dir);
720 if(SvCUR(RETVAL) == 0 || SvPVX(RETVAL)[SvCUR(RETVAL)-1] != '/')
721 sv_catsv(RETVAL, SLASH_STRING_SV);
722 sv_catsv(RETVAL, file);
731 PUSHs(items == 1 ? &PL_sv_undef : ST(items-1));
733 call_method("canonpath", G_SCALAR);
738 RETVAL = SvREFCNT_inc(file);
747 call_method("catdir", G_SCALAR);
752 need_slash = len == 0 || pv[len-1] != '/';
753 RETVAL = newSVsv(dir);
754 if(need_slash) sv_catsv(RETVAL, SLASH_STRING_SV);
755 sv_catsv(RETVAL, file);
767 RETVAL = &PL_sv_undef;
769 SV *file = unix_canonpath(ST(items-1));
773 SV *dir = sv_newmortal();
775 ST(items-1) = EMPTY_STRING_SV;
776 do_join(dir, SLASH_STRING_SV, &ST(-1), &ST(items-1));
777 RETVAL = unix_canonpath(dir);
778 if(SvCUR(RETVAL) == 0 || SvPVX(RETVAL)[SvCUR(RETVAL)-1] != '/')
779 sv_catsv(RETVAL, SLASH_STRING_SV);
780 sv_catsv(RETVAL, file);