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[MAXPATHLEN])
71 return Perl_rmsexpand(aTHX_ (char*)path, resolved, NULL, 0);
74 size_t left_len, resolved_len;
77 char left[MAXPATHLEN], next_token[MAXPATHLEN], symlink[MAXPATHLEN];
87 left_len = my_strlcpy(left, path + 1, sizeof(left));
89 if (getcwd(resolved, MAXPATHLEN) == NULL) {
90 my_strlcpy(resolved, ".", MAXPATHLEN);
93 resolved_len = strlen(resolved);
94 left_len = my_strlcpy(left, path, sizeof(left));
96 if (left_len >= sizeof(left) || resolved_len >= MAXPATHLEN) {
102 * Iterate over path components in `left'.
104 while (left_len != 0) {
106 * Extract the next path component and adjust `left'
109 p = strchr(left, '/');
110 s = p ? p : left + left_len;
111 if (s - left >= sizeof(next_token)) {
112 errno = ENAMETOOLONG;
115 memcpy(next_token, left, s - left);
116 next_token[s - left] = '\0';
117 left_len -= s - left;
119 memmove(left, s + 1, left_len + 1);
120 if (resolved[resolved_len - 1] != '/') {
121 if (resolved_len + 1 >= MAXPATHLEN) {
122 errno = ENAMETOOLONG;
125 resolved[resolved_len++] = '/';
126 resolved[resolved_len] = '\0';
128 if (next_token[0] == '\0')
130 else if (strcmp(next_token, ".") == 0)
132 else if (strcmp(next_token, "..") == 0) {
134 * Strip the last path component except when we have
137 if (resolved_len > 1) {
138 resolved[resolved_len - 1] = '\0';
139 q = strrchr(resolved, '/') + 1;
141 resolved_len = q - resolved;
147 * Append the next path component and lstat() it. If
148 * lstat() fails we still can return successfully if
149 * there are no more path components left.
151 resolved_len = my_strlcat(resolved, next_token, MAXPATHLEN);
152 if (resolved_len >= MAXPATHLEN) {
153 errno = ENAMETOOLONG;
156 #if defined(HAS_LSTAT) && defined(HAS_READLINK) && defined(HAS_SYMLINK)
159 if (lstat(resolved, &sb) != 0) {
160 if (errno == ENOENT && p == NULL) {
166 if (S_ISLNK(sb.st_mode)) {
169 if (symlinks++ > MAXSYMLINKS) {
173 slen = readlink(resolved, symlink, sizeof(symlink) - 1);
176 symlink[slen] = '\0';
177 if (symlink[0] == '/') {
180 } else if (resolved_len > 1) {
181 /* Strip the last path component. */
182 resolved[resolved_len - 1] = '\0';
183 q = strrchr(resolved, '/') + 1;
185 resolved_len = q - resolved;
189 * If there are any path components left, then
190 * append them to symlink. The result is placed
194 if (symlink[slen - 1] != '/') {
195 if (slen + 1 >= sizeof(symlink)) {
196 errno = ENAMETOOLONG;
200 symlink[slen + 1] = 0;
202 left_len = my_strlcat(symlink, left, sizeof(left));
203 if (left_len >= sizeof(left)) {
204 errno = ENAMETOOLONG;
208 left_len = my_strlcpy(left, symlink, sizeof(left));
215 * Remove trailing slash except when the resolved pathname
218 if (resolved_len > 1 && resolved[resolved_len - 1] == '/')
219 resolved[resolved_len - 1] = '\0';
224 #ifndef SV_CWD_RETURN_UNDEF
225 #define SV_CWD_RETURN_UNDEF \
226 sv_setsv(sv, &PL_sv_undef); \
230 #ifndef OPpENTERSUB_HASTARG
231 #define OPpENTERSUB_HASTARG 32 /* Called from OP tree. */
235 #define dXSTARG SV * targ = ((PL_op->op_private & OPpENTERSUB_HASTARG) \
236 ? PAD_SV(PL_op->op_targ) : sv_newmortal())
240 #define XSprePUSH (sp = PL_stack_base + ax - 1)
244 #define SV_CWD_ISDOT(dp) \
245 (dp->d_name[0] == '.' && (dp->d_name[1] == '\0' || \
246 (dp->d_name[1] == '.' && dp->d_name[2] == '\0')))
250 /* Taken from perl 5.8's util.c */
251 #define getcwd_sv(a) Perl_getcwd_sv(aTHX_ a)
252 int Perl_getcwd_sv(pTHX_ register SV *sv)
256 #ifndef INCOMPLETE_TAINTS
262 char buf[MAXPATHLEN];
264 /* Some getcwd()s automatically allocate a buffer of the given
265 * size from the heap if they are given a NULL buffer pointer.
266 * The problem is that this behaviour is not portable. */
267 if (getcwd(buf, sizeof(buf) - 1)) {
268 STRLEN len = strlen(buf);
269 sv_setpvn(sv, buf, len);
273 sv_setsv(sv, &PL_sv_undef);
281 int orig_cdev, orig_cino, cdev, cino, odev, oino, tdev, tino;
282 int namelen, pathlen=0;
286 (void)SvUPGRADE(sv, SVt_PV);
288 if (PerlLIO_lstat(".", &statbuf) < 0) {
292 orig_cdev = statbuf.st_dev;
293 orig_cino = statbuf.st_ino;
301 if (PerlDir_chdir("..") < 0) {
304 if (PerlLIO_stat(".", &statbuf) < 0) {
308 cdev = statbuf.st_dev;
309 cino = statbuf.st_ino;
311 if (odev == cdev && oino == cino) {
314 if (!(dir = PerlDir_open("."))) {
318 while ((dp = PerlDir_read(dir)) != NULL) {
320 namelen = dp->d_namlen;
322 namelen = strlen(dp->d_name);
325 if (SV_CWD_ISDOT(dp)) {
329 if (PerlLIO_lstat(dp->d_name, &statbuf) < 0) {
333 tdev = statbuf.st_dev;
334 tino = statbuf.st_ino;
335 if (tino == oino && tdev == odev) {
344 if (pathlen + namelen + 1 >= MAXPATHLEN) {
348 SvGROW(sv, pathlen + namelen + 1);
352 Move(SvPVX(sv), SvPVX(sv) + namelen + 1, pathlen, char);
355 /* prepend current directory to the front */
357 Move(dp->d_name, SvPVX(sv)+1, namelen, char);
358 pathlen += (namelen + 1);
363 if (PerlDir_close(dir) < 0) {
370 SvCUR_set(sv, pathlen);
374 if (PerlDir_chdir(SvPVX(sv)) < 0) {
378 if (PerlLIO_stat(".", &statbuf) < 0) {
382 cdev = statbuf.st_dev;
383 cino = statbuf.st_ino;
385 if (cdev != orig_cdev || cino != orig_cino) {
386 Perl_croak(aTHX_ "Unstable directory path, "
387 "current directory changed unexpectedly");
402 MODULE = Cwd PACKAGE = Cwd
414 #ifndef INCOMPLETE_TAINTS
427 #ifndef INCOMPLETE_TAINTS
433 abs_path(pathsv=Nullsv)
440 char buf[MAXPATHLEN];
442 path = pathsv ? SvPV_nolen(pathsv) : (char *)".";
444 if (bsd_realpath(path, buf)) {
445 sv_setpvn(TARG, buf, strlen(buf));
450 sv_setsv(TARG, &PL_sv_undef);
453 #ifndef INCOMPLETE_TAINTS
458 #if defined(WIN32) && !defined(UNDER_CE)
468 /* Drive 0 is the current drive, 1 is A:, 2 is B:, 3 is C: and so on. */
470 (items == 1 && (!SvOK(ST(0)) || (SvPOK(ST(0)) && !SvCUR(ST(0))))))
472 else if (items == 1 && SvPOK(ST(0)) && SvCUR(ST(0)) &&
473 isALPHA(SvPVX(ST(0))[0]))
474 drive = toUPPER(SvPVX(ST(0))[0]) - 'A' + 1;
476 croak("Usage: getdcwd(DRIVE)");
478 New(0,dir,MAXPATHLEN,char);
479 if (_getdcwd(drive, dir, MAXPATHLEN)) {
480 sv_setpvn(TARG, dir, strlen(dir));
484 sv_setsv(TARG, &PL_sv_undef);
489 #ifndef INCOMPLETE_TAINTS