This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Re: [PATCH lib/Cwd.pm] fixing proto mismatch warning
[perl5.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        dTHX;
74        return Perl_rmsexpand(aTHX_ (char*)path, resolved, NULL, 0);
75 #else
76         struct stat sb;
77         int n, rootd, serrno;
78         char *p, *q, wbuf[MAXPATHLEN];
79         int symlinks = 0;
80
81         /* Save the starting point. */
82 #ifdef HAS_FCHDIR
83         int fd;
84
85         if ((fd = open(".", O_RDONLY)) < 0) {
86                 (void)strcpy(resolved, ".");
87                 return (NULL);
88         }
89 #else
90         char wd[MAXPATHLEN];
91
92         if (getcwd(wd, MAXPATHLEN - 1) == NULL) {
93                 (void)strcpy(resolved, ".");
94                 return (NULL);
95         }
96 #endif
97
98         /*
99          * Find the dirname and basename from the path to be resolved.
100          * Change directory to the dirname component.
101          * lstat the basename part.
102          *     if it is a symlink, read in the value and loop.
103          *     if it is a directory, then change to that directory.
104          * get the current directory name and append the basename.
105          */
106         (void)strncpy(resolved, path, MAXPATHLEN - 1);
107         resolved[MAXPATHLEN - 1] = '\0';
108 loop:
109         q = strrchr(resolved, '/');
110         if (q != NULL) {
111                 p = q + 1;
112                 if (q == resolved)
113                         q = "/";
114                 else {
115                         do {
116                                 --q;
117                         } while (q > resolved && *q == '/');
118                         q[1] = '\0';
119                         q = resolved;
120                 }
121                 if (chdir(q) < 0)
122                         goto err1;
123         } else
124                 p = resolved;
125
126 #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)) {
130                         if (++symlinks > MAXSYMLINKS) {
131                                 errno = ELOOP;
132                                 goto err1;
133                         }
134                         n = readlink(p, resolved, MAXPATHLEN-1);
135                         if (n < 0)
136                                 goto err1;
137                         resolved[n] = '\0';
138                         goto loop;
139                 }
140                 if (S_ISDIR(sb.st_mode)) {
141                         if (chdir(p) < 0)
142                                 goto err1;
143                         p = "";
144                 }
145         }
146 #endif
147
148         /*
149          * Save the last component name and get the full pathname of
150          * the current directory.
151          */
152         (void)strcpy(wbuf, p);
153         if (getcwd(resolved, MAXPATHLEN) == 0)
154                 goto err1;
155
156         /*
157          * Join the two strings together, ensuring that the right thing
158          * happens if the last component is empty, or the dirname is root.
159          */
160         if (resolved[0] == '/' && resolved[1] == '\0')
161                 rootd = 1;
162         else
163                 rootd = 0;
164
165         if (*wbuf) {
166                 if (strlen(resolved) + strlen(wbuf) + rootd + 1 > MAXPATHLEN) {
167                         errno = ENAMETOOLONG;
168                         goto err1;
169                 }
170                 if (rootd == 0)
171                         (void)strcat(resolved, "/");
172                 (void)strcat(resolved, wbuf);
173         }
174
175         /* Go back to where we came from. */
176 #ifdef HAS_FCHDIR
177         if (fchdir(fd) < 0) {
178                 serrno = errno;
179                 goto err2;
180         }
181 #else
182         if (chdir(wd) < 0) {
183                 serrno = errno;
184                 goto err2;
185         }
186 #endif
187
188         /* It's okay if the close fails, what's an fd more or less? */
189 #ifdef HAS_FCHDIR
190         (void)close(fd);
191 #endif
192         return (resolved);
193
194 err1:   serrno = errno;
195 #ifdef HAS_FCHDIR
196         (void)fchdir(fd);
197 #else
198         (void)chdir(wd);
199 #endif
200
201 err2:
202 #ifdef HAS_FCHDIR
203         (void)close(fd);
204 #endif
205         errno = serrno;
206         return (NULL);
207 #endif
208 }
209
210 MODULE = Cwd            PACKAGE = Cwd
211
212 PROTOTYPES: ENABLE
213
214 void
215 fastcwd()
216 PROTOTYPE: DISABLE
217 PPCODE:
218 {
219     dXSTARG;
220     getcwd_sv(TARG);
221     XSprePUSH; PUSHTARG;
222 }
223
224 void
225 abs_path(pathsv=Nullsv)
226     SV *pathsv
227 PPCODE:
228 {
229     dXSTARG;
230     char *path;
231     char buf[MAXPATHLEN];
232
233     path = pathsv ? SvPV_nolen(pathsv) : ".";
234
235     if (bsd_realpath(path, buf)) {
236         sv_setpvn(TARG, buf, strlen(buf));
237         SvPOK_only(TARG);
238         SvTAINTED_on(TARG);
239     }
240     else
241         sv_setsv(TARG, &PL_sv_undef);
242
243     XSprePUSH; PUSHTARG;
244 #ifndef INCOMPLETE_TAINTS
245     SvTAINTED_on(TARG);
246 #endif
247 }