1N/A#include "EXTERN.h"
1N/A#include "perl.h"
1N/A#include "XSUB.h"
1N/A
1N/A#ifdef I_UNISTD
1N/A# include <unistd.h>
1N/A#endif
1N/A
1N/A/* The realpath() implementation from OpenBSD 2.9 (realpath.c 1.4)
1N/A * Renamed here to bsd_realpath() to avoid library conflicts.
1N/A * --jhi 2000-06-20 */
1N/A
1N/A/*
1N/A * Copyright (c) 1994
1N/A * The Regents of the University of California. All rights reserved.
1N/A *
1N/A * This code is derived from software contributed to Berkeley by
1N/A * Jan-Simon Pendry.
1N/A *
1N/A * Redistribution and use in source and binary forms, with or without
1N/A * modification, are permitted provided that the following conditions
1N/A * are met:
1N/A * 1. Redistributions of source code must retain the above copyright
1N/A * notice, this list of conditions and the following disclaimer.
1N/A * 2. Redistributions in binary form must reproduce the above copyright
1N/A * notice, this list of conditions and the following disclaimer in the
1N/A * documentation and/or other materials provided with the distribution.
1N/A * 3. All advertising materials mentioning features or use of this software
1N/A * must display the following acknowledgement:
1N/A * This product includes software developed by the University of
1N/A * California, Berkeley and its contributors.
1N/A * 4. Neither the name of the University nor the names of its contributors
1N/A * may be used to endorse or promote products derived from this software
1N/A * without specific prior written permission.
1N/A *
1N/A * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
1N/A * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1N/A * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1N/A * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
1N/A * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1N/A * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1N/A * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1N/A * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
1N/A * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
1N/A * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
1N/A * SUCH DAMAGE.
1N/A */
1N/A
1N/A#if defined(LIBC_SCCS) && !defined(lint)
1N/Astatic char *rcsid = "$OpenBSD: realpath.c,v 1.4 1998/05/18 09:55:19 deraadt Exp $";
1N/A#endif /* LIBC_SCCS and not lint */
1N/A
1N/A/* OpenBSD system #includes removed since the Perl ones should do. --jhi */
1N/A
1N/A#ifndef MAXSYMLINKS
1N/A#define MAXSYMLINKS 8
1N/A#endif
1N/A
1N/A/*
1N/A * char *realpath(const char *path, char resolved_path[MAXPATHLEN]);
1N/A *
1N/A * Find the real name of path, by removing all ".", ".." and symlink
1N/A * components. Returns (resolved) on success, or (NULL) on failure,
1N/A * in which case the path which caused trouble is left in (resolved).
1N/A */
1N/Astatic
1N/Achar *
1N/Absd_realpath(path, resolved)
1N/A const char *path;
1N/A char *resolved;
1N/A{
1N/A#ifdef VMS
1N/A dTHX;
1N/A return Perl_rmsexpand(aTHX_ (char*)path, resolved, NULL, 0);
1N/A#else
1N/A int rootd, serrno;
1N/A char *p, *q, wbuf[MAXPATHLEN];
1N/A int symlinks = 0;
1N/A
1N/A /* Save the starting point. */
1N/A#ifdef HAS_FCHDIR
1N/A int fd;
1N/A
1N/A if ((fd = open(".", O_RDONLY)) < 0) {
1N/A (void)strcpy(resolved, ".");
1N/A return (NULL);
1N/A }
1N/A#else
1N/A char wd[MAXPATHLEN];
1N/A
1N/A if (getcwd(wd, MAXPATHLEN - 1) == NULL) {
1N/A (void)strcpy(resolved, ".");
1N/A return (NULL);
1N/A }
1N/A#endif
1N/A
1N/A /*
1N/A * Find the dirname and basename from the path to be resolved.
1N/A * Change directory to the dirname component.
1N/A * lstat the basename part.
1N/A * if it is a symlink, read in the value and loop.
1N/A * if it is a directory, then change to that directory.
1N/A * get the current directory name and append the basename.
1N/A */
1N/A (void)strncpy(resolved, path, MAXPATHLEN - 1);
1N/A resolved[MAXPATHLEN - 1] = '\0';
1N/Aloop:
1N/A q = strrchr(resolved, '/');
1N/A if (q != NULL) {
1N/A p = q + 1;
1N/A if (q == resolved)
1N/A q = "/";
1N/A else {
1N/A do {
1N/A --q;
1N/A } while (q > resolved && *q == '/');
1N/A q[1] = '\0';
1N/A q = resolved;
1N/A }
1N/A if (chdir(q) < 0)
1N/A goto err1;
1N/A } else
1N/A p = resolved;
1N/A
1N/A#if defined(HAS_LSTAT) && defined(HAS_READLINK) && defined(HAS_SYMLINK)
1N/A {
1N/A struct stat sb;
1N/A /* Deal with the last component. */
1N/A if (lstat(p, &sb) == 0) {
1N/A if (S_ISLNK(sb.st_mode)) {
1N/A int n;
1N/A if (++symlinks > MAXSYMLINKS) {
1N/A errno = ELOOP;
1N/A goto err1;
1N/A }
1N/A n = readlink(p, resolved, MAXPATHLEN-1);
1N/A if (n < 0)
1N/A goto err1;
1N/A resolved[n] = '\0';
1N/A goto loop;
1N/A }
1N/A if (S_ISDIR(sb.st_mode)) {
1N/A if (chdir(p) < 0)
1N/A goto err1;
1N/A p = "";
1N/A }
1N/A }
1N/A }
1N/A#endif
1N/A
1N/A /*
1N/A * Save the last component name and get the full pathname of
1N/A * the current directory.
1N/A */
1N/A (void)strcpy(wbuf, p);
1N/A if (getcwd(resolved, MAXPATHLEN) == 0)
1N/A goto err1;
1N/A
1N/A /*
1N/A * Join the two strings together, ensuring that the right thing
1N/A * happens if the last component is empty, or the dirname is root.
1N/A */
1N/A if (resolved[0] == '/' && resolved[1] == '\0')
1N/A rootd = 1;
1N/A else
1N/A rootd = 0;
1N/A
1N/A if (*wbuf) {
1N/A if (strlen(resolved) + strlen(wbuf) + (1 - rootd) + 1 > MAXPATHLEN) {
1N/A errno = ENAMETOOLONG;
1N/A goto err1;
1N/A }
1N/A if (rootd == 0)
1N/A (void)strcat(resolved, "/");
1N/A (void)strcat(resolved, wbuf);
1N/A }
1N/A
1N/A /* Go back to where we came from. */
1N/A#ifdef HAS_FCHDIR
1N/A if (fchdir(fd) < 0) {
1N/A serrno = errno;
1N/A goto err2;
1N/A }
1N/A#else
1N/A if (chdir(wd) < 0) {
1N/A serrno = errno;
1N/A goto err2;
1N/A }
1N/A#endif
1N/A
1N/A /* It's okay if the close fails, what's an fd more or less? */
1N/A#ifdef HAS_FCHDIR
1N/A (void)close(fd);
1N/A#endif
1N/A return (resolved);
1N/A
1N/Aerr1: serrno = errno;
1N/A#ifdef HAS_FCHDIR
1N/A (void)fchdir(fd);
1N/A#else
1N/A (void)chdir(wd);
1N/A#endif
1N/A
1N/Aerr2:
1N/A#ifdef HAS_FCHDIR
1N/A (void)close(fd);
1N/A#endif
1N/A errno = serrno;
1N/A return (NULL);
1N/A#endif
1N/A}
1N/A
1N/A#ifndef getcwd_sv
1N/A/* Taken from perl 5.8's util.c */
1N/Aint getcwd_sv(pTHX_ register SV *sv)
1N/A{
1N/A#ifndef PERL_MICRO
1N/A
1N/A#ifndef INCOMPLETE_TAINTS
1N/A SvTAINTED_on(sv);
1N/A#endif
1N/A
1N/A#ifdef HAS_GETCWD
1N/A {
1N/A char buf[MAXPATHLEN];
1N/A
1N/A /* Some getcwd()s automatically allocate a buffer of the given
1N/A * size from the heap if they are given a NULL buffer pointer.
1N/A * The problem is that this behaviour is not portable. */
1N/A if (getcwd(buf, sizeof(buf) - 1)) {
1N/A STRLEN len = strlen(buf);
1N/A sv_setpvn(sv, buf, len);
1N/A return TRUE;
1N/A }
1N/A else {
1N/A sv_setsv(sv, &PL_sv_undef);
1N/A return FALSE;
1N/A }
1N/A }
1N/A
1N/A#else
1N/A
1N/A Stat_t statbuf;
1N/A int orig_cdev, orig_cino, cdev, cino, odev, oino, tdev, tino;
1N/A int namelen, pathlen=0;
1N/A DIR *dir;
1N/A Direntry_t *dp;
1N/A
1N/A (void)SvUPGRADE(sv, SVt_PV);
1N/A
1N/A if (PerlLIO_lstat(".", &statbuf) < 0) {
1N/A SV_CWD_RETURN_UNDEF;
1N/A }
1N/A
1N/A orig_cdev = statbuf.st_dev;
1N/A orig_cino = statbuf.st_ino;
1N/A cdev = orig_cdev;
1N/A cino = orig_cino;
1N/A
1N/A for (;;) {
1N/A odev = cdev;
1N/A oino = cino;
1N/A
1N/A if (PerlDir_chdir("..") < 0) {
1N/A SV_CWD_RETURN_UNDEF;
1N/A }
1N/A if (PerlLIO_stat(".", &statbuf) < 0) {
1N/A SV_CWD_RETURN_UNDEF;
1N/A }
1N/A
1N/A cdev = statbuf.st_dev;
1N/A cino = statbuf.st_ino;
1N/A
1N/A if (odev == cdev && oino == cino) {
1N/A break;
1N/A }
1N/A if (!(dir = PerlDir_open("."))) {
1N/A SV_CWD_RETURN_UNDEF;
1N/A }
1N/A
1N/A while ((dp = PerlDir_read(dir)) != NULL) {
1N/A#ifdef DIRNAMLEN
1N/A namelen = dp->d_namlen;
1N/A#else
1N/A namelen = strlen(dp->d_name);
1N/A#endif
1N/A /* skip . and .. */
1N/A if (SV_CWD_ISDOT(dp)) {
1N/A continue;
1N/A }
1N/A
1N/A if (PerlLIO_lstat(dp->d_name, &statbuf) < 0) {
1N/A SV_CWD_RETURN_UNDEF;
1N/A }
1N/A
1N/A tdev = statbuf.st_dev;
1N/A tino = statbuf.st_ino;
1N/A if (tino == oino && tdev == odev) {
1N/A break;
1N/A }
1N/A }
1N/A
1N/A if (!dp) {
1N/A SV_CWD_RETURN_UNDEF;
1N/A }
1N/A
1N/A if (pathlen + namelen + 1 >= MAXPATHLEN) {
1N/A SV_CWD_RETURN_UNDEF;
1N/A }
1N/A
1N/A SvGROW(sv, pathlen + namelen + 1);
1N/A
1N/A if (pathlen) {
1N/A /* shift down */
1N/A Move(SvPVX(sv), SvPVX(sv) + namelen + 1, pathlen, char);
1N/A }
1N/A
1N/A /* prepend current directory to the front */
1N/A *SvPVX(sv) = '/';
1N/A Move(dp->d_name, SvPVX(sv)+1, namelen, char);
1N/A pathlen += (namelen + 1);
1N/A
1N/A#ifdef VOID_CLOSEDIR
1N/A PerlDir_close(dir);
1N/A#else
1N/A if (PerlDir_close(dir) < 0) {
1N/A SV_CWD_RETURN_UNDEF;
1N/A }
1N/A#endif
1N/A }
1N/A
1N/A if (pathlen) {
1N/A SvCUR_set(sv, pathlen);
1N/A *SvEND(sv) = '\0';
1N/A SvPOK_only(sv);
1N/A
1N/A if (PerlDir_chdir(SvPVX(sv)) < 0) {
1N/A SV_CWD_RETURN_UNDEF;
1N/A }
1N/A }
1N/A if (PerlLIO_stat(".", &statbuf) < 0) {
1N/A SV_CWD_RETURN_UNDEF;
1N/A }
1N/A
1N/A cdev = statbuf.st_dev;
1N/A cino = statbuf.st_ino;
1N/A
1N/A if (cdev != orig_cdev || cino != orig_cino) {
1N/A Perl_croak(aTHX_ "Unstable directory path, "
1N/A "current directory changed unexpectedly");
1N/A }
1N/A
1N/A return TRUE;
1N/A#endif
1N/A
1N/A#else
1N/A return FALSE;
1N/A#endif
1N/A}
1N/A
1N/A#endif
1N/A
1N/A
1N/AMODULE = Cwd PACKAGE = Cwd
1N/A
1N/APROTOTYPES: ENABLE
1N/A
1N/Avoid
1N/Afastcwd()
1N/APROTOTYPE: DISABLE
1N/APPCODE:
1N/A{
1N/A dXSTARG;
1N/A getcwd_sv(TARG);
1N/A XSprePUSH; PUSHTARG;
1N/A#ifndef INCOMPLETE_TAINTS
1N/A SvTAINTED_on(TARG);
1N/A#endif
1N/A}
1N/A
1N/Avoid
1N/Aabs_path(pathsv=Nullsv)
1N/A SV *pathsv
1N/APPCODE:
1N/A{
1N/A dXSTARG;
1N/A char *path;
1N/A char buf[MAXPATHLEN];
1N/A
1N/A path = pathsv ? SvPV_nolen(pathsv) : ".";
1N/A
1N/A if (bsd_realpath(path, buf)) {
1N/A sv_setpvn(TARG, buf, strlen(buf));
1N/A SvPOK_only(TARG);
1N/A SvTAINTED_on(TARG);
1N/A }
1N/A else
1N/A sv_setsv(TARG, &PL_sv_undef);
1N/A
1N/A XSprePUSH; PUSHTARG;
1N/A#ifndef INCOMPLETE_TAINTS
1N/A SvTAINTED_on(TARG);
1N/A#endif
1N/A}