5 # define NEED_my_strlcpy
6 # 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 */
59 * char *realpath(const char *path, char resolved[MAXPATHLEN]);
61 * Find the real name of path, by removing all ".", ".." and symlink
62 * components. Returns (resolved) on success, or (NULL) on failure,
63 * in which case the path which caused trouble is left in (resolved).
67 bsd_realpath(const char *path, char *resolved)
71 return Perl_rmsexpand(aTHX_ (char*)path, resolved, NULL, 0);
75 char *q, wbuf[MAXPATHLEN];
78 /* Save the starting point. */
82 if ((fd = open(".", O_RDONLY)) < 0) {
83 (void)strcpy(resolved, ".");
89 if (getcwd(wd, MAXPATHLEN - 1) == NULL) {
90 (void)strcpy(resolved, ".");
96 * Find the dirname and basename from the path to be resolved.
97 * Change directory to the dirname component.
98 * lstat the basename part.
99 * if it is a symlink, read in the value and loop.
100 * if it is a directory, then change to that directory.
101 * get the current directory name and append the basename.
103 (void)strncpy(resolved, path, MAXPATHLEN - 1);
104 resolved[MAXPATHLEN - 1] = '\0';
106 q = strrchr(resolved, '/');
115 } while (q > resolved && *q == '/');
124 #if defined(HAS_LSTAT) && defined(HAS_READLINK) && defined(HAS_SYMLINK)
127 /* Deal with the last component. */
128 if (lstat(p, &sb) == 0) {
129 if (S_ISLNK(sb.st_mode)) {
131 if (++symlinks > MAXSYMLINKS) {
135 n = readlink(p, resolved, MAXPATHLEN-1);
141 if (S_ISDIR(sb.st_mode)) {
151 * Save the last component name and get the full pathname of
152 * the current directory.
154 (void)strcpy(wbuf, p);
155 if (getcwd(resolved, MAXPATHLEN) == 0)
159 * Join the two strings together, ensuring that the right thing
160 * happens if the last component is empty, or the dirname is root.
162 if (resolved[0] == '/' && resolved[1] == '\0')
168 if (strlen(resolved) + strlen(wbuf) + (1 - rootd) + 1 > MAXPATHLEN) {
169 errno = ENAMETOOLONG;
173 (void)strcat(resolved, "/");
174 (void)strcat(resolved, wbuf);
177 /* Go back to where we came from. */
179 if (fchdir(fd) < 0) {
190 /* It's okay if the close fails, what's an fd more or less? */
196 err1: serrno = errno;
212 #ifndef SV_CWD_RETURN_UNDEF
213 #define SV_CWD_RETURN_UNDEF \
214 sv_setsv(sv, &PL_sv_undef); \
218 #ifndef OPpENTERSUB_HASTARG
219 #define OPpENTERSUB_HASTARG 32 /* Called from OP tree. */
223 #define dXSTARG SV * targ = ((PL_op->op_private & OPpENTERSUB_HASTARG) \
224 ? PAD_SV(PL_op->op_targ) : sv_newmortal())
228 #define XSprePUSH (sp = PL_stack_base + ax - 1)
232 #define SV_CWD_ISDOT(dp) \
233 (dp->d_name[0] == '.' && (dp->d_name[1] == '\0' || \
234 (dp->d_name[1] == '.' && dp->d_name[2] == '\0')))
238 /* Taken from perl 5.8's util.c */
239 #define getcwd_sv(a) Perl_getcwd_sv(aTHX_ a)
240 int Perl_getcwd_sv(pTHX_ register SV *sv)
244 #ifndef INCOMPLETE_TAINTS
250 char buf[MAXPATHLEN];
252 /* Some getcwd()s automatically allocate a buffer of the given
253 * size from the heap if they are given a NULL buffer pointer.
254 * The problem is that this behaviour is not portable. */
255 if (getcwd(buf, sizeof(buf) - 1)) {
256 STRLEN len = strlen(buf);
257 sv_setpvn(sv, buf, len);
261 sv_setsv(sv, &PL_sv_undef);
269 int orig_cdev, orig_cino, cdev, cino, odev, oino, tdev, tino;
270 int namelen, pathlen=0;
274 (void)SvUPGRADE(sv, SVt_PV);
276 if (PerlLIO_lstat(".", &statbuf) < 0) {
280 orig_cdev = statbuf.st_dev;
281 orig_cino = statbuf.st_ino;
289 if (PerlDir_chdir("..") < 0) {
292 if (PerlLIO_stat(".", &statbuf) < 0) {
296 cdev = statbuf.st_dev;
297 cino = statbuf.st_ino;
299 if (odev == cdev && oino == cino) {
302 if (!(dir = PerlDir_open("."))) {
306 while ((dp = PerlDir_read(dir)) != NULL) {
308 namelen = dp->d_namlen;
310 namelen = strlen(dp->d_name);
313 if (SV_CWD_ISDOT(dp)) {
317 if (PerlLIO_lstat(dp->d_name, &statbuf) < 0) {
321 tdev = statbuf.st_dev;
322 tino = statbuf.st_ino;
323 if (tino == oino && tdev == odev) {
332 if (pathlen + namelen + 1 >= MAXPATHLEN) {
336 SvGROW(sv, pathlen + namelen + 1);
340 Move(SvPVX(sv), SvPVX(sv) + namelen + 1, pathlen, char);
343 /* prepend current directory to the front */
345 Move(dp->d_name, SvPVX(sv)+1, namelen, char);
346 pathlen += (namelen + 1);
351 if (PerlDir_close(dir) < 0) {
358 SvCUR_set(sv, pathlen);
362 if (PerlDir_chdir(SvPVX(sv)) < 0) {
366 if (PerlLIO_stat(".", &statbuf) < 0) {
370 cdev = statbuf.st_dev;
371 cino = statbuf.st_ino;
373 if (cdev != orig_cdev || cino != orig_cino) {
374 Perl_croak(aTHX_ "Unstable directory path, "
375 "current directory changed unexpectedly");
390 MODULE = Cwd PACKAGE = Cwd
402 #ifndef INCOMPLETE_TAINTS
415 #ifndef INCOMPLETE_TAINTS
421 abs_path(pathsv=Nullsv)
428 char buf[MAXPATHLEN];
430 path = pathsv ? SvPV_nolen(pathsv) : (char *)".";
432 if (bsd_realpath(path, buf)) {
433 sv_setpvn(TARG, buf, strlen(buf));
438 sv_setsv(TARG, &PL_sv_undef);
441 #ifndef INCOMPLETE_TAINTS
446 #if defined(WIN32) && !defined(UNDER_CE)
456 /* Drive 0 is the current drive, 1 is A:, 2 is B:, 3 is C: and so on. */
458 (items == 1 && (!SvOK(ST(0)) || (SvPOK(ST(0)) && !SvCUR(ST(0))))))
460 else if (items == 1 && SvPOK(ST(0)) && SvCUR(ST(0)) &&
461 isALPHA(SvPVX(ST(0))[0]))
462 drive = toUPPER(SvPVX(ST(0))[0]) - 'A' + 1;
464 croak("Usage: getdcwd(DRIVE)");
466 New(0,dir,MAXPATHLEN,char);
467 if (_getdcwd(drive, dir, MAXPATHLEN)) {
468 sv_setpvn(TARG, dir, strlen(dir));
472 sv_setsv(TARG, &PL_sv_undef);
477 #ifndef INCOMPLETE_TAINTS