4 #define NEED_sv_2pv_nolen
11 /* The realpath() implementation from OpenBSD 2.9 (realpath.c 1.4)
12 * Renamed here to bsd_realpath() to avoid library conflicts.
17 * http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/2004-11/msg00979.html
18 * for the details of why the BSD license is compatible with the
19 * AL/GPL standard perl license.
24 * The Regents of the University of California. All rights reserved.
26 * This code is derived from software contributed to Berkeley by
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions
32 * 1. Redistributions of source code must retain the above copyright
33 * notice, this list of conditions and the following disclaimer.
34 * 2. Redistributions in binary form must reproduce the above copyright
35 * notice, this list of conditions and the following disclaimer in the
36 * documentation and/or other materials provided with the distribution.
37 * 3. Neither the name of the University nor the names of its contributors
38 * may be used to endorse or promote products derived from this software
39 * without specific prior written permission.
41 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
54 #if defined(LIBC_SCCS) && !defined(lint)
55 static char *rcsid = "$OpenBSD: realpath.c,v 1.4 1998/05/18 09:55:19 deraadt Exp $";
56 #endif /* LIBC_SCCS and not lint */
58 /* OpenBSD system #includes removed since the Perl ones should do. --jhi */
65 * char *realpath(const char *path, char resolved_path[MAXPATHLEN]);
67 * Find the real name of path, by removing all ".", ".." and symlink
68 * components. Returns (resolved) on success, or (NULL) on failure,
69 * in which case the path which caused trouble is left in (resolved).
73 bsd_realpath(const char *path, char *resolved)
77 return Perl_rmsexpand(aTHX_ (char*)path, resolved, NULL, 0);
80 char *p, *q, wbuf[MAXPATHLEN];
83 /* Save the starting point. */
87 if ((fd = open(".", O_RDONLY)) < 0) {
88 (void)strcpy(resolved, ".");
94 if (getcwd(wd, MAXPATHLEN - 1) == NULL) {
95 (void)strcpy(resolved, ".");
101 * Find the dirname and basename from the path to be resolved.
102 * Change directory to the dirname component.
103 * lstat the basename part.
104 * if it is a symlink, read in the value and loop.
105 * if it is a directory, then change to that directory.
106 * get the current directory name and append the basename.
108 (void)strncpy(resolved, path, MAXPATHLEN - 1);
109 resolved[MAXPATHLEN - 1] = '\0';
111 q = strrchr(resolved, '/');
119 } while (q > resolved && *q == '/');
128 #if defined(HAS_LSTAT) && defined(HAS_READLINK) && defined(HAS_SYMLINK)
131 /* Deal with the last component. */
132 if (lstat(p, &sb) == 0) {
133 if (S_ISLNK(sb.st_mode)) {
135 if (++symlinks > MAXSYMLINKS) {
139 n = readlink(p, resolved, MAXPATHLEN-1);
145 if (S_ISDIR(sb.st_mode)) {
155 * Save the last component name and get the full pathname of
156 * the current directory.
158 (void)strcpy(wbuf, p);
159 if (getcwd(resolved, MAXPATHLEN) == 0)
163 * Join the two strings together, ensuring that the right thing
164 * happens if the last component is empty, or the dirname is root.
166 if (resolved[0] == '/' && resolved[1] == '\0')
172 if (strlen(resolved) + strlen(wbuf) + (1 - rootd) + 1 > MAXPATHLEN) {
173 errno = ENAMETOOLONG;
177 (void)strcat(resolved, "/");
178 (void)strcat(resolved, wbuf);
181 /* Go back to where we came from. */
183 if (fchdir(fd) < 0) {
194 /* It's okay if the close fails, what's an fd more or less? */
200 err1: serrno = errno;
216 #ifndef SV_CWD_RETURN_UNDEF
217 #define SV_CWD_RETURN_UNDEF \
218 sv_setsv(sv, &PL_sv_undef); \
222 #ifndef OPpENTERSUB_HASTARG
223 #define OPpENTERSUB_HASTARG 32 /* Called from OP tree. */
227 #define dXSTARG SV * targ = ((PL_op->op_private & OPpENTERSUB_HASTARG) \
228 ? PAD_SV(PL_op->op_targ) : sv_newmortal())
232 #define XSprePUSH (sp = PL_stack_base + ax - 1)
236 #define SV_CWD_ISDOT(dp) \
237 (dp->d_name[0] == '.' && (dp->d_name[1] == '\0' || \
238 (dp->d_name[1] == '.' && dp->d_name[2] == '\0')))
242 /* Taken from perl 5.8's util.c */
243 #define getcwd_sv(a) Perl_getcwd_sv(aTHX_ a)
244 int Perl_getcwd_sv(pTHX_ register SV *sv)
248 #ifndef INCOMPLETE_TAINTS
254 char buf[MAXPATHLEN];
256 /* Some getcwd()s automatically allocate a buffer of the given
257 * size from the heap if they are given a NULL buffer pointer.
258 * The problem is that this behaviour is not portable. */
259 if (getcwd(buf, sizeof(buf) - 1)) {
260 STRLEN len = strlen(buf);
261 sv_setpvn(sv, buf, len);
265 sv_setsv(sv, &PL_sv_undef);
273 int orig_cdev, orig_cino, cdev, cino, odev, oino, tdev, tino;
274 int namelen, pathlen=0;
278 (void)SvUPGRADE(sv, SVt_PV);
280 if (PerlLIO_lstat(".", &statbuf) < 0) {
284 orig_cdev = statbuf.st_dev;
285 orig_cino = statbuf.st_ino;
293 if (PerlDir_chdir("..") < 0) {
296 if (PerlLIO_stat(".", &statbuf) < 0) {
300 cdev = statbuf.st_dev;
301 cino = statbuf.st_ino;
303 if (odev == cdev && oino == cino) {
306 if (!(dir = PerlDir_open("."))) {
310 while ((dp = PerlDir_read(dir)) != NULL) {
312 namelen = dp->d_namlen;
314 namelen = strlen(dp->d_name);
317 if (SV_CWD_ISDOT(dp)) {
321 if (PerlLIO_lstat(dp->d_name, &statbuf) < 0) {
325 tdev = statbuf.st_dev;
326 tino = statbuf.st_ino;
327 if (tino == oino && tdev == odev) {
336 if (pathlen + namelen + 1 >= MAXPATHLEN) {
340 SvGROW(sv, pathlen + namelen + 1);
344 Move(SvPVX(sv), SvPVX(sv) + namelen + 1, pathlen, char);
347 /* prepend current directory to the front */
349 Move(dp->d_name, SvPVX(sv)+1, namelen, char);
350 pathlen += (namelen + 1);
355 if (PerlDir_close(dir) < 0) {
362 SvCUR_set(sv, pathlen);
366 if (PerlDir_chdir(SvPVX(sv)) < 0) {
370 if (PerlLIO_stat(".", &statbuf) < 0) {
374 cdev = statbuf.st_dev;
375 cino = statbuf.st_ino;
377 if (cdev != orig_cdev || cino != orig_cino) {
378 Perl_croak(aTHX_ "Unstable directory path, "
379 "current directory changed unexpectedly");
393 MODULE = Cwd PACKAGE = Cwd
405 #ifndef INCOMPLETE_TAINTS
411 abs_path(pathsv=Nullsv)
418 char buf[MAXPATHLEN];
420 path = pathsv ? SvPV_nolen(pathsv) : ".";
422 if (bsd_realpath(path, buf)) {
423 sv_setpvn(TARG, buf, strlen(buf));
428 sv_setsv(TARG, &PL_sv_undef);
431 #ifndef INCOMPLETE_TAINTS
446 /* Drive 0 is the current drive, 1 is A:, 2 is B:, 3 is C: and so on. */
448 (items == 1 && (!SvOK(ST(0)) || (SvPOK(ST(0)) && !SvCUR(ST(0))))))
450 else if (items == 1 && SvPOK(ST(0)) && SvCUR(ST(0)) &&
451 isALPHA(SvPVX(ST(0))[0]))
452 drive = toUPPER(SvPVX(ST(0))[0]) - 'A' + 1;
454 croak("Usage: getdcwd(DRIVE)");
456 New(0,dir,MAXPATHLEN,char);
457 if (_getdcwd(drive, dir, MAXPATHLEN)) {
458 sv_setpvn(TARG, dir, strlen(dir));
462 sv_setsv(TARG, &PL_sv_undef);
467 #ifndef INCOMPLETE_TAINTS