This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
revamp the op_dump() output format
[perl5.git] / doio.c
CommitLineData
a0d0e21e 1/* doio.c
a687059c 2 *
1129b882
NC
3 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
4 * 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 by Larry Wall and others
a687059c 5 *
6e21c824
LW
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
LW
9 */
10
11/*
4ac71550
TC
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.
16 *
17 * [p.684 of _The Lord of the Rings_, IV/vi: "The Forbidden Pool"]
a687059c
LW
18 */
19
166f8a29
DM
20/* This file contains functions that do the actual I/O on behalf of ops.
21 * For example, pp_print() calls the do_print() function in this file for
22 * each argument needing printing.
23 */
24
a687059c 25#include "EXTERN.h"
864dbfa3 26#define PERL_IN_DOIO_C
a687059c
LW
27#include "perl.h"
28
fe14fcc3 29#if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
aec308ec 30#ifndef HAS_SEM
c2ab57d4 31#include <sys/ipc.h>
aec308ec 32#endif
fe14fcc3 33#ifdef HAS_MSG
c2ab57d4 34#include <sys/msg.h>
e5d73d77 35#endif
fe14fcc3 36#ifdef HAS_SHM
c2ab57d4 37#include <sys/shm.h>
a0d0e21e 38# ifndef HAS_SHMAT_PROTOTYPE
20ce7b12 39 extern Shmat_t shmat (int, char *, int);
a0d0e21e 40# endif
c2ab57d4 41#endif
e5d73d77 42#endif
c2ab57d4 43
663a0e37 44#ifdef I_UTIME
3730b96e 45# if defined(_MSC_VER) || defined(__MINGW32__)
3fe9a6f1 46# include <sys/utime.h>
47# else
48# include <utime.h>
49# endif
663a0e37 50#endif
85aff577 51
85aff577
CS
52#ifdef O_EXCL
53# define OPEN_EXCL O_EXCL
54#else
55# define OPEN_EXCL 0
56#endif
a687059c 57
0c19750d
SP
58#define PERL_MODE_MAX 8
59#define PERL_FLAGS_MAX 10
60
76121258 61#include <signal.h>
76121258 62
a2b41d5c
NC
63static IO *
64S_openn_setup(pTHX_ GV *gv, char *mode, PerlIO **saveifp, PerlIO **saveofp,
65 int *savefd, char *savetype)
a567e93b 66{
eb578fdb 67 IO * const io = GvIOn(gv);
a687059c 68
a2b41d5c
NC
69 PERL_ARGS_ASSERT_OPENN_SETUP;
70
71 *saveifp = NULL;
72 *saveofp = NULL;
73 *savefd = -1;
74 *savetype = IoTYPE_CLOSED;
7918f24d 75
b931b1d9 76 Zero(mode,sizeof(mode),char);
3280af22 77 PL_forkprocess = 1; /* assume true if no fork */
c07a80fd 78
b931b1d9 79 /* If currently open - close before we re-open */
a0d0e21e 80 if (IoIFP(io)) {
ee518936
NIS
81 if (IoTYPE(io) == IoTYPE_STD) {
82 /* This is a clone of one of STD* handles */
ee518936 83 }
26297fe9
NC
84 else {
85 const int old_fd = PerlIO_fileno(IoIFP(io));
86
87 if (old_fd >= 0 && old_fd <= PL_maxsysfd) {
88 /* This is one of the original STD* handles */
a2b41d5c
NC
89 *saveifp = IoIFP(io);
90 *saveofp = IoOFP(io);
91 *savetype = IoTYPE(io);
92 *savefd = old_fd;
26297fe9
NC
93 }
94 else {
95 int result;
96
97 if (IoTYPE(io) == IoTYPE_PIPE)
98 result = PerlProc_pclose(IoIFP(io));
99 else if (IoIFP(io) != IoOFP(io)) {
100 if (IoOFP(io)) {
101 result = PerlIO_close(IoOFP(io));
102 PerlIO_close(IoIFP(io)); /* clear stdio, fd already closed */
103 }
104 else
105 result = PerlIO_close(IoIFP(io));
106 }
107 else
108 result = PerlIO_close(IoIFP(io));
109
110 if (result == EOF && old_fd > PL_maxsysfd) {
111 /* Why is this not Perl_warn*() call ? */
112 PerlIO_printf(Perl_error_log,
147e3846
KW
113 "Warning: unable to close filehandle %" HEKf
114 " properly.\n",
26297fe9
NC
115 HEKfARG(GvENAME_HEK(gv))
116 );
117 }
118 }
119 }
4608196e 120 IoOFP(io) = IoIFP(io) = NULL;
a687059c 121 }
a2b41d5c
NC
122 return io;
123}
124
125bool
126Perl_do_openn(pTHX_ GV *gv, const char *oname, I32 len, int as_raw,
127 int rawmode, int rawperm, PerlIO *supplied_fp, SV **svp,
128 I32 num_svs)
129{
4b451737
NC
130 PERL_ARGS_ASSERT_DO_OPENN;
131
132 if (as_raw) {
133 /* sysopen style args, i.e. integer mode and permissions */
134
135 if (num_svs != 0) {
136 Perl_croak(aTHX_ "panic: sysopen with multiple args, num_svs=%ld",
137 (long) num_svs);
138 }
139 return do_open_raw(gv, oname, len, rawmode, rawperm);
140 }
141 return do_open6(gv, oname, len, supplied_fp, svp, num_svs);
142}
143
144bool
145Perl_do_open_raw(pTHX_ GV *gv, const char *oname, STRLEN len,
146 int rawmode, int rawperm)
147{
a2b41d5c
NC
148 PerlIO *saveifp;
149 PerlIO *saveofp;
150 int savefd;
151 char savetype;
152 char mode[PERL_MODE_MAX]; /* file mode ("r\0", "rb\0", "ab\0" etc.) */
153 IO * const io = openn_setup(gv, mode, &saveifp, &saveofp, &savefd, &savetype);
154 int writing = 0;
155 PerlIO *fp;
a2b41d5c 156
4b451737 157 PERL_ARGS_ASSERT_DO_OPEN_RAW;
c07a80fd 158
4b451737
NC
159 /* For ease of blame back to 5.000, keep the existing indenting. */
160 {
b931b1d9 161 /* sysopen style args, i.e. integer mode and permissions */
ee518936 162 STRLEN ix = 0;
e1ec3a88 163 const int appendtrunc =
3dccc55c 164 0
d1da7611 165#ifdef O_APPEND /* Not fully portable. */
3dccc55c 166 |O_APPEND
d1da7611
JH
167#endif
168#ifdef O_TRUNC /* Not fully portable. */
3dccc55c 169 |O_TRUNC
d1da7611 170#endif
3dccc55c 171 ;
6867be6d 172 const int modifyingmode = O_WRONLY|O_RDWR|O_CREAT|appendtrunc;
3dccc55c 173 int ismodifying;
9229bf8d 174 SV *namesv;
3dccc55c 175
3dccc55c
JH
176 /* It's not always
177
178 O_RDONLY 0
179 O_WRONLY 1
180 O_RDWR 2
181
182 It might be (in OS/390 and Mac OS Classic it is)
183
184 O_WRONLY 1
185 O_RDONLY 2
186 O_RDWR 3
187
188 This means that simple & with O_RDWR would look
189 like O_RDONLY is present. Therefore we have to
190 be more careful.
191 */
192 if ((ismodifying = (rawmode & modifyingmode))) {
193 if ((ismodifying & O_WRONLY) == O_WRONLY ||
194 (ismodifying & O_RDWR) == O_RDWR ||
195 (ismodifying & (O_CREAT|appendtrunc)))
196 TAINT_PROPER("sysopen");
197 }
3b6c1aba 198 mode[ix++] = IoTYPE_NUMERIC; /* Marker to openn to use numeric "sysopen" */
b931b1d9 199
09458382 200#if defined(USE_64_BIT_RAWIO) && defined(O_LARGEFILE)
b94c04ac 201 rawmode |= O_LARGEFILE; /* Transparently largefiley. */
5ff3f7a4
GS
202#endif
203
06c7082d 204 IoTYPE(io) = PerlIO_intmode2str(rawmode, &mode[ix], &writing);
ee518936 205
59cd0e26 206 namesv = newSVpvn_flags(oname, len, SVs_TEMP);
4b451737 207 fp = PerlIO_openn(aTHX_ NULL, mode, -1, rawmode, rawperm, NULL, 1, &namesv);
a687059c 208 }
4b451737
NC
209 return openn_cleanup(gv, io, fp, mode, oname, saveifp, saveofp, savefd,
210 savetype, writing, 0, NULL);
211}
212
213bool
214Perl_do_open6(pTHX_ GV *gv, const char *oname, STRLEN len,
215 PerlIO *supplied_fp, SV **svp, U32 num_svs)
216{
4b451737
NC
217 PerlIO *saveifp;
218 PerlIO *saveofp;
219 int savefd;
220 char savetype;
221 char mode[PERL_MODE_MAX]; /* file mode ("r\0", "rb\0", "ab\0" etc.) */
222 IO * const io = openn_setup(gv, mode, &saveifp, &saveofp, &savefd, &savetype);
223 int writing = 0;
224 PerlIO *fp;
225 bool was_fdopen = FALSE;
226 char *type = NULL;
227
228 PERL_ARGS_ASSERT_DO_OPEN6;
229
230 /* For ease of blame back to 5.000, keep the existing indenting. */
231 {
b931b1d9 232 /* Regular (non-sys) open */
2fbb330f 233 char *name;
faecd977 234 STRLEN olen = len;
b931b1d9
NIS
235 char *tend;
236 int dodup = 0;
c564b489
NC
237 bool in_raw = 0, in_crlf = 0, out_raw = 0, out_crlf = 0;
238
239 /* Collect default raw/crlf info from the op */
240 if (PL_op && PL_op->op_type == OP_OPEN) {
241 /* set up IO layers */
242 const U8 flags = PL_op->op_private;
243 in_raw = (flags & OPpOPEN_IN_RAW);
244 in_crlf = (flags & OPpOPEN_IN_CRLF);
245 out_raw = (flags & OPpOPEN_OUT_RAW);
246 out_crlf = (flags & OPpOPEN_OUT_CRLF);
247 }
c07a80fd 248
2fbb330f 249 type = savepvn(oname, len);
b931b1d9 250 tend = type+len;
faecd977 251 SAVEFREEPV(type);
eb649f83
AMS
252
253 /* Lose leading and trailing white space */
294b3b39
AL
254 while (isSPACE(*type))
255 type++;
eb649f83
AMS
256 while (tend > type && isSPACE(tend[-1]))
257 *--tend = '\0';
258
6170680b 259 if (num_svs) {
41188aa0
TC
260 const char *p;
261 STRLEN nlen = 0;
c2be40b1 262 /* New style explicit name, type is just mode and layer info */
9a869a14 263#ifdef USE_STDIO
2fbb330f 264 if (SvROK(*svp) && !strchr(oname,'&')) {
9a869a14
RGS
265 if (ckWARN(WARN_IO))
266 Perl_warner(aTHX_ packWARN(WARN_IO),
267 "Can't open a reference");
93189314 268 SETERRNO(EINVAL, LIB_INVARG);
a6fc70e5 269 fp = NULL;
9a869a14
RGS
270 goto say_false;
271 }
272#endif /* USE_STDIO */
41188aa0
TC
273 p = (SvOK(*svp) || SvGMAGICAL(*svp)) ? SvPV(*svp, nlen) : NULL;
274
a6fc70e5
NC
275 if (p && !IS_SAFE_PATHNAME(p, nlen, "open")) {
276 fp = NULL;
c8028aa6 277 goto say_false;
a6fc70e5 278 }
c8028aa6 279
41188aa0
TC
280 name = p ? savepvn(p, nlen) : savepvs("");
281
faecd977 282 SAVEFREEPV(name);
6170680b 283 }
faecd977 284 else {
faecd977 285 name = type;
b931b1d9 286 len = tend-type;
faecd977 287 }
6170680b 288 IoTYPE(io) = *type;
516a5887 289 if ((*type == IoTYPE_RDWR) && /* scary */
01a8ea99 290 (*(type+1) == IoTYPE_RDONLY || *(type+1) == IoTYPE_WRONLY) &&
516a5887 291 ((!num_svs || (tend > type+1 && tend[-1] != IoTYPE_PIPE)))) {
c2be40b1 292 TAINT_PROPER("open");
6170680b 293 mode[1] = *type++;
c07a80fd 294 writing = 1;
a687059c 295 }
c07a80fd 296
9f37169a 297 if (*type == IoTYPE_PIPE) {
b931b1d9
NIS
298 if (num_svs) {
299 if (type[1] != IoTYPE_STD) {
c2be40b1 300 unknown_open_mode:
b931b1d9
NIS
301 Perl_croak(aTHX_ "Unknown open() mode '%.*s'", (int)olen, oname);
302 }
303 type++;
6170680b 304 }
294b3b39
AL
305 do {
306 type++;
307 } while (isSPACE(*type));
faecd977 308 if (!num_svs) {
6170680b 309 name = type;
b931b1d9 310 len = tend-type;
faecd977 311 }
4a7d1889
NIS
312 if (*name == '\0') {
313 /* command is missing 19990114 */
06eaf0bc 314 if (ckWARN(WARN_PIPE))
9014280d 315 Perl_warner(aTHX_ packWARN(WARN_PIPE), "Missing command in piped open");
06eaf0bc 316 errno = EPIPE;
a6fc70e5 317 fp = NULL;
06eaf0bc
GS
318 goto say_false;
319 }
f27977c3 320 if (!(*name == '-' && name[1] == '\0') || num_svs)
c07a80fd 321 TAINT_ENV();
322 TAINT_PROPER("piped open");
b931b1d9 323 if (!num_svs && name[len-1] == '|') {
faecd977 324 name[--len] = '\0' ;
599cee73 325 if (ckWARN(WARN_PIPE))
9014280d 326 Perl_warner(aTHX_ packWARN(WARN_PIPE), "Can't open bidirectional pipe");
7b8d334a 327 }
a1d180c4 328 mode[0] = 'w';
c07a80fd 329 writing = 1;
0c19750d 330 if (out_raw)
5686ee58 331 mode[1] = 'b';
0c19750d 332 else if (out_crlf)
5686ee58 333 mode[1] = 't';
4a7d1889
NIS
334 if (num_svs > 1) {
335 fp = PerlProc_popen_list(mode, num_svs, svp);
336 }
337 else {
338 fp = PerlProc_popen(name,mode);
339 }
1771866f
NIS
340 if (num_svs) {
341 if (*type) {
342 if (PerlIO_apply_layers(aTHX_ fp, mode, type) != 0) {
a6fc70e5 343 fp = NULL;
1771866f
NIS
344 goto say_false;
345 }
346 }
347 }
c2be40b1 348 } /* IoTYPE_PIPE */
9f37169a 349 else if (*type == IoTYPE_WRONLY) {
c07a80fd 350 TAINT_PROPER("open");
6170680b 351 type++;
9f37169a
JH
352 if (*type == IoTYPE_WRONLY) {
353 /* Two IoTYPE_WRONLYs in a row make for an IoTYPE_APPEND. */
50952442 354 mode[0] = IoTYPE(io) = IoTYPE_APPEND;
6170680b 355 type++;
a0d0e21e 356 }
ee518936 357 else {
c07a80fd 358 mode[0] = 'w';
ee518936 359 }
c07a80fd 360 writing = 1;
361
0c19750d 362 if (out_raw)
5686ee58 363 mode[1] = 'b';
0c19750d 364 else if (out_crlf)
5686ee58 365 mode[1] = 't';
6170680b 366 if (*type == '&') {
c07a80fd 367 duplicity:
ecdeb87c 368 dodup = PERLIO_DUP_FD;
e620cd72
NIS
369 type++;
370 if (*type == '=') {
c07a80fd 371 dodup = 0;
e620cd72 372 type++;
4a7d1889 373 }
ee518936 374 if (!num_svs && !*type && supplied_fp) {
4a7d1889 375 /* "<+&" etc. is used by typemaps */
c07a80fd 376 fp = supplied_fp;
ee518936 377 }
a0d0e21e 378 else {
35da51f7 379 PerlIO *that_fp = NULL;
b4464d55 380 int wanted_fd;
22ff3130 381 UV uv;
e620cd72 382 if (num_svs > 1) {
fe13d51d 383 /* diag_listed_as: More than one argument to '%s' open */
e620cd72
NIS
384 Perl_croak(aTHX_ "More than one argument to '%c&' open",IoTYPE(io));
385 }
294b3b39
AL
386 while (isSPACE(*type))
387 type++;
f90b7232
FC
388 if (num_svs && (
389 SvIOK(*svp)
390 || (SvPOKp(*svp) && looks_like_number(*svp))
391 )) {
b4464d55 392 wanted_fd = SvUV(*svp);
24a7a40d 393 num_svs = 0;
ee518936 394 }
22ff3130
HS
395 else if (isDIGIT(*type)
396 && grok_atoUV(type, &uv, NULL)
397 && uv <= INT_MAX
398 ) {
399 wanted_fd = (int)uv;
e620cd72 400 }
c07a80fd 401 else {
e1ec3a88 402 const IO* thatio;
e620cd72
NIS
403 if (num_svs) {
404 thatio = sv_2io(*svp);
405 }
406 else {
35da51f7 407 GV * const thatgv = gv_fetchpvn_flags(type, tend - type,
90e5519e 408 0, SVt_PVIO);
e620cd72
NIS
409 thatio = GvIO(thatgv);
410 }
c07a80fd 411 if (!thatio) {
6e21c824 412#ifdef EINVAL
93189314 413 SETERRNO(EINVAL,SS_IVCHAN);
6e21c824 414#endif
a6fc70e5 415 fp = NULL;
c07a80fd 416 goto say_false;
417 }
f4e789af 418 if ((that_fp = IoIFP(thatio))) {
7211d486
JH
419 /* Flush stdio buffer before dup. --mjd
420 * Unfortunately SEEK_CURing 0 seems to
421 * be optimized away on most platforms;
422 * only Solaris and Linux seem to flush
423 * on that. --jhi */
7211d486
JH
424 /* On the other hand, do all platforms
425 * take gracefully to flushing a read-only
426 * filehandle? Perhaps we should do
427 * fsetpos(src)+fgetpos(dst)? --nik */
ecdeb87c 428 PerlIO_flush(that_fp);
b4464d55 429 wanted_fd = PerlIO_fileno(that_fp);
0759c907
JH
430 /* When dup()ing STDIN, STDOUT or STDERR
431 * explicitly set appropriate access mode */
f4e789af
NC
432 if (that_fp == PerlIO_stdout()
433 || that_fp == PerlIO_stderr())
0759c907 434 IoTYPE(io) = IoTYPE_WRONLY;
f4e789af 435 else if (that_fp == PerlIO_stdin())
0759c907
JH
436 IoTYPE(io) = IoTYPE_RDONLY;
437 /* When dup()ing a socket, say result is
438 * one as well */
439 else if (IoTYPE(thatio) == IoTYPE_SOCKET)
50952442 440 IoTYPE(io) = IoTYPE_SOCKET;
c07a80fd 441 }
0c9375a5
TC
442 else {
443 SETERRNO(EBADF, RMS_IFI);
444 fp = NULL;
445 goto say_false;
446 }
a0d0e21e 447 }
ee518936 448 if (!num_svs)
bd61b366 449 type = NULL;
ecdeb87c
NIS
450 if (that_fp) {
451 fp = PerlIO_fdupopen(aTHX_ that_fp, NULL, dodup);
452 }
453 else {
c07a80fd 454 if (dodup)
b4464d55 455 wanted_fd = PerlLIO_dup(wanted_fd);
ecdeb87c
NIS
456 else
457 was_fdopen = TRUE;
b4464d55
NC
458 if (!(fp = PerlIO_openn(aTHX_ type,mode,wanted_fd,0,0,NULL,num_svs,svp))) {
459 if (dodup && wanted_fd >= 0)
460 PerlLIO_close(wanted_fd);
ecdeb87c 461 }
faecd977 462 }
c07a80fd 463 }
ee518936 464 } /* & */
c07a80fd 465 else {
294b3b39
AL
466 while (isSPACE(*type))
467 type++;
b931b1d9 468 if (*type == IoTYPE_STD && (!type[1] || isSPACE(type[1]) || type[1] == ':')) {
b931b1d9 469 type++;
760ac839 470 fp = PerlIO_stdout();
50952442 471 IoTYPE(io) = IoTYPE_STD;
7cf31beb 472 if (num_svs > 1) {
fe13d51d 473 /* diag_listed_as: More than one argument to '%s' open */
7cf31beb
NIS
474 Perl_croak(aTHX_ "More than one argument to '>%c' open",IoTYPE_STD);
475 }
c07a80fd 476 }
477 else {
9229bf8d
NC
478 if (num_svs) {
479 fp = PerlIO_openn(aTHX_ type,mode,-1,0,0,NULL,num_svs,svp);
480 }
481 else {
482 SV *namesv = newSVpvn_flags(type, tend - type, SVs_TEMP);
bd61b366 483 type = NULL;
9229bf8d 484 fp = PerlIO_openn(aTHX_ type,mode,-1,0,0,NULL,1,&namesv);
ee518936 485 }
c07a80fd 486 }
ee518936 487 } /* !& */
7e72d509
JH
488 if (!fp && type && *type && *type != ':' && !isIDFIRST(*type))
489 goto unknown_open_mode;
c2be40b1 490 } /* IoTYPE_WRONLY */
9f37169a 491 else if (*type == IoTYPE_RDONLY) {
294b3b39
AL
492 do {
493 type++;
494 } while (isSPACE(*type));
bf38876a 495 mode[0] = 'r';
0c19750d 496 if (in_raw)
5686ee58 497 mode[1] = 'b';
0c19750d 498 else if (in_crlf)
5686ee58 499 mode[1] = 't';
6170680b 500 if (*type == '&') {
bf38876a 501 goto duplicity;
6170680b 502 }
b931b1d9 503 if (*type == IoTYPE_STD && (!type[1] || isSPACE(type[1]) || type[1] == ':')) {
b931b1d9 504 type++;
760ac839 505 fp = PerlIO_stdin();
50952442 506 IoTYPE(io) = IoTYPE_STD;
7cf31beb 507 if (num_svs > 1) {
fe13d51d 508 /* diag_listed_as: More than one argument to '%s' open */
7cf31beb
NIS
509 Perl_croak(aTHX_ "More than one argument to '<%c' open",IoTYPE_STD);
510 }
a687059c 511 }
ee518936 512 else {
9229bf8d
NC
513 if (num_svs) {
514 fp = PerlIO_openn(aTHX_ type,mode,-1,0,0,NULL,num_svs,svp);
515 }
516 else {
517 SV *namesv = newSVpvn_flags(type, tend - type, SVs_TEMP);
bd61b366 518 type = NULL;
9229bf8d 519 fp = PerlIO_openn(aTHX_ type,mode,-1,0,0,NULL,1,&namesv);
ee518936 520 }
ee518936 521 }
7e72d509
JH
522 if (!fp && type && *type && *type != ':' && !isIDFIRST(*type))
523 goto unknown_open_mode;
c2be40b1
JH
524 } /* IoTYPE_RDONLY */
525 else if ((num_svs && /* '-|...' or '...|' */
526 type[0] == IoTYPE_STD && type[1] == IoTYPE_PIPE) ||
b931b1d9 527 (!num_svs && tend > type+1 && tend[-1] == IoTYPE_PIPE)) {
6170680b 528 if (num_svs) {
b931b1d9 529 type += 2; /* skip over '-|' */
6170680b
IZ
530 }
531 else {
b931b1d9
NIS
532 *--tend = '\0';
533 while (tend > type && isSPACE(tend[-1]))
534 *--tend = '\0';
a6e20a40
AL
535 for (; isSPACE(*type); type++)
536 ;
6170680b 537 name = type;
b931b1d9 538 len = tend-type;
6170680b 539 }
4a7d1889
NIS
540 if (*name == '\0') {
541 /* command is missing 19990114 */
06eaf0bc 542 if (ckWARN(WARN_PIPE))
9014280d 543 Perl_warner(aTHX_ packWARN(WARN_PIPE), "Missing command in piped open");
06eaf0bc 544 errno = EPIPE;
a6fc70e5 545 fp = NULL;
06eaf0bc
GS
546 goto say_false;
547 }
770526c1 548 if (!(*name == '-' && name[1] == '\0') || num_svs)
79072805
LW
549 TAINT_ENV();
550 TAINT_PROPER("piped open");
a1d180c4 551 mode[0] = 'r';
0c19750d 552
0c19750d 553 if (in_raw)
5686ee58 554 mode[1] = 'b';
0c19750d 555 else if (in_crlf)
5686ee58 556 mode[1] = 't';
0c19750d 557
4a7d1889
NIS
558 if (num_svs > 1) {
559 fp = PerlProc_popen_list(mode,num_svs,svp);
560 }
e620cd72 561 else {
4a7d1889
NIS
562 fp = PerlProc_popen(name,mode);
563 }
50952442 564 IoTYPE(io) = IoTYPE_PIPE;
1771866f 565 if (num_svs) {
294b3b39
AL
566 while (isSPACE(*type))
567 type++;
1771866f
NIS
568 if (*type) {
569 if (PerlIO_apply_layers(aTHX_ fp, mode, type) != 0) {
a6fc70e5 570 fp = NULL;
1771866f
NIS
571 goto say_false;
572 }
573 }
574 }
a687059c 575 }
c2be40b1 576 else { /* layer(Args) */
6170680b 577 if (num_svs)
c2be40b1 578 goto unknown_open_mode;
6170680b 579 name = type;
50952442 580 IoTYPE(io) = IoTYPE_RDONLY;
a6e20a40
AL
581 for (; isSPACE(*name); name++)
582 ;
88b61e10 583 mode[0] = 'r';
0c19750d 584
0c19750d 585 if (in_raw)
5686ee58 586 mode[1] = 'b';
0c19750d 587 else if (in_crlf)
5686ee58 588 mode[1] = 't';
0c19750d 589
770526c1 590 if (*name == '-' && name[1] == '\0') {
760ac839 591 fp = PerlIO_stdin();
50952442 592 IoTYPE(io) = IoTYPE_STD;
a687059c 593 }
16fe6d59 594 else {
9229bf8d
NC
595 if (num_svs) {
596 fp = PerlIO_openn(aTHX_ type,mode,-1,0,0,NULL,num_svs,svp);
597 }
598 else {
599 SV *namesv = newSVpvn_flags(type, tend - type, SVs_TEMP);
bd61b366 600 type = NULL;
9229bf8d 601 fp = PerlIO_openn(aTHX_ type,mode,-1,0,0,NULL,1,&namesv);
ee518936 602 }
16fe6d59 603 }
a687059c
LW
604 }
605 }
a6fc70e5
NC
606
607 say_false:
608 return openn_cleanup(gv, io, fp, mode, oname, saveifp, saveofp, savefd,
609 savetype, writing, was_fdopen, type);
610}
611
612/* Yes, this is ugly, but it's private, and I don't see a cleaner way to
613 simplify the two-headed public interface of do_openn. */
614static bool
615S_openn_cleanup(pTHX_ GV *gv, IO *io, PerlIO *fp, char *mode, const char *oname,
616 PerlIO *saveifp, PerlIO *saveofp, int savefd, char savetype,
617 int writing, bool was_fdopen, const char *type)
618{
619 int fd;
620
621 PERL_ARGS_ASSERT_OPENN_CLEANUP;
622
bee1dbe2 623 if (!fp) {
ce44635a 624 if (IoTYPE(io) == IoTYPE_RDONLY && ckWARN(WARN_NEWLINE)
7cb3f959 625 && should_warn_nl(oname)
ce44635a 626
041457d9 627 )
5d37acd6
DM
628 {
629 GCC_DIAG_IGNORE(-Wformat-nonliteral); /* PL_warn_nl is constant */
9014280d 630 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "open");
5d37acd6
DM
631 GCC_DIAG_RESTORE;
632 }
6e21c824 633 goto say_false;
bee1dbe2 634 }
a00b5bd3
NIS
635
636 if (ckWARN(WARN_IO)) {
637 if ((IoTYPE(io) == IoTYPE_RDONLY) &&
638 (fp == PerlIO_stdout() || fp == PerlIO_stderr())) {
9014280d 639 Perl_warner(aTHX_ packWARN(WARN_IO),
147e3846 640 "Filehandle STD%s reopened as %" HEKf
d0c0e7dd 641 " only for input",
97828cef 642 ((fp == PerlIO_stdout()) ? "OUT" : "ERR"),
d0c0e7dd 643 HEKfARG(GvENAME_HEK(gv)));
a00b5bd3 644 }
ee518936 645 else if ((IoTYPE(io) == IoTYPE_WRONLY) && fp == PerlIO_stdin()) {
9014280d 646 Perl_warner(aTHX_ packWARN(WARN_IO),
147e3846 647 "Filehandle STDIN reopened as %" HEKf " only for output",
d0c0e7dd
FC
648 HEKfARG(GvENAME_HEK(gv))
649 );
a00b5bd3
NIS
650 }
651 }
652
e99cca91 653 fd = PerlIO_fileno(fp);
375ed12a
JH
654 /* Do NOT do: "if (fd < 0) goto say_false;" here. If there is no
655 * fd assume it isn't a socket - this covers PerlIO::scalar -
656 * otherwise unless we "know" the type probe for socket-ness.
e99cca91
NIS
657 */
658 if (IoTYPE(io) && IoTYPE(io) != IoTYPE_PIPE && IoTYPE(io) != IoTYPE_STD && fd >= 0) {
659 if (PerlLIO_fstat(fd,&PL_statbuf) < 0) {
660 /* If PerlIO claims to have fd we had better be able to fstat() it. */
661 (void) PerlIO_close(fp);
6e21c824 662 goto say_false;
a687059c 663 }
7114a2d2 664#ifndef PERL_MICRO
3280af22 665 if (S_ISSOCK(PL_statbuf.st_mode))
50952442 666 IoTYPE(io) = IoTYPE_SOCKET; /* in case a socket was passed in to us */
99b89507
LW
667#ifdef HAS_SOCKET
668 else if (
3280af22 669 !(PL_statbuf.st_mode & S_IFMT)
0759c907
JH
670 && IoTYPE(io) != IoTYPE_WRONLY /* Dups of STD* filehandles already have */
671 && IoTYPE(io) != IoTYPE_RDONLY /* type so they aren't marked as sockets */
672 ) { /* on OS's that return 0 on fstat()ed pipe */
e99cca91
NIS
673 char tmpbuf[256];
674 Sock_size_t buflen = sizeof tmpbuf;
675 if (PerlSock_getsockname(fd, (struct sockaddr *)tmpbuf, &buflen) >= 0
676 || errno != ENOTSOCK)
677 IoTYPE(io) = IoTYPE_SOCKET; /* some OS's return 0 on fstat()ed socket */
678 /* but some return 0 for streams too, sigh */
99b89507 679 }
e99cca91 680#endif /* HAS_SOCKET */
7114a2d2 681#endif /* !PERL_MICRO */
a687059c 682 }
e99cca91
NIS
683
684 /* Eeek - FIXME !!!
685 * If this is a standard handle we discard all the layer stuff
686 * and just dup the fd into whatever was on the handle before !
687 */
688
6e21c824 689 if (saveifp) { /* must use old fp? */
f5b9d040 690 /* If fd is less that PL_maxsysfd i.e. STDIN..STDERR
24c23ab4 691 then dup the new fileno down
f5b9d040 692 */
6e21c824 693 if (saveofp) {
f5b9d040 694 PerlIO_flush(saveofp); /* emulate PerlIO_close() */
6e21c824 695 if (saveofp != saveifp) { /* was a socket? */
760ac839 696 PerlIO_close(saveofp);
6e21c824
LW
697 }
698 }
6e60e805 699 if (savefd != fd) {
e934609f 700 /* Still a small can-of-worms here if (say) PerlIO::scalar
ecdeb87c
NIS
701 is assigned to (say) STDOUT - for now let dup2() fail
702 and provide the error
703 */
375ed12a
JH
704 if (fd < 0) {
705 SETERRNO(EBADF,RMS_IFI);
706 goto say_false;
707 } else if (PerlLIO_dup2(fd, savefd) < 0) {
bd4a5668
NIS
708 (void)PerlIO_close(fp);
709 goto say_false;
710 }
d082dcd6 711#ifdef VMS
6e60e805 712 if (savefd != PerlIO_fileno(PerlIO_stdin())) {
d0e2cf63
AMS
713 char newname[FILENAME_MAX+1];
714 if (PerlIO_getname(fp, newname)) {
715 if (fd == PerlIO_fileno(PerlIO_stdout()))
0db50132 716 vmssetuserlnm("SYS$OUTPUT", newname);
d0e2cf63 717 if (fd == PerlIO_fileno(PerlIO_stderr()))
0db50132 718 vmssetuserlnm("SYS$ERROR", newname);
d0e2cf63 719 }
d082dcd6
JH
720 }
721#endif
d0e2cf63
AMS
722
723#if !defined(WIN32)
724 /* PL_fdpid isn't used on Windows, so avoid this useless work.
725 * XXX Probably the same for a lot of other places. */
726 {
727 Pid_t pid;
728 SV *sv;
729
d0e2cf63 730 sv = *av_fetch(PL_fdpid,fd,TRUE);
862a34c6 731 SvUPGRADE(sv, SVt_IV);
d0e2cf63 732 pid = SvIVX(sv);
45977657 733 SvIV_set(sv, 0);
d0e2cf63 734 sv = *av_fetch(PL_fdpid,savefd,TRUE);
862a34c6 735 SvUPGRADE(sv, SVt_IV);
45977657 736 SvIV_set(sv, pid);
d0e2cf63
AMS
737 }
738#endif
739
e212fc47
AMS
740 if (was_fdopen) {
741 /* need to close fp without closing underlying fd */
742 int ofd = PerlIO_fileno(fp);
375ed12a 743 int dupfd = ofd >= 0 ? PerlLIO_dup(ofd) : -1;
03e631df 744#if defined(HAS_FCNTL) && defined(F_SETFD)
5798d631
JH
745 /* Assume if we have F_SETFD we have F_GETFD. */
746 /* Get a copy of all the fd flags. */
747 int fd_flags = ofd >= 0 ? fcntl(ofd, F_GETFD) : -1;
748 if (fd_flags < 0) {
375ed12a
JH
749 if (dupfd >= 0)
750 PerlLIO_close(dupfd);
751 goto say_false;
752 }
03e631df 753#endif
375ed12a
JH
754 if (ofd < 0 || dupfd < 0) {
755 if (dupfd >= 0)
756 PerlLIO_close(dupfd);
757 goto say_false;
758 }
e212fc47 759 PerlIO_close(fp);
375ed12a 760 PerlLIO_dup2(dupfd, ofd);
03e631df 761#if defined(HAS_FCNTL) && defined(F_SETFD)
5798d631
JH
762 /* The dup trick has lost close-on-exec on ofd,
763 * and possibly any other flags, so restore them. */
764 fcntl(ofd,F_SETFD, fd_flags);
03e631df 765#endif
e212fc47 766 PerlLIO_close(dupfd);
ecdeb87c 767 }
e212fc47
AMS
768 else
769 PerlIO_close(fp);
6e21c824
LW
770 }
771 fp = saveifp;
760ac839 772 PerlIO_clearerr(fp);
e99cca91 773 fd = PerlIO_fileno(fp);
6e21c824 774 }
194e5090 775#if defined(HAS_FCNTL) && defined(F_SETFD) && defined(FD_CLOEXEC)
f6ec3cd3 776 if (fd >= 0 && fd > PL_maxsysfd && fcntl(fd, F_SETFD, FD_CLOEXEC) < 0) {
194e5090
JH
777 PerlLIO_close(fd);
778 goto say_false;
a8710ca1 779 }
1462b684 780#endif
8990e307 781 IoIFP(io) = fp;
b931b1d9 782
684bef36 783 IoFLAGS(io) &= ~IOf_NOLINE;
bf38876a 784 if (writing) {
50952442 785 if (IoTYPE(io) == IoTYPE_SOCKET
e99cca91 786 || (IoTYPE(io) == IoTYPE_WRONLY && fd >= 0 && S_ISCHR(PL_statbuf.st_mode)) ) {
a33cf58c 787 char *s = mode;
3b6c1aba
JH
788 if (*s == IoTYPE_IMPLICIT || *s == IoTYPE_NUMERIC)
789 s++;
a33cf58c 790 *s = 'w';
7c491510 791 if (!(IoOFP(io) = PerlIO_openn(aTHX_ type,s,fd,0,0,NULL,0,NULL))) {
760ac839 792 PerlIO_close(fp);
6e21c824 793 goto say_false;
fe14fcc3 794 }
1462b684
LW
795 }
796 else
8990e307 797 IoOFP(io) = fp;
bf38876a 798 }
a687059c 799 return TRUE;
6e21c824 800
7b52d656 801 say_false:
8990e307
LW
802 IoIFP(io) = saveifp;
803 IoOFP(io) = saveofp;
804 IoTYPE(io) = savetype;
6e21c824 805 return FALSE;
a687059c
LW
806}
807
760ac839 808PerlIO *
157fb5a1 809Perl_nextargv(pTHX_ GV *gv, bool nomagicopen)
a687059c 810{
2d03de9c 811 IO * const io = GvIOp(gv);
502aca56 812 SV *const old_out_name = PL_inplace ? newSVsv(GvSV(gv)) : NULL;
fe14fcc3 813
7918f24d
NC
814 PERL_ARGS_ASSERT_NEXTARGV;
815
502aca56
TC
816 if (old_out_name)
817 SAVEFREESV(old_out_name);
818
3280af22 819 if (!PL_argvoutgv)
fafc274c 820 PL_argvoutgv = gv_fetchpvs("ARGVOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO);
5513c2cf 821 if (io && (IoFLAGS(io) & (IOf_ARGV|IOf_START)) == (IOf_ARGV|IOf_START)) {
18708f5a 822 IoFLAGS(io) &= ~IOf_START;
7a1c5554 823 if (PL_inplace) {
294b3b39 824 assert(PL_defoutgv);
29a861e7
NC
825 Perl_av_create_and_push(aTHX_ &PL_argvout_stack,
826 SvREFCNT_inc_simple_NN(PL_defoutgv));
7a1c5554 827 }
18708f5a 828 }
3280af22
NIS
829 if (PL_filemode & (S_ISUID|S_ISGID)) {
830 PerlIO_flush(IoIFP(GvIOn(PL_argvoutgv))); /* chmod must follow last write */
fe14fcc3 831#ifdef HAS_FCHMOD
c797f2d8
DM
832 if (PL_lastfd != -1)
833 (void)fchmod(PL_lastfd,PL_filemode);
fe14fcc3 834#else
b28d0864 835 (void)PerlLIO_chmod(PL_oldname,PL_filemode);
fe14fcc3
LW
836#endif
837 }
c797f2d8 838 PL_lastfd = -1;
3280af22 839 PL_filemode = 0;
5c501b37 840 if (!GvAV(gv))
4608196e 841 return NULL;
b9f2b683 842 while (av_tindex(GvAV(gv)) >= 0) {
45a23732 843 Stat_t statbuf;
85aff577 844 STRLEN oldlen;
1fa0529f 845 SV *const sv = av_shift(GvAV(gv));
8990e307 846 SAVEFREESV(sv);
4bac9ae4 847 SvTAINTED_off(GvSVn(gv)); /* previous tainting irrelevant */
e203899d 848 sv_setsv(GvSVn(gv),sv);
79072805 849 SvSETMAGIC(GvSV(gv));
3280af22 850 PL_oldname = SvPVx(GvSV(gv), oldlen);
d8015975 851 if (LIKELY(!PL_inplace)) {
157fb5a1
RGS
852 if (nomagicopen
853 ? do_open6(gv, "<", 1, NULL, &GvSV(gv), 1)
854 : do_open6(gv, PL_oldname, oldlen, NULL, NULL, 0)
855 ) {
d8015975
NC
856 return IoIFP(GvIOp(gv));
857 }
858 }
859 else {
502aca56
TC
860 {
861 IO * const io = GvIOp(PL_argvoutgv);
862 if (io && IoIFP(io) && old_out_name && !io_close(io, PL_argvoutgv, FALSE, FALSE)) {
147e3846
KW
863 Perl_croak(aTHX_ "Failed to close in-place edit file %"
864 SVf ": %s\n", old_out_name, Strerror(errno));
502aca56
TC
865 }
866 }
d8015975
NC
867 /* This very long block ends with return IoIFP(GvIOp(gv));
868 Both this block and the block above fall through on open
869 failure to the warning code, and then the while loop above tries
870 the next entry. */
871 if (do_open_raw(gv, PL_oldname, oldlen, O_RDONLY, 0)) {
1fa0529f
NC
872#ifndef FLEXFILENAMES
873 int filedev;
874 int fileino;
875#endif
876 Uid_t fileuid;
877 Gid_t filegid;
878
79072805 879 TAINT_PROPER("inplace open");
3280af22 880 if (oldlen == 1 && *PL_oldname == '-') {
fafc274c
NC
881 setdefout(gv_fetchpvs("STDOUT", GV_ADD|GV_NOTQUAL,
882 SVt_PVIO));
a0d0e21e 883 return IoIFP(GvIOp(gv));
c623bd54 884 }
99b89507 885#ifndef FLEXFILENAMES
b28d0864
NIS
886 filedev = PL_statbuf.st_dev;
887 fileino = PL_statbuf.st_ino;
99b89507 888#endif
3280af22
NIS
889 PL_filemode = PL_statbuf.st_mode;
890 fileuid = PL_statbuf.st_uid;
891 filegid = PL_statbuf.st_gid;
892 if (!S_ISREG(PL_filemode)) {
9b387841
NC
893 Perl_ck_warner_d(aTHX_ packWARN(WARN_INPLACE),
894 "Can't do inplace edit: %s is not a regular file",
895 PL_oldname );
79072805 896 do_close(gv,FALSE);
c623bd54
LW
897 continue;
898 }
c9930541 899 if (*PL_inplace && strNE(PL_inplace, "*")) {
2d03de9c 900 const char *star = strchr(PL_inplace, '*');
2d259d92 901 if (star) {
2d03de9c 902 const char *begin = PL_inplace;
8062ff11 903 SvPVCLEAR(sv);
2d259d92
CK
904 do {
905 sv_catpvn(sv, begin, star - begin);
3280af22 906 sv_catpvn(sv, PL_oldname, oldlen);
2d259d92
CK
907 begin = ++star;
908 } while ((star = strchr(begin, '*')));
3d66d7bb
GS
909 if (*begin)
910 sv_catpv(sv,begin);
2d259d92
CK
911 }
912 else {
3280af22 913 sv_catpv(sv,PL_inplace);
2d259d92 914 }
c623bd54 915#ifndef FLEXFILENAMES
95a20fc0 916 if ((PerlLIO_stat(SvPVX_const(sv),&PL_statbuf) >= 0
5f74f29c
JH
917 && PL_statbuf.st_dev == filedev
918 && PL_statbuf.st_ino == fileino)
39e571d4 919#ifdef DJGPP
5f74f29c 920 || ((_djstat_fail_bits & _STFAIL_TRUENAME)!=0)
39e571d4 921#endif
f248d071
GS
922 )
923 {
9b387841 924 Perl_ck_warner_d(aTHX_ packWARN(WARN_INPLACE),
147e3846
KW
925 "Can't do inplace edit: %"
926 SVf " would not be unique",
9b387841 927 SVfARG(sv));
79072805 928 do_close(gv,FALSE);
c623bd54
LW
929 continue;
930 }
931#endif
fe14fcc3 932#ifdef HAS_RENAME
739a0b84 933#if !defined(DOSISH) && !defined(__CYGWIN__)
95a20fc0 934 if (PerlLIO_rename(PL_oldname,SvPVX_const(sv)) < 0) {
9b387841 935 Perl_ck_warner_d(aTHX_ packWARN(WARN_INPLACE),
147e3846
KW
936 "Can't rename %s to %" SVf
937 ": %s, skipping file",
938 PL_oldname, SVfARG(sv),
939 Strerror(errno));
79072805 940 do_close(gv,FALSE);
c623bd54
LW
941 continue;
942 }
a687059c 943#else
79072805 944 do_close(gv,FALSE);
95a20fc0
SP
945 (void)PerlLIO_unlink(SvPVX_const(sv));
946 (void)PerlLIO_rename(PL_oldname,SvPVX_const(sv));
d5eb9a46 947 do_open_raw(gv, SvPVX_const(sv), SvCUR(sv), O_RDONLY, 0);
55497cff 948#endif /* DOSISH */
ff8e2863 949#else
95a20fc0
SP
950 (void)UNLINK(SvPVX_const(sv));
951 if (link(PL_oldname,SvPVX_const(sv)) < 0) {
9b387841 952 Perl_ck_warner_d(aTHX_ packWARN(WARN_INPLACE),
147e3846 953 "Can't rename %s to %" SVf ": %s, skipping file",
9b387841 954 PL_oldname, SVfARG(sv), Strerror(errno) );
79072805 955 do_close(gv,FALSE);
c623bd54
LW
956 continue;
957 }
b28d0864 958 (void)UNLINK(PL_oldname);
a687059c
LW
959#endif
960 }
961 else {
91e4b3b3 962#if !defined(DOSISH) && !defined(__amigaos4__)
edc7bc49 963# ifndef VMS /* Don't delete; use automatic file versioning */
3280af22 964 if (UNLINK(PL_oldname) < 0) {
9b387841
NC
965 Perl_ck_warner_d(aTHX_ packWARN(WARN_INPLACE),
966 "Can't remove %s: %s, skipping file",
967 PL_oldname, Strerror(errno) );
79072805 968 do_close(gv,FALSE);
fe14fcc3
LW
969 continue;
970 }
edc7bc49 971# endif
ff8e2863 972#else
cea2e8a9 973 Perl_croak(aTHX_ "Can't do inplace edit without backup");
ff8e2863 974#endif
a687059c
LW
975 }
976
30fc4309 977 sv_setpvn(sv,PL_oldname,oldlen);
748a9306 978 SETERRNO(0,0); /* in case sprintf set errno */
d5eb9a46
NC
979 if (!Perl_do_open_raw(aTHX_ PL_argvoutgv, SvPVX_const(sv),
980 SvCUR(sv),
4119ab01 981#ifdef VMS
d5eb9a46 982 O_WRONLY|O_CREAT|O_TRUNC, 0
4119ab01 983#else
d5eb9a46 984 O_WRONLY|O_CREAT|OPEN_EXCL, 0600
4119ab01 985#endif
d5eb9a46 986 )) {
9b387841
NC
987 Perl_ck_warner_d(aTHX_ packWARN(WARN_INPLACE), "Can't do inplace edit on %s: %s",
988 PL_oldname, Strerror(errno) );
79072805 989 do_close(gv,FALSE);
fe14fcc3
LW
990 continue;
991 }
3280af22
NIS
992 setdefout(PL_argvoutgv);
993 PL_lastfd = PerlIO_fileno(IoIFP(GvIOp(PL_argvoutgv)));
375ed12a 994 if (PL_lastfd >= 0) {
45a23732 995 (void)PerlLIO_fstat(PL_lastfd,&statbuf);
fe14fcc3 996#ifdef HAS_FCHMOD
375ed12a 997 (void)fchmod(PL_lastfd,PL_filemode);
a687059c 998#else
375ed12a 999 (void)PerlLIO_chmod(PL_oldname,PL_filemode);
a687059c 1000#endif
45a23732 1001 if (fileuid != statbuf.st_uid || filegid != statbuf.st_gid) {
b469f1e0 1002 /* XXX silently ignore failures */
fe14fcc3 1003#ifdef HAS_FCHOWN
b469f1e0 1004 PERL_UNUSED_RESULT(fchown(PL_lastfd,fileuid,filegid));
a687059c 1005#else
fe14fcc3 1006#ifdef HAS_CHOWN
b469f1e0 1007 PERL_UNUSED_RESULT(PerlLIO_chown(PL_oldname,fileuid,filegid));
a687059c 1008#endif
b1248f16 1009#endif
375ed12a 1010 }
fe14fcc3 1011 }
d8015975 1012 return IoIFP(GvIOp(gv));
a687059c 1013 }
d8015975
NC
1014 } /* successful do_open_raw(), PL_inplace non-NULL */
1015
1016 if (ckWARN_d(WARN_INPLACE)) {
1017 const int eno = errno;
45a23732
DD
1018 if (PerlLIO_stat(PL_oldname, &statbuf) >= 0
1019 && !S_ISREG(statbuf.st_mode)) {
d8015975
NC
1020 Perl_warner(aTHX_ packWARN(WARN_INPLACE),
1021 "Can't do inplace edit: %s is not a regular file",
1022 PL_oldname);
1023 }
1024 else {
1025 Perl_warner(aTHX_ packWARN(WARN_INPLACE), "Can't open %s: %s",
1026 PL_oldname, Strerror(eno));
1027 }
4d61ec05 1028 }
a687059c 1029 }
18708f5a
GS
1030 if (io && (IoFLAGS(io) & IOf_ARGV))
1031 IoFLAGS(io) |= IOf_START;
3280af22 1032 if (PL_inplace) {
502aca56
TC
1033 if (old_out_name) {
1034 IO * const io = GvIOp(PL_argvoutgv);
1035 if (io && IoIFP(io) && !io_close(io, PL_argvoutgv, FALSE, FALSE)) {
147e3846 1036 Perl_croak(aTHX_ "Failed to close in-place edit file %" SVf ": %s\n",
502aca56
TC
1037 old_out_name, Strerror(errno));
1038 }
1039 }
1040 else {
1041 /* maybe this is no longer wanted */
1042 (void)do_close(PL_argvoutgv,FALSE);
1043 }
7a1c5554
GS
1044 if (io && (IoFLAGS(io) & IOf_ARGV)
1045 && PL_argvout_stack && AvFILLp(PL_argvout_stack) >= 0)
1046 {
159b6efe 1047 GV * const oldout = MUTABLE_GV(av_pop(PL_argvout_stack));
18708f5a 1048 setdefout(oldout);
8e217d4a 1049 SvREFCNT_dec_NN(oldout);
4608196e 1050 return NULL;
18708f5a 1051 }
fafc274c 1052 setdefout(gv_fetchpvs("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO));
a687059c 1053 }
4608196e 1054 return NULL;
a687059c
LW
1055}
1056
517844ec 1057/* explicit renamed to avoid C++ conflict -- kja */
a687059c 1058bool
864dbfa3 1059Perl_do_close(pTHX_ GV *gv, bool not_implicit)
a687059c 1060{
1193dd27
IZ
1061 bool retval;
1062 IO *io;
a687059c 1063
79072805 1064 if (!gv)
3280af22 1065 gv = PL_argvgv;
6e592b3a 1066 if (!gv || !isGV_with_GP(gv)) {
1d2dff63 1067 if (not_implicit)
93189314 1068 SETERRNO(EBADF,SS_IVCHAN);
c2ab57d4 1069 return FALSE;
99b89507 1070 }
79072805
LW
1071 io = GvIO(gv);
1072 if (!io) { /* never opened */
1d2dff63 1073 if (not_implicit) {
51087808 1074 report_evil_fh(gv);
93189314 1075 SETERRNO(EBADF,SS_IVCHAN);
1d2dff63 1076 }
a687059c
LW
1077 return FALSE;
1078 }
96d7c888 1079 retval = io_close(io, NULL, not_implicit, FALSE);
517844ec 1080 if (not_implicit) {
1193dd27
IZ
1081 IoLINES(io) = 0;
1082 IoPAGE(io) = 0;
1083 IoLINES_LEFT(io) = IoPAGE_LEN(io);
1084 }
50952442 1085 IoTYPE(io) = IoTYPE_CLOSED;
1193dd27
IZ
1086 return retval;
1087}
1088
1089bool
96d7c888 1090Perl_io_close(pTHX_ IO *io, GV *gv, bool not_implicit, bool warn_on_fail)
1193dd27
IZ
1091{
1092 bool retval = FALSE;
1193dd27 1093
7918f24d
NC
1094 PERL_ARGS_ASSERT_IO_CLOSE;
1095
8990e307 1096 if (IoIFP(io)) {
50952442 1097 if (IoTYPE(io) == IoTYPE_PIPE) {
4373e329 1098 const int status = PerlProc_pclose(IoIFP(io));
f2b5be74 1099 if (not_implicit) {
37038d91 1100 STATUS_NATIVE_CHILD_SET(status);
e5218da5 1101 retval = (STATUS_UNIX == 0);
f2b5be74
GS
1102 }
1103 else {
1104 retval = (status != -1);
1105 }
a687059c 1106 }
50952442 1107 else if (IoTYPE(io) == IoTYPE_STD)
a687059c
LW
1108 retval = TRUE;
1109 else {
8990e307 1110 if (IoOFP(io) && IoOFP(io) != IoIFP(io)) { /* a socket */
0bcc34c2 1111 const bool prev_err = PerlIO_error(IoOFP(io));
f4725fad
FC
1112#ifdef USE_PERLIO
1113 if (prev_err)
1114 PerlIO_restore_errno(IoOFP(io));
1115#endif
e199e3be 1116 retval = (PerlIO_close(IoOFP(io)) != EOF && !prev_err);
760ac839 1117 PerlIO_close(IoIFP(io)); /* clear stdio, fd already closed */
c2ab57d4 1118 }
e199e3be 1119 else {
0bcc34c2 1120 const bool prev_err = PerlIO_error(IoIFP(io));
f4725fad
FC
1121#ifdef USE_PERLIO
1122 if (prev_err)
1123 PerlIO_restore_errno(IoIFP(io));
1124#endif
e199e3be
RGS
1125 retval = (PerlIO_close(IoIFP(io)) != EOF && !prev_err);
1126 }
a687059c 1127 }
4608196e 1128 IoOFP(io) = IoIFP(io) = NULL;
96d7c888
FC
1129
1130 if (warn_on_fail && !retval) {
1131 if (gv)
1132 Perl_ck_warner_d(aTHX_ packWARN(WARN_IO),
1133 "Warning: unable to close filehandle %"
147e3846 1134 HEKf " properly: %" SVf,
ac892e4a
DM
1135 HEKfARG(GvNAME_HEK(gv)),
1136 SVfARG(get_sv("!",GV_ADD)));
96d7c888
FC
1137 else
1138 Perl_ck_warner_d(aTHX_ packWARN(WARN_IO),
1139 "Warning: unable to close filehandle "
147e3846 1140 "properly: %" SVf,
ac892e4a 1141 SVfARG(get_sv("!",GV_ADD)));
96d7c888 1142 }
79072805 1143 }
f2b5be74 1144 else if (not_implicit) {
93189314 1145 SETERRNO(EBADF,SS_IVCHAN);
20408e3c 1146 }
1193dd27 1147
a687059c
LW
1148 return retval;
1149}
1150
1151bool
864dbfa3 1152Perl_do_eof(pTHX_ GV *gv)
a687059c 1153{
eb578fdb 1154 IO * const io = GvIO(gv);
a687059c 1155
7918f24d
NC
1156 PERL_ARGS_ASSERT_DO_EOF;
1157
79072805 1158 if (!io)
a687059c 1159 return TRUE;
7716c5c5 1160 else if (IoTYPE(io) == IoTYPE_WRONLY)
a5390457 1161 report_wrongway_fh(gv, '>');
a687059c 1162
8990e307 1163 while (IoIFP(io)) {
760ac839 1164 if (PerlIO_has_cntptr(IoIFP(io))) { /* (the code works without this) */
a20bf0c3 1165 if (PerlIO_get_cnt(IoIFP(io)) > 0) /* cheat a little, since */
760ac839
LW
1166 return FALSE; /* this is the most usual case */
1167 }
a687059c 1168
79852593
NC
1169 {
1170 /* getc and ungetc can stomp on errno */
4ee39169 1171 dSAVE_ERRNO;
79852593
NC
1172 const int ch = PerlIO_getc(IoIFP(io));
1173 if (ch != EOF) {
1174 (void)PerlIO_ungetc(IoIFP(io),ch);
4ee39169 1175 RESTORE_ERRNO;
79852593
NC
1176 return FALSE;
1177 }
4ee39169 1178 RESTORE_ERRNO;
a687059c 1179 }
fab3f3a7 1180
760ac839 1181 if (PerlIO_has_cntptr(IoIFP(io)) && PerlIO_canset_cnt(IoIFP(io))) {
a20bf0c3
JH
1182 if (PerlIO_get_cnt(IoIFP(io)) < -1)
1183 PerlIO_set_cnt(IoIFP(io),-1);
760ac839 1184 }
533c011a 1185 if (PL_op->op_flags & OPf_SPECIAL) { /* not necessarily a real EOF yet? */
157fb5a1 1186 if (gv != PL_argvgv || !nextargv(gv, FALSE)) /* get another fp handy */
a687059c
LW
1187 return TRUE;
1188 }
1189 else
1190 return TRUE; /* normal fp, definitely end of file */
1191 }
1192 return TRUE;
1193}
1194
5ff3f7a4 1195Off_t
864dbfa3 1196Perl_do_tell(pTHX_ GV *gv)
a687059c 1197{
9c9f25b8 1198 IO *const io = GvIO(gv);
eb578fdb 1199 PerlIO *fp;
a687059c 1200
7918f24d
NC
1201 PERL_ARGS_ASSERT_DO_TELL;
1202
9c9f25b8 1203 if (io && (fp = IoIFP(io))) {
8903cb82 1204 return PerlIO_tell(fp);
96e4d5b1 1205 }
51087808 1206 report_evil_fh(gv);
93189314 1207 SETERRNO(EBADF,RMS_IFI);
5ff3f7a4 1208 return (Off_t)-1;
a687059c
LW
1209}
1210
1211bool
864dbfa3 1212Perl_do_seek(pTHX_ GV *gv, Off_t pos, int whence)
a687059c 1213{
9c9f25b8 1214 IO *const io = GvIO(gv);
eb578fdb 1215 PerlIO *fp;
a687059c 1216
9c9f25b8 1217 if (io && (fp = IoIFP(io))) {
8903cb82 1218 return PerlIO_seek(fp, pos, whence) >= 0;
137443ea 1219 }
51087808 1220 report_evil_fh(gv);
93189314 1221 SETERRNO(EBADF,RMS_IFI);
a687059c
LW
1222 return FALSE;
1223}
1224
97cc44eb 1225Off_t
864dbfa3 1226Perl_do_sysseek(pTHX_ GV *gv, Off_t pos, int whence)
8903cb82 1227{
9c9f25b8 1228 IO *const io = GvIO(gv);
eb578fdb 1229 PerlIO *fp;
8903cb82 1230
7918f24d
NC
1231 PERL_ARGS_ASSERT_DO_SYSSEEK;
1232
375ed12a
JH
1233 if (io && (fp = IoIFP(io))) {
1234 int fd = PerlIO_fileno(fp);
07bd88da
JH
1235 if (fd < 0 || (whence == SEEK_SET && pos < 0)) {
1236 SETERRNO(EINVAL,LIB_INVARG);
1237 return -1;
1238 } else {
375ed12a
JH
1239 return PerlLIO_lseek(fd, pos, whence);
1240 }
1241 }
51087808 1242 report_evil_fh(gv);
93189314 1243 SETERRNO(EBADF,RMS_IFI);
d9b3e12d 1244 return (Off_t)-1;
8903cb82 1245}
1246
6ff81951 1247int
a79b25b7 1248Perl_mode_from_discipline(pTHX_ const char *s, STRLEN len)
16fe6d59
GS
1249{
1250 int mode = O_BINARY;
81611534 1251 PERL_UNUSED_CONTEXT;
a79b25b7 1252 if (s) {
16fe6d59
GS
1253 while (*s) {
1254 if (*s == ':') {
1255 switch (s[1]) {
1256 case 'r':
e963d6d2 1257 if (s[2] == 'a' && s[3] == 'w'
16fe6d59
GS
1258 && (!s[4] || s[4] == ':' || isSPACE(s[4])))
1259 {
1260 mode = O_BINARY;
1261 s += 4;
1262 len -= 4;
1263 break;
1264 }
924ba076 1265 /* FALLTHROUGH */
16fe6d59 1266 case 'c':
e963d6d2 1267 if (s[2] == 'r' && s[3] == 'l' && s[4] == 'f'
16fe6d59
GS
1268 && (!s[5] || s[5] == ':' || isSPACE(s[5])))
1269 {
1270 mode = O_TEXT;
1271 s += 5;
1272 len -= 5;
1273 break;
1274 }
924ba076 1275 /* FALLTHROUGH */
16fe6d59
GS
1276 default:
1277 goto fail_discipline;
1278 }
1279 }
1280 else if (isSPACE(*s)) {
1281 ++s;
1282 --len;
1283 }
1284 else {
4373e329 1285 const char *end;
7b52d656 1286 fail_discipline:
16fe6d59
GS
1287 end = strchr(s+1, ':');
1288 if (!end)
1289 end = s+len;
60382766 1290#ifndef PERLIO_LAYERS
363c40c4 1291 Perl_croak(aTHX_ "IO layers (like '%.*s') unavailable", end-s, s);
60382766 1292#else
18a33fb5 1293 len -= end-s;
60382766
NIS
1294 s = end;
1295#endif
16fe6d59
GS
1296 }
1297 }
1298 }
1299 return mode;
1300}
1301
58e24eff 1302#if !defined(HAS_TRUNCATE) && !defined(HAS_CHSIZE)
27da23d5
JH
1303I32
1304my_chsize(int fd, Off_t length)
6eb13c3b 1305{
58e24eff
SH
1306#ifdef F_FREESP
1307 /* code courtesy of William Kucharski */
1308#define HAS_CHSIZE
1309
c623ac67 1310 Stat_t filebuf;
6eb13c3b 1311
3028581b 1312 if (PerlLIO_fstat(fd, &filebuf) < 0)
6eb13c3b
LW
1313 return -1;
1314
1315 if (filebuf.st_size < length) {
1316
1317 /* extend file length */
1318
3028581b 1319 if ((PerlLIO_lseek(fd, (length - 1), 0)) < 0)
6eb13c3b
LW
1320 return -1;
1321
1322 /* write a "0" byte */
1323
3028581b 1324 if ((PerlLIO_write(fd, "", 1)) != 1)
6eb13c3b
LW
1325 return -1;
1326 }
1327 else {
1328 /* truncate length */
35da51f7 1329 struct flock fl;
6eb13c3b
LW
1330 fl.l_whence = 0;
1331 fl.l_len = 0;
1332 fl.l_start = length;
a0d0e21e 1333 fl.l_type = F_WRLCK; /* write lock on file space */
6eb13c3b
LW
1334
1335 /*
a0d0e21e 1336 * This relies on the UNDOCUMENTED F_FREESP argument to
6eb13c3b
LW
1337 * fcntl(2), which truncates the file so that it ends at the
1338 * position indicated by fl.l_start.
1339 *
1340 * Will minor miracles never cease?
1341 */
1342
a0d0e21e 1343 if (fcntl(fd, F_FREESP, &fl) < 0)
6eb13c3b
LW
1344 return -1;
1345
1346 }
6eb13c3b 1347 return 0;
58e24eff 1348#else
27da23d5 1349 Perl_croak_nocontext("truncate not implemented");
a0d0e21e 1350#endif /* F_FREESP */
27da23d5 1351 return -1;
58e24eff
SH
1352}
1353#endif /* !HAS_TRUNCATE && !HAS_CHSIZE */
ff8e2863 1354
a687059c 1355bool
5aaab254 1356Perl_do_print(pTHX_ SV *sv, PerlIO *fp)
a687059c 1357{
7918f24d
NC
1358 PERL_ARGS_ASSERT_DO_PRINT;
1359
79072805
LW
1360 /* assuming fp is checked earlier */
1361 if (!sv)
1362 return TRUE;
e9950d3b
NC
1363 if (SvTYPE(sv) == SVt_IV && SvIOK(sv)) {
1364 assert(!SvGMAGICAL(sv));
1365 if (SvIsUV(sv))
147e3846 1366 PerlIO_printf(fp, "%" UVuf, (UV)SvUVX(sv));
e9950d3b 1367 else
147e3846 1368 PerlIO_printf(fp, "%" IVdf, (IV)SvIVX(sv));
e9950d3b
NC
1369 return !PerlIO_error(fp);
1370 }
1371 else {
1372 STRLEN len;
676f44e7 1373 /* Do this first to trigger any overloading. */
e9950d3b
NC
1374 const char *tmps = SvPV_const(sv, len);
1375 U8 *tmpbuf = NULL;
1376 bool happy = TRUE;
1377
d791f93f
KW
1378 if (PerlIO_isutf8(fp)) { /* If the stream is utf8 ... */
1379 if (!SvUTF8(sv)) { /* Convert to utf8 if necessary */
676f44e7
NC
1380 /* We don't modify the original scalar. */
1381 tmpbuf = bytes_to_utf8((const U8*) tmps, &len);
1382 tmps = (char *) tmpbuf;
1383 }
a099aed4 1384 else if (ckWARN4_d(WARN_UTF8, WARN_SURROGATE, WARN_NON_UNICODE, WARN_NONCHAR)) {
0876b9a0
KW
1385 (void) check_utf8_print((const U8*) tmps, len);
1386 }
d791f93f
KW
1387 } /* else stream isn't utf8 */
1388 else if (DO_UTF8(sv)) { /* But if is utf8 internally, attempt to
1389 convert to bytes */
676f44e7
NC
1390 STRLEN tmplen = len;
1391 bool utf8 = TRUE;
35da51f7 1392 U8 * const result = bytes_from_utf8((const U8*) tmps, &tmplen, &utf8);
676f44e7 1393 if (!utf8) {
d791f93f
KW
1394
1395 /* Here, succeeded in downgrading from utf8. Set up to below
1396 * output the converted value */
676f44e7
NC
1397 tmpbuf = result;
1398 tmps = (char *) tmpbuf;
1399 len = tmplen;
1400 }
d791f93f
KW
1401 else { /* Non-utf8 output stream, but string only representable in
1402 utf8 */
676f44e7 1403 assert((char *)result == tmps);
9b387841 1404 Perl_ck_warner_d(aTHX_ packWARN(WARN_UTF8),
21630838
FC
1405 "Wide character in %s",
1406 PL_op ? OP_DESC(PL_op) : "print"
1407 );
0876b9a0
KW
1408 /* Could also check that isn't one of the things to avoid
1409 * in utf8 by using check_utf8_print(), but not doing so,
1410 * since the stream isn't a UTF8 stream */
ae798467
NIS
1411 }
1412 }
e9950d3b
NC
1413 /* To detect whether the process is about to overstep its
1414 * filesize limit we would need getrlimit(). We could then
1415 * also transparently raise the limit with setrlimit() --
1416 * but only until the system hard limit/the filesystem limit,
1417 * at which we would get EPERM. Note that when using buffered
1418 * io the write failure can be delayed until the flush/close. --jhi */
1419 if (len && (PerlIO_write(fp,tmps,len) == 0))
1420 happy = FALSE;
1421 Safefree(tmpbuf);
1422 return happy ? !PerlIO_error(fp) : FALSE;
ff8e2863 1423 }
a687059c
LW
1424}
1425
79072805 1426I32
0d7d409d 1427Perl_my_stat_flags(pTHX_ const U32 flags)
a687059c 1428{
39644a26 1429 dSP;
79072805 1430 IO *io;
2dd78f96 1431 GV* gv;
79072805 1432
533c011a 1433 if (PL_op->op_flags & OPf_REF) {
2dd78f96 1434 gv = cGVOP_gv;
748a9306 1435 do_fstat:
5228a96c
SP
1436 if (gv == PL_defgv)
1437 return PL_laststatval;
2dd78f96 1438 io = GvIO(gv);
ad02613c 1439 do_fstat_have_io:
5228a96c 1440 PL_laststype = OP_STAT;
bd5f6c01 1441 PL_statgv = gv ? gv : (GV *)io;
8062ff11 1442 SvPVCLEAR(PL_statname);
77616968 1443 if (io) {
5228a96c 1444 if (IoIFP(io)) {
375ed12a 1445 int fd = PerlIO_fileno(IoIFP(io));
77616968
JH
1446 if (fd < 0) {
1447 /* E.g. PerlIO::scalar has no real fd. */
1448 return (PL_laststatval = -1);
1449 } else {
375ed12a
JH
1450 return (PL_laststatval = PerlLIO_fstat(fd, &PL_statcache));
1451 }
5228a96c 1452 } else if (IoDIRP(io)) {
3497a01f 1453 return (PL_laststatval = PerlLIO_fstat(my_dirfd(IoDIRP(io)), &PL_statcache));
5228a96c 1454 }
5228a96c 1455 }
3888144c
FC
1456 PL_laststatval = -1;
1457 report_evil_fh(gv);
1458 return -1;
a687059c 1459 }
d2c4d2d1 1460 else if ((PL_op->op_private & (OPpFT_STACKED|OPpFT_AFTER_t))
8db8f6b6
FC
1461 == OPpFT_STACKED)
1462 return PL_laststatval;
d2c4d2d1
FC
1463 else {
1464 SV* const sv = TOPs;
4373e329 1465 const char *s;
4ecd490c 1466 STRLEN len;
094a3eec 1467 if ((gv = MAYBE_DEREF_GV_flags(sv,flags))) {
748a9306
LW
1468 goto do_fstat;
1469 }
ad02613c 1470 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVIO) {
a45c7426 1471 io = MUTABLE_IO(SvRV(sv));
7f39519f 1472 gv = NULL;
ad02613c
SP
1473 goto do_fstat_have_io;
1474 }
748a9306 1475
0d7d409d 1476 s = SvPV_flags_const(sv, len, flags);
a0714e2c 1477 PL_statgv = NULL;
4ecd490c 1478 sv_setpvn(PL_statname, s, len);
95a20fc0 1479 s = SvPVX_const(PL_statname); /* s now NUL-terminated */
3280af22
NIS
1480 PL_laststype = OP_STAT;
1481 PL_laststatval = PerlLIO_stat(s, &PL_statcache);
7cb3f959 1482 if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && should_warn_nl(s)) {
5d37acd6 1483 GCC_DIAG_IGNORE(-Wformat-nonliteral); /* PL_warn_nl is constant */
9014280d 1484 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "stat");
5d37acd6
DM
1485 GCC_DIAG_RESTORE;
1486 }
3280af22 1487 return PL_laststatval;
a687059c
LW
1488 }
1489}
1490
fbb0b3b3 1491
79072805 1492I32
0d7d409d 1493Perl_my_lstat_flags(pTHX_ const U32 flags)
c623bd54 1494{
a1894d81 1495 static const char* const no_prev_lstat = "The stat preceding -l _ wasn't an lstat";
39644a26 1496 dSP;
b16276bb 1497 const char *file;
cd22fad3 1498 SV* const sv = TOPs;
5840701a 1499 bool isio = FALSE;
533c011a 1500 if (PL_op->op_flags & OPf_REF) {
638eceb6 1501 if (cGVOP_gv == PL_defgv) {
3280af22 1502 if (PL_laststype != OP_LSTAT)
0157ef98 1503 Perl_croak(aTHX_ "%s", no_prev_lstat);
3280af22 1504 return PL_laststatval;
fe14fcc3 1505 }
31b139ba 1506 PL_laststatval = -1;
5d3e98de 1507 if (ckWARN(WARN_IO)) {
5840701a 1508 /* diag_listed_as: Use of -l on filehandle%s */
d0c0e7dd 1509 Perl_warner(aTHX_ packWARN(WARN_IO),
147e3846 1510 "Use of -l on filehandle %" HEKf,
d0c0e7dd 1511 HEKfARG(GvENAME_HEK(cGVOP_gv)));
5d3e98de 1512 }
31b139ba 1513 return -1;
fe14fcc3 1514 }
8db8f6b6
FC
1515 if ((PL_op->op_private & (OPpFT_STACKED|OPpFT_AFTER_t))
1516 == OPpFT_STACKED) {
1f26655e 1517 if (PL_laststype != OP_LSTAT)
0157ef98 1518 Perl_croak(aTHX_ "%s", no_prev_lstat);
1f26655e 1519 return PL_laststatval;
cd22fad3 1520 }
c623bd54 1521
3280af22 1522 PL_laststype = OP_LSTAT;
a0714e2c 1523 PL_statgv = NULL;
5840701a
FC
1524 if ( ( (SvROK(sv) && ( isGV_with_GP(SvRV(sv))
1525 || (isio = SvTYPE(SvRV(sv)) == SVt_PVIO) )
1526 )
1527 || isGV_with_GP(sv)
1528 )
1529 && ckWARN(WARN_IO)) {
1530 if (isio)
1531 /* diag_listed_as: Use of -l on filehandle%s */
1532 Perl_warner(aTHX_ packWARN(WARN_IO),
1533 "Use of -l on filehandle");
1534 else
1535 /* diag_listed_as: Use of -l on filehandle%s */
1536 Perl_warner(aTHX_ packWARN(WARN_IO),
147e3846 1537 "Use of -l on filehandle %" HEKf,
10bafe90
BF
1538 HEKfARG(GvENAME_HEK((const GV *)
1539 (SvROK(sv) ? SvRV(sv) : sv))));
cd22fad3
RS
1540 }
1541 file = SvPV_flags_const_nolen(sv, flags);
b16276bb
VP
1542 sv_setpv(PL_statname,file);
1543 PL_laststatval = PerlLIO_lstat(file,&PL_statcache);
7cb3f959 1544 if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && should_warn_nl(file)) {
5d37acd6
DM
1545 GCC_DIAG_IGNORE(-Wformat-nonliteral); /* PL_warn_nl is constant */
1546 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "lstat");
1547 GCC_DIAG_RESTORE;
1548 }
3280af22 1549 return PL_laststatval;
c623bd54
LW
1550}
1551
a0f2c8ec
JD
1552static void
1553S_exec_failed(pTHX_ const char *cmd, int fd, int do_report)
1554{
1555 const int e = errno;
7918f24d 1556 PERL_ARGS_ASSERT_EXEC_FAILED;
738ab09f
AB
1557
1558 if (ckWARN(WARN_EXEC))
1559 Perl_warner(aTHX_ packWARN(WARN_EXEC), "Can't exec \"%s\": %s",
1560 cmd, Strerror(e));
a0f2c8ec 1561 if (do_report) {
b469f1e0
JH
1562 /* XXX silently ignore failures */
1563 PERL_UNUSED_RESULT(PerlLIO_write(fd, (void*)&e, sizeof(int)));
a0f2c8ec
JD
1564 PerlLIO_close(fd);
1565 }
1566}
1567
738ab09f 1568bool
5aaab254 1569Perl_do_aexec5(pTHX_ SV *really, SV **mark, SV **sp,
2aa1486d 1570 int fd, int do_report)
d5a9bfb0 1571{
27da23d5 1572 dVAR;
7918f24d 1573 PERL_ARGS_ASSERT_DO_AEXEC5;
e37778c2 1574#if defined(__SYMBIAN32__) || defined(__LIBCATAMOUNT__)
cd39f2b6
JH
1575 Perl_croak(aTHX_ "exec? I'm not *that* kind of operating system");
1576#else
79072805 1577 if (sp > mark) {
360ea906 1578 const char **a;
6136c704 1579 const char *tmps = NULL;
360ea906 1580 Newx(PL_Argv, sp - mark + 1, const char*);
c3c5fad7 1581 a = PL_Argv;
890ce7af 1582
79072805
LW
1583 while (++mark <= sp) {
1584 if (*mark)
360ea906 1585 *a++ = SvPV_nolen_const(*mark);
a687059c
LW
1586 else
1587 *a++ = "";
1588 }
6136c704 1589 *a = NULL;
91b2752f 1590 if (really)
e62f0680 1591 tmps = SvPV_nolen_const(really);
91b2752f
RG
1592 if ((!really && *PL_Argv[0] != '/') ||
1593 (really && *tmps != '/')) /* will execvp use PATH? */
79072805 1594 TAINT_ENV(); /* testing IFS here is overkill, probably */
b35112e7 1595 PERL_FPU_PRE_EXEC
839a9f02 1596 if (really && *tmps) {
738ab09f 1597 PerlProc_execvp(tmps,EXEC_ARGV_CAST(PL_Argv));
839a9f02 1598 } else {
738ab09f 1599 PerlProc_execvp(PL_Argv[0],EXEC_ARGV_CAST(PL_Argv));
839a9f02 1600 }
b35112e7 1601 PERL_FPU_POST_EXEC
a0f2c8ec 1602 S_exec_failed(aTHX_ (really ? tmps : PL_Argv[0]), fd, do_report);
a687059c 1603 }
bee1dbe2 1604 do_execfree();
cd39f2b6 1605#endif
738ab09f 1606 return FALSE;
a687059c
LW
1607}
1608
fe14fcc3 1609void
864dbfa3 1610Perl_do_execfree(pTHX)
ff8e2863 1611{
43c5f42d 1612 Safefree(PL_Argv);
4608196e 1613 PL_Argv = NULL;
43c5f42d 1614 Safefree(PL_Cmd);
bd61b366 1615 PL_Cmd = NULL;
ff8e2863
LW
1616}
1617
9555a685 1618#ifdef PERL_DEFAULT_DO_EXEC3_IMPLEMENTATION
e446cec8 1619
738ab09f 1620bool
2fbb330f 1621Perl_do_exec3(pTHX_ const char *incmd, int fd, int do_report)
e446cec8 1622{
27da23d5 1623 dVAR;
eb578fdb
KW
1624 const char **a;
1625 char *s;
15db3ae2 1626 char *buf;
2fbb330f 1627 char *cmd;
2fbb330f 1628 /* Make a copy so we can change it */
6fca0082 1629 const Size_t cmdlen = strlen(incmd) + 1;
7918f24d
NC
1630
1631 PERL_ARGS_ASSERT_DO_EXEC3;
1632
15db3ae2
DM
1633 Newx(buf, cmdlen, char);
1634 cmd = buf;
cfff9797 1635 memcpy(cmd, incmd, cmdlen);
a687059c 1636
748a9306
LW
1637 while (*cmd && isSPACE(*cmd))
1638 cmd++;
1639
a687059c
LW
1640 /* save an extra exec if possible */
1641
bf38876a 1642#ifdef CSH
d05c1ba0 1643 {
0c19750d 1644 char flags[PERL_FLAGS_MAX];
d05c1ba0 1645 if (strnEQ(cmd,PL_cshname,PL_cshlen) &&
8062ff11 1646 strEQs(cmd+PL_cshlen," -c")) {
28f0d0ec 1647 my_strlcpy(flags, "-c", PERL_FLAGS_MAX);
d05c1ba0
JH
1648 s = cmd+PL_cshlen+3;
1649 if (*s == 'f') {
1650 s++;
28f0d0ec 1651 my_strlcat(flags, "f", PERL_FLAGS_MAX - 2);
d05c1ba0
JH
1652 }
1653 if (*s == ' ')
1654 s++;
1655 if (*s++ == '\'') {
0bcc34c2 1656 char * const ncmd = s;
d05c1ba0
JH
1657
1658 while (*s)
1659 s++;
1660 if (s[-1] == '\n')
1661 *--s = '\0';
1662 if (s[-1] == '\'') {
1663 *--s = '\0';
b35112e7 1664 PERL_FPU_PRE_EXEC
738ab09f 1665 PerlProc_execl(PL_cshname, "csh", flags, ncmd, (char*)NULL);
b35112e7 1666 PERL_FPU_POST_EXEC
d05c1ba0 1667 *s = '\'';
a0f2c8ec 1668 S_exec_failed(aTHX_ PL_cshname, fd, do_report);
15db3ae2 1669 Safefree(buf);
738ab09f 1670 return FALSE;
d05c1ba0
JH
1671 }
1672 }
a687059c
LW
1673 }
1674 }
bf38876a 1675#endif /* CSH */
a687059c
LW
1676
1677 /* see if there are shell metacharacters in it */
1678
748a9306
LW
1679 if (*cmd == '.' && isSPACE(cmd[1]))
1680 goto doshell;
1681
8062ff11 1682 if (strEQs(cmd,"exec") && isSPACE(cmd[4]))
748a9306
LW
1683 goto doshell;
1684
294b3b39 1685 s = cmd;
0eb30aeb 1686 while (isWORDCHAR(*s))
294b3b39 1687 s++; /* catch VAR=val gizmo */
63f2c1e1
LW
1688 if (*s == '=')
1689 goto doshell;
748a9306 1690
a687059c 1691 for (s = cmd; *s; s++) {
d05c1ba0
JH
1692 if (*s != ' ' && !isALPHA(*s) &&
1693 strchr("$&*(){}[]'\";\\|?<>~`\n",*s)) {
a687059c
LW
1694 if (*s == '\n' && !s[1]) {
1695 *s = '\0';
1696 break;
1697 }
603a98b0
IZ
1698 /* handle the 2>&1 construct at the end */
1699 if (*s == '>' && s[1] == '&' && s[2] == '1'
1700 && s > cmd + 1 && s[-1] == '2' && isSPACE(s[-2])
1701 && (!s[3] || isSPACE(s[3])))
1702 {
6867be6d 1703 const char *t = s + 3;
603a98b0
IZ
1704
1705 while (*t && isSPACE(*t))
1706 ++t;
943bbd07 1707 if (!*t && (PerlLIO_dup2(1,2) != -1)) {
603a98b0
IZ
1708 s[-2] = '\0';
1709 break;
1710 }
1711 }
a687059c 1712 doshell:
b35112e7 1713 PERL_FPU_PRE_EXEC
738ab09f 1714 PerlProc_execl(PL_sh_path, "sh", "-c", cmd, (char *)NULL);
b35112e7 1715 PERL_FPU_POST_EXEC
a0f2c8ec 1716 S_exec_failed(aTHX_ PL_sh_path, fd, do_report);
15db3ae2 1717 Safefree(buf);
738ab09f 1718 return FALSE;
a687059c
LW
1719 }
1720 }
748a9306 1721
360ea906 1722 Newx(PL_Argv, (s - cmd) / 2 + 2, const char*);
3280af22
NIS
1723 PL_Cmd = savepvn(cmd, s-cmd);
1724 a = PL_Argv;
1725 for (s = PL_Cmd; *s;) {
294b3b39
AL
1726 while (isSPACE(*s))
1727 s++;
a687059c
LW
1728 if (*s)
1729 *(a++) = s;
294b3b39
AL
1730 while (*s && !isSPACE(*s))
1731 s++;
a687059c
LW
1732 if (*s)
1733 *s++ = '\0';
1734 }
6136c704 1735 *a = NULL;
3280af22 1736 if (PL_Argv[0]) {
b35112e7 1737 PERL_FPU_PRE_EXEC
738ab09f 1738 PerlProc_execvp(PL_Argv[0],EXEC_ARGV_CAST(PL_Argv));
b35112e7 1739 PERL_FPU_POST_EXEC
b1248f16 1740 if (errno == ENOEXEC) { /* for system V NIH syndrome */
ff8e2863 1741 do_execfree();
a687059c 1742 goto doshell;
b1248f16 1743 }
a0f2c8ec 1744 S_exec_failed(aTHX_ PL_Argv[0], fd, do_report);
a687059c 1745 }
ff8e2863 1746 do_execfree();
15db3ae2 1747 Safefree(buf);
738ab09f 1748 return FALSE;
a687059c
LW
1749}
1750
6890e559 1751#endif /* OS2 || WIN32 */
760ac839 1752
79072805 1753I32
5aaab254 1754Perl_apply(pTHX_ I32 type, SV **mark, SV **sp)
a687059c 1755{
eb578fdb
KW
1756 I32 val;
1757 I32 tot = 0;
4634a855 1758 const char *const what = PL_op_name[type];
5c144d81 1759 const char *s;
84c7b88c 1760 STRLEN len;
890ce7af 1761 SV ** const oldmark = mark;
885b4b39 1762 bool killgp = FALSE;
a687059c 1763
7918f24d
NC
1764 PERL_ARGS_ASSERT_APPLY;
1765
9a9b5ec9
DM
1766 PERL_UNUSED_VAR(what); /* may not be used depending on compile options */
1767
1444765e
NC
1768 /* Doing this ahead of the switch statement preserves the old behaviour,
1769 where attempting to use kill as a taint test test would fail on
1770 platforms where kill was not defined. */
1771#ifndef HAS_KILL
1772 if (type == OP_KILL)
4634a855 1773 Perl_die(aTHX_ PL_no_func, what);
1444765e
NC
1774#endif
1775#ifndef HAS_CHOWN
1776 if (type == OP_CHOWN)
4634a855 1777 Perl_die(aTHX_ PL_no_func, what);
1444765e
NC
1778#endif
1779
1780
20408e3c 1781#define APPLY_TAINT_PROPER() \
3280af22 1782 STMT_START { \
284167a5 1783 if (TAINT_get) { TAINT_PROPER(what); } \
873ef191 1784 } STMT_END
20408e3c
GS
1785
1786 /* This is a first heuristic; it doesn't catch tainting magic. */
284167a5 1787 if (TAINTING_get) {
463ee0b2 1788 while (++mark <= sp) {
bbce6d69 1789 if (SvTAINTED(*mark)) {
1790 TAINT;
1791 break;
1792 }
463ee0b2
LW
1793 }
1794 mark = oldmark;
1795 }
a687059c 1796 switch (type) {
79072805 1797 case OP_CHMOD:
20408e3c 1798 APPLY_TAINT_PROPER();
79072805 1799 if (++mark <= sp) {
4ea561bc 1800 val = SvIV(*mark);
20408e3c
GS
1801 APPLY_TAINT_PROPER();
1802 tot = sp - mark;
79072805 1803 while (++mark <= sp) {
c4aca7d0 1804 GV* gv;
2ea1cce7 1805 if ((gv = MAYBE_DEREF_GV(*mark))) {
c4aca7d0
GA
1806 if (GvIO(gv) && IoIFP(GvIOp(gv))) {
1807#ifdef HAS_FCHMOD
375ed12a 1808 int fd = PerlIO_fileno(IoIFP(GvIOn(gv)));
c4aca7d0 1809 APPLY_TAINT_PROPER();
375ed12a
JH
1810 if (fd < 0) {
1811 SETERRNO(EBADF,RMS_IFI);
1812 tot--;
1813 } else if (fchmod(fd, val))
1814 tot--;
c4aca7d0 1815#else
b9c6780e 1816 Perl_die(aTHX_ PL_no_func, "fchmod");
c4aca7d0
GA
1817#endif
1818 }
1819 else {
8334cae6 1820 SETERRNO(EBADF,RMS_IFI);
c4aca7d0
GA
1821 tot--;
1822 }
1823 }
c4aca7d0 1824 else {
41188aa0 1825 const char *name = SvPV_nomg_const(*mark, len);
c4aca7d0 1826 APPLY_TAINT_PROPER();
41188aa0 1827 if (!IS_SAFE_PATHNAME(name, len, "chmod") ||
c8028aa6
TC
1828 PerlLIO_chmod(name, val)) {
1829 tot--;
1830 }
c4aca7d0 1831 }
a687059c
LW
1832 }
1833 }
1834 break;
fe14fcc3 1835#ifdef HAS_CHOWN
79072805 1836 case OP_CHOWN:
20408e3c 1837 APPLY_TAINT_PROPER();
79072805 1838 if (sp - mark > 2) {
eb578fdb 1839 I32 val2;
463ee0b2
LW
1840 val = SvIVx(*++mark);
1841 val2 = SvIVx(*++mark);
20408e3c 1842 APPLY_TAINT_PROPER();
a0d0e21e 1843 tot = sp - mark;
79072805 1844 while (++mark <= sp) {
c4aca7d0 1845 GV* gv;
2ea1cce7 1846 if ((gv = MAYBE_DEREF_GV(*mark))) {
c4aca7d0
GA
1847 if (GvIO(gv) && IoIFP(GvIOp(gv))) {
1848#ifdef HAS_FCHOWN
375ed12a 1849 int fd = PerlIO_fileno(IoIFP(GvIOn(gv)));
c4aca7d0 1850 APPLY_TAINT_PROPER();
375ed12a 1851 if (fd < 0) {
dd1dbff0 1852 SETERRNO(EBADF,RMS_IFI);
f95ba548 1853 tot--;
375ed12a 1854 } else if (fchown(fd, val, val2))
c4aca7d0
GA
1855 tot--;
1856#else
b9c6780e 1857 Perl_die(aTHX_ PL_no_func, "fchown");
c4aca7d0
GA
1858#endif
1859 }
1860 else {
8334cae6 1861 SETERRNO(EBADF,RMS_IFI);
c4aca7d0
GA
1862 tot--;
1863 }
1864 }
c4aca7d0 1865 else {
41188aa0 1866 const char *name = SvPV_nomg_const(*mark, len);
c4aca7d0 1867 APPLY_TAINT_PROPER();
41188aa0 1868 if (!IS_SAFE_PATHNAME(name, len, "chown") ||
c8028aa6 1869 PerlLIO_chown(name, val, val2)) {
c4aca7d0 1870 tot--;
c8028aa6 1871 }
c4aca7d0 1872 }
a687059c
LW
1873 }
1874 }
1875 break;
b1248f16 1876#endif
a1d180c4 1877/*
dd64f1c3
AD
1878XXX Should we make lchown() directly available from perl?
1879For now, we'll let Configure test for HAS_LCHOWN, but do
1880nothing in the core.
1881 --AD 5/1998
1882*/
fe14fcc3 1883#ifdef HAS_KILL
79072805 1884 case OP_KILL:
20408e3c 1885 APPLY_TAINT_PROPER();
55497cff 1886 if (mark == sp)
1887 break;
84c7b88c 1888 s = SvPVx_const(*++mark, len);
c2fd40cb
DM
1889 if (*s == '-' && isALPHA(s[1]))
1890 {
1891 s++;
1892 len--;
885b4b39 1893 killgp = TRUE;
c2fd40cb 1894 }
e02bfb16 1895 if (isALPHA(*s)) {
84c7b88c 1896 if (*s == 'S' && s[1] == 'I' && s[2] == 'G') {
79072805 1897 s += 3;
84c7b88c
BF
1898 len -= 3;
1899 }
1900 if ((val = whichsig_pvn(s, len)) < 0)
147e3846
KW
1901 Perl_croak(aTHX_ "Unrecognized signal name \"%" SVf "\"",
1902 SVfARG(*mark));
79072805
LW
1903 }
1904 else
c2fd40cb 1905 {
4ea561bc 1906 val = SvIV(*mark);
c2fd40cb
DM
1907 if (val < 0)
1908 {
885b4b39 1909 killgp = TRUE;
c2fd40cb
DM
1910 val = -val;
1911 }
1912 }
20408e3c
GS
1913 APPLY_TAINT_PROPER();
1914 tot = sp - mark;
fbcd93f0 1915
c2fd40cb 1916 while (++mark <= sp) {
60082291 1917 Pid_t proc;
c2fd40cb 1918 SvGETMAGIC(*mark);
60082291 1919 if (!(SvNIOK(*mark) || looks_like_number(*mark)))
c2fd40cb
DM
1920 Perl_croak(aTHX_ "Can't kill a non-numeric process ID");
1921 proc = SvIV_nomg(*mark);
c2fd40cb 1922 APPLY_TAINT_PROPER();
111f73b5
DM
1923#ifdef HAS_KILLPG
1924 /* use killpg in preference, as the killpg() wrapper for Win32
1925 * understands process groups, but the kill() wrapper doesn't */
1926 if (killgp ? PerlProc_killpg(proc, val)
1927 : PerlProc_kill(proc, val))
1928#else
1929 if (PerlProc_kill(killgp ? -proc: proc, val))
1930#endif
c2fd40cb 1931 tot--;
a687059c 1932 }
8165faea 1933 PERL_ASYNC_CHECK();
a687059c 1934 break;
b1248f16 1935#endif
79072805 1936 case OP_UNLINK:
20408e3c 1937 APPLY_TAINT_PROPER();
79072805
LW
1938 tot = sp - mark;
1939 while (++mark <= sp) {
41188aa0 1940 s = SvPV_const(*mark, len);
20408e3c 1941 APPLY_TAINT_PROPER();
41188aa0 1942 if (!IS_SAFE_PATHNAME(s, len, "unlink")) {
c8028aa6
TC
1943 tot--;
1944 }
f0d85c30 1945 else if (PL_unsafe) {
b8ffc8df 1946 if (UNLINK(s))
5cdd1fc2 1947 {
a687059c 1948 tot--;
5cdd1fc2
AB
1949 }
1950#if defined(__amigaos4__) && defined(NEWLIB)
1951 else
1952 {
1953 /* Under AmigaOS4 unlink only 'fails' if the
1954 * filename is invalid. It may not remove the file
1955 * if it's locked, so check if it's still around. */
1956 if ((access(s,F_OK) != -1))
1957 {
1958 tot--;
1959 }
1960 }
1961#endif
a687059c
LW
1962 }
1963 else { /* don't let root wipe out directories without -U */
45a23732
DD
1964 Stat_t statbuf;
1965 if (PerlLIO_lstat(s, &statbuf) < 0)
1dcae8b8 1966 tot--;
45a23732 1967 else if (S_ISDIR(statbuf.st_mode)) {
cd52bc19 1968 SETERRNO(EISDIR, SS_NOPRIV);
45a23732 1969 tot--;
1dcae8b8 1970 }
a687059c 1971 else {
b8ffc8df 1972 if (UNLINK(s))
5cdd1fc2
AB
1973 {
1974 tot--;
1975 }
1976#if defined(__amigaos4__) && defined(NEWLIB)
1977 else
1978 {
1979 /* Under AmigaOS4 unlink only 'fails' if the filename is invalid */
1980 /* It may not remove the file if it's Locked, so check if it's still */
1981 /* arround */
1982 if((access(s,F_OK) != -1))
1983 {
1984 tot--;
1985 }
1986 }
1987#endif
a687059c
LW
1988 }
1989 }
1990 }
1991 break;
e96b369d 1992#if defined(HAS_UTIME) || defined(HAS_FUTIMES)
79072805 1993 case OP_UTIME:
20408e3c 1994 APPLY_TAINT_PROPER();
79072805 1995 if (sp - mark > 2) {
e96b369d
GA
1996#if defined(HAS_FUTIMES)
1997 struct timeval utbuf[2];
1998 void *utbufp = utbuf;
1999#elif defined(I_UTIME) || defined(VMS)
663a0e37 2000 struct utimbuf utbuf;
07409e01 2001 struct utimbuf *utbufp = &utbuf;
663a0e37 2002#else
a687059c 2003 struct {
dd2821f6
GS
2004 Time_t actime;
2005 Time_t modtime;
a687059c 2006 } utbuf;
07409e01 2007 void *utbufp = &utbuf;
663a0e37 2008#endif
a687059c 2009
0bcc34c2
AL
2010 SV* const accessed = *++mark;
2011 SV* const modified = *++mark;
c6f7b413 2012
6ec06612
SB
2013 /* Be like C, and if both times are undefined, let the C
2014 * library figure out what to do. This usually means
2015 * "current time". */
c6f7b413
RS
2016
2017 if ( accessed == &PL_sv_undef && modified == &PL_sv_undef )
6ec06612
SB
2018 utbufp = NULL;
2019 else {
2020 Zero(&utbuf, sizeof utbuf, char);
e96b369d 2021#ifdef HAS_FUTIMES
4ea561bc 2022 utbuf[0].tv_sec = (long)SvIV(accessed); /* time accessed */
e96b369d 2023 utbuf[0].tv_usec = 0;
4ea561bc 2024 utbuf[1].tv_sec = (long)SvIV(modified); /* time modified */
e96b369d
GA
2025 utbuf[1].tv_usec = 0;
2026#elif defined(BIG_TIME)
4ea561bc
NC
2027 utbuf.actime = (Time_t)SvNV(accessed); /* time accessed */
2028 utbuf.modtime = (Time_t)SvNV(modified); /* time modified */
517844ec 2029#else
4ea561bc
NC
2030 utbuf.actime = (Time_t)SvIV(accessed); /* time accessed */
2031 utbuf.modtime = (Time_t)SvIV(modified); /* time modified */
517844ec 2032#endif
6ec06612 2033 }
4373e329 2034 APPLY_TAINT_PROPER();
79072805
LW
2035 tot = sp - mark;
2036 while (++mark <= sp) {
e96b369d 2037 GV* gv;
64617da9 2038 if ((gv = MAYBE_DEREF_GV(*mark))) {
e96b369d
GA
2039 if (GvIO(gv) && IoIFP(GvIOp(gv))) {
2040#ifdef HAS_FUTIMES
375ed12a 2041 int fd = PerlIO_fileno(IoIFP(GvIOn(gv)));
e96b369d 2042 APPLY_TAINT_PROPER();
375ed12a
JH
2043 if (fd < 0) {
2044 SETERRNO(EBADF,RMS_IFI);
2045 tot--;
2046 } else if (futimes(fd, (struct timeval *) utbufp))
e96b369d
GA
2047 tot--;
2048#else
2049 Perl_die(aTHX_ PL_no_func, "futimes");
2050#endif
2051 }
2052 else {
2053 tot--;
2054 }
2055 }
e96b369d 2056 else {
41188aa0 2057 const char * const name = SvPV_nomg_const(*mark, len);
e96b369d 2058 APPLY_TAINT_PROPER();
41188aa0 2059 if (!IS_SAFE_PATHNAME(name, len, "utime")) {
c8028aa6
TC
2060 tot--;
2061 }
2062 else
e96b369d 2063#ifdef HAS_FUTIMES
8b7231d9 2064 if (utimes(name, (struct timeval *)utbufp))
e96b369d
GA
2065#else
2066 if (PerlLIO_utime(name, utbufp))
2067#endif
2068 tot--;
2069 }
2070
a687059c 2071 }
a687059c
LW
2072 }
2073 else
79072805 2074 tot = 0;
a687059c 2075 break;
a0d0e21e 2076#endif
a687059c
LW
2077 }
2078 return tot;
20408e3c 2079
20408e3c 2080#undef APPLY_TAINT_PROPER
a687059c
LW
2081}
2082
bd93adf5 2083/* Do the permissions in *statbufp allow some operation? */
a0d0e21e 2084#ifndef VMS /* VMS' cando is in vms.c */
7f4774ae 2085bool
5aaab254 2086Perl_cando(pTHX_ Mode_t mode, bool effective, const Stat_t *statbufp)
ae1951c1
NC
2087/* effective is a flag, true for EUID, or for checking if the effective gid
2088 * is in the list of groups returned from getgroups().
2089 */
a687059c 2090{
7918f24d 2091 PERL_ARGS_ASSERT_CANDO;
81611534 2092 PERL_UNUSED_CONTEXT;
7918f24d 2093
bee1dbe2 2094#ifdef DOSISH
fe14fcc3
LW
2095 /* [Comments and code from Len Reed]
2096 * MS-DOS "user" is similar to UNIX's "superuser," but can't write
2097 * to write-protected files. The execute permission bit is set
486ec47a 2098 * by the Microsoft C library stat() function for the following:
fe14fcc3
LW
2099 * .exe files
2100 * .com files
2101 * .bat files
2102 * directories
2103 * All files and directories are readable.
2104 * Directories and special files, e.g. "CON", cannot be
2105 * write-protected.
2106 * [Comment by Tom Dinger -- a directory can have the write-protect
2107 * bit set in the file system, but DOS permits changes to
2108 * the directory anyway. In addition, all bets are off
2109 * here for networked software, such as Novell and
2110 * Sun's PC-NFS.]
2111 */
2112
bee1dbe2
LW
2113 /* Atari stat() does pretty much the same thing. we set x_bit_set_in_stat
2114 * too so it will actually look into the files for magic numbers
2115 */
7f4774ae 2116 return (mode & statbufp->st_mode) ? TRUE : FALSE;
fe14fcc3 2117
55497cff 2118#else /* ! DOSISH */
b595cd4b
RU
2119# ifdef __CYGWIN__
2120 if (ingroup(544,effective)) { /* member of Administrators */
2121# else
985213f2 2122 if ((effective ? PerlProc_geteuid() : PerlProc_getuid()) == 0) { /* root is special */
b595cd4b 2123# endif
7f4774ae 2124 if (mode == S_IXUSR) {
c623bd54 2125 if (statbufp->st_mode & 0111 || S_ISDIR(statbufp->st_mode))
a687059c
LW
2126 return TRUE;
2127 }
2128 else
2129 return TRUE; /* root reads and writes anything */
2130 return FALSE;
2131 }
985213f2 2132 if (statbufp->st_uid == (effective ? PerlProc_geteuid() : PerlProc_getuid()) ) {
7f4774ae 2133 if (statbufp->st_mode & mode)
a687059c
LW
2134 return TRUE; /* ok as "user" */
2135 }
d8eceb89 2136 else if (ingroup(statbufp->st_gid,effective)) {
7f4774ae 2137 if (statbufp->st_mode & mode >> 3)
a687059c
LW
2138 return TRUE; /* ok as "group" */
2139 }
7f4774ae 2140 else if (statbufp->st_mode & mode >> 6)
a687059c
LW
2141 return TRUE; /* ok as "other" */
2142 return FALSE;
55497cff 2143#endif /* ! DOSISH */
a687059c 2144}
a0d0e21e 2145#endif /* ! VMS */
a687059c 2146
1f676739 2147static bool
0da8eb3a 2148S_ingroup(pTHX_ Gid_t testgid, bool effective)
a687059c 2149{
81611534
JH
2150#ifndef PERL_IMPLICIT_SYS
2151 /* PERL_IMPLICIT_SYS like Win32: getegid() etc. require the context. */
2152 PERL_UNUSED_CONTEXT;
2153#endif
985213f2 2154 if (testgid == (effective ? PerlProc_getegid() : PerlProc_getgid()))
a687059c 2155 return TRUE;
fe14fcc3 2156#ifdef HAS_GETGROUPS
a687059c 2157 {
331b57bc 2158 Groups_t *gary = NULL;
79072805 2159 I32 anum;
331b57bc 2160 bool rc = FALSE;
a687059c 2161
331b57bc 2162 anum = getgroups(0, gary);
375ed12a
JH
2163 if (anum > 0) {
2164 Newx(gary, anum, Groups_t);
2165 anum = getgroups(anum, gary);
2166 while (--anum >= 0)
2167 if (gary[anum] == testgid) {
2168 rc = TRUE;
2169 break;
2170 }
331b57bc 2171
375ed12a
JH
2172 Safefree(gary);
2173 }
331b57bc 2174 return rc;
a687059c 2175 }
c685562b 2176#else
a687059c 2177 return FALSE;
cd39f2b6 2178#endif
a687059c 2179}
c2ab57d4 2180
fe14fcc3 2181#if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
c2ab57d4 2182
79072805 2183I32
864dbfa3 2184Perl_do_ipcget(pTHX_ I32 optype, SV **mark, SV **sp)
c2ab57d4 2185{
0bcc34c2 2186 const key_t key = (key_t)SvNVx(*++mark);
c3312966 2187 SV *nsv = optype == OP_MSGGET ? NULL : *++mark;
6867be6d 2188 const I32 flags = SvIVx(*++mark);
294a48e9 2189
7918f24d 2190 PERL_ARGS_ASSERT_DO_IPCGET;
294a48e9 2191 PERL_UNUSED_ARG(sp);
c2ab57d4 2192
748a9306 2193 SETERRNO(0,0);
c2ab57d4
LW
2194 switch (optype)
2195 {
fe14fcc3 2196#ifdef HAS_MSG
79072805 2197 case OP_MSGGET:
c2ab57d4 2198 return msgget(key, flags);
e5d73d77 2199#endif
fe14fcc3 2200#ifdef HAS_SEM
79072805 2201 case OP_SEMGET:
c3312966 2202 return semget(key, (int) SvIV(nsv), flags);
e5d73d77 2203#endif
fe14fcc3 2204#ifdef HAS_SHM
79072805 2205 case OP_SHMGET:
c3312966 2206 return shmget(key, (size_t) SvUV(nsv), flags);
e5d73d77 2207#endif
fe14fcc3 2208#if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
e5d73d77 2209 default:
fe13d51d 2210 /* diag_listed_as: msg%s not implemented */
cea2e8a9 2211 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
e5d73d77 2212#endif
c2ab57d4
LW
2213 }
2214 return -1; /* should never happen */
2215}
2216
79072805 2217I32
864dbfa3 2218Perl_do_ipcctl(pTHX_ I32 optype, SV **mark, SV **sp)
c2ab57d4 2219{
c2ab57d4 2220 char *a;
a0d0e21e 2221 I32 ret = -1;
6867be6d 2222 const I32 id = SvIVx(*++mark);
95b63a38 2223#ifdef Semctl
6867be6d 2224 const I32 n = (optype == OP_SEMCTL) ? SvIVx(*++mark) : 0;
95b63a38 2225#endif
6867be6d 2226 const I32 cmd = SvIVx(*++mark);
0bcc34c2
AL
2227 SV * const astr = *++mark;
2228 STRLEN infosize = 0;
2229 I32 getinfo = (cmd == IPC_STAT);
c2ab57d4 2230
7918f24d 2231 PERL_ARGS_ASSERT_DO_IPCCTL;
0bcc34c2 2232 PERL_UNUSED_ARG(sp);
c2ab57d4
LW
2233
2234 switch (optype)
2235 {
fe14fcc3 2236#ifdef HAS_MSG
79072805 2237 case OP_MSGCTL:
c2ab57d4
LW
2238 if (cmd == IPC_STAT || cmd == IPC_SET)
2239 infosize = sizeof(struct msqid_ds);
2240 break;
e5d73d77 2241#endif
fe14fcc3 2242#ifdef HAS_SHM
79072805 2243 case OP_SHMCTL:
c2ab57d4
LW
2244 if (cmd == IPC_STAT || cmd == IPC_SET)
2245 infosize = sizeof(struct shmid_ds);
2246 break;
e5d73d77 2247#endif
fe14fcc3 2248#ifdef HAS_SEM
79072805 2249 case OP_SEMCTL:
39398f3f 2250#ifdef Semctl
c2ab57d4
LW
2251 if (cmd == IPC_STAT || cmd == IPC_SET)
2252 infosize = sizeof(struct semid_ds);
2253 else if (cmd == GETALL || cmd == SETALL)
2254 {
8e591e46 2255 struct semid_ds semds;
bd89102f 2256 union semun semun;
e6f0bdd6
GS
2257#ifdef EXTRA_F_IN_SEMUN_BUF
2258 semun.buff = &semds;
2259#else
84902520 2260 semun.buf = &semds;
e6f0bdd6 2261#endif
c2ab57d4 2262 getinfo = (cmd == GETALL);
9b89d93d
GB
2263 if (Semctl(id, 0, IPC_STAT, semun) == -1)
2264 return -1;
6e21c824
LW
2265 infosize = semds.sem_nsems * sizeof(short);
2266 /* "short" is technically wrong but much more portable
2267 than guessing about u_?short(_t)? */
c2ab57d4 2268 }
39398f3f 2269#else
fe13d51d 2270 /* diag_listed_as: sem%s not implemented */
cea2e8a9 2271 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
39398f3f 2272#endif
c2ab57d4 2273 break;
e5d73d77 2274#endif
fe14fcc3 2275#if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
e5d73d77 2276 default:
fe13d51d 2277 /* diag_listed_as: shm%s not implemented */
cea2e8a9 2278 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
e5d73d77 2279#endif
c2ab57d4
LW
2280 }
2281
2282 if (infosize)
2283 {
2284 if (getinfo)
2285 {
93524f2b 2286 SvPV_force_nolen(astr);
a0d0e21e 2287 a = SvGROW(astr, infosize+1);
c2ab57d4
LW
2288 }
2289 else
2290 {
93524f2b 2291 STRLEN len;
463ee0b2
LW
2292 a = SvPV(astr, len);
2293 if (len != infosize)
cea2e8a9 2294 Perl_croak(aTHX_ "Bad arg length for %s, is %lu, should be %ld",
4ec43091
JH
2295 PL_op_desc[optype],
2296 (unsigned long)len,
2297 (long)infosize);
c2ab57d4
LW
2298 }
2299 }
2300 else
2301 {
0bcc34c2 2302 const IV i = SvIV(astr);
56431972 2303 a = INT2PTR(char *,i); /* ouch */
c2ab57d4 2304 }
748a9306 2305 SETERRNO(0,0);
c2ab57d4
LW
2306 switch (optype)
2307 {
fe14fcc3 2308#ifdef HAS_MSG
79072805 2309 case OP_MSGCTL:
bee1dbe2 2310 ret = msgctl(id, cmd, (struct msqid_ds *)a);
c2ab57d4 2311 break;
e5d73d77 2312#endif
fe14fcc3 2313#ifdef HAS_SEM
bd89102f 2314 case OP_SEMCTL: {
39398f3f 2315#ifdef Semctl
bd89102f
AD
2316 union semun unsemds;
2317
64d76282
BC
2318 if(cmd == SETVAL) {
2319 unsemds.val = PTR2nat(a);
2320 }
2321 else {
e6f0bdd6 2322#ifdef EXTRA_F_IN_SEMUN_BUF
64d76282 2323 unsemds.buff = (struct semid_ds *)a;
e6f0bdd6 2324#else
64d76282 2325 unsemds.buf = (struct semid_ds *)a;
e6f0bdd6 2326#endif
64d76282 2327 }
bd89102f 2328 ret = Semctl(id, n, cmd, unsemds);
39398f3f 2329#else
fe13d51d 2330 /* diag_listed_as: sem%s not implemented */
cea2e8a9 2331 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
39398f3f 2332#endif
bd89102f 2333 }
c2ab57d4 2334 break;
e5d73d77 2335#endif
fe14fcc3 2336#ifdef HAS_SHM
79072805 2337 case OP_SHMCTL:
bee1dbe2 2338 ret = shmctl(id, cmd, (struct shmid_ds *)a);
c2ab57d4 2339 break;
e5d73d77 2340#endif
c2ab57d4
LW
2341 }
2342 if (getinfo && ret >= 0) {
79072805
LW
2343 SvCUR_set(astr, infosize);
2344 *SvEND(astr) = '\0';
a0d0e21e 2345 SvSETMAGIC(astr);
c2ab57d4
LW
2346 }
2347 return ret;
2348}
2349
79072805 2350I32
864dbfa3 2351Perl_do_msgsnd(pTHX_ SV **mark, SV **sp)
c2ab57d4 2352{
fe14fcc3 2353#ifdef HAS_MSG
463ee0b2 2354 STRLEN len;
6867be6d 2355 const I32 id = SvIVx(*++mark);
0bcc34c2
AL
2356 SV * const mstr = *++mark;
2357 const I32 flags = SvIVx(*++mark);
2358 const char * const mbuf = SvPV_const(mstr, len);
2359 const I32 msize = len - sizeof(long);
2360
7918f24d 2361 PERL_ARGS_ASSERT_DO_MSGSND;
890ce7af 2362 PERL_UNUSED_ARG(sp);
c2ab57d4 2363
0bcc34c2 2364 if (msize < 0)
cea2e8a9 2365 Perl_croak(aTHX_ "Arg too short for msgsnd");
748a9306 2366 SETERRNO(0,0);
681fb693
JH
2367 if (id >= 0 && flags >= 0) {
2368 return msgsnd(id, (struct msgbuf *)mbuf, msize, flags);
2369 } else {
2370 SETERRNO(EINVAL,LIB_INVARG);
2371 return -1;
2372 }
e5d73d77 2373#else
2d51fa4d
RGS
2374 PERL_UNUSED_ARG(sp);
2375 PERL_UNUSED_ARG(mark);
fe13d51d 2376 /* diag_listed_as: msg%s not implemented */
cea2e8a9 2377 Perl_croak(aTHX_ "msgsnd not implemented");
7c522378 2378 return -1;
e5d73d77 2379#endif
c2ab57d4
LW
2380}
2381
79072805 2382I32
864dbfa3 2383Perl_do_msgrcv(pTHX_ SV **mark, SV **sp)
c2ab57d4 2384{
fe14fcc3 2385#ifdef HAS_MSG
c2ab57d4
LW
2386 char *mbuf;
2387 long mtype;
6867be6d 2388 I32 msize, flags, ret;
6867be6d 2389 const I32 id = SvIVx(*++mark);
0bcc34c2 2390 SV * const mstr = *++mark;
7918f24d
NC
2391
2392 PERL_ARGS_ASSERT_DO_MSGRCV;
890ce7af 2393 PERL_UNUSED_ARG(sp);
79072805 2394
c2e66d9e
GS
2395 /* suppress warning when reading into undef var --jhi */
2396 if (! SvOK(mstr))
8062ff11 2397 SvPVCLEAR(mstr);
463ee0b2
LW
2398 msize = SvIVx(*++mark);
2399 mtype = (long)SvIVx(*++mark);
2400 flags = SvIVx(*++mark);
93524f2b 2401 SvPV_force_nolen(mstr);
a0d0e21e 2402 mbuf = SvGROW(mstr, sizeof(long)+msize+1);
a1d180c4 2403
748a9306 2404 SETERRNO(0,0);
d2607e1e
JH
2405 if (id >= 0 && msize >= 0 && flags >= 0) {
2406 ret = msgrcv(id, (struct msgbuf *)mbuf, msize, mtype, flags);
2407 } else {
2408 SETERRNO(EINVAL,LIB_INVARG);
2409 ret = -1;
2410 }
c2ab57d4 2411 if (ret >= 0) {
79072805
LW
2412 SvCUR_set(mstr, sizeof(long)+ret);
2413 *SvEND(mstr) = '\0';
41d6edb2
JH
2414 /* who knows who has been playing with this message? */
2415 SvTAINTED_on(mstr);
c2ab57d4
LW
2416 }
2417 return ret;
e5d73d77 2418#else
2d51fa4d
RGS
2419 PERL_UNUSED_ARG(sp);
2420 PERL_UNUSED_ARG(mark);
fe13d51d 2421 /* diag_listed_as: msg%s not implemented */
cea2e8a9 2422 Perl_croak(aTHX_ "msgrcv not implemented");
7c522378 2423 return -1;
e5d73d77 2424#endif
c2ab57d4
LW
2425}
2426
79072805 2427I32
864dbfa3 2428Perl_do_semop(pTHX_ SV **mark, SV **sp)
c2ab57d4 2429{
fe14fcc3 2430#ifdef HAS_SEM
463ee0b2 2431 STRLEN opsize;
6867be6d 2432 const I32 id = SvIVx(*++mark);
0bcc34c2
AL
2433 SV * const opstr = *++mark;
2434 const char * const opbuf = SvPV_const(opstr, opsize);
7918f24d
NC
2435
2436 PERL_ARGS_ASSERT_DO_SEMOP;
890ce7af 2437 PERL_UNUSED_ARG(sp);
c2ab57d4 2438
248ff010
NC
2439 if (opsize < 3 * SHORTSIZE
2440 || (opsize % (3 * SHORTSIZE))) {
93189314 2441 SETERRNO(EINVAL,LIB_INVARG);
c2ab57d4
LW
2442 return -1;
2443 }
748a9306 2444 SETERRNO(0,0);
248ff010
NC
2445 /* We can't assume that sizeof(struct sembuf) == 3 * sizeof(short). */
2446 {
6867be6d 2447 const int nsops = opsize / (3 * sizeof (short));
248ff010 2448 int i = nsops;
0bcc34c2 2449 short * const ops = (short *) opbuf;
248ff010
NC
2450 short *o = ops;
2451 struct sembuf *temps, *t;
2452 I32 result;
2453
a02a5408 2454 Newx (temps, nsops, struct sembuf);
248ff010
NC
2455 t = temps;
2456 while (i--) {
2457 t->sem_num = *o++;
2458 t->sem_op = *o++;
2459 t->sem_flg = *o++;
2460 t++;
2461 }
2462 result = semop(id, temps, nsops);
248ff010
NC
2463 Safefree(temps);
2464 return result;
2465 }
e5d73d77 2466#else
fe13d51d 2467 /* diag_listed_as: sem%s not implemented */
cea2e8a9 2468 Perl_croak(aTHX_ "semop not implemented");
e5d73d77 2469#endif
c2ab57d4
LW
2470}
2471
79072805 2472I32
864dbfa3 2473Perl_do_shmio(pTHX_ I32 optype, SV **mark, SV **sp)
c2ab57d4 2474{
fe14fcc3 2475#ifdef HAS_SHM
4373e329 2476 char *shm;
c2ab57d4 2477 struct shmid_ds shmds;
6867be6d 2478 const I32 id = SvIVx(*++mark);
0bcc34c2
AL
2479 SV * const mstr = *++mark;
2480 const I32 mpos = SvIVx(*++mark);
2481 const I32 msize = SvIVx(*++mark);
7918f24d
NC
2482
2483 PERL_ARGS_ASSERT_DO_SHMIO;
890ce7af 2484 PERL_UNUSED_ARG(sp);
c2ab57d4 2485
748a9306 2486 SETERRNO(0,0);
c2ab57d4
LW
2487 if (shmctl(id, IPC_STAT, &shmds) == -1)
2488 return -1;
7f39519f
NC
2489 if (mpos < 0 || msize < 0
2490 || (size_t)mpos + msize > (size_t)shmds.shm_segsz) {
93189314 2491 SETERRNO(EFAULT,SS_ACCVIO); /* can't do as caller requested */
c2ab57d4
LW
2492 return -1;
2493 }
568fc267
JH
2494 if (id >= 0) {
2495 shm = (char *)shmat(id, NULL, (optype == OP_SHMREAD) ? SHM_RDONLY : 0);
2496 } else {
2497 SETERRNO(EINVAL,LIB_INVARG);
2498 return -1;
2499 }
c2ab57d4
LW
2500 if (shm == (char *)-1) /* I hate System V IPC, I really do */
2501 return -1;
79072805 2502 if (optype == OP_SHMREAD) {
c8ae91a8 2503 char *mbuf;
9f538c04 2504 /* suppress warning when reading into undef var (tchrist 3/Mar/00) */
b399897d
CS
2505 SvGETMAGIC(mstr);
2506 SvUPGRADE(mstr, SVt_PV);
9f538c04 2507 if (! SvOK(mstr))
8062ff11 2508 SvPVCLEAR(mstr);
af8ff727 2509 SvPOK_only(mstr);
bb7a0f54 2510 mbuf = SvGROW(mstr, (STRLEN)msize+1);
a0d0e21e 2511
bee1dbe2 2512 Copy(shm + mpos, mbuf, msize, char);
79072805
LW
2513 SvCUR_set(mstr, msize);
2514 *SvEND(mstr) = '\0';
a0d0e21e 2515 SvSETMAGIC(mstr);
d929ce6f
JH
2516 /* who knows who has been playing with this shared memory? */
2517 SvTAINTED_on(mstr);
c2ab57d4
LW
2518 }
2519 else {
93524f2b 2520 STRLEN len;
c2ab57d4 2521
93524f2b 2522 const char *mbuf = SvPV_const(mstr, len);
027aa12d 2523 const I32 n = ((I32)len > msize) ? msize : (I32)len;
bee1dbe2 2524 Copy(mbuf, shm + mpos, n, char);
c2ab57d4 2525 if (n < msize)
bee1dbe2 2526 memzero(shm + mpos + n, msize - n);
c2ab57d4
LW
2527 }
2528 return shmdt(shm);
e5d73d77 2529#else
fe13d51d 2530 /* diag_listed_as: shm%s not implemented */
cea2e8a9 2531 Perl_croak(aTHX_ "shm I/O not implemented");
7c522378 2532 return -1;
e5d73d77 2533#endif
c2ab57d4
LW
2534}
2535
fe14fcc3 2536#endif /* SYSV IPC */
4e35701f 2537
0d44d22b 2538/*
ccfc67b7
JH
2539=head1 IO Functions
2540
0d44d22b
NC
2541=for apidoc start_glob
2542
2543Function called by C<do_readline> to spawn a glob (or do the glob inside
154e47c8 2544perl on VMS). This code used to be inline, but now perl uses C<File::Glob>
25047fde
KW
2545this glob starter is only used by miniperl during the build process,
2546or when PERL_EXTERNAL_GLOB is defined.
75af9d73 2547Moving it away shrinks F<pp_hot.c>; shrinking F<pp_hot.c> helps speed perl up.
fab3f3a7 2548
0d44d22b
NC
2549=cut
2550*/
2551
2552PerlIO *
2553Perl_start_glob (pTHX_ SV *tmpglob, IO *io)
2554{
561b68a9 2555 SV * const tmpcmd = newSV(0);
0d44d22b 2556 PerlIO *fp;
41188aa0
TC
2557 STRLEN len;
2558 const char *s = SvPV(tmpglob, len);
7918f24d
NC
2559
2560 PERL_ARGS_ASSERT_START_GLOB;
2561
41188aa0 2562 if (!IS_SAFE_SYSCALL(s, len, "pattern", "glob"))
c8028aa6
TC
2563 return NULL;
2564
0d44d22b
NC
2565 ENTER;
2566 SAVEFREESV(tmpcmd);
2567#ifdef VMS /* expand the wildcards right here, rather than opening a pipe, */
2568 /* since spawning off a process is a real performance hit */
dca5a913
JM
2569
2570PerlIO *
2571Perl_vms_start_glob
2572 (pTHX_ SV *tmpglob,
2573 IO *io);
2574
49a7a762 2575 fp = Perl_vms_start_glob(aTHX_ tmpglob, io);
dca5a913 2576
0d44d22b 2577#else /* !VMS */
0d44d22b
NC
2578#ifdef DOSISH
2579#ifdef OS2
2580 sv_setpv(tmpcmd, "for a in ");
2581 sv_catsv(tmpcmd, tmpglob);
2582 sv_catpv(tmpcmd, "; do echo \"$a\\0\\c\"; done |");
2583#else
2584#ifdef DJGPP
2585 sv_setpv(tmpcmd, "/dev/dosglob/"); /* File System Extension */
2586 sv_catsv(tmpcmd, tmpglob);
2587#else
2588 sv_setpv(tmpcmd, "perlglob ");
2589 sv_catsv(tmpcmd, tmpglob);
2590 sv_catpv(tmpcmd, " |");
2591#endif /* !DJGPP */
2592#endif /* !OS2 */
2593#else /* !DOSISH */
2594#if defined(CSH)
2595 sv_setpvn(tmpcmd, PL_cshname, PL_cshlen);
2596 sv_catpv(tmpcmd, " -cf 'set nonomatch; glob ");
2597 sv_catsv(tmpcmd, tmpglob);
2598 sv_catpv(tmpcmd, "' 2>/dev/null |");
2599#else
2600 sv_setpv(tmpcmd, "echo ");
2601 sv_catsv(tmpcmd, tmpglob);
0d44d22b 2602 sv_catpv(tmpcmd, "|tr -s ' \t\f\r' '\\n\\n\\n\\n'|");
0d44d22b
NC
2603#endif /* !CSH */
2604#endif /* !DOSISH */
93b2dae1 2605 {
acffc8af
FC
2606 SV ** const svp = hv_fetchs(GvHVn(PL_envgv), "LS_COLORS", 0);
2607 if (svp && *svp)
2608 save_helem_flags(GvHV(PL_envgv),
2609 newSVpvs_flags("LS_COLORS", SVs_TEMP), svp,
2610 SAVEf_SETMAGIC);
93b2dae1 2611 }
d5eb9a46
NC
2612 (void)do_open6(PL_last_in_gv, SvPVX_const(tmpcmd), SvCUR(tmpcmd),
2613 NULL, NULL, 0);
0d44d22b
NC
2614 fp = IoIFP(io);
2615#endif /* !VMS */
2616 LEAVE;
de7dabb6
TC
2617
2618 if (!fp && ckWARN(WARN_GLOB)) {
2619 Perl_warner(aTHX_ packWARN(WARN_GLOB), "glob failed (can't start child: %s)",
2620 Strerror(errno));
2621 }
2622
0d44d22b
NC
2623 return fp;
2624}
66610fdd
RGS
2625
2626/*
14d04a33 2627 * ex: set ts=8 sts=4 sw=4 et:
37442d52 2628 */