Commit | Line | Data |
a0d0e21e |
1 | /* doio.c |
a687059c |
2 | * |
4bb101f2 |
3 | * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, |
770526c1 |
4 | * 2000, 2001, 2002, 2003, 2004, 2005, by Larry Wall and others |
a687059c |
5 | * |
6e21c824 |
6 | * You may distribute under the terms of either the GNU General Public |
7 | * License or the Artistic License, as specified in the README file. |
a687059c |
8 | * |
a0d0e21e |
9 | */ |
10 | |
11 | /* |
12 | * "Far below them they saw the white waters pour into a foaming bowl, and |
13 | * then swirl darkly about a deep oval basin in the rocks, until they found |
14 | * their way out again through a narrow gate, and flowed away, fuming and |
15 | * chattering, into calmer and more level reaches." |
a687059c |
16 | */ |
17 | |
166f8a29 |
18 | /* This file contains functions that do the actual I/O on behalf of ops. |
19 | * For example, pp_print() calls the do_print() function in this file for |
20 | * each argument needing printing. |
21 | */ |
22 | |
a687059c |
23 | #include "EXTERN.h" |
864dbfa3 |
24 | #define PERL_IN_DOIO_C |
a687059c |
25 | #include "perl.h" |
26 | |
fe14fcc3 |
27 | #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM) |
aec308ec |
28 | #ifndef HAS_SEM |
c2ab57d4 |
29 | #include <sys/ipc.h> |
aec308ec |
30 | #endif |
fe14fcc3 |
31 | #ifdef HAS_MSG |
c2ab57d4 |
32 | #include <sys/msg.h> |
e5d73d77 |
33 | #endif |
fe14fcc3 |
34 | #ifdef HAS_SHM |
c2ab57d4 |
35 | #include <sys/shm.h> |
a0d0e21e |
36 | # ifndef HAS_SHMAT_PROTOTYPE |
20ce7b12 |
37 | extern Shmat_t shmat (int, char *, int); |
a0d0e21e |
38 | # endif |
c2ab57d4 |
39 | #endif |
e5d73d77 |
40 | #endif |
c2ab57d4 |
41 | |
663a0e37 |
42 | #ifdef I_UTIME |
3730b96e |
43 | # if defined(_MSC_VER) || defined(__MINGW32__) |
3fe9a6f1 |
44 | # include <sys/utime.h> |
45 | # else |
46 | # include <utime.h> |
47 | # endif |
663a0e37 |
48 | #endif |
85aff577 |
49 | |
85aff577 |
50 | #ifdef O_EXCL |
51 | # define OPEN_EXCL O_EXCL |
52 | #else |
53 | # define OPEN_EXCL 0 |
54 | #endif |
a687059c |
55 | |
0c19750d |
56 | #define PERL_MODE_MAX 8 |
57 | #define PERL_FLAGS_MAX 10 |
58 | |
76121258 |
59 | #include <signal.h> |
76121258 |
60 | |
a687059c |
61 | bool |
6170680b |
62 | Perl_do_open(pTHX_ GV *gv, register char *name, I32 len, int as_raw, |
63 | int rawmode, int rawperm, PerlIO *supplied_fp) |
64 | { |
a567e93b |
65 | return do_openn(gv, name, len, as_raw, rawmode, rawperm, |
66 | supplied_fp, (SV **) NULL, 0); |
6170680b |
67 | } |
68 | |
69 | bool |
70 | Perl_do_open9(pTHX_ GV *gv, register char *name, I32 len, int as_raw, |
71 | int rawmode, int rawperm, PerlIO *supplied_fp, SV *svs, |
72 | I32 num_svs) |
a687059c |
73 | { |
a567e93b |
74 | return do_openn(gv, name, len, as_raw, rawmode, rawperm, |
75 | supplied_fp, &svs, 1); |
76 | } |
77 | |
78 | bool |
79 | Perl_do_openn(pTHX_ GV *gv, register char *name, I32 len, int as_raw, |
80 | int rawmode, int rawperm, PerlIO *supplied_fp, SV **svp, |
81 | I32 num_svs) |
82 | { |
a0d0e21e |
83 | register IO *io = GvIOn(gv); |
760ac839 |
84 | PerlIO *saveifp = Nullfp; |
85 | PerlIO *saveofp = Nullfp; |
ee518936 |
86 | int savefd = -1; |
9f37169a |
87 | char savetype = IoTYPE_CLOSED; |
c07a80fd |
88 | int writing = 0; |
760ac839 |
89 | PerlIO *fp; |
c07a80fd |
90 | int fd; |
91 | int result; |
3500f679 |
92 | bool was_fdopen = FALSE; |
16fe6d59 |
93 | bool in_raw = 0, in_crlf = 0, out_raw = 0, out_crlf = 0; |
b931b1d9 |
94 | char *type = NULL; |
0c19750d |
95 | char mode[PERL_MODE_MAX]; /* stdio file mode ("r\0", "rb\0", "r+b\0" etc.) */ |
ee518936 |
96 | SV *namesv; |
a687059c |
97 | |
b931b1d9 |
98 | Zero(mode,sizeof(mode),char); |
3280af22 |
99 | PL_forkprocess = 1; /* assume true if no fork */ |
c07a80fd |
100 | |
b931b1d9 |
101 | /* Collect default raw/crlf info from the op */ |
16fe6d59 |
102 | if (PL_op && PL_op->op_type == OP_OPEN) { |
c2be40b1 |
103 | /* set up IO layers */ |
16fe6d59 |
104 | U8 flags = PL_op->op_private; |
105 | in_raw = (flags & OPpOPEN_IN_RAW); |
106 | in_crlf = (flags & OPpOPEN_IN_CRLF); |
107 | out_raw = (flags & OPpOPEN_OUT_RAW); |
108 | out_crlf = (flags & OPpOPEN_OUT_CRLF); |
109 | } |
110 | |
b931b1d9 |
111 | /* If currently open - close before we re-open */ |
a0d0e21e |
112 | if (IoIFP(io)) { |
760ac839 |
113 | fd = PerlIO_fileno(IoIFP(io)); |
ee518936 |
114 | if (IoTYPE(io) == IoTYPE_STD) { |
115 | /* This is a clone of one of STD* handles */ |
c2ab57d4 |
116 | result = 0; |
ee518936 |
117 | } |
118 | else if (fd >= 0 && fd <= PL_maxsysfd) { |
119 | /* This is one of the original STD* handles */ |
120 | saveifp = IoIFP(io); |
121 | saveofp = IoOFP(io); |
8990e307 |
122 | savetype = IoTYPE(io); |
ee518936 |
123 | savefd = fd; |
124 | result = 0; |
6e21c824 |
125 | } |
50952442 |
126 | else if (IoTYPE(io) == IoTYPE_PIPE) |
3028581b |
127 | result = PerlProc_pclose(IoIFP(io)); |
8990e307 |
128 | else if (IoIFP(io) != IoOFP(io)) { |
129 | if (IoOFP(io)) { |
760ac839 |
130 | result = PerlIO_close(IoOFP(io)); |
6170680b |
131 | PerlIO_close(IoIFP(io)); /* clear stdio, fd already closed */ |
c2ab57d4 |
132 | } |
133 | else |
760ac839 |
134 | result = PerlIO_close(IoIFP(io)); |
a687059c |
135 | } |
a687059c |
136 | else |
760ac839 |
137 | result = PerlIO_close(IoIFP(io)); |
ee518936 |
138 | if (result == EOF && fd > PL_maxsysfd) { |
139 | /* Why is this not Perl_warn*() call ? */ |
bf49b057 |
140 | PerlIO_printf(Perl_error_log, |
6170680b |
141 | "Warning: unable to close filehandle %s properly.\n", |
142 | GvENAME(gv)); |
ee518936 |
143 | } |
8990e307 |
144 | IoOFP(io) = IoIFP(io) = Nullfp; |
a687059c |
145 | } |
c07a80fd |
146 | |
147 | if (as_raw) { |
b931b1d9 |
148 | /* sysopen style args, i.e. integer mode and permissions */ |
ee518936 |
149 | STRLEN ix = 0; |
3dccc55c |
150 | int appendtrunc = |
151 | 0 |
d1da7611 |
152 | #ifdef O_APPEND /* Not fully portable. */ |
3dccc55c |
153 | |O_APPEND |
d1da7611 |
154 | #endif |
155 | #ifdef O_TRUNC /* Not fully portable. */ |
3dccc55c |
156 | |O_TRUNC |
d1da7611 |
157 | #endif |
3dccc55c |
158 | ; |
159 | int modifyingmode = |
160 | O_WRONLY|O_RDWR|O_CREAT|appendtrunc; |
161 | int ismodifying; |
162 | |
163 | if (num_svs != 0) { |
164 | Perl_croak(aTHX_ "panic: sysopen with multiple args"); |
165 | } |
166 | /* It's not always |
167 | |
168 | O_RDONLY 0 |
169 | O_WRONLY 1 |
170 | O_RDWR 2 |
171 | |
172 | It might be (in OS/390 and Mac OS Classic it is) |
173 | |
174 | O_WRONLY 1 |
175 | O_RDONLY 2 |
176 | O_RDWR 3 |
177 | |
178 | This means that simple & with O_RDWR would look |
179 | like O_RDONLY is present. Therefore we have to |
180 | be more careful. |
181 | */ |
182 | if ((ismodifying = (rawmode & modifyingmode))) { |
183 | if ((ismodifying & O_WRONLY) == O_WRONLY || |
184 | (ismodifying & O_RDWR) == O_RDWR || |
185 | (ismodifying & (O_CREAT|appendtrunc))) |
186 | TAINT_PROPER("sysopen"); |
187 | } |
3b6c1aba |
188 | mode[ix++] = IoTYPE_NUMERIC; /* Marker to openn to use numeric "sysopen" */ |
b931b1d9 |
189 | |
09458382 |
190 | #if defined(USE_64_BIT_RAWIO) && defined(O_LARGEFILE) |
b94c04ac |
191 | rawmode |= O_LARGEFILE; /* Transparently largefiley. */ |
5ff3f7a4 |
192 | #endif |
193 | |
06c7082d |
194 | IoTYPE(io) = PerlIO_intmode2str(rawmode, &mode[ix], &writing); |
ee518936 |
195 | |
196 | namesv = sv_2mortal(newSVpvn(name,strlen(name))); |
197 | num_svs = 1; |
198 | svp = &namesv; |
1141d9f8 |
199 | type = Nullch; |
b94c04ac |
200 | fp = PerlIO_openn(aTHX_ type, mode, -1, rawmode, rawperm, NULL, num_svs, svp); |
a687059c |
201 | } |
c07a80fd |
202 | else { |
b931b1d9 |
203 | /* Regular (non-sys) open */ |
faecd977 |
204 | char *oname = name; |
faecd977 |
205 | STRLEN olen = len; |
b931b1d9 |
206 | char *tend; |
207 | int dodup = 0; |
ecdeb87c |
208 | PerlIO *that_fp = NULL; |
c07a80fd |
209 | |
faecd977 |
210 | type = savepvn(name, len); |
b931b1d9 |
211 | tend = type+len; |
faecd977 |
212 | SAVEFREEPV(type); |
eb649f83 |
213 | |
214 | /* Lose leading and trailing white space */ |
215 | /*SUPPRESS 530*/ |
216 | for (; isSPACE(*type); type++) ; |
217 | while (tend > type && isSPACE(tend[-1])) |
218 | *--tend = '\0'; |
219 | |
6170680b |
220 | if (num_svs) { |
c2be40b1 |
221 | /* New style explicit name, type is just mode and layer info */ |
fe9745bf |
222 | STRLEN l = 0; |
9a869a14 |
223 | #ifdef USE_STDIO |
224 | if (SvROK(*svp) && !strchr(name,'&')) { |
225 | if (ckWARN(WARN_IO)) |
226 | Perl_warner(aTHX_ packWARN(WARN_IO), |
227 | "Can't open a reference"); |
93189314 |
228 | SETERRNO(EINVAL, LIB_INVARG); |
9a869a14 |
229 | goto say_false; |
230 | } |
231 | #endif /* USE_STDIO */ |
f6c77cf1 |
232 | name = SvOK(*svp) ? SvPV(*svp, l) : ""; |
faecd977 |
233 | len = (I32)l; |
234 | name = savepvn(name, len); |
235 | SAVEFREEPV(name); |
6170680b |
236 | } |
faecd977 |
237 | else { |
faecd977 |
238 | name = type; |
b931b1d9 |
239 | len = tend-type; |
faecd977 |
240 | } |
6170680b |
241 | IoTYPE(io) = *type; |
516a5887 |
242 | if ((*type == IoTYPE_RDWR) && /* scary */ |
01a8ea99 |
243 | (*(type+1) == IoTYPE_RDONLY || *(type+1) == IoTYPE_WRONLY) && |
516a5887 |
244 | ((!num_svs || (tend > type+1 && tend[-1] != IoTYPE_PIPE)))) { |
c2be40b1 |
245 | TAINT_PROPER("open"); |
6170680b |
246 | mode[1] = *type++; |
c07a80fd |
247 | writing = 1; |
a687059c |
248 | } |
c07a80fd |
249 | |
9f37169a |
250 | if (*type == IoTYPE_PIPE) { |
b931b1d9 |
251 | if (num_svs) { |
252 | if (type[1] != IoTYPE_STD) { |
c2be40b1 |
253 | unknown_open_mode: |
b931b1d9 |
254 | Perl_croak(aTHX_ "Unknown open() mode '%.*s'", (int)olen, oname); |
255 | } |
256 | type++; |
6170680b |
257 | } |
c07a80fd |
258 | /*SUPPRESS 530*/ |
b931b1d9 |
259 | for (type++; isSPACE(*type); type++) ; |
faecd977 |
260 | if (!num_svs) { |
6170680b |
261 | name = type; |
b931b1d9 |
262 | len = tend-type; |
faecd977 |
263 | } |
4a7d1889 |
264 | if (*name == '\0') { |
265 | /* command is missing 19990114 */ |
06eaf0bc |
266 | if (ckWARN(WARN_PIPE)) |
9014280d |
267 | Perl_warner(aTHX_ packWARN(WARN_PIPE), "Missing command in piped open"); |
06eaf0bc |
268 | errno = EPIPE; |
269 | goto say_false; |
270 | } |
770526c1 |
271 | if ((*name == '-' && name[1] == '\0') || num_svs) |
c07a80fd |
272 | TAINT_ENV(); |
273 | TAINT_PROPER("piped open"); |
b931b1d9 |
274 | if (!num_svs && name[len-1] == '|') { |
faecd977 |
275 | name[--len] = '\0' ; |
599cee73 |
276 | if (ckWARN(WARN_PIPE)) |
9014280d |
277 | Perl_warner(aTHX_ packWARN(WARN_PIPE), "Can't open bidirectional pipe"); |
7b8d334a |
278 | } |
a1d180c4 |
279 | mode[0] = 'w'; |
c07a80fd |
280 | writing = 1; |
0c19750d |
281 | #ifdef HAS_STRLCAT |
282 | if (out_raw) |
283 | strlcat(mode, "b", PERL_MODE_MAX); |
284 | else if (out_crlf) |
285 | strlcat(mode, "t", PERL_MODE_MAX); |
286 | #else |
a1d180c4 |
287 | if (out_raw) |
288 | strcat(mode, "b"); |
289 | else if (out_crlf) |
290 | strcat(mode, "t"); |
0c19750d |
291 | #endif |
4a7d1889 |
292 | if (num_svs > 1) { |
293 | fp = PerlProc_popen_list(mode, num_svs, svp); |
294 | } |
295 | else { |
296 | fp = PerlProc_popen(name,mode); |
297 | } |
1771866f |
298 | if (num_svs) { |
299 | if (*type) { |
300 | if (PerlIO_apply_layers(aTHX_ fp, mode, type) != 0) { |
301 | goto say_false; |
302 | } |
303 | } |
304 | } |
c2be40b1 |
305 | } /* IoTYPE_PIPE */ |
9f37169a |
306 | else if (*type == IoTYPE_WRONLY) { |
c07a80fd |
307 | TAINT_PROPER("open"); |
6170680b |
308 | type++; |
9f37169a |
309 | if (*type == IoTYPE_WRONLY) { |
310 | /* Two IoTYPE_WRONLYs in a row make for an IoTYPE_APPEND. */ |
50952442 |
311 | mode[0] = IoTYPE(io) = IoTYPE_APPEND; |
6170680b |
312 | type++; |
a0d0e21e |
313 | } |
ee518936 |
314 | else { |
c07a80fd |
315 | mode[0] = 'w'; |
ee518936 |
316 | } |
c07a80fd |
317 | writing = 1; |
318 | |
0c19750d |
319 | #ifdef HAS_STRLCAT |
320 | if (out_raw) |
321 | strlcat(mode, "b", PERL_MODE_MAX); |
322 | else if (out_crlf) |
323 | strlcat(mode, "t", PERL_MODE_MAX); |
324 | #else |
16fe6d59 |
325 | if (out_raw) |
326 | strcat(mode, "b"); |
327 | else if (out_crlf) |
328 | strcat(mode, "t"); |
0c19750d |
329 | #endif |
6170680b |
330 | if (*type == '&') { |
c07a80fd |
331 | duplicity: |
ecdeb87c |
332 | dodup = PERLIO_DUP_FD; |
e620cd72 |
333 | type++; |
334 | if (*type == '=') { |
c07a80fd |
335 | dodup = 0; |
e620cd72 |
336 | type++; |
4a7d1889 |
337 | } |
ee518936 |
338 | if (!num_svs && !*type && supplied_fp) { |
4a7d1889 |
339 | /* "<+&" etc. is used by typemaps */ |
c07a80fd |
340 | fp = supplied_fp; |
ee518936 |
341 | } |
a0d0e21e |
342 | else { |
e620cd72 |
343 | if (num_svs > 1) { |
344 | Perl_croak(aTHX_ "More than one argument to '%c&' open",IoTYPE(io)); |
345 | } |
54edfe98 |
346 | /*SUPPRESS 530*/ |
347 | for (; isSPACE(*type); type++) ; |
1771866f |
348 | if (num_svs && (SvIOK(*svp) || (SvPOK(*svp) && looks_like_number(*svp)))) { |
e620cd72 |
349 | fd = SvUV(*svp); |
24a7a40d |
350 | num_svs = 0; |
ee518936 |
351 | } |
e620cd72 |
352 | else if (isDIGIT(*type)) { |
e620cd72 |
353 | fd = atoi(type); |
354 | } |
c07a80fd |
355 | else { |
356 | IO* thatio; |
e620cd72 |
357 | if (num_svs) { |
358 | thatio = sv_2io(*svp); |
359 | } |
360 | else { |
361 | GV *thatgv; |
e620cd72 |
362 | thatgv = gv_fetchpv(type,FALSE,SVt_PVIO); |
363 | thatio = GvIO(thatgv); |
364 | } |
c07a80fd |
365 | if (!thatio) { |
6e21c824 |
366 | #ifdef EINVAL |
93189314 |
367 | SETERRNO(EINVAL,SS_IVCHAN); |
6e21c824 |
368 | #endif |
c07a80fd |
369 | goto say_false; |
370 | } |
f4e789af |
371 | if ((that_fp = IoIFP(thatio))) { |
7211d486 |
372 | /* Flush stdio buffer before dup. --mjd |
373 | * Unfortunately SEEK_CURing 0 seems to |
374 | * be optimized away on most platforms; |
375 | * only Solaris and Linux seem to flush |
376 | * on that. --jhi */ |
2c534a3f |
377 | #ifdef USE_SFIO |
378 | /* sfio fails to clear error on next |
379 | sfwrite, contrary to documentation. |
380 | -- Nick Clark */ |
ecdeb87c |
381 | if (PerlIO_seek(that_fp, 0, SEEK_CUR) == -1) |
382 | PerlIO_clearerr(that_fp); |
2c534a3f |
383 | #endif |
7211d486 |
384 | /* On the other hand, do all platforms |
385 | * take gracefully to flushing a read-only |
386 | * filehandle? Perhaps we should do |
387 | * fsetpos(src)+fgetpos(dst)? --nik */ |
ecdeb87c |
388 | PerlIO_flush(that_fp); |
389 | fd = PerlIO_fileno(that_fp); |
0759c907 |
390 | /* When dup()ing STDIN, STDOUT or STDERR |
391 | * explicitly set appropriate access mode */ |
f4e789af |
392 | if (that_fp == PerlIO_stdout() |
393 | || that_fp == PerlIO_stderr()) |
0759c907 |
394 | IoTYPE(io) = IoTYPE_WRONLY; |
f4e789af |
395 | else if (that_fp == PerlIO_stdin()) |
0759c907 |
396 | IoTYPE(io) = IoTYPE_RDONLY; |
397 | /* When dup()ing a socket, say result is |
398 | * one as well */ |
399 | else if (IoTYPE(thatio) == IoTYPE_SOCKET) |
50952442 |
400 | IoTYPE(io) = IoTYPE_SOCKET; |
c07a80fd |
401 | } |
402 | else |
403 | fd = -1; |
a0d0e21e |
404 | } |
ee518936 |
405 | if (!num_svs) |
1141d9f8 |
406 | type = Nullch; |
ecdeb87c |
407 | if (that_fp) { |
408 | fp = PerlIO_fdupopen(aTHX_ that_fp, NULL, dodup); |
409 | } |
410 | else { |
c07a80fd |
411 | if (dodup) |
ecdeb87c |
412 | fd = PerlLIO_dup(fd); |
413 | else |
414 | was_fdopen = TRUE; |
415 | if (!(fp = PerlIO_openn(aTHX_ type,mode,fd,0,0,NULL,num_svs,svp))) { |
416 | if (dodup) |
417 | PerlLIO_close(fd); |
418 | } |
faecd977 |
419 | } |
c07a80fd |
420 | } |
ee518936 |
421 | } /* & */ |
c07a80fd |
422 | else { |
423 | /*SUPPRESS 530*/ |
6170680b |
424 | for (; isSPACE(*type); type++) ; |
b931b1d9 |
425 | if (*type == IoTYPE_STD && (!type[1] || isSPACE(type[1]) || type[1] == ':')) { |
426 | /*SUPPRESS 530*/ |
427 | type++; |
760ac839 |
428 | fp = PerlIO_stdout(); |
50952442 |
429 | IoTYPE(io) = IoTYPE_STD; |
7cf31beb |
430 | if (num_svs > 1) { |
431 | Perl_croak(aTHX_ "More than one argument to '>%c' open",IoTYPE_STD); |
432 | } |
c07a80fd |
433 | } |
434 | else { |
ee518936 |
435 | if (!num_svs) { |
436 | namesv = sv_2mortal(newSVpvn(type,strlen(type))); |
437 | num_svs = 1; |
438 | svp = &namesv; |
1141d9f8 |
439 | type = Nullch; |
ee518936 |
440 | } |
6e60e805 |
441 | fp = PerlIO_openn(aTHX_ type,mode,-1,0,0,NULL,num_svs,svp); |
c07a80fd |
442 | } |
ee518936 |
443 | } /* !& */ |
7e72d509 |
444 | if (!fp && type && *type && *type != ':' && !isIDFIRST(*type)) |
445 | goto unknown_open_mode; |
c2be40b1 |
446 | } /* IoTYPE_WRONLY */ |
9f37169a |
447 | else if (*type == IoTYPE_RDONLY) { |
c07a80fd |
448 | /*SUPPRESS 530*/ |
6170680b |
449 | for (type++; isSPACE(*type); type++) ; |
bf38876a |
450 | mode[0] = 'r'; |
0c19750d |
451 | #ifdef HAS_STRLCAT |
452 | if (in_raw) |
453 | strlcat(mode, "b", PERL_MODE_MAX); |
454 | else if (in_crlf) |
455 | strlcat(mode, "t", PERL_MODE_MAX); |
456 | #else |
16fe6d59 |
457 | if (in_raw) |
458 | strcat(mode, "b"); |
459 | else if (in_crlf) |
460 | strcat(mode, "t"); |
0c19750d |
461 | #endif |
6170680b |
462 | if (*type == '&') { |
bf38876a |
463 | goto duplicity; |
6170680b |
464 | } |
b931b1d9 |
465 | if (*type == IoTYPE_STD && (!type[1] || isSPACE(type[1]) || type[1] == ':')) { |
466 | /*SUPPRESS 530*/ |
467 | type++; |
760ac839 |
468 | fp = PerlIO_stdin(); |
50952442 |
469 | IoTYPE(io) = IoTYPE_STD; |
7cf31beb |
470 | if (num_svs > 1) { |
471 | Perl_croak(aTHX_ "More than one argument to '<%c' open",IoTYPE_STD); |
472 | } |
a687059c |
473 | } |
ee518936 |
474 | else { |
475 | if (!num_svs) { |
476 | namesv = sv_2mortal(newSVpvn(type,strlen(type))); |
477 | num_svs = 1; |
478 | svp = &namesv; |
1141d9f8 |
479 | type = Nullch; |
ee518936 |
480 | } |
6e60e805 |
481 | fp = PerlIO_openn(aTHX_ type,mode,-1,0,0,NULL,num_svs,svp); |
ee518936 |
482 | } |
7e72d509 |
483 | if (!fp && type && *type && *type != ':' && !isIDFIRST(*type)) |
484 | goto unknown_open_mode; |
c2be40b1 |
485 | } /* IoTYPE_RDONLY */ |
486 | else if ((num_svs && /* '-|...' or '...|' */ |
487 | type[0] == IoTYPE_STD && type[1] == IoTYPE_PIPE) || |
b931b1d9 |
488 | (!num_svs && tend > type+1 && tend[-1] == IoTYPE_PIPE)) { |
6170680b |
489 | if (num_svs) { |
b931b1d9 |
490 | type += 2; /* skip over '-|' */ |
6170680b |
491 | } |
492 | else { |
b931b1d9 |
493 | *--tend = '\0'; |
494 | while (tend > type && isSPACE(tend[-1])) |
495 | *--tend = '\0'; |
6170680b |
496 | /*SUPPRESS 530*/ |
497 | for (; isSPACE(*type); type++) ; |
498 | name = type; |
b931b1d9 |
499 | len = tend-type; |
6170680b |
500 | } |
4a7d1889 |
501 | if (*name == '\0') { |
502 | /* command is missing 19990114 */ |
06eaf0bc |
503 | if (ckWARN(WARN_PIPE)) |
9014280d |
504 | Perl_warner(aTHX_ packWARN(WARN_PIPE), "Missing command in piped open"); |
06eaf0bc |
505 | errno = EPIPE; |
506 | goto say_false; |
507 | } |
770526c1 |
508 | if (!(*name == '-' && name[1] == '\0') || num_svs) |
79072805 |
509 | TAINT_ENV(); |
510 | TAINT_PROPER("piped open"); |
a1d180c4 |
511 | mode[0] = 'r'; |
0c19750d |
512 | |
513 | #ifdef HAS_STRLCAT |
514 | if (in_raw) |
515 | strlcat(mode, "b", PERL_MODE_MAX); |
516 | else if (in_crlf) |
517 | strlcat(mode, "t", PERL_MODE_MAX); |
518 | #else |
a1d180c4 |
519 | if (in_raw) |
520 | strcat(mode, "b"); |
521 | else if (in_crlf) |
522 | strcat(mode, "t"); |
0c19750d |
523 | #endif |
524 | |
4a7d1889 |
525 | if (num_svs > 1) { |
526 | fp = PerlProc_popen_list(mode,num_svs,svp); |
527 | } |
e620cd72 |
528 | else { |
4a7d1889 |
529 | fp = PerlProc_popen(name,mode); |
530 | } |
50952442 |
531 | IoTYPE(io) = IoTYPE_PIPE; |
1771866f |
532 | if (num_svs) { |
533 | for (; isSPACE(*type); type++) ; |
534 | if (*type) { |
535 | if (PerlIO_apply_layers(aTHX_ fp, mode, type) != 0) { |
536 | goto say_false; |
537 | } |
538 | } |
539 | } |
a687059c |
540 | } |
c2be40b1 |
541 | else { /* layer(Args) */ |
6170680b |
542 | if (num_svs) |
c2be40b1 |
543 | goto unknown_open_mode; |
6170680b |
544 | name = type; |
50952442 |
545 | IoTYPE(io) = IoTYPE_RDONLY; |
99b89507 |
546 | /*SUPPRESS 530*/ |
547 | for (; isSPACE(*name); name++) ; |
88b61e10 |
548 | mode[0] = 'r'; |
0c19750d |
549 | |
550 | #ifdef HAS_STRLCAT |
551 | if (in_raw) |
552 | strlcat(mode, "b", PERL_MODE_MAX); |
553 | else if (in_crlf) |
554 | strlcat(mode, "t", PERL_MODE_MAX); |
555 | #else |
88b61e10 |
556 | if (in_raw) |
557 | strcat(mode, "b"); |
558 | else if (in_crlf) |
559 | strcat(mode, "t"); |
0c19750d |
560 | #endif |
561 | |
770526c1 |
562 | if (*name == '-' && name[1] == '\0') { |
760ac839 |
563 | fp = PerlIO_stdin(); |
50952442 |
564 | IoTYPE(io) = IoTYPE_STD; |
a687059c |
565 | } |
16fe6d59 |
566 | else { |
ee518936 |
567 | if (!num_svs) { |
568 | namesv = sv_2mortal(newSVpvn(type,strlen(type))); |
569 | num_svs = 1; |
570 | svp = &namesv; |
1141d9f8 |
571 | type = Nullch; |
ee518936 |
572 | } |
6e60e805 |
573 | fp = PerlIO_openn(aTHX_ type,mode,-1,0,0,NULL,num_svs,svp); |
16fe6d59 |
574 | } |
a687059c |
575 | } |
576 | } |
bee1dbe2 |
577 | if (!fp) { |
50952442 |
578 | if (ckWARN(WARN_NEWLINE) && IoTYPE(io) == IoTYPE_RDONLY && strchr(name, '\n')) |
9014280d |
579 | Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "open"); |
6e21c824 |
580 | goto say_false; |
bee1dbe2 |
581 | } |
a00b5bd3 |
582 | |
583 | if (ckWARN(WARN_IO)) { |
584 | if ((IoTYPE(io) == IoTYPE_RDONLY) && |
585 | (fp == PerlIO_stdout() || fp == PerlIO_stderr())) { |
9014280d |
586 | Perl_warner(aTHX_ packWARN(WARN_IO), |
97828cef |
587 | "Filehandle STD%s reopened as %s only for input", |
588 | ((fp == PerlIO_stdout()) ? "OUT" : "ERR"), |
589 | GvENAME(gv)); |
a00b5bd3 |
590 | } |
ee518936 |
591 | else if ((IoTYPE(io) == IoTYPE_WRONLY) && fp == PerlIO_stdin()) { |
9014280d |
592 | Perl_warner(aTHX_ packWARN(WARN_IO), |
97828cef |
593 | "Filehandle STDIN reopened as %s only for output", |
594 | GvENAME(gv)); |
a00b5bd3 |
595 | } |
596 | } |
597 | |
e99cca91 |
598 | fd = PerlIO_fileno(fp); |
e934609f |
599 | /* If there is no fd (e.g. PerlIO::scalar) assume it isn't a |
600 | * socket - this covers PerlIO::scalar - otherwise unless we "know" the |
e99cca91 |
601 | * type probe for socket-ness. |
602 | */ |
603 | if (IoTYPE(io) && IoTYPE(io) != IoTYPE_PIPE && IoTYPE(io) != IoTYPE_STD && fd >= 0) { |
604 | if (PerlLIO_fstat(fd,&PL_statbuf) < 0) { |
605 | /* If PerlIO claims to have fd we had better be able to fstat() it. */ |
606 | (void) PerlIO_close(fp); |
6e21c824 |
607 | goto say_false; |
a687059c |
608 | } |
7114a2d2 |
609 | #ifndef PERL_MICRO |
3280af22 |
610 | if (S_ISSOCK(PL_statbuf.st_mode)) |
50952442 |
611 | IoTYPE(io) = IoTYPE_SOCKET; /* in case a socket was passed in to us */ |
99b89507 |
612 | #ifdef HAS_SOCKET |
613 | else if ( |
c623bd54 |
614 | #ifdef S_IFMT |
3280af22 |
615 | !(PL_statbuf.st_mode & S_IFMT) |
99b89507 |
616 | #else |
b28d0864 |
617 | !PL_statbuf.st_mode |
99b89507 |
618 | #endif |
0759c907 |
619 | && IoTYPE(io) != IoTYPE_WRONLY /* Dups of STD* filehandles already have */ |
620 | && IoTYPE(io) != IoTYPE_RDONLY /* type so they aren't marked as sockets */ |
621 | ) { /* on OS's that return 0 on fstat()ed pipe */ |
e99cca91 |
622 | char tmpbuf[256]; |
623 | Sock_size_t buflen = sizeof tmpbuf; |
624 | if (PerlSock_getsockname(fd, (struct sockaddr *)tmpbuf, &buflen) >= 0 |
625 | || errno != ENOTSOCK) |
626 | IoTYPE(io) = IoTYPE_SOCKET; /* some OS's return 0 on fstat()ed socket */ |
627 | /* but some return 0 for streams too, sigh */ |
99b89507 |
628 | } |
e99cca91 |
629 | #endif /* HAS_SOCKET */ |
7114a2d2 |
630 | #endif /* !PERL_MICRO */ |
a687059c |
631 | } |
e99cca91 |
632 | |
633 | /* Eeek - FIXME !!! |
634 | * If this is a standard handle we discard all the layer stuff |
635 | * and just dup the fd into whatever was on the handle before ! |
636 | */ |
637 | |
6e21c824 |
638 | if (saveifp) { /* must use old fp? */ |
f5b9d040 |
639 | /* If fd is less that PL_maxsysfd i.e. STDIN..STDERR |
24c23ab4 |
640 | then dup the new fileno down |
f5b9d040 |
641 | */ |
6e21c824 |
642 | if (saveofp) { |
f5b9d040 |
643 | PerlIO_flush(saveofp); /* emulate PerlIO_close() */ |
6e21c824 |
644 | if (saveofp != saveifp) { /* was a socket? */ |
760ac839 |
645 | PerlIO_close(saveofp); |
6e21c824 |
646 | } |
647 | } |
6e60e805 |
648 | if (savefd != fd) { |
e934609f |
649 | /* Still a small can-of-worms here if (say) PerlIO::scalar |
ecdeb87c |
650 | is assigned to (say) STDOUT - for now let dup2() fail |
651 | and provide the error |
652 | */ |
bd4a5668 |
653 | if (PerlLIO_dup2(fd, savefd) < 0) { |
654 | (void)PerlIO_close(fp); |
655 | goto say_false; |
656 | } |
d082dcd6 |
657 | #ifdef VMS |
6e60e805 |
658 | if (savefd != PerlIO_fileno(PerlIO_stdin())) { |
d0e2cf63 |
659 | char newname[FILENAME_MAX+1]; |
660 | if (PerlIO_getname(fp, newname)) { |
661 | if (fd == PerlIO_fileno(PerlIO_stdout())) |
662 | Perl_vmssetuserlnm(aTHX_ "SYS$OUTPUT", newname); |
663 | if (fd == PerlIO_fileno(PerlIO_stderr())) |
664 | Perl_vmssetuserlnm(aTHX_ "SYS$ERROR", newname); |
665 | } |
d082dcd6 |
666 | } |
667 | #endif |
d0e2cf63 |
668 | |
669 | #if !defined(WIN32) |
670 | /* PL_fdpid isn't used on Windows, so avoid this useless work. |
671 | * XXX Probably the same for a lot of other places. */ |
672 | { |
673 | Pid_t pid; |
674 | SV *sv; |
675 | |
676 | LOCK_FDPID_MUTEX; |
677 | sv = *av_fetch(PL_fdpid,fd,TRUE); |
678 | (void)SvUPGRADE(sv, SVt_IV); |
679 | pid = SvIVX(sv); |
680 | SvIVX(sv) = 0; |
681 | sv = *av_fetch(PL_fdpid,savefd,TRUE); |
682 | (void)SvUPGRADE(sv, SVt_IV); |
683 | SvIVX(sv) = pid; |
684 | UNLOCK_FDPID_MUTEX; |
685 | } |
686 | #endif |
687 | |
e212fc47 |
688 | if (was_fdopen) { |
689 | /* need to close fp without closing underlying fd */ |
690 | int ofd = PerlIO_fileno(fp); |
691 | int dupfd = PerlLIO_dup(ofd); |
03e631df |
692 | #if defined(HAS_FCNTL) && defined(F_SETFD) |
693 | /* Assume if we have F_SETFD we have F_GETFD */ |
694 | int coe = fcntl(ofd,F_GETFD); |
695 | #endif |
e212fc47 |
696 | PerlIO_close(fp); |
697 | PerlLIO_dup2(dupfd,ofd); |
03e631df |
698 | #if defined(HAS_FCNTL) && defined(F_SETFD) |
699 | /* The dup trick has lost close-on-exec on ofd */ |
700 | fcntl(ofd,F_SETFD, coe); |
701 | #endif |
e212fc47 |
702 | PerlLIO_close(dupfd); |
ecdeb87c |
703 | } |
e212fc47 |
704 | else |
705 | PerlIO_close(fp); |
6e21c824 |
706 | } |
707 | fp = saveifp; |
760ac839 |
708 | PerlIO_clearerr(fp); |
e99cca91 |
709 | fd = PerlIO_fileno(fp); |
6e21c824 |
710 | } |
a0d0e21e |
711 | #if defined(HAS_FCNTL) && defined(F_SETFD) |
e99cca91 |
712 | if (fd >= 0) { |
a8710ca1 |
713 | int save_errno = errno; |
a8710ca1 |
714 | fcntl(fd,F_SETFD,fd > PL_maxsysfd); /* can change errno */ |
715 | errno = save_errno; |
716 | } |
1462b684 |
717 | #endif |
8990e307 |
718 | IoIFP(io) = fp; |
b931b1d9 |
719 | |
684bef36 |
720 | IoFLAGS(io) &= ~IOf_NOLINE; |
bf38876a |
721 | if (writing) { |
50952442 |
722 | if (IoTYPE(io) == IoTYPE_SOCKET |
e99cca91 |
723 | || (IoTYPE(io) == IoTYPE_WRONLY && fd >= 0 && S_ISCHR(PL_statbuf.st_mode)) ) { |
a33cf58c |
724 | char *s = mode; |
3b6c1aba |
725 | if (*s == IoTYPE_IMPLICIT || *s == IoTYPE_NUMERIC) |
726 | s++; |
a33cf58c |
727 | *s = 'w'; |
1feb1812 |
728 | if (!(IoOFP(io) = PerlIO_openn(aTHX_ type,s,fd,0,0,NULL,0,svp))) { |
760ac839 |
729 | PerlIO_close(fp); |
8990e307 |
730 | IoIFP(io) = Nullfp; |
6e21c824 |
731 | goto say_false; |
fe14fcc3 |
732 | } |
1462b684 |
733 | } |
734 | else |
8990e307 |
735 | IoOFP(io) = fp; |
bf38876a |
736 | } |
a687059c |
737 | return TRUE; |
6e21c824 |
738 | |
739 | say_false: |
8990e307 |
740 | IoIFP(io) = saveifp; |
741 | IoOFP(io) = saveofp; |
742 | IoTYPE(io) = savetype; |
6e21c824 |
743 | return FALSE; |
a687059c |
744 | } |
745 | |
760ac839 |
746 | PerlIO * |
864dbfa3 |
747 | Perl_nextargv(pTHX_ register GV *gv) |
a687059c |
748 | { |
79072805 |
749 | register SV *sv; |
99b89507 |
750 | #ifndef FLEXFILENAMES |
c623bd54 |
751 | int filedev; |
752 | int fileino; |
99b89507 |
753 | #endif |
761237fe |
754 | Uid_t fileuid; |
755 | Gid_t filegid; |
18708f5a |
756 | IO *io = GvIOp(gv); |
fe14fcc3 |
757 | |
3280af22 |
758 | if (!PL_argvoutgv) |
759 | PL_argvoutgv = gv_fetchpv("ARGVOUT",TRUE,SVt_PVIO); |
18708f5a |
760 | if (io && (IoFLAGS(io) & IOf_ARGV) && (IoFLAGS(io) & IOf_START)) { |
761 | IoFLAGS(io) &= ~IOf_START; |
7a1c5554 |
762 | if (PL_inplace) { |
763 | if (!PL_argvout_stack) |
764 | PL_argvout_stack = newAV(); |
18708f5a |
765 | av_push(PL_argvout_stack, SvREFCNT_inc(PL_defoutgv)); |
7a1c5554 |
766 | } |
18708f5a |
767 | } |
3280af22 |
768 | if (PL_filemode & (S_ISUID|S_ISGID)) { |
769 | PerlIO_flush(IoIFP(GvIOn(PL_argvoutgv))); /* chmod must follow last write */ |
fe14fcc3 |
770 | #ifdef HAS_FCHMOD |
c797f2d8 |
771 | if (PL_lastfd != -1) |
772 | (void)fchmod(PL_lastfd,PL_filemode); |
fe14fcc3 |
773 | #else |
b28d0864 |
774 | (void)PerlLIO_chmod(PL_oldname,PL_filemode); |
fe14fcc3 |
775 | #endif |
776 | } |
c797f2d8 |
777 | PL_lastfd = -1; |
3280af22 |
778 | PL_filemode = 0; |
5c501b37 |
779 | if (!GvAV(gv)) |
780 | return Nullfp; |
79072805 |
781 | while (av_len(GvAV(gv)) >= 0) { |
85aff577 |
782 | STRLEN oldlen; |
79072805 |
783 | sv = av_shift(GvAV(gv)); |
8990e307 |
784 | SAVEFREESV(sv); |
79072805 |
785 | sv_setsv(GvSV(gv),sv); |
786 | SvSETMAGIC(GvSV(gv)); |
3280af22 |
787 | PL_oldname = SvPVx(GvSV(gv), oldlen); |
9d116dd7 |
788 | if (do_open(gv,PL_oldname,oldlen,PL_inplace!=0,O_RDONLY,0,Nullfp)) { |
3280af22 |
789 | if (PL_inplace) { |
79072805 |
790 | TAINT_PROPER("inplace open"); |
3280af22 |
791 | if (oldlen == 1 && *PL_oldname == '-') { |
4633a7c4 |
792 | setdefout(gv_fetchpv("STDOUT",TRUE,SVt_PVIO)); |
a0d0e21e |
793 | return IoIFP(GvIOp(gv)); |
c623bd54 |
794 | } |
99b89507 |
795 | #ifndef FLEXFILENAMES |
b28d0864 |
796 | filedev = PL_statbuf.st_dev; |
797 | fileino = PL_statbuf.st_ino; |
99b89507 |
798 | #endif |
3280af22 |
799 | PL_filemode = PL_statbuf.st_mode; |
800 | fileuid = PL_statbuf.st_uid; |
801 | filegid = PL_statbuf.st_gid; |
802 | if (!S_ISREG(PL_filemode)) { |
0453d815 |
803 | if (ckWARN_d(WARN_INPLACE)) |
9014280d |
804 | Perl_warner(aTHX_ packWARN(WARN_INPLACE), |
0453d815 |
805 | "Can't do inplace edit: %s is not a regular file", |
806 | PL_oldname ); |
79072805 |
807 | do_close(gv,FALSE); |
c623bd54 |
808 | continue; |
809 | } |
3280af22 |
810 | if (*PL_inplace) { |
811 | char *star = strchr(PL_inplace, '*'); |
2d259d92 |
812 | if (star) { |
3280af22 |
813 | char *begin = PL_inplace; |
2d259d92 |
814 | sv_setpvn(sv, "", 0); |
815 | do { |
816 | sv_catpvn(sv, begin, star - begin); |
3280af22 |
817 | sv_catpvn(sv, PL_oldname, oldlen); |
2d259d92 |
818 | begin = ++star; |
819 | } while ((star = strchr(begin, '*'))); |
3d66d7bb |
820 | if (*begin) |
821 | sv_catpv(sv,begin); |
2d259d92 |
822 | } |
823 | else { |
3280af22 |
824 | sv_catpv(sv,PL_inplace); |
2d259d92 |
825 | } |
c623bd54 |
826 | #ifndef FLEXFILENAMES |
5f74f29c |
827 | if ((PerlLIO_stat(SvPVX(sv),&PL_statbuf) >= 0 |
828 | && PL_statbuf.st_dev == filedev |
829 | && PL_statbuf.st_ino == fileino) |
39e571d4 |
830 | #ifdef DJGPP |
5f74f29c |
831 | || ((_djstat_fail_bits & _STFAIL_TRUENAME)!=0) |
39e571d4 |
832 | #endif |
f248d071 |
833 | ) |
834 | { |
835 | if (ckWARN_d(WARN_INPLACE)) |
9014280d |
836 | Perl_warner(aTHX_ packWARN(WARN_INPLACE), |
35c1215d |
837 | "Can't do inplace edit: %"SVf" would not be unique", |
838 | sv); |
79072805 |
839 | do_close(gv,FALSE); |
c623bd54 |
840 | continue; |
841 | } |
842 | #endif |
fe14fcc3 |
843 | #ifdef HAS_RENAME |
2585f9a3 |
844 | #if !defined(DOSISH) && !defined(__CYGWIN__) && !defined(EPOC) |
3280af22 |
845 | if (PerlLIO_rename(PL_oldname,SvPVX(sv)) < 0) { |
0453d815 |
846 | if (ckWARN_d(WARN_INPLACE)) |
9014280d |
847 | Perl_warner(aTHX_ packWARN(WARN_INPLACE), |
35c1215d |
848 | "Can't rename %s to %"SVf": %s, skipping file", |
849 | PL_oldname, sv, Strerror(errno) ); |
79072805 |
850 | do_close(gv,FALSE); |
c623bd54 |
851 | continue; |
852 | } |
a687059c |
853 | #else |
79072805 |
854 | do_close(gv,FALSE); |
3028581b |
855 | (void)PerlLIO_unlink(SvPVX(sv)); |
b28d0864 |
856 | (void)PerlLIO_rename(PL_oldname,SvPVX(sv)); |
9d116dd7 |
857 | do_open(gv,SvPVX(sv),SvCUR(sv),PL_inplace!=0,O_RDONLY,0,Nullfp); |
55497cff |
858 | #endif /* DOSISH */ |
ff8e2863 |
859 | #else |
463ee0b2 |
860 | (void)UNLINK(SvPVX(sv)); |
b28d0864 |
861 | if (link(PL_oldname,SvPVX(sv)) < 0) { |
0453d815 |
862 | if (ckWARN_d(WARN_INPLACE)) |
9014280d |
863 | Perl_warner(aTHX_ packWARN(WARN_INPLACE), |
35c1215d |
864 | "Can't rename %s to %"SVf": %s, skipping file", |
865 | PL_oldname, sv, Strerror(errno) ); |
79072805 |
866 | do_close(gv,FALSE); |
c623bd54 |
867 | continue; |
868 | } |
b28d0864 |
869 | (void)UNLINK(PL_oldname); |
a687059c |
870 | #endif |
871 | } |
872 | else { |
c030f24b |
873 | #if !defined(DOSISH) && !defined(AMIGAOS) |
edc7bc49 |
874 | # ifndef VMS /* Don't delete; use automatic file versioning */ |
3280af22 |
875 | if (UNLINK(PL_oldname) < 0) { |
0453d815 |
876 | if (ckWARN_d(WARN_INPLACE)) |
9014280d |
877 | Perl_warner(aTHX_ packWARN(WARN_INPLACE), |
0453d815 |
878 | "Can't remove %s: %s, skipping file", |
879 | PL_oldname, Strerror(errno) ); |
79072805 |
880 | do_close(gv,FALSE); |
fe14fcc3 |
881 | continue; |
882 | } |
edc7bc49 |
883 | # endif |
ff8e2863 |
884 | #else |
cea2e8a9 |
885 | Perl_croak(aTHX_ "Can't do inplace edit without backup"); |
ff8e2863 |
886 | #endif |
a687059c |
887 | } |
888 | |
3280af22 |
889 | sv_setpvn(sv,">",!PL_inplace); |
890 | sv_catpvn(sv,PL_oldname,oldlen); |
748a9306 |
891 | SETERRNO(0,0); /* in case sprintf set errno */ |
4119ab01 |
892 | #ifdef VMS |
893 | if (!do_open(PL_argvoutgv,SvPVX(sv),SvCUR(sv),PL_inplace!=0, |
18708f5a |
894 | O_WRONLY|O_CREAT|O_TRUNC,0,Nullfp)) |
4119ab01 |
895 | #else |
3280af22 |
896 | if (!do_open(PL_argvoutgv,SvPVX(sv),SvCUR(sv),PL_inplace!=0, |
18708f5a |
897 | O_WRONLY|O_CREAT|OPEN_EXCL,0666,Nullfp)) |
4119ab01 |
898 | #endif |
18708f5a |
899 | { |
0453d815 |
900 | if (ckWARN_d(WARN_INPLACE)) |
9014280d |
901 | Perl_warner(aTHX_ packWARN(WARN_INPLACE), "Can't do inplace edit on %s: %s", |
0453d815 |
902 | PL_oldname, Strerror(errno) ); |
79072805 |
903 | do_close(gv,FALSE); |
fe14fcc3 |
904 | continue; |
905 | } |
3280af22 |
906 | setdefout(PL_argvoutgv); |
907 | PL_lastfd = PerlIO_fileno(IoIFP(GvIOp(PL_argvoutgv))); |
908 | (void)PerlLIO_fstat(PL_lastfd,&PL_statbuf); |
fe14fcc3 |
909 | #ifdef HAS_FCHMOD |
3280af22 |
910 | (void)fchmod(PL_lastfd,PL_filemode); |
a687059c |
911 | #else |
3e3baf6d |
912 | # if !(defined(WIN32) && defined(__BORLANDC__)) |
913 | /* Borland runtime creates a readonly file! */ |
b28d0864 |
914 | (void)PerlLIO_chmod(PL_oldname,PL_filemode); |
3e3baf6d |
915 | # endif |
a687059c |
916 | #endif |
3280af22 |
917 | if (fileuid != PL_statbuf.st_uid || filegid != PL_statbuf.st_gid) { |
fe14fcc3 |
918 | #ifdef HAS_FCHOWN |
3280af22 |
919 | (void)fchown(PL_lastfd,fileuid,filegid); |
a687059c |
920 | #else |
fe14fcc3 |
921 | #ifdef HAS_CHOWN |
b28d0864 |
922 | (void)PerlLIO_chown(PL_oldname,fileuid,filegid); |
a687059c |
923 | #endif |
b1248f16 |
924 | #endif |
fe14fcc3 |
925 | } |
a687059c |
926 | } |
a0d0e21e |
927 | return IoIFP(GvIOp(gv)); |
a687059c |
928 | } |
4d61ec05 |
929 | else { |
4d61ec05 |
930 | if (ckWARN_d(WARN_INPLACE)) { |
6af84f9f |
931 | int eno = errno; |
932 | if (PerlLIO_stat(PL_oldname, &PL_statbuf) >= 0 |
933 | && !S_ISREG(PL_statbuf.st_mode)) |
934 | { |
9014280d |
935 | Perl_warner(aTHX_ packWARN(WARN_INPLACE), |
4d61ec05 |
936 | "Can't do inplace edit: %s is not a regular file", |
9a7dcd9c |
937 | PL_oldname); |
6af84f9f |
938 | } |
4d61ec05 |
939 | else |
9014280d |
940 | Perl_warner(aTHX_ packWARN(WARN_INPLACE), "Can't open %s: %s", |
6af84f9f |
941 | PL_oldname, Strerror(eno)); |
4d61ec05 |
942 | } |
943 | } |
a687059c |
944 | } |
18708f5a |
945 | if (io && (IoFLAGS(io) & IOf_ARGV)) |
946 | IoFLAGS(io) |= IOf_START; |
3280af22 |
947 | if (PL_inplace) { |
948 | (void)do_close(PL_argvoutgv,FALSE); |
7a1c5554 |
949 | if (io && (IoFLAGS(io) & IOf_ARGV) |
950 | && PL_argvout_stack && AvFILLp(PL_argvout_stack) >= 0) |
951 | { |
18708f5a |
952 | GV *oldout = (GV*)av_pop(PL_argvout_stack); |
953 | setdefout(oldout); |
954 | SvREFCNT_dec(oldout); |
955 | return Nullfp; |
956 | } |
4633a7c4 |
957 | setdefout(gv_fetchpv("STDOUT",TRUE,SVt_PVIO)); |
a687059c |
958 | } |
959 | return Nullfp; |
960 | } |
961 | |
fe14fcc3 |
962 | #ifdef HAS_PIPE |
afd9f252 |
963 | void |
864dbfa3 |
964 | Perl_do_pipe(pTHX_ SV *sv, GV *rgv, GV *wgv) |
afd9f252 |
965 | { |
79072805 |
966 | register IO *rstio; |
967 | register IO *wstio; |
afd9f252 |
968 | int fd[2]; |
969 | |
79072805 |
970 | if (!rgv) |
afd9f252 |
971 | goto badexit; |
79072805 |
972 | if (!wgv) |
afd9f252 |
973 | goto badexit; |
974 | |
a0d0e21e |
975 | rstio = GvIOn(rgv); |
976 | wstio = GvIOn(wgv); |
afd9f252 |
977 | |
a0d0e21e |
978 | if (IoIFP(rstio)) |
79072805 |
979 | do_close(rgv,FALSE); |
a0d0e21e |
980 | if (IoIFP(wstio)) |
79072805 |
981 | do_close(wgv,FALSE); |
afd9f252 |
982 | |
3028581b |
983 | if (PerlProc_pipe(fd) < 0) |
afd9f252 |
984 | goto badexit; |
460c8493 |
985 | IoIFP(rstio) = PerlIO_fdopen(fd[0], "r"PIPE_OPEN_MODE); |
986 | IoOFP(wstio) = PerlIO_fdopen(fd[1], "w"PIPE_OPEN_MODE); |
b5ac89c3 |
987 | IoOFP(rstio) = IoIFP(rstio); |
8990e307 |
988 | IoIFP(wstio) = IoOFP(wstio); |
50952442 |
989 | IoTYPE(rstio) = IoTYPE_RDONLY; |
990 | IoTYPE(wstio) = IoTYPE_WRONLY; |
8990e307 |
991 | if (!IoIFP(rstio) || !IoOFP(wstio)) { |
760ac839 |
992 | if (IoIFP(rstio)) PerlIO_close(IoIFP(rstio)); |
3028581b |
993 | else PerlLIO_close(fd[0]); |
760ac839 |
994 | if (IoOFP(wstio)) PerlIO_close(IoOFP(wstio)); |
3028581b |
995 | else PerlLIO_close(fd[1]); |
fe14fcc3 |
996 | goto badexit; |
997 | } |
afd9f252 |
998 | |
3280af22 |
999 | sv_setsv(sv,&PL_sv_yes); |
afd9f252 |
1000 | return; |
1001 | |
1002 | badexit: |
3280af22 |
1003 | sv_setsv(sv,&PL_sv_undef); |
afd9f252 |
1004 | return; |
1005 | } |
b1248f16 |
1006 | #endif |
afd9f252 |
1007 | |
517844ec |
1008 | /* explicit renamed to avoid C++ conflict -- kja */ |
a687059c |
1009 | bool |
864dbfa3 |
1010 | Perl_do_close(pTHX_ GV *gv, bool not_implicit) |
a687059c |
1011 | { |
1193dd27 |
1012 | bool retval; |
1013 | IO *io; |
a687059c |
1014 | |
79072805 |
1015 | if (!gv) |
3280af22 |
1016 | gv = PL_argvgv; |
a0d0e21e |
1017 | if (!gv || SvTYPE(gv) != SVt_PVGV) { |
1d2dff63 |
1018 | if (not_implicit) |
93189314 |
1019 | SETERRNO(EBADF,SS_IVCHAN); |
c2ab57d4 |
1020 | return FALSE; |
99b89507 |
1021 | } |
79072805 |
1022 | io = GvIO(gv); |
1023 | if (!io) { /* never opened */ |
1d2dff63 |
1024 | if (not_implicit) { |
2dd78f96 |
1025 | if (ckWARN(WARN_UNOPENED)) /* no check for closed here */ |
1026 | report_evil_fh(gv, io, PL_op->op_type); |
93189314 |
1027 | SETERRNO(EBADF,SS_IVCHAN); |
1d2dff63 |
1028 | } |
a687059c |
1029 | return FALSE; |
1030 | } |
f2b5be74 |
1031 | retval = io_close(io, not_implicit); |
517844ec |
1032 | if (not_implicit) { |
1193dd27 |
1033 | IoLINES(io) = 0; |
1034 | IoPAGE(io) = 0; |
1035 | IoLINES_LEFT(io) = IoPAGE_LEN(io); |
1036 | } |
50952442 |
1037 | IoTYPE(io) = IoTYPE_CLOSED; |
1193dd27 |
1038 | return retval; |
1039 | } |
1040 | |
1041 | bool |
f2b5be74 |
1042 | Perl_io_close(pTHX_ IO *io, bool not_implicit) |
1193dd27 |
1043 | { |
1044 | bool retval = FALSE; |
1045 | int status; |
1046 | |
8990e307 |
1047 | if (IoIFP(io)) { |
50952442 |
1048 | if (IoTYPE(io) == IoTYPE_PIPE) { |
3028581b |
1049 | status = PerlProc_pclose(IoIFP(io)); |
f2b5be74 |
1050 | if (not_implicit) { |
1051 | STATUS_NATIVE_SET(status); |
1052 | retval = (STATUS_POSIX == 0); |
1053 | } |
1054 | else { |
1055 | retval = (status != -1); |
1056 | } |
a687059c |
1057 | } |
50952442 |
1058 | else if (IoTYPE(io) == IoTYPE_STD) |
a687059c |
1059 | retval = TRUE; |
1060 | else { |
8990e307 |
1061 | if (IoOFP(io) && IoOFP(io) != IoIFP(io)) { /* a socket */ |
760ac839 |
1062 | retval = (PerlIO_close(IoOFP(io)) != EOF); |
1063 | PerlIO_close(IoIFP(io)); /* clear stdio, fd already closed */ |
c2ab57d4 |
1064 | } |
1065 | else |
760ac839 |
1066 | retval = (PerlIO_close(IoIFP(io)) != EOF); |
a687059c |
1067 | } |
8990e307 |
1068 | IoOFP(io) = IoIFP(io) = Nullfp; |
79072805 |
1069 | } |
f2b5be74 |
1070 | else if (not_implicit) { |
93189314 |
1071 | SETERRNO(EBADF,SS_IVCHAN); |
20408e3c |
1072 | } |
1193dd27 |
1073 | |
a687059c |
1074 | return retval; |
1075 | } |
1076 | |
1077 | bool |
864dbfa3 |
1078 | Perl_do_eof(pTHX_ GV *gv) |
a687059c |
1079 | { |
79072805 |
1080 | register IO *io; |
a687059c |
1081 | int ch; |
1082 | |
79072805 |
1083 | io = GvIO(gv); |
a687059c |
1084 | |
79072805 |
1085 | if (!io) |
a687059c |
1086 | return TRUE; |
a00b5bd3 |
1087 | else if (ckWARN(WARN_IO) && (IoTYPE(io) == IoTYPE_WRONLY)) |
06bcfee8 |
1088 | report_evil_fh(gv, io, OP_phoney_OUTPUT_ONLY); |
a687059c |
1089 | |
8990e307 |
1090 | while (IoIFP(io)) { |
505fdf5d |
1091 | int saverrno; |
a687059c |
1092 | |
760ac839 |
1093 | if (PerlIO_has_cntptr(IoIFP(io))) { /* (the code works without this) */ |
a20bf0c3 |
1094 | if (PerlIO_get_cnt(IoIFP(io)) > 0) /* cheat a little, since */ |
760ac839 |
1095 | return FALSE; /* this is the most usual case */ |
1096 | } |
a687059c |
1097 | |
505fdf5d |
1098 | saverrno = errno; /* getc and ungetc can stomp on errno */ |
760ac839 |
1099 | ch = PerlIO_getc(IoIFP(io)); |
a687059c |
1100 | if (ch != EOF) { |
760ac839 |
1101 | (void)PerlIO_ungetc(IoIFP(io),ch); |
505fdf5d |
1102 | errno = saverrno; |
a687059c |
1103 | return FALSE; |
1104 | } |
505fdf5d |
1105 | errno = saverrno; |
fab3f3a7 |
1106 | |
760ac839 |
1107 | if (PerlIO_has_cntptr(IoIFP(io)) && PerlIO_canset_cnt(IoIFP(io))) { |
a20bf0c3 |
1108 | if (PerlIO_get_cnt(IoIFP(io)) < -1) |
1109 | PerlIO_set_cnt(IoIFP(io),-1); |
760ac839 |
1110 | } |
533c011a |
1111 | if (PL_op->op_flags & OPf_SPECIAL) { /* not necessarily a real EOF yet? */ |
ed2c6b9b |
1112 | if (gv != PL_argvgv || !nextargv(gv)) /* get another fp handy */ |
a687059c |
1113 | return TRUE; |
1114 | } |
1115 | else |
1116 | return TRUE; /* normal fp, definitely end of file */ |
1117 | } |
1118 | return TRUE; |
1119 | } |
1120 | |
5ff3f7a4 |
1121 | Off_t |
864dbfa3 |
1122 | Perl_do_tell(pTHX_ GV *gv) |
a687059c |
1123 | { |
9c5ffd7c |
1124 | register IO *io = 0; |
96e4d5b1 |
1125 | register PerlIO *fp; |
a687059c |
1126 | |
96e4d5b1 |
1127 | if (gv && (io = GvIO(gv)) && (fp = IoIFP(io))) { |
bee1dbe2 |
1128 | #ifdef ULTRIX_STDIO_BOTCH |
96e4d5b1 |
1129 | if (PerlIO_eof(fp)) |
1130 | (void)PerlIO_seek(fp, 0L, 2); /* ultrix 1.2 workaround */ |
bee1dbe2 |
1131 | #endif |
8903cb82 |
1132 | return PerlIO_tell(fp); |
96e4d5b1 |
1133 | } |
411caa50 |
1134 | if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)) |
1135 | report_evil_fh(gv, io, PL_op->op_type); |
93189314 |
1136 | SETERRNO(EBADF,RMS_IFI); |
5ff3f7a4 |
1137 | return (Off_t)-1; |
a687059c |
1138 | } |
1139 | |
1140 | bool |
864dbfa3 |
1141 | Perl_do_seek(pTHX_ GV *gv, Off_t pos, int whence) |
a687059c |
1142 | { |
9c5ffd7c |
1143 | register IO *io = 0; |
137443ea |
1144 | register PerlIO *fp; |
a687059c |
1145 | |
137443ea |
1146 | if (gv && (io = GvIO(gv)) && (fp = IoIFP(io))) { |
bee1dbe2 |
1147 | #ifdef ULTRIX_STDIO_BOTCH |
137443ea |
1148 | if (PerlIO_eof(fp)) |
1149 | (void)PerlIO_seek(fp, 0L, 2); /* ultrix 1.2 workaround */ |
bee1dbe2 |
1150 | #endif |
8903cb82 |
1151 | return PerlIO_seek(fp, pos, whence) >= 0; |
137443ea |
1152 | } |
411caa50 |
1153 | if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)) |
1154 | report_evil_fh(gv, io, PL_op->op_type); |
93189314 |
1155 | SETERRNO(EBADF,RMS_IFI); |
a687059c |
1156 | return FALSE; |
1157 | } |
1158 | |
97cc44eb |
1159 | Off_t |
864dbfa3 |
1160 | Perl_do_sysseek(pTHX_ GV *gv, Off_t pos, int whence) |
8903cb82 |
1161 | { |
9c5ffd7c |
1162 | register IO *io = 0; |
8903cb82 |
1163 | register PerlIO *fp; |
1164 | |
1165 | if (gv && (io = GvIO(gv)) && (fp = IoIFP(io))) |
3028581b |
1166 | return PerlLIO_lseek(PerlIO_fileno(fp), pos, whence); |
411caa50 |
1167 | if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)) |
1168 | report_evil_fh(gv, io, PL_op->op_type); |
93189314 |
1169 | SETERRNO(EBADF,RMS_IFI); |
d9b3e12d |
1170 | return (Off_t)-1; |
8903cb82 |
1171 | } |
1172 | |
6ff81951 |
1173 | int |
16fe6d59 |
1174 | Perl_mode_from_discipline(pTHX_ SV *discp) |
1175 | { |
1176 | int mode = O_BINARY; |
1177 | if (discp) { |
1178 | STRLEN len; |
1179 | char *s = SvPV(discp,len); |
1180 | while (*s) { |
1181 | if (*s == ':') { |
1182 | switch (s[1]) { |
1183 | case 'r': |
e963d6d2 |
1184 | if (s[2] == 'a' && s[3] == 'w' |
16fe6d59 |
1185 | && (!s[4] || s[4] == ':' || isSPACE(s[4]))) |
1186 | { |
1187 | mode = O_BINARY; |
1188 | s += 4; |
1189 | len -= 4; |
1190 | break; |
1191 | } |
1192 | /* FALL THROUGH */ |
1193 | case 'c': |
e963d6d2 |
1194 | if (s[2] == 'r' && s[3] == 'l' && s[4] == 'f' |
16fe6d59 |
1195 | && (!s[5] || s[5] == ':' || isSPACE(s[5]))) |
1196 | { |
1197 | mode = O_TEXT; |
1198 | s += 5; |
1199 | len -= 5; |
1200 | break; |
1201 | } |
1202 | /* FALL THROUGH */ |
1203 | default: |
1204 | goto fail_discipline; |
1205 | } |
1206 | } |
1207 | else if (isSPACE(*s)) { |
1208 | ++s; |
1209 | --len; |
1210 | } |
1211 | else { |
1212 | char *end; |
1213 | fail_discipline: |
1214 | end = strchr(s+1, ':'); |
1215 | if (!end) |
1216 | end = s+len; |
60382766 |
1217 | #ifndef PERLIO_LAYERS |
363c40c4 |
1218 | Perl_croak(aTHX_ "IO layers (like '%.*s') unavailable", end-s, s); |
60382766 |
1219 | #else |
18a33fb5 |
1220 | len -= end-s; |
60382766 |
1221 | s = end; |
1222 | #endif |
16fe6d59 |
1223 | } |
1224 | } |
1225 | } |
1226 | return mode; |
1227 | } |
1228 | |
1229 | int |
1230 | Perl_do_binmode(pTHX_ PerlIO *fp, int iotype, int mode) |
6ff81951 |
1231 | { |
60382766 |
1232 | /* The old body of this is now in non-LAYER part of perlio.c |
1233 | * This is a stub for any XS code which might have been calling it. |
1234 | */ |
6ce75a77 |
1235 | char *name = ":raw"; |
35990314 |
1236 | #ifdef PERLIO_USING_CRLF |
8b24e160 |
1237 | if (!(mode & O_BINARY)) |
6ce75a77 |
1238 | name = ":crlf"; |
1239 | #endif |
60382766 |
1240 | return PerlIO_binmode(aTHX_ fp, iotype, mode, name); |
6ff81951 |
1241 | } |
1242 | |
a0d0e21e |
1243 | #if !defined(HAS_TRUNCATE) && !defined(HAS_CHSIZE) && defined(F_FREESP) |
c2ab57d4 |
1244 | /* code courtesy of William Kucharski */ |
fe14fcc3 |
1245 | #define HAS_CHSIZE |
6eb13c3b |
1246 | |
517844ec |
1247 | I32 my_chsize(fd, length) |
79072805 |
1248 | I32 fd; /* file descriptor */ |
85e6fe83 |
1249 | Off_t length; /* length to set file to */ |
6eb13c3b |
1250 | { |
6eb13c3b |
1251 | struct flock fl; |
c623ac67 |
1252 | Stat_t filebuf; |
6eb13c3b |
1253 | |
3028581b |
1254 | if (PerlLIO_fstat(fd, &filebuf) < 0) |
6eb13c3b |
1255 | return -1; |
1256 | |
1257 | if (filebuf.st_size < length) { |
1258 | |
1259 | /* extend file length */ |
1260 | |
3028581b |
1261 | if ((PerlLIO_lseek(fd, (length - 1), 0)) < 0) |
6eb13c3b |
1262 | return -1; |
1263 | |
1264 | /* write a "0" byte */ |
1265 | |
3028581b |
1266 | if ((PerlLIO_write(fd, "", 1)) != 1) |
6eb13c3b |
1267 | return -1; |
1268 | } |
1269 | else { |
1270 | /* truncate length */ |
1271 | |
1272 | fl.l_whence = 0; |
1273 | fl.l_len = 0; |
1274 | fl.l_start = length; |
a0d0e21e |
1275 | fl.l_type = F_WRLCK; /* write lock on file space */ |
6eb13c3b |
1276 | |
1277 | /* |
a0d0e21e |
1278 | * This relies on the UNDOCUMENTED F_FREESP argument to |
6eb13c3b |
1279 | * fcntl(2), which truncates the file so that it ends at the |
1280 | * position indicated by fl.l_start. |
1281 | * |
1282 | * Will minor miracles never cease? |
1283 | */ |
1284 | |
a0d0e21e |
1285 | if (fcntl(fd, F_FREESP, &fl) < 0) |
6eb13c3b |
1286 | return -1; |
1287 | |
1288 | } |
1289 | |
1290 | return 0; |
1291 | } |
a0d0e21e |
1292 | #endif /* F_FREESP */ |
ff8e2863 |
1293 | |
a687059c |
1294 | bool |
864dbfa3 |
1295 | Perl_do_print(pTHX_ register SV *sv, PerlIO *fp) |
a687059c |
1296 | { |
1297 | register char *tmps; |
463ee0b2 |
1298 | STRLEN len; |
a687059c |
1299 | |
79072805 |
1300 | /* assuming fp is checked earlier */ |
1301 | if (!sv) |
1302 | return TRUE; |
3280af22 |
1303 | if (PL_ofmt) { |
8990e307 |
1304 | if (SvGMAGICAL(sv)) |
79072805 |
1305 | mg_get(sv); |
463ee0b2 |
1306 | if (SvIOK(sv) && SvIVX(sv) != 0) { |
65202027 |
1307 | PerlIO_printf(fp, PL_ofmt, (NV)SvIVX(sv)); |
760ac839 |
1308 | return !PerlIO_error(fp); |
79072805 |
1309 | } |
463ee0b2 |
1310 | if ( (SvNOK(sv) && SvNVX(sv) != 0.0) |
79072805 |
1311 | || (looks_like_number(sv) && sv_2nv(sv) != 0.0) ) { |
3280af22 |
1312 | PerlIO_printf(fp, PL_ofmt, SvNVX(sv)); |
760ac839 |
1313 | return !PerlIO_error(fp); |
79072805 |
1314 | } |
a687059c |
1315 | } |
79072805 |
1316 | switch (SvTYPE(sv)) { |
1317 | case SVt_NULL: |
411caa50 |
1318 | if (ckWARN(WARN_UNINITIALIZED)) |
29489e7c |
1319 | report_uninit(sv); |
ff8e2863 |
1320 | return TRUE; |
79072805 |
1321 | case SVt_IV: |
a0d0e21e |
1322 | if (SvIOK(sv)) { |
1323 | if (SvGMAGICAL(sv)) |
1324 | mg_get(sv); |
cf2093f6 |
1325 | if (SvIsUV(sv)) |
57def98f |
1326 | PerlIO_printf(fp, "%"UVuf, (UV)SvUVX(sv)); |
cf2093f6 |
1327 | else |
57def98f |
1328 | PerlIO_printf(fp, "%"IVdf, (IV)SvIVX(sv)); |
760ac839 |
1329 | return !PerlIO_error(fp); |
a0d0e21e |
1330 | } |
1331 | /* FALL THROUGH */ |
79072805 |
1332 | default: |
7d59b7e4 |
1333 | if (PerlIO_isutf8(fp)) { |
f9a63242 |
1334 | if (!SvUTF8(sv)) |
88632417 |
1335 | sv_utf8_upgrade_flags(sv = sv_mortalcopy(sv), |
1336 | SV_GMAGIC|SV_UTF8_NO_ENCODING); |
7d59b7e4 |
1337 | } |
ae798467 |
1338 | else if (DO_UTF8(sv)) { |
2da16753 |
1339 | if (!sv_utf8_downgrade((sv = sv_mortalcopy(sv)), TRUE) |
62961d2e |
1340 | && ckWARN_d(WARN_UTF8)) |
2da16753 |
1341 | { |
9014280d |
1342 | Perl_warner(aTHX_ packWARN(WARN_UTF8), "Wide character in print"); |
ae798467 |
1343 | } |
1344 | } |
f9a63242 |
1345 | tmps = SvPV(sv, len); |
79072805 |
1346 | break; |
ff8e2863 |
1347 | } |
94e4c244 |
1348 | /* To detect whether the process is about to overstep its |
1349 | * filesize limit we would need getrlimit(). We could then |
1350 | * also transparently raise the limit with setrlimit() -- |
1351 | * but only until the system hard limit/the filesystem limit, |
c5dd3cdd |
1352 | * at which we would get EPERM. Note that when using buffered |
1353 | * io the write failure can be delayed until the flush/close. --jhi */ |
a21ac455 |
1354 | if (len && (PerlIO_write(fp,tmps,len) == 0)) |
a687059c |
1355 | return FALSE; |
760ac839 |
1356 | return !PerlIO_error(fp); |
a687059c |
1357 | } |
1358 | |
79072805 |
1359 | I32 |
cea2e8a9 |
1360 | Perl_my_stat(pTHX) |
a687059c |
1361 | { |
39644a26 |
1362 | dSP; |
79072805 |
1363 | IO *io; |
2dd78f96 |
1364 | GV* gv; |
79072805 |
1365 | |
533c011a |
1366 | if (PL_op->op_flags & OPf_REF) { |
924508f0 |
1367 | EXTEND(SP,1); |
2dd78f96 |
1368 | gv = cGVOP_gv; |
748a9306 |
1369 | do_fstat: |
2dd78f96 |
1370 | io = GvIO(gv); |
8990e307 |
1371 | if (io && IoIFP(io)) { |
2dd78f96 |
1372 | PL_statgv = gv; |
3280af22 |
1373 | sv_setpv(PL_statname,""); |
1374 | PL_laststype = OP_STAT; |
1375 | return (PL_laststatval = PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache)); |
a687059c |
1376 | } |
1377 | else { |
2dd78f96 |
1378 | if (gv == PL_defgv) |
3280af22 |
1379 | return PL_laststatval; |
2dd78f96 |
1380 | if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)) |
1381 | report_evil_fh(gv, io, PL_op->op_type); |
3280af22 |
1382 | PL_statgv = Nullgv; |
1383 | sv_setpv(PL_statname,""); |
1384 | return (PL_laststatval = -1); |
a687059c |
1385 | } |
1386 | } |
fbb0b3b3 |
1387 | else if (PL_op->op_private & OPpFT_STACKED) { |
1388 | return PL_laststatval; |
1389 | } |
a687059c |
1390 | else { |
748a9306 |
1391 | SV* sv = POPs; |
4b74e3fb |
1392 | char *s; |
4ecd490c |
1393 | STRLEN len; |
79072805 |
1394 | PUTBACK; |
748a9306 |
1395 | if (SvTYPE(sv) == SVt_PVGV) { |
2dd78f96 |
1396 | gv = (GV*)sv; |
748a9306 |
1397 | goto do_fstat; |
1398 | } |
1399 | else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) { |
2dd78f96 |
1400 | gv = (GV*)SvRV(sv); |
748a9306 |
1401 | goto do_fstat; |
1402 | } |
1403 | |
4ecd490c |
1404 | s = SvPV(sv, len); |
3280af22 |
1405 | PL_statgv = Nullgv; |
4ecd490c |
1406 | sv_setpvn(PL_statname, s, len); |
1407 | s = SvPVX(PL_statname); /* s now NUL-terminated */ |
3280af22 |
1408 | PL_laststype = OP_STAT; |
1409 | PL_laststatval = PerlLIO_stat(s, &PL_statcache); |
599cee73 |
1410 | if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(s, '\n')) |
9014280d |
1411 | Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "stat"); |
3280af22 |
1412 | return PL_laststatval; |
a687059c |
1413 | } |
1414 | } |
1415 | |
fbb0b3b3 |
1416 | static char no_prev_lstat[] = "The stat preceding -l _ wasn't an lstat"; |
1417 | |
79072805 |
1418 | I32 |
cea2e8a9 |
1419 | Perl_my_lstat(pTHX) |
c623bd54 |
1420 | { |
39644a26 |
1421 | dSP; |
79072805 |
1422 | SV *sv; |
2d8e6c8d |
1423 | STRLEN n_a; |
533c011a |
1424 | if (PL_op->op_flags & OPf_REF) { |
924508f0 |
1425 | EXTEND(SP,1); |
638eceb6 |
1426 | if (cGVOP_gv == PL_defgv) { |
3280af22 |
1427 | if (PL_laststype != OP_LSTAT) |
fbb0b3b3 |
1428 | Perl_croak(aTHX_ no_prev_lstat); |
3280af22 |
1429 | return PL_laststatval; |
fe14fcc3 |
1430 | } |
5d3e98de |
1431 | if (ckWARN(WARN_IO)) { |
9014280d |
1432 | Perl_warner(aTHX_ packWARN(WARN_IO), "Use of -l on filehandle %s", |
5d3e98de |
1433 | GvENAME(cGVOP_gv)); |
1434 | return (PL_laststatval = -1); |
1435 | } |
fe14fcc3 |
1436 | } |
fbb0b3b3 |
1437 | else if (ckWARN(WARN_IO) && PL_laststype != OP_LSTAT |
1438 | && (PL_op->op_private & OPpFT_STACKED)) |
1439 | Perl_croak(aTHX_ no_prev_lstat); |
c623bd54 |
1440 | |
3280af22 |
1441 | PL_laststype = OP_LSTAT; |
1442 | PL_statgv = Nullgv; |
79072805 |
1443 | sv = POPs; |
1444 | PUTBACK; |
5d3e98de |
1445 | if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV && ckWARN(WARN_IO)) { |
9014280d |
1446 | Perl_warner(aTHX_ packWARN(WARN_IO), "Use of -l on filehandle %s", |
5d3e98de |
1447 | GvENAME((GV*) SvRV(sv))); |
1448 | return (PL_laststatval = -1); |
1449 | } |
2d8e6c8d |
1450 | sv_setpv(PL_statname,SvPV(sv, n_a)); |
2d8e6c8d |
1451 | PL_laststatval = PerlLIO_lstat(SvPV(sv, n_a),&PL_statcache); |
2d8e6c8d |
1452 | if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(SvPV(sv, n_a), '\n')) |
9014280d |
1453 | Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "lstat"); |
3280af22 |
1454 | return PL_laststatval; |
c623bd54 |
1455 | } |
1456 | |
622913ab |
1457 | #ifndef OS2 |
a687059c |
1458 | bool |
864dbfa3 |
1459 | Perl_do_aexec(pTHX_ SV *really, register SV **mark, register SV **sp) |
a687059c |
1460 | { |
d5a9bfb0 |
1461 | return do_aexec5(really, mark, sp, 0, 0); |
1462 | } |
622913ab |
1463 | #endif |
d5a9bfb0 |
1464 | |
1465 | bool |
2aa1486d |
1466 | Perl_do_aexec5(pTHX_ SV *really, register SV **mark, register SV **sp, |
1467 | int fd, int do_report) |
d5a9bfb0 |
1468 | { |
cd39f2b6 |
1469 | #ifdef MACOS_TRADITIONAL |
1470 | Perl_croak(aTHX_ "exec? I'm not *that* kind of operating system"); |
1471 | #else |
a687059c |
1472 | register char **a; |
9c5ffd7c |
1473 | char *tmps = Nullch; |
2d8e6c8d |
1474 | STRLEN n_a; |
a687059c |
1475 | |
79072805 |
1476 | if (sp > mark) { |
3280af22 |
1477 | New(401,PL_Argv, sp - mark + 1, char*); |
1478 | a = PL_Argv; |
79072805 |
1479 | while (++mark <= sp) { |
1480 | if (*mark) |
2d8e6c8d |
1481 | *a++ = SvPVx(*mark, n_a); |
a687059c |
1482 | else |
1483 | *a++ = ""; |
1484 | } |
1485 | *a = Nullch; |
91b2752f |
1486 | if (really) |
1487 | tmps = SvPV(really, n_a); |
1488 | if ((!really && *PL_Argv[0] != '/') || |
1489 | (really && *tmps != '/')) /* will execvp use PATH? */ |
79072805 |
1490 | TAINT_ENV(); /* testing IFS here is overkill, probably */ |
b35112e7 |
1491 | PERL_FPU_PRE_EXEC |
91b2752f |
1492 | if (really && *tmps) |
b4748376 |
1493 | PerlProc_execvp(tmps,EXEC_ARGV_CAST(PL_Argv)); |
a687059c |
1494 | else |
b4748376 |
1495 | PerlProc_execvp(PL_Argv[0],EXEC_ARGV_CAST(PL_Argv)); |
b35112e7 |
1496 | PERL_FPU_POST_EXEC |
599cee73 |
1497 | if (ckWARN(WARN_EXEC)) |
9014280d |
1498 | Perl_warner(aTHX_ packWARN(WARN_EXEC), "Can't exec \"%s\": %s", |
91b2752f |
1499 | (really ? tmps : PL_Argv[0]), Strerror(errno)); |
d5a9bfb0 |
1500 | if (do_report) { |
1501 | int e = errno; |
1502 | |
1503 | PerlLIO_write(fd, (void*)&e, sizeof(int)); |
1504 | PerlLIO_close(fd); |
1505 | } |
a687059c |
1506 | } |
bee1dbe2 |
1507 | do_execfree(); |
cd39f2b6 |
1508 | #endif |
a687059c |
1509 | return FALSE; |
1510 | } |
1511 | |
fe14fcc3 |
1512 | void |
864dbfa3 |
1513 | Perl_do_execfree(pTHX) |
ff8e2863 |
1514 | { |
3280af22 |
1515 | if (PL_Argv) { |
1516 | Safefree(PL_Argv); |
1517 | PL_Argv = Null(char **); |
ff8e2863 |
1518 | } |
3280af22 |
1519 | if (PL_Cmd) { |
1520 | Safefree(PL_Cmd); |
1521 | PL_Cmd = Nullch; |
ff8e2863 |
1522 | } |
1523 | } |
1524 | |
cd39f2b6 |
1525 | #if !defined(OS2) && !defined(WIN32) && !defined(DJGPP) && !defined(EPOC) && !defined(MACOS_TRADITIONAL) |
760ac839 |
1526 | |
a687059c |
1527 | bool |
864dbfa3 |
1528 | Perl_do_exec(pTHX_ char *cmd) |
a687059c |
1529 | { |
e446cec8 |
1530 | return do_exec3(cmd,0,0); |
1531 | } |
1532 | |
1533 | bool |
864dbfa3 |
1534 | Perl_do_exec3(pTHX_ char *cmd, int fd, int do_report) |
e446cec8 |
1535 | { |
a687059c |
1536 | register char **a; |
1537 | register char *s; |
a687059c |
1538 | |
748a9306 |
1539 | while (*cmd && isSPACE(*cmd)) |
1540 | cmd++; |
1541 | |
a687059c |
1542 | /* save an extra exec if possible */ |
1543 | |
bf38876a |
1544 | #ifdef CSH |
d05c1ba0 |
1545 | { |
0c19750d |
1546 | char flags[PERL_FLAGS_MAX]; |
d05c1ba0 |
1547 | if (strnEQ(cmd,PL_cshname,PL_cshlen) && |
1548 | strnEQ(cmd+PL_cshlen," -c",3)) { |
0c19750d |
1549 | #ifdef HAS_STRLCPY |
1550 | strlcpy(flags, "-c", PERL_FLAGS_MAX); |
1551 | #else |
d05c1ba0 |
1552 | strcpy(flags,"-c"); |
0c19750d |
1553 | #endif |
d05c1ba0 |
1554 | s = cmd+PL_cshlen+3; |
1555 | if (*s == 'f') { |
1556 | s++; |
0c19750d |
1557 | #ifdef HAS_STRLCPY |
1558 | strlcat(flags, "f", PERL_FLAGS_MAX); |
1559 | #else |
d05c1ba0 |
1560 | strcat(flags,"f"); |
0c19750d |
1561 | #endif |
d05c1ba0 |
1562 | } |
1563 | if (*s == ' ') |
1564 | s++; |
1565 | if (*s++ == '\'') { |
1566 | char *ncmd = s; |
1567 | |
1568 | while (*s) |
1569 | s++; |
1570 | if (s[-1] == '\n') |
1571 | *--s = '\0'; |
1572 | if (s[-1] == '\'') { |
1573 | *--s = '\0'; |
b35112e7 |
1574 | PERL_FPU_PRE_EXEC |
d05c1ba0 |
1575 | PerlProc_execl(PL_cshname,"csh", flags, ncmd, (char*)0); |
b35112e7 |
1576 | PERL_FPU_POST_EXEC |
d05c1ba0 |
1577 | *s = '\''; |
1578 | return FALSE; |
1579 | } |
1580 | } |
a687059c |
1581 | } |
1582 | } |
bf38876a |
1583 | #endif /* CSH */ |
a687059c |
1584 | |
1585 | /* see if there are shell metacharacters in it */ |
1586 | |
748a9306 |
1587 | if (*cmd == '.' && isSPACE(cmd[1])) |
1588 | goto doshell; |
1589 | |
1590 | if (strnEQ(cmd,"exec",4) && isSPACE(cmd[4])) |
1591 | goto doshell; |
1592 | |
c170e444 |
1593 | for (s = cmd; *s && isALNUM(*s); s++) ; /* catch VAR=val gizmo */ |
63f2c1e1 |
1594 | if (*s == '=') |
1595 | goto doshell; |
748a9306 |
1596 | |
a687059c |
1597 | for (s = cmd; *s; s++) { |
d05c1ba0 |
1598 | if (*s != ' ' && !isALPHA(*s) && |
1599 | strchr("$&*(){}[]'\";\\|?<>~`\n",*s)) { |
a687059c |
1600 | if (*s == '\n' && !s[1]) { |
1601 | *s = '\0'; |
1602 | break; |
1603 | } |
603a98b0 |
1604 | /* handle the 2>&1 construct at the end */ |
1605 | if (*s == '>' && s[1] == '&' && s[2] == '1' |
1606 | && s > cmd + 1 && s[-1] == '2' && isSPACE(s[-2]) |
1607 | && (!s[3] || isSPACE(s[3]))) |
1608 | { |
1609 | char *t = s + 3; |
1610 | |
1611 | while (*t && isSPACE(*t)) |
1612 | ++t; |
943bbd07 |
1613 | if (!*t && (PerlLIO_dup2(1,2) != -1)) { |
603a98b0 |
1614 | s[-2] = '\0'; |
1615 | break; |
1616 | } |
1617 | } |
a687059c |
1618 | doshell: |
b35112e7 |
1619 | PERL_FPU_PRE_EXEC |
3280af22 |
1620 | PerlProc_execl(PL_sh_path, "sh", "-c", cmd, (char*)0); |
b35112e7 |
1621 | PERL_FPU_POST_EXEC |
a687059c |
1622 | return FALSE; |
1623 | } |
1624 | } |
748a9306 |
1625 | |
3280af22 |
1626 | New(402,PL_Argv, (s - cmd) / 2 + 2, char*); |
1627 | PL_Cmd = savepvn(cmd, s-cmd); |
1628 | a = PL_Argv; |
1629 | for (s = PL_Cmd; *s;) { |
99b89507 |
1630 | while (*s && isSPACE(*s)) s++; |
a687059c |
1631 | if (*s) |
1632 | *(a++) = s; |
99b89507 |
1633 | while (*s && !isSPACE(*s)) s++; |
a687059c |
1634 | if (*s) |
1635 | *s++ = '\0'; |
1636 | } |
1637 | *a = Nullch; |
3280af22 |
1638 | if (PL_Argv[0]) { |
b35112e7 |
1639 | PERL_FPU_PRE_EXEC |
3280af22 |
1640 | PerlProc_execvp(PL_Argv[0],PL_Argv); |
b35112e7 |
1641 | PERL_FPU_POST_EXEC |
b1248f16 |
1642 | if (errno == ENOEXEC) { /* for system V NIH syndrome */ |
ff8e2863 |
1643 | do_execfree(); |
a687059c |
1644 | goto doshell; |
b1248f16 |
1645 | } |
d008e5eb |
1646 | { |
e446cec8 |
1647 | int e = errno; |
1648 | |
d008e5eb |
1649 | if (ckWARN(WARN_EXEC)) |
9014280d |
1650 | Perl_warner(aTHX_ packWARN(WARN_EXEC), "Can't exec \"%s\": %s", |
d008e5eb |
1651 | PL_Argv[0], Strerror(errno)); |
e446cec8 |
1652 | if (do_report) { |
1653 | PerlLIO_write(fd, (void*)&e, sizeof(int)); |
1654 | PerlLIO_close(fd); |
1655 | } |
d008e5eb |
1656 | } |
a687059c |
1657 | } |
ff8e2863 |
1658 | do_execfree(); |
a687059c |
1659 | return FALSE; |
1660 | } |
1661 | |
6890e559 |
1662 | #endif /* OS2 || WIN32 */ |
760ac839 |
1663 | |
79072805 |
1664 | I32 |
864dbfa3 |
1665 | Perl_apply(pTHX_ I32 type, register SV **mark, register SV **sp) |
a687059c |
1666 | { |
79072805 |
1667 | register I32 val; |
1668 | register I32 val2; |
1669 | register I32 tot = 0; |
20408e3c |
1670 | char *what; |
a687059c |
1671 | char *s; |
79072805 |
1672 | SV **oldmark = mark; |
2d8e6c8d |
1673 | STRLEN n_a; |
a687059c |
1674 | |
20408e3c |
1675 | #define APPLY_TAINT_PROPER() \ |
3280af22 |
1676 | STMT_START { \ |
17406bd6 |
1677 | if (PL_tainted) { TAINT_PROPER(what); } \ |
873ef191 |
1678 | } STMT_END |
20408e3c |
1679 | |
1680 | /* This is a first heuristic; it doesn't catch tainting magic. */ |
3280af22 |
1681 | if (PL_tainting) { |
463ee0b2 |
1682 | while (++mark <= sp) { |
bbce6d69 |
1683 | if (SvTAINTED(*mark)) { |
1684 | TAINT; |
1685 | break; |
1686 | } |
463ee0b2 |
1687 | } |
1688 | mark = oldmark; |
1689 | } |
a687059c |
1690 | switch (type) { |
79072805 |
1691 | case OP_CHMOD: |
20408e3c |
1692 | what = "chmod"; |
1693 | APPLY_TAINT_PROPER(); |
79072805 |
1694 | if (++mark <= sp) { |
463ee0b2 |
1695 | val = SvIVx(*mark); |
20408e3c |
1696 | APPLY_TAINT_PROPER(); |
1697 | tot = sp - mark; |
79072805 |
1698 | while (++mark <= sp) { |
2d8e6c8d |
1699 | char *name = SvPVx(*mark, n_a); |
20408e3c |
1700 | APPLY_TAINT_PROPER(); |
1701 | if (PerlLIO_chmod(name, val)) |
a687059c |
1702 | tot--; |
1703 | } |
1704 | } |
1705 | break; |
fe14fcc3 |
1706 | #ifdef HAS_CHOWN |
79072805 |
1707 | case OP_CHOWN: |
20408e3c |
1708 | what = "chown"; |
1709 | APPLY_TAINT_PROPER(); |
79072805 |
1710 | if (sp - mark > 2) { |
463ee0b2 |
1711 | val = SvIVx(*++mark); |
1712 | val2 = SvIVx(*++mark); |
20408e3c |
1713 | APPLY_TAINT_PROPER(); |
a0d0e21e |
1714 | tot = sp - mark; |
79072805 |
1715 | while (++mark <= sp) { |
2d8e6c8d |
1716 | char *name = SvPVx(*mark, n_a); |
20408e3c |
1717 | APPLY_TAINT_PROPER(); |
36660982 |
1718 | if (PerlLIO_chown(name, val, val2)) |
a687059c |
1719 | tot--; |
1720 | } |
1721 | } |
1722 | break; |
b1248f16 |
1723 | #endif |
a1d180c4 |
1724 | /* |
dd64f1c3 |
1725 | XXX Should we make lchown() directly available from perl? |
1726 | For now, we'll let Configure test for HAS_LCHOWN, but do |
1727 | nothing in the core. |
1728 | --AD 5/1998 |
1729 | */ |
fe14fcc3 |
1730 | #ifdef HAS_KILL |
79072805 |
1731 | case OP_KILL: |
20408e3c |
1732 | what = "kill"; |
1733 | APPLY_TAINT_PROPER(); |
55497cff |
1734 | if (mark == sp) |
1735 | break; |
2d8e6c8d |
1736 | s = SvPVx(*++mark, n_a); |
e02bfb16 |
1737 | if (isALPHA(*s)) { |
79072805 |
1738 | if (*s == 'S' && s[1] == 'I' && s[2] == 'G') |
1739 | s += 3; |
e02bfb16 |
1740 | if ((val = whichsig(s)) < 0) |
cea2e8a9 |
1741 | Perl_croak(aTHX_ "Unrecognized signal name \"%s\"",s); |
79072805 |
1742 | } |
1743 | else |
463ee0b2 |
1744 | val = SvIVx(*mark); |
20408e3c |
1745 | APPLY_TAINT_PROPER(); |
1746 | tot = sp - mark; |
3595fcef |
1747 | #ifdef VMS |
1748 | /* kill() doesn't do process groups (job trees?) under VMS */ |
1749 | if (val < 0) val = -val; |
1750 | if (val == SIGKILL) { |
1751 | # include <starlet.h> |
1752 | /* Use native sys$delprc() to insure that target process is |
1753 | * deleted; supervisor-mode images don't pay attention to |
1754 | * CRTL's emulation of Unix-style signals and kill() |
1755 | */ |
1756 | while (++mark <= sp) { |
1757 | I32 proc = SvIVx(*mark); |
1758 | register unsigned long int __vmssts; |
20408e3c |
1759 | APPLY_TAINT_PROPER(); |
3595fcef |
1760 | if (!((__vmssts = sys$delprc(&proc,0)) & 1)) { |
1761 | tot--; |
1762 | switch (__vmssts) { |
1763 | case SS$_NONEXPR: |
1764 | case SS$_NOSUCHNODE: |
1765 | SETERRNO(ESRCH,__vmssts); |
1766 | break; |
1767 | case SS$_NOPRIV: |
1768 | SETERRNO(EPERM,__vmssts); |
1769 | break; |
1770 | default: |
1771 | SETERRNO(EVMSERR,__vmssts); |
1772 | } |
1773 | } |
1774 | } |
1775 | break; |
1776 | } |
1777 | #endif |
79072805 |
1778 | if (val < 0) { |
1779 | val = -val; |
1780 | while (++mark <= sp) { |
463ee0b2 |
1781 | I32 proc = SvIVx(*mark); |
20408e3c |
1782 | APPLY_TAINT_PROPER(); |
fe14fcc3 |
1783 | #ifdef HAS_KILLPG |
3028581b |
1784 | if (PerlProc_killpg(proc,val)) /* BSD */ |
a687059c |
1785 | #else |
3028581b |
1786 | if (PerlProc_kill(-proc,val)) /* SYSV */ |
a687059c |
1787 | #endif |
79072805 |
1788 | tot--; |
a687059c |
1789 | } |
79072805 |
1790 | } |
1791 | else { |
1792 | while (++mark <= sp) { |
20408e3c |
1793 | I32 proc = SvIVx(*mark); |
1794 | APPLY_TAINT_PROPER(); |
1795 | if (PerlProc_kill(proc, val)) |
79072805 |
1796 | tot--; |
a687059c |
1797 | } |
1798 | } |
1799 | break; |
b1248f16 |
1800 | #endif |
79072805 |
1801 | case OP_UNLINK: |
20408e3c |
1802 | what = "unlink"; |
1803 | APPLY_TAINT_PROPER(); |
79072805 |
1804 | tot = sp - mark; |
1805 | while (++mark <= sp) { |
2d8e6c8d |
1806 | s = SvPVx(*mark, n_a); |
20408e3c |
1807 | APPLY_TAINT_PROPER(); |
3280af22 |
1808 | if (PL_euid || PL_unsafe) { |
a687059c |
1809 | if (UNLINK(s)) |
1810 | tot--; |
1811 | } |
1812 | else { /* don't let root wipe out directories without -U */ |
3280af22 |
1813 | if (PerlLIO_lstat(s,&PL_statbuf) < 0 || S_ISDIR(PL_statbuf.st_mode)) |
a687059c |
1814 | tot--; |
1815 | else { |
1816 | if (UNLINK(s)) |
1817 | tot--; |
1818 | } |
1819 | } |
1820 | } |
1821 | break; |
a0d0e21e |
1822 | #ifdef HAS_UTIME |
79072805 |
1823 | case OP_UTIME: |
20408e3c |
1824 | what = "utime"; |
1825 | APPLY_TAINT_PROPER(); |
79072805 |
1826 | if (sp - mark > 2) { |
748a9306 |
1827 | #if defined(I_UTIME) || defined(VMS) |
663a0e37 |
1828 | struct utimbuf utbuf; |
1829 | #else |
a687059c |
1830 | struct { |
dd2821f6 |
1831 | Time_t actime; |
1832 | Time_t modtime; |
a687059c |
1833 | } utbuf; |
663a0e37 |
1834 | #endif |
a687059c |
1835 | |
c6f7b413 |
1836 | SV* accessed = *++mark; |
1837 | SV* modified = *++mark; |
1838 | void * utbufp = &utbuf; |
1839 | |
6ec06612 |
1840 | /* Be like C, and if both times are undefined, let the C |
1841 | * library figure out what to do. This usually means |
1842 | * "current time". */ |
c6f7b413 |
1843 | |
1844 | if ( accessed == &PL_sv_undef && modified == &PL_sv_undef ) |
6ec06612 |
1845 | utbufp = NULL; |
1846 | else { |
1847 | Zero(&utbuf, sizeof utbuf, char); |
517844ec |
1848 | #ifdef BIG_TIME |
6ec06612 |
1849 | utbuf.actime = (Time_t)SvNVx(accessed); /* time accessed */ |
1850 | utbuf.modtime = (Time_t)SvNVx(modified); /* time modified */ |
517844ec |
1851 | #else |
6ec06612 |
1852 | utbuf.actime = (Time_t)SvIVx(accessed); /* time accessed */ |
1853 | utbuf.modtime = (Time_t)SvIVx(modified); /* time modified */ |
517844ec |
1854 | #endif |
6ec06612 |
1855 | } |
1856 | APPLY_TAINT_PROPER(); |
79072805 |
1857 | tot = sp - mark; |
1858 | while (++mark <= sp) { |
2d8e6c8d |
1859 | char *name = SvPVx(*mark, n_a); |
20408e3c |
1860 | APPLY_TAINT_PROPER(); |
c6f7b413 |
1861 | if (PerlLIO_utime(name, utbufp)) |
a687059c |
1862 | tot--; |
1863 | } |
a687059c |
1864 | } |
1865 | else |
79072805 |
1866 | tot = 0; |
a687059c |
1867 | break; |
a0d0e21e |
1868 | #endif |
a687059c |
1869 | } |
1870 | return tot; |
20408e3c |
1871 | |
20408e3c |
1872 | #undef APPLY_TAINT_PROPER |
a687059c |
1873 | } |
1874 | |
1875 | /* Do the permissions allow some operation? Assumes statcache already set. */ |
a0d0e21e |
1876 | #ifndef VMS /* VMS' cando is in vms.c */ |
7f4774ae |
1877 | bool |
1878 | Perl_cando(pTHX_ Mode_t mode, Uid_t effective, register Stat_t *statbufp) |
1879 | /* Note: we use `effective' both for uids and gids. |
1880 | * Here we are betting on Uid_t being equal or wider than Gid_t. */ |
a687059c |
1881 | { |
bee1dbe2 |
1882 | #ifdef DOSISH |
fe14fcc3 |
1883 | /* [Comments and code from Len Reed] |
1884 | * MS-DOS "user" is similar to UNIX's "superuser," but can't write |
1885 | * to write-protected files. The execute permission bit is set |
1886 | * by the Miscrosoft C library stat() function for the following: |
1887 | * .exe files |
1888 | * .com files |
1889 | * .bat files |
1890 | * directories |
1891 | * All files and directories are readable. |
1892 | * Directories and special files, e.g. "CON", cannot be |
1893 | * write-protected. |
1894 | * [Comment by Tom Dinger -- a directory can have the write-protect |
1895 | * bit set in the file system, but DOS permits changes to |
1896 | * the directory anyway. In addition, all bets are off |
1897 | * here for networked software, such as Novell and |
1898 | * Sun's PC-NFS.] |
1899 | */ |
1900 | |
bee1dbe2 |
1901 | /* Atari stat() does pretty much the same thing. we set x_bit_set_in_stat |
1902 | * too so it will actually look into the files for magic numbers |
1903 | */ |
7f4774ae |
1904 | return (mode & statbufp->st_mode) ? TRUE : FALSE; |
fe14fcc3 |
1905 | |
55497cff |
1906 | #else /* ! DOSISH */ |
3280af22 |
1907 | if ((effective ? PL_euid : PL_uid) == 0) { /* root is special */ |
7f4774ae |
1908 | if (mode == S_IXUSR) { |
c623bd54 |
1909 | if (statbufp->st_mode & 0111 || S_ISDIR(statbufp->st_mode)) |
a687059c |
1910 | return TRUE; |
1911 | } |
1912 | else |
1913 | return TRUE; /* root reads and writes anything */ |
1914 | return FALSE; |
1915 | } |
3280af22 |
1916 | if (statbufp->st_uid == (effective ? PL_euid : PL_uid) ) { |
7f4774ae |
1917 | if (statbufp->st_mode & mode) |
a687059c |
1918 | return TRUE; /* ok as "user" */ |
1919 | } |
d8eceb89 |
1920 | else if (ingroup(statbufp->st_gid,effective)) { |
7f4774ae |
1921 | if (statbufp->st_mode & mode >> 3) |
a687059c |
1922 | return TRUE; /* ok as "group" */ |
1923 | } |
7f4774ae |
1924 | else if (statbufp->st_mode & mode >> 6) |
a687059c |
1925 | return TRUE; /* ok as "other" */ |
1926 | return FALSE; |
55497cff |
1927 | #endif /* ! DOSISH */ |
a687059c |
1928 | } |
a0d0e21e |
1929 | #endif /* ! VMS */ |
a687059c |
1930 | |
d8eceb89 |
1931 | bool |
1932 | Perl_ingroup(pTHX_ Gid_t testgid, Uid_t effective) |
a687059c |
1933 | { |
cd39f2b6 |
1934 | #ifdef MACOS_TRADITIONAL |
1935 | /* This is simply not correct for AppleShare, but fix it yerself. */ |
1936 | return TRUE; |
1937 | #else |
3280af22 |
1938 | if (testgid == (effective ? PL_egid : PL_gid)) |
a687059c |
1939 | return TRUE; |
fe14fcc3 |
1940 | #ifdef HAS_GETGROUPS |
a687059c |
1941 | #ifndef NGROUPS |
1942 | #define NGROUPS 32 |
1943 | #endif |
1944 | { |
a0d0e21e |
1945 | Groups_t gary[NGROUPS]; |
79072805 |
1946 | I32 anum; |
a687059c |
1947 | |
1948 | anum = getgroups(NGROUPS,gary); |
1949 | while (--anum >= 0) |
1950 | if (gary[anum] == testgid) |
1951 | return TRUE; |
1952 | } |
1953 | #endif |
1954 | return FALSE; |
cd39f2b6 |
1955 | #endif |
a687059c |
1956 | } |
c2ab57d4 |
1957 | |
fe14fcc3 |
1958 | #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM) |
c2ab57d4 |
1959 | |
79072805 |
1960 | I32 |
864dbfa3 |
1961 | Perl_do_ipcget(pTHX_ I32 optype, SV **mark, SV **sp) |
c2ab57d4 |
1962 | { |
c2ab57d4 |
1963 | key_t key; |
79072805 |
1964 | I32 n, flags; |
c2ab57d4 |
1965 | |
463ee0b2 |
1966 | key = (key_t)SvNVx(*++mark); |
1967 | n = (optype == OP_MSGGET) ? 0 : SvIVx(*++mark); |
1968 | flags = SvIVx(*++mark); |
748a9306 |
1969 | SETERRNO(0,0); |
c2ab57d4 |
1970 | switch (optype) |
1971 | { |
fe14fcc3 |
1972 | #ifdef HAS_MSG |
79072805 |
1973 | case OP_MSGGET: |
c2ab57d4 |
1974 | return msgget(key, flags); |
e5d73d77 |
1975 | #endif |
fe14fcc3 |
1976 | #ifdef HAS_SEM |
79072805 |
1977 | case OP_SEMGET: |
c2ab57d4 |
1978 | return semget(key, n, flags); |
e5d73d77 |
1979 | #endif |
fe14fcc3 |
1980 | #ifdef HAS_SHM |
79072805 |
1981 | case OP_SHMGET: |
c2ab57d4 |
1982 | return shmget(key, n, flags); |
e5d73d77 |
1983 | #endif |
fe14fcc3 |
1984 | #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM) |
e5d73d77 |
1985 | default: |
cea2e8a9 |
1986 | Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]); |
e5d73d77 |
1987 | #endif |
c2ab57d4 |
1988 | } |
1989 | return -1; /* should never happen */ |
1990 | } |
1991 | |
79072805 |
1992 | I32 |
864dbfa3 |
1993 | Perl_do_ipcctl(pTHX_ I32 optype, SV **mark, SV **sp) |
c2ab57d4 |
1994 | { |
79072805 |
1995 | SV *astr; |
c2ab57d4 |
1996 | char *a; |
a0d0e21e |
1997 | I32 id, n, cmd, infosize, getinfo; |
1998 | I32 ret = -1; |
c2ab57d4 |
1999 | |
463ee0b2 |
2000 | id = SvIVx(*++mark); |
2001 | n = (optype == OP_SEMCTL) ? SvIVx(*++mark) : 0; |
2002 | cmd = SvIVx(*++mark); |
79072805 |
2003 | astr = *++mark; |
c2ab57d4 |
2004 | infosize = 0; |
2005 | getinfo = (cmd == IPC_STAT); |
2006 | |
2007 | switch (optype) |
2008 | { |
fe14fcc3 |
2009 | #ifdef HAS_MSG |
79072805 |
2010 | case OP_MSGCTL: |
c2ab57d4 |
2011 | if (cmd == IPC_STAT || cmd == IPC_SET) |
2012 | infosize = sizeof(struct msqid_ds); |
2013 | break; |
e5d73d77 |
2014 | #endif |
fe14fcc3 |
2015 | #ifdef HAS_SHM |
79072805 |
2016 | case OP_SHMCTL: |
c2ab57d4 |
2017 | if (cmd == IPC_STAT || cmd == IPC_SET) |
2018 | infosize = sizeof(struct shmid_ds); |
2019 | break; |
e5d73d77 |
2020 | #endif |
fe14fcc3 |
2021 | #ifdef HAS_SEM |
79072805 |
2022 | case OP_SEMCTL: |
39398f3f |
2023 | #ifdef Semctl |
c2ab57d4 |
2024 | if (cmd == IPC_STAT || cmd == IPC_SET) |
2025 | infosize = sizeof(struct semid_ds); |
2026 | else if (cmd == GETALL || cmd == SETALL) |
2027 | { |
8e591e46 |
2028 | struct semid_ds semds; |
bd89102f |
2029 | union semun semun; |
e6f0bdd6 |
2030 | #ifdef EXTRA_F_IN_SEMUN_BUF |
2031 | semun.buff = &semds; |
2032 | #else |
84902520 |
2033 | semun.buf = &semds; |
e6f0bdd6 |
2034 | #endif |
c2ab57d4 |
2035 | getinfo = (cmd == GETALL); |
9b89d93d |
2036 | if (Semctl(id, 0, IPC_STAT, semun) == -1) |
2037 | return -1; |
6e21c824 |
2038 | infosize = semds.sem_nsems * sizeof(short); |
2039 | /* "short" is technically wrong but much more portable |
2040 | than guessing about u_?short(_t)? */ |
c2ab57d4 |
2041 | } |
39398f3f |
2042 | #else |
cea2e8a9 |
2043 | Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]); |
39398f3f |
2044 | #endif |
c2ab57d4 |
2045 | break; |
e5d73d77 |
2046 | #endif |
fe14fcc3 |
2047 | #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM) |
e5d73d77 |
2048 | default: |
cea2e8a9 |
2049 | Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]); |
e5d73d77 |
2050 | #endif |
c2ab57d4 |
2051 | } |
2052 | |
2053 | if (infosize) |
2054 | { |
a0d0e21e |
2055 | STRLEN len; |
c2ab57d4 |
2056 | if (getinfo) |
2057 | { |
a0d0e21e |
2058 | SvPV_force(astr, len); |
2059 | a = SvGROW(astr, infosize+1); |
c2ab57d4 |
2060 | } |
2061 | else |
2062 | { |
463ee0b2 |
2063 | a = SvPV(astr, len); |
2064 | if (len != infosize) |
cea2e8a9 |
2065 | Perl_croak(aTHX_ "Bad arg length for %s, is %lu, should be %ld", |
4ec43091 |
2066 | PL_op_desc[optype], |
2067 | (unsigned long)len, |
2068 | (long)infosize); |
c2ab57d4 |
2069 | } |
2070 | } |
2071 | else |
2072 | { |
c030ccd9 |
2073 | IV i = SvIV(astr); |
56431972 |
2074 | a = INT2PTR(char *,i); /* ouch */ |
c2ab57d4 |
2075 | } |
748a9306 |
2076 | SETERRNO(0,0); |
c2ab57d4 |
2077 | switch (optype) |
2078 | { |
fe14fcc3 |
2079 | #ifdef HAS_MSG |
79072805 |
2080 | case OP_MSGCTL: |
bee1dbe2 |
2081 | ret = msgctl(id, cmd, (struct msqid_ds *)a); |
c2ab57d4 |
2082 | break; |
e5d73d77 |
2083 | #endif |
fe14fcc3 |
2084 | #ifdef HAS_SEM |
bd89102f |
2085 | case OP_SEMCTL: { |
39398f3f |
2086 | #ifdef Semctl |
bd89102f |
2087 | union semun unsemds; |
2088 | |
e6f0bdd6 |
2089 | #ifdef EXTRA_F_IN_SEMUN_BUF |
2090 | unsemds.buff = (struct semid_ds *)a; |
2091 | #else |
bd89102f |
2092 | unsemds.buf = (struct semid_ds *)a; |
e6f0bdd6 |
2093 | #endif |
bd89102f |
2094 | ret = Semctl(id, n, cmd, unsemds); |
39398f3f |
2095 | #else |
cea2e8a9 |
2096 | Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]); |
39398f3f |
2097 | #endif |
bd89102f |
2098 | } |
c2ab57d4 |
2099 | break; |
e5d73d77 |
2100 | #endif |
fe14fcc3 |
2101 | #ifdef HAS_SHM |
79072805 |
2102 | case OP_SHMCTL: |
bee1dbe2 |
2103 | ret = shmctl(id, cmd, (struct shmid_ds *)a); |
c2ab57d4 |
2104 | break; |
e5d73d77 |
2105 | #endif |
c2ab57d4 |
2106 | } |
2107 | if (getinfo && ret >= 0) { |
79072805 |
2108 | SvCUR_set(astr, infosize); |
2109 | *SvEND(astr) = '\0'; |
a0d0e21e |
2110 | SvSETMAGIC(astr); |
c2ab57d4 |
2111 | } |
2112 | return ret; |
2113 | } |
2114 | |
79072805 |
2115 | I32 |
864dbfa3 |
2116 | Perl_do_msgsnd(pTHX_ SV **mark, SV **sp) |
c2ab57d4 |
2117 | { |
fe14fcc3 |
2118 | #ifdef HAS_MSG |
79072805 |
2119 | SV *mstr; |
c2ab57d4 |
2120 | char *mbuf; |
79072805 |
2121 | I32 id, msize, flags; |
463ee0b2 |
2122 | STRLEN len; |
c2ab57d4 |
2123 | |
463ee0b2 |
2124 | id = SvIVx(*++mark); |
79072805 |
2125 | mstr = *++mark; |
463ee0b2 |
2126 | flags = SvIVx(*++mark); |
2127 | mbuf = SvPV(mstr, len); |
2128 | if ((msize = len - sizeof(long)) < 0) |
cea2e8a9 |
2129 | Perl_croak(aTHX_ "Arg too short for msgsnd"); |
748a9306 |
2130 | SETERRNO(0,0); |
bee1dbe2 |
2131 | return msgsnd(id, (struct msgbuf *)mbuf, msize, flags); |
e5d73d77 |
2132 | #else |
cea2e8a9 |
2133 | Perl_croak(aTHX_ "msgsnd not implemented"); |
e5d73d77 |
2134 | #endif |
c2ab57d4 |
2135 | } |
2136 | |
79072805 |
2137 | I32 |
864dbfa3 |
2138 | Perl_do_msgrcv(pTHX_ SV **mark, SV **sp) |
c2ab57d4 |
2139 | { |
fe14fcc3 |
2140 | #ifdef HAS_MSG |
79072805 |
2141 | SV *mstr; |
c2ab57d4 |
2142 | char *mbuf; |
2143 | long mtype; |
79072805 |
2144 | I32 id, msize, flags, ret; |
463ee0b2 |
2145 | STRLEN len; |
79072805 |
2146 | |
463ee0b2 |
2147 | id = SvIVx(*++mark); |
79072805 |
2148 | mstr = *++mark; |
c2e66d9e |
2149 | /* suppress warning when reading into undef var --jhi */ |
2150 | if (! SvOK(mstr)) |
2151 | sv_setpvn(mstr, "", 0); |
463ee0b2 |
2152 | msize = SvIVx(*++mark); |
2153 | mtype = (long)SvIVx(*++mark); |
2154 | flags = SvIVx(*++mark); |
a0d0e21e |
2155 | SvPV_force(mstr, len); |
2156 | mbuf = SvGROW(mstr, sizeof(long)+msize+1); |
a1d180c4 |
2157 | |
748a9306 |
2158 | SETERRNO(0,0); |
bee1dbe2 |
2159 | ret = msgrcv(id, (struct msgbuf *)mbuf, msize, mtype, flags); |
c2ab57d4 |
2160 | if (ret >= 0) { |
79072805 |
2161 | SvCUR_set(mstr, sizeof(long)+ret); |
2162 | *SvEND(mstr) = '\0'; |
41d6edb2 |
2163 | #ifndef INCOMPLETE_TAINTS |
2164 | /* who knows who has been playing with this message? */ |
2165 | SvTAINTED_on(mstr); |
2166 | #endif |
c2ab57d4 |
2167 | } |
2168 | return ret; |
e5d73d77 |
2169 | #else |
cea2e8a9 |
2170 | Perl_croak(aTHX_ "msgrcv not implemented"); |
e5d73d77 |
2171 | #endif |
c2ab57d4 |
2172 | } |
2173 | |
79072805 |
2174 | I32 |
864dbfa3 |
2175 | Perl_do_semop(pTHX_ SV **mark, SV **sp) |
c2ab57d4 |
2176 | { |
fe14fcc3 |
2177 | #ifdef HAS_SEM |
79072805 |
2178 | SV *opstr; |
c2ab57d4 |
2179 | char *opbuf; |
463ee0b2 |
2180 | I32 id; |
2181 | STRLEN opsize; |
c2ab57d4 |
2182 | |
463ee0b2 |
2183 | id = SvIVx(*++mark); |
79072805 |
2184 | opstr = *++mark; |
463ee0b2 |
2185 | opbuf = SvPV(opstr, opsize); |
248ff010 |
2186 | if (opsize < 3 * SHORTSIZE |
2187 | || (opsize % (3 * SHORTSIZE))) { |
93189314 |
2188 | SETERRNO(EINVAL,LIB_INVARG); |
c2ab57d4 |
2189 | return -1; |
2190 | } |
748a9306 |
2191 | SETERRNO(0,0); |
248ff010 |
2192 | /* We can't assume that sizeof(struct sembuf) == 3 * sizeof(short). */ |
2193 | { |
2194 | int nsops = opsize / (3 * sizeof (short)); |
2195 | int i = nsops; |
2196 | short *ops = (short *) opbuf; |
2197 | short *o = ops; |
2198 | struct sembuf *temps, *t; |
2199 | I32 result; |
2200 | |
2201 | New (0, temps, nsops, struct sembuf); |
2202 | t = temps; |
2203 | while (i--) { |
2204 | t->sem_num = *o++; |
2205 | t->sem_op = *o++; |
2206 | t->sem_flg = *o++; |
2207 | t++; |
2208 | } |
2209 | result = semop(id, temps, nsops); |
2210 | t = temps; |
2211 | o = ops; |
2212 | i = nsops; |
2213 | while (i--) { |
2214 | *o++ = t->sem_num; |
2215 | *o++ = t->sem_op; |
2216 | *o++ = t->sem_flg; |
2217 | t++; |
2218 | } |
2219 | Safefree(temps); |
2220 | return result; |
2221 | } |
e5d73d77 |
2222 | #else |
cea2e8a9 |
2223 | Perl_croak(aTHX_ "semop not implemented"); |
e5d73d77 |
2224 | #endif |
c2ab57d4 |
2225 | } |
2226 | |
79072805 |
2227 | I32 |
864dbfa3 |
2228 | Perl_do_shmio(pTHX_ I32 optype, SV **mark, SV **sp) |
c2ab57d4 |
2229 | { |
fe14fcc3 |
2230 | #ifdef HAS_SHM |
79072805 |
2231 | SV *mstr; |
c2ab57d4 |
2232 | char *mbuf, *shm; |
79072805 |
2233 | I32 id, mpos, msize; |
463ee0b2 |
2234 | STRLEN len; |
c2ab57d4 |
2235 | struct shmid_ds shmds; |
c2ab57d4 |
2236 | |
463ee0b2 |
2237 | id = SvIVx(*++mark); |
79072805 |
2238 | mstr = *++mark; |
463ee0b2 |
2239 | mpos = SvIVx(*++mark); |
2240 | msize = SvIVx(*++mark); |
748a9306 |
2241 | SETERRNO(0,0); |
c2ab57d4 |
2242 | if (shmctl(id, IPC_STAT, &shmds) == -1) |
2243 | return -1; |
2244 | if (mpos < 0 || msize < 0 || mpos + msize > shmds.shm_segsz) { |
93189314 |
2245 | SETERRNO(EFAULT,SS_ACCVIO); /* can't do as caller requested */ |
c2ab57d4 |
2246 | return -1; |
2247 | } |
8ac85365 |
2248 | shm = (char *)shmat(id, (char*)NULL, (optype == OP_SHMREAD) ? SHM_RDONLY : 0); |
c2ab57d4 |
2249 | if (shm == (char *)-1) /* I hate System V IPC, I really do */ |
2250 | return -1; |
79072805 |
2251 | if (optype == OP_SHMREAD) { |
9f538c04 |
2252 | /* suppress warning when reading into undef var (tchrist 3/Mar/00) */ |
2253 | if (! SvOK(mstr)) |
2254 | sv_setpvn(mstr, "", 0); |
a0d0e21e |
2255 | SvPV_force(mstr, len); |
2256 | mbuf = SvGROW(mstr, msize+1); |
2257 | |
bee1dbe2 |
2258 | Copy(shm + mpos, mbuf, msize, char); |
79072805 |
2259 | SvCUR_set(mstr, msize); |
2260 | *SvEND(mstr) = '\0'; |
a0d0e21e |
2261 | SvSETMAGIC(mstr); |
d929ce6f |
2262 | #ifndef INCOMPLETE_TAINTS |
2263 | /* who knows who has been playing with this shared memory? */ |
2264 | SvTAINTED_on(mstr); |
2265 | #endif |
c2ab57d4 |
2266 | } |
2267 | else { |
79072805 |
2268 | I32 n; |
c2ab57d4 |
2269 | |
a0d0e21e |
2270 | mbuf = SvPV(mstr, len); |
463ee0b2 |
2271 | if ((n = len) > msize) |
c2ab57d4 |
2272 | n = msize; |
bee1dbe2 |
2273 | Copy(mbuf, shm + mpos, n, char); |
c2ab57d4 |
2274 | if (n < msize) |
bee1dbe2 |
2275 | memzero(shm + mpos + n, msize - n); |
c2ab57d4 |
2276 | } |
2277 | return shmdt(shm); |
e5d73d77 |
2278 | #else |
cea2e8a9 |
2279 | Perl_croak(aTHX_ "shm I/O not implemented"); |
e5d73d77 |
2280 | #endif |
c2ab57d4 |
2281 | } |
2282 | |
fe14fcc3 |
2283 | #endif /* SYSV IPC */ |
4e35701f |
2284 | |
0d44d22b |
2285 | /* |
ccfc67b7 |
2286 | =head1 IO Functions |
2287 | |
0d44d22b |
2288 | =for apidoc start_glob |
2289 | |
2290 | Function called by C<do_readline> to spawn a glob (or do the glob inside |
2291 | perl on VMS). This code used to be inline, but now perl uses C<File::Glob> |
210b36aa |
2292 | this glob starter is only used by miniperl during the build process. |
0d44d22b |
2293 | Moving it away shrinks pp_hot.c; shrinking pp_hot.c helps speed perl up. |
fab3f3a7 |
2294 | |
0d44d22b |
2295 | =cut |
2296 | */ |
2297 | |
2298 | PerlIO * |
2299 | Perl_start_glob (pTHX_ SV *tmpglob, IO *io) |
2300 | { |
2301 | SV *tmpcmd = NEWSV(55, 0); |
2302 | PerlIO *fp; |
2303 | ENTER; |
2304 | SAVEFREESV(tmpcmd); |
2305 | #ifdef VMS /* expand the wildcards right here, rather than opening a pipe, */ |
2306 | /* since spawning off a process is a real performance hit */ |
2307 | { |
2308 | #include <descrip.h> |
2309 | #include <lib$routines.h> |
2310 | #include <nam.h> |
2311 | #include <rmsdef.h> |
2312 | char rslt[NAM$C_MAXRSS+1+sizeof(unsigned short int)] = {'\0','\0'}; |
2313 | char vmsspec[NAM$C_MAXRSS+1]; |
2314 | char *rstr = rslt + sizeof(unsigned short int), *begin, *end, *cp; |
0d44d22b |
2315 | $DESCRIPTOR(dfltdsc,"SYS$DISK:[]*.*;"); |
2316 | PerlIO *tmpfp; |
2317 | STRLEN i; |
2318 | struct dsc$descriptor_s wilddsc |
2319 | = {0, DSC$K_DTYPE_T, DSC$K_CLASS_S, 0}; |
2320 | struct dsc$descriptor_vs rsdsc |
2321 | = {sizeof rslt, DSC$K_DTYPE_VT, DSC$K_CLASS_VS, rslt}; |
2322 | unsigned long int cxt = 0, sts = 0, ok = 1, hasdir = 0, hasver = 0, isunix = 0; |
2323 | |
2324 | /* We could find out if there's an explicit dev/dir or version |
2325 | by peeking into lib$find_file's internal context at |
2326 | ((struct NAM *)((struct FAB *)cxt)->fab$l_nam)->nam$l_fnb |
2327 | but that's unsupported, so I don't want to do it now and |
2328 | have it bite someone in the future. */ |
0d44d22b |
2329 | cp = SvPV(tmpglob,i); |
2330 | for (; i; i--) { |
2331 | if (cp[i] == ';') hasver = 1; |
2332 | if (cp[i] == '.') { |
2333 | if (sts) hasver = 1; |
2334 | else sts = 1; |
2335 | } |
2336 | if (cp[i] == '/') { |
2337 | hasdir = isunix = 1; |
2338 | break; |
2339 | } |
2340 | if (cp[i] == ']' || cp[i] == '>' || cp[i] == ':') { |
2341 | hasdir = 1; |
2342 | break; |
2343 | } |
2344 | } |
a15cef0c |
2345 | if ((tmpfp = PerlIO_tmpfile()) != NULL) { |
0d44d22b |
2346 | Stat_t st; |
2347 | if (!PerlLIO_stat(SvPVX(tmpglob),&st) && S_ISDIR(st.st_mode)) |
2348 | ok = ((wilddsc.dsc$a_pointer = tovmspath(SvPVX(tmpglob),vmsspec)) != NULL); |
2349 | else ok = ((wilddsc.dsc$a_pointer = tovmsspec(SvPVX(tmpglob),vmsspec)) != NULL); |
2350 | if (ok) wilddsc.dsc$w_length = (unsigned short int) strlen(wilddsc.dsc$a_pointer); |
a3987cb8 |
2351 | for (cp=wilddsc.dsc$a_pointer; ok && cp && *cp; cp++) |
2352 | if (*cp == '?') *cp = '%'; /* VMS style single-char wildcard */ |
0d44d22b |
2353 | while (ok && ((sts = lib$find_file(&wilddsc,&rsdsc,&cxt, |
2354 | &dfltdsc,NULL,NULL,NULL))&1)) { |
3ce6b0cb |
2355 | /* with varying string, 1st word of buffer contains result length */ |
2356 | end = rstr + *((unsigned short int*)rslt); |
2357 | if (!hasver) while (*end != ';' && end > rstr) end--; |
0d44d22b |
2358 | *(end++) = '\n'; *end = '\0'; |
2359 | for (cp = rstr; *cp; cp++) *cp = _tolower(*cp); |
2360 | if (hasdir) { |
2361 | if (isunix) trim_unixpath(rstr,SvPVX(tmpglob),1); |
2362 | begin = rstr; |
2363 | } |
2364 | else { |
2365 | begin = end; |
2366 | while (*(--begin) != ']' && *begin != '>') ; |
2367 | ++begin; |
2368 | } |
2369 | ok = (PerlIO_puts(tmpfp,begin) != EOF); |
2370 | } |
2371 | if (cxt) (void)lib$find_file_end(&cxt); |
2372 | if (ok && sts != RMS$_NMF && |
93189314 |
2373 | sts != RMS$_DNF && sts != RMS_FNF) ok = 0; |
0d44d22b |
2374 | if (!ok) { |
2375 | if (!(sts & 1)) { |
2376 | SETERRNO((sts == RMS$_SYN ? EINVAL : EVMSERR),sts); |
2377 | } |
2378 | PerlIO_close(tmpfp); |
2379 | fp = NULL; |
2380 | } |
2381 | else { |
2382 | PerlIO_rewind(tmpfp); |
2383 | IoTYPE(io) = IoTYPE_RDONLY; |
2384 | IoIFP(io) = fp = tmpfp; |
2385 | IoFLAGS(io) &= ~IOf_UNTAINT; /* maybe redundant */ |
2386 | } |
2387 | } |
2388 | } |
2389 | #else /* !VMS */ |
2390 | #ifdef MACOS_TRADITIONAL |
2391 | sv_setpv(tmpcmd, "glob "); |
2392 | sv_catsv(tmpcmd, tmpglob); |
2393 | sv_catpv(tmpcmd, " |"); |
2394 | #else |
2395 | #ifdef DOSISH |
2396 | #ifdef OS2 |
2397 | sv_setpv(tmpcmd, "for a in "); |
2398 | sv_catsv(tmpcmd, tmpglob); |
2399 | sv_catpv(tmpcmd, "; do echo \"$a\\0\\c\"; done |"); |
2400 | #else |
2401 | #ifdef DJGPP |
2402 | sv_setpv(tmpcmd, "/dev/dosglob/"); /* File System Extension */ |
2403 | sv_catsv(tmpcmd, tmpglob); |
2404 | #else |
2405 | sv_setpv(tmpcmd, "perlglob "); |
2406 | sv_catsv(tmpcmd, tmpglob); |
2407 | sv_catpv(tmpcmd, " |"); |
2408 | #endif /* !DJGPP */ |
2409 | #endif /* !OS2 */ |
2410 | #else /* !DOSISH */ |
2411 | #if defined(CSH) |
2412 | sv_setpvn(tmpcmd, PL_cshname, PL_cshlen); |
2413 | sv_catpv(tmpcmd, " -cf 'set nonomatch; glob "); |
2414 | sv_catsv(tmpcmd, tmpglob); |
2415 | sv_catpv(tmpcmd, "' 2>/dev/null |"); |
2416 | #else |
2417 | sv_setpv(tmpcmd, "echo "); |
2418 | sv_catsv(tmpcmd, tmpglob); |
2419 | #if 'z' - 'a' == 25 |
2420 | sv_catpv(tmpcmd, "|tr -s ' \t\f\r' '\\012\\012\\012\\012'|"); |
2421 | #else |
2422 | sv_catpv(tmpcmd, "|tr -s ' \t\f\r' '\\n\\n\\n\\n'|"); |
2423 | #endif |
2424 | #endif /* !CSH */ |
2425 | #endif /* !DOSISH */ |
2426 | #endif /* MACOS_TRADITIONAL */ |
2427 | (void)do_open(PL_last_in_gv, SvPVX(tmpcmd), SvCUR(tmpcmd), |
2428 | FALSE, O_RDONLY, 0, Nullfp); |
2429 | fp = IoIFP(io); |
2430 | #endif /* !VMS */ |
2431 | LEAVE; |
2432 | return fp; |
2433 | } |