9 /* The realpath() implementation from OpenBSD 2.9 (realpath.c 1.4)
10 * Renamed here to bsd_realpath() to avoid library conflicts.
15 * The Regents of the University of California. All rights reserved.
17 * This code is derived from software contributed to Berkeley by
20 * Redistribution and use in source and binary forms, with or without
21 * modification, are permitted provided that the following conditions
23 * 1. Redistributions of source code must retain the above copyright
24 * notice, this list of conditions and the following disclaimer.
25 * 2. Redistributions in binary form must reproduce the above copyright
26 * notice, this list of conditions and the following disclaimer in the
27 * documentation and/or other materials provided with the distribution.
28 * 3. All advertising materials mentioning features or use of this software
29 * must display the following acknowledgement:
30 * This product includes software developed by the University of
31 * California, Berkeley and its contributors.
32 * 4. Neither the name of the University nor the names of its contributors
33 * may be used to endorse or promote products derived from this software
34 * without specific prior written permission.
36 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
37 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
39 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
40 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
41 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
42 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
43 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
44 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
45 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
49 #if defined(LIBC_SCCS) && !defined(lint)
50 static char *rcsid = "$OpenBSD: realpath.c,v 1.4 1998/05/18 09:55:19 deraadt Exp $";
51 #endif /* LIBC_SCCS and not lint */
53 /* OpenBSD system #includes removed since the Perl ones should do. --jhi */
60 * char *realpath(const char *path, char resolved_path[MAXPATHLEN]);
62 * Find the real name of path, by removing all ".", ".." and symlink
63 * components. Returns (resolved) on success, or (NULL) on failure,
64 * in which case the path which caused trouble is left in (resolved).
68 bsd_realpath(path, resolved)
74 return Perl_rmsexpand(aTHX_ (char*)path, resolved, NULL, 0);
77 char *p, *q, wbuf[MAXPATHLEN];
80 /* Save the starting point. */
84 if ((fd = open(".", O_RDONLY)) < 0) {
85 (void)strcpy(resolved, ".");
91 if (getcwd(wd, MAXPATHLEN - 1) == NULL) {
92 (void)strcpy(resolved, ".");
98 * Find the dirname and basename from the path to be resolved.
99 * Change directory to the dirname component.
100 * lstat the basename part.
101 * if it is a symlink, read in the value and loop.
102 * if it is a directory, then change to that directory.
103 * get the current directory name and append the basename.
105 (void)strncpy(resolved, path, MAXPATHLEN - 1);
106 resolved[MAXPATHLEN - 1] = '\0';
108 q = strrchr(resolved, '/');
116 } while (q > resolved && *q == '/');
125 #if defined(HAS_LSTAT) && defined(HAS_READLINK) && defined(HAS_SYMLINK)
128 /* Deal with the last component. */
129 if (lstat(p, &sb) == 0) {
130 if (S_ISLNK(sb.st_mode)) {
132 if (++symlinks > MAXSYMLINKS) {
136 n = readlink(p, resolved, MAXPATHLEN-1);
142 if (S_ISDIR(sb.st_mode)) {
152 * Save the last component name and get the full pathname of
153 * the current directory.
155 (void)strcpy(wbuf, p);
156 if (getcwd(resolved, MAXPATHLEN) == 0)
160 * Join the two strings together, ensuring that the right thing
161 * happens if the last component is empty, or the dirname is root.
163 if (resolved[0] == '/' && resolved[1] == '\0')
169 if (strlen(resolved) + strlen(wbuf) + (1 - rootd) + 1 > MAXPATHLEN) {
170 errno = ENAMETOOLONG;
174 (void)strcat(resolved, "/");
175 (void)strcat(resolved, wbuf);
178 /* Go back to where we came from. */
180 if (fchdir(fd) < 0) {
191 /* It's okay if the close fails, what's an fd more or less? */
197 err1: serrno = errno;
214 /* Taken from perl 5.8's util.c */
215 #define getcwd_sv(a) Perl_getcwd_sv(aTHX_ a)
216 int Perl_getcwd_sv(pTHX_ register SV *sv)
220 #ifndef INCOMPLETE_TAINTS
226 char buf[MAXPATHLEN];
228 /* Some getcwd()s automatically allocate a buffer of the given
229 * size from the heap if they are given a NULL buffer pointer.
230 * The problem is that this behaviour is not portable. */
231 if (getcwd(buf, sizeof(buf) - 1)) {
232 STRLEN len = strlen(buf);
233 sv_setpvn(sv, buf, len);
237 sv_setsv(sv, &PL_sv_undef);
245 int orig_cdev, orig_cino, cdev, cino, odev, oino, tdev, tino;
246 int namelen, pathlen=0;
250 (void)SvUPGRADE(sv, SVt_PV);
252 if (PerlLIO_lstat(".", &statbuf) < 0) {
256 orig_cdev = statbuf.st_dev;
257 orig_cino = statbuf.st_ino;
265 if (PerlDir_chdir("..") < 0) {
268 if (PerlLIO_stat(".", &statbuf) < 0) {
272 cdev = statbuf.st_dev;
273 cino = statbuf.st_ino;
275 if (odev == cdev && oino == cino) {
278 if (!(dir = PerlDir_open("."))) {
282 while ((dp = PerlDir_read(dir)) != NULL) {
284 namelen = dp->d_namlen;
286 namelen = strlen(dp->d_name);
289 if (SV_CWD_ISDOT(dp)) {
293 if (PerlLIO_lstat(dp->d_name, &statbuf) < 0) {
297 tdev = statbuf.st_dev;
298 tino = statbuf.st_ino;
299 if (tino == oino && tdev == odev) {
308 if (pathlen + namelen + 1 >= MAXPATHLEN) {
312 SvGROW(sv, pathlen + namelen + 1);
316 Move(SvPVX(sv), SvPVX(sv) + namelen + 1, pathlen, char);
319 /* prepend current directory to the front */
321 Move(dp->d_name, SvPVX(sv)+1, namelen, char);
322 pathlen += (namelen + 1);
327 if (PerlDir_close(dir) < 0) {
334 SvCUR_set(sv, pathlen);
338 if (PerlDir_chdir(SvPVX(sv)) < 0) {
342 if (PerlLIO_stat(".", &statbuf) < 0) {
346 cdev = statbuf.st_dev;
347 cino = statbuf.st_ino;
349 if (cdev != orig_cdev || cino != orig_cino) {
350 Perl_croak(aTHX_ "Unstable directory path, "
351 "current directory changed unexpectedly");
365 MODULE = Cwd PACKAGE = Cwd
377 #ifndef INCOMPLETE_TAINTS
383 abs_path(pathsv=Nullsv)
389 char buf[MAXPATHLEN];
391 path = pathsv ? SvPV_nolen(pathsv) : ".";
393 if (bsd_realpath(path, buf)) {
394 sv_setpvn(TARG, buf, strlen(buf));
399 sv_setsv(TARG, &PL_sv_undef);
402 #ifndef INCOMPLETE_TAINTS
417 /* Drive 0 is the current drive, 1 is A:, 2 is B:, 3 is C: and so on. */
419 (items == 1 && (!SvOK(ST(0)) || (SvPOK(ST(0)) && !SvCUR(ST(0))))))
421 else if (items == 1 && SvPOK(ST(0)) && SvCUR(ST(0)) &&
422 isALPHA(SvPVX(ST(0))[0]))
423 drive = toUPPER(SvPVX(ST(0))[0]) - 'A' + 1;
425 croak("Usage: getdcwd(DRIVE)");
427 New(0,dir,MAXPATHLEN,char);
428 if (_getdcwd(drive, dir, MAXPATHLEN)) {
429 sv_setpvn(TARG, dir, strlen(dir));
434 sv_setsv(TARG, &PL_sv_undef);
437 #ifndef INCOMPLETE_TAINTS