a82404f15654bdeb976eb92b81ad59d9901dd902
[p5sagit/p5-mst-13.2.git] / ext / Cwd / Cwd.xs
1 #include "EXTERN.h"
2 #include "perl.h"
3 #include "XSUB.h"
4
5 #ifdef I_UNISTD
6 #   include <unistd.h>
7 #endif
8
9 /* The realpath() implementation from OpenBSD 2.9 (realpath.c 1.4)
10  * Renamed here to bsd_realpath() to avoid library conflicts.
11  * --jhi 2000-06-20 */
12
13 /*
14  * Copyright (c) 1994
15  *      The Regents of the University of California.  All rights reserved.
16  *
17  * This code is derived from software contributed to Berkeley by
18  * Jan-Simon Pendry.
19  *
20  * Redistribution and use in source and binary forms, with or without
21  * modification, are permitted provided that the following conditions
22  * are met:
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.
35  *
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
46  * SUCH DAMAGE.
47  */
48
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 */
52
53 /* OpenBSD system #includes removed since the Perl ones should do. --jhi */
54
55 #ifndef MAXSYMLINKS
56 #define MAXSYMLINKS 8
57 #endif
58
59 /*
60  * char *realpath(const char *path, char resolved_path[MAXPATHLEN]);
61  *
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).
65  */
66 static
67 char *
68 bsd_realpath(path, resolved)
69         const char *path;
70         char *resolved;
71 {
72 #ifdef VMS
73        return Perl_rmsexpand((char*)path, resolved, NULL, 0);
74 #else
75         struct stat sb;
76         int n, rootd, serrno;
77         char *p, *q, wbuf[MAXPATHLEN];
78         int symlinks = 0;
79
80         /* Save the starting point. */
81 #ifdef HAS_FCHDIR
82         int fd;
83
84         if ((fd = open(".", O_RDONLY)) < 0) {
85                 (void)strcpy(resolved, ".");
86                 return (NULL);
87         }
88 #else
89         char wd[MAXPATHLEN];
90
91         if (getcwd(wd, MAXPATHLEN - 1) == NULL) {
92                 (void)strcpy(resolved, ".");
93                 return (NULL);
94         }
95 #endif
96
97         /*
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.
104          */
105         (void)strncpy(resolved, path, MAXPATHLEN - 1);
106         resolved[MAXPATHLEN - 1] = '\0';
107 loop:
108         q = strrchr(resolved, '/');
109         if (q != NULL) {
110                 p = q + 1;
111                 if (q == resolved)
112                         q = "/";
113                 else {
114                         do {
115                                 --q;
116                         } while (q > resolved && *q == '/');
117                         q[1] = '\0';
118                         q = resolved;
119                 }
120                 if (chdir(q) < 0)
121                         goto err1;
122         } else
123                 p = resolved;
124
125 #if defined(HAS_LSTAT) && defined(HAS_READLINK) && defined(HAS_SYMLINK)
126         /* Deal with the last component. */
127         if (lstat(p, &sb) == 0) {
128                 if (S_ISLNK(sb.st_mode)) {
129                         if (++symlinks > MAXSYMLINKS) {
130                                 errno = ELOOP;
131                                 goto err1;
132                         }
133                         n = readlink(p, resolved, MAXPATHLEN-1);
134                         if (n < 0)
135                                 goto err1;
136                         resolved[n] = '\0';
137                         goto loop;
138                 }
139                 if (S_ISDIR(sb.st_mode)) {
140                         if (chdir(p) < 0)
141                                 goto err1;
142                         p = "";
143                 }
144         }
145 #endif
146
147         /*
148          * Save the last component name and get the full pathname of
149          * the current directory.
150          */
151         (void)strcpy(wbuf, p);
152         if (getcwd(resolved, MAXPATHLEN) == 0)
153                 goto err1;
154
155         /*
156          * Join the two strings together, ensuring that the right thing
157          * happens if the last component is empty, or the dirname is root.
158          */
159         if (resolved[0] == '/' && resolved[1] == '\0')
160                 rootd = 1;
161         else
162                 rootd = 0;
163
164         if (*wbuf) {
165                 if (strlen(resolved) + strlen(wbuf) + rootd + 1 > MAXPATHLEN) {
166                         errno = ENAMETOOLONG;
167                         goto err1;
168                 }
169                 if (rootd == 0)
170                         (void)strcat(resolved, "/");
171                 (void)strcat(resolved, wbuf);
172         }
173
174         /* Go back to where we came from. */
175 #ifdef HAS_FCHDIR
176         if (fchdir(fd) < 0) {
177                 serrno = errno;
178                 goto err2;
179         }
180 #else
181         if (chdir(wd) < 0) {
182                 serrno = errno;
183                 goto err2;
184         }
185 #endif
186
187         /* It's okay if the close fails, what's an fd more or less? */
188 #ifdef HAS_FCHDIR
189         (void)close(fd);
190 #endif
191         return (resolved);
192
193 err1:   serrno = errno;
194 #ifdef HAS_FCHDIR
195         (void)fchdir(fd);
196 #else
197         (void)chdir(wd);
198 #endif
199
200 err2:
201 #ifdef HAS_FCHDIR
202         (void)close(fd);
203 #endif
204         errno = serrno;
205         return (NULL);
206 #endif
207 }
208
209 MODULE = Cwd            PACKAGE = Cwd
210
211 PROTOTYPES: ENABLE
212
213 void
214 fastcwd()
215 PPCODE:
216 {
217     dXSTARG;
218     getcwd_sv(TARG);
219     XSprePUSH; PUSHTARG;
220 }
221
222 void
223 abs_path(pathsv=Nullsv)
224     SV *pathsv
225 PPCODE:
226 {
227     dXSTARG;
228     char *path;
229     char buf[MAXPATHLEN];
230
231     path = pathsv ? SvPV_nolen(pathsv) : ".";
232
233     if (bsd_realpath(path, buf)) {
234         sv_setpvn(TARG, buf, strlen(buf));
235         SvPOK_only(TARG);
236         SvTAINTED_on(TARG);
237     }
238     else
239         sv_setsv(TARG, &PL_sv_undef);
240
241     XSprePUSH; PUSHTARG;
242 #ifndef INCOMPLETE_TAINTS
243     SvTAINTED_on(TARG);
244 #endif
245 }