This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
perlre: Don't name exact max non-consume depth
[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. */
57d92825
JH
764 if (fcntl(ofd,F_SETFD, fd_flags) < 0) {
765 if (dupfd >= 0)
766 PerlLIO_close(dupfd);
767 goto say_false;
768 }
03e631df 769#endif
e212fc47 770 PerlLIO_close(dupfd);
ecdeb87c 771 }
e212fc47
AMS
772 else
773 PerlIO_close(fp);
6e21c824
LW
774 }
775 fp = saveifp;
760ac839 776 PerlIO_clearerr(fp);
e99cca91 777 fd = PerlIO_fileno(fp);
6e21c824 778 }
194e5090 779#if defined(HAS_FCNTL) && defined(F_SETFD) && defined(FD_CLOEXEC)
f6ec3cd3 780 if (fd >= 0 && fd > PL_maxsysfd && fcntl(fd, F_SETFD, FD_CLOEXEC) < 0) {
194e5090
JH
781 PerlLIO_close(fd);
782 goto say_false;
a8710ca1 783 }
1462b684 784#endif
8990e307 785 IoIFP(io) = fp;
b931b1d9 786
684bef36 787 IoFLAGS(io) &= ~IOf_NOLINE;
bf38876a 788 if (writing) {
50952442 789 if (IoTYPE(io) == IoTYPE_SOCKET
e99cca91 790 || (IoTYPE(io) == IoTYPE_WRONLY && fd >= 0 && S_ISCHR(PL_statbuf.st_mode)) ) {
a33cf58c 791 char *s = mode;
3b6c1aba
JH
792 if (*s == IoTYPE_IMPLICIT || *s == IoTYPE_NUMERIC)
793 s++;
a33cf58c 794 *s = 'w';
7c491510 795 if (!(IoOFP(io) = PerlIO_openn(aTHX_ type,s,fd,0,0,NULL,0,NULL))) {
760ac839 796 PerlIO_close(fp);
6e21c824 797 goto say_false;
fe14fcc3 798 }
1462b684
LW
799 }
800 else
8990e307 801 IoOFP(io) = fp;
bf38876a 802 }
a687059c 803 return TRUE;
6e21c824 804
7b52d656 805 say_false:
8990e307
LW
806 IoIFP(io) = saveifp;
807 IoOFP(io) = saveofp;
808 IoTYPE(io) = savetype;
6e21c824 809 return FALSE;
a687059c
LW
810}
811
760ac839 812PerlIO *
157fb5a1 813Perl_nextargv(pTHX_ GV *gv, bool nomagicopen)
a687059c 814{
2d03de9c 815 IO * const io = GvIOp(gv);
502aca56 816 SV *const old_out_name = PL_inplace ? newSVsv(GvSV(gv)) : NULL;
fe14fcc3 817
7918f24d
NC
818 PERL_ARGS_ASSERT_NEXTARGV;
819
502aca56
TC
820 if (old_out_name)
821 SAVEFREESV(old_out_name);
822
3280af22 823 if (!PL_argvoutgv)
fafc274c 824 PL_argvoutgv = gv_fetchpvs("ARGVOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO);
5513c2cf 825 if (io && (IoFLAGS(io) & (IOf_ARGV|IOf_START)) == (IOf_ARGV|IOf_START)) {
18708f5a 826 IoFLAGS(io) &= ~IOf_START;
7a1c5554 827 if (PL_inplace) {
294b3b39 828 assert(PL_defoutgv);
29a861e7
NC
829 Perl_av_create_and_push(aTHX_ &PL_argvout_stack,
830 SvREFCNT_inc_simple_NN(PL_defoutgv));
7a1c5554 831 }
18708f5a 832 }
3280af22
NIS
833 if (PL_filemode & (S_ISUID|S_ISGID)) {
834 PerlIO_flush(IoIFP(GvIOn(PL_argvoutgv))); /* chmod must follow last write */
fe14fcc3 835#ifdef HAS_FCHMOD
c797f2d8
DM
836 if (PL_lastfd != -1)
837 (void)fchmod(PL_lastfd,PL_filemode);
fe14fcc3 838#else
b28d0864 839 (void)PerlLIO_chmod(PL_oldname,PL_filemode);
fe14fcc3
LW
840#endif
841 }
c797f2d8 842 PL_lastfd = -1;
3280af22 843 PL_filemode = 0;
5c501b37 844 if (!GvAV(gv))
4608196e 845 return NULL;
b9f2b683 846 while (av_tindex(GvAV(gv)) >= 0) {
45a23732 847 Stat_t statbuf;
85aff577 848 STRLEN oldlen;
1fa0529f 849 SV *const sv = av_shift(GvAV(gv));
8990e307 850 SAVEFREESV(sv);
4bac9ae4 851 SvTAINTED_off(GvSVn(gv)); /* previous tainting irrelevant */
e203899d 852 sv_setsv(GvSVn(gv),sv);
79072805 853 SvSETMAGIC(GvSV(gv));
3280af22 854 PL_oldname = SvPVx(GvSV(gv), oldlen);
d8015975 855 if (LIKELY(!PL_inplace)) {
157fb5a1
RGS
856 if (nomagicopen
857 ? do_open6(gv, "<", 1, NULL, &GvSV(gv), 1)
858 : do_open6(gv, PL_oldname, oldlen, NULL, NULL, 0)
859 ) {
d8015975
NC
860 return IoIFP(GvIOp(gv));
861 }
862 }
863 else {
502aca56
TC
864 {
865 IO * const io = GvIOp(PL_argvoutgv);
866 if (io && IoIFP(io) && old_out_name && !io_close(io, PL_argvoutgv, FALSE, FALSE)) {
147e3846
KW
867 Perl_croak(aTHX_ "Failed to close in-place edit file %"
868 SVf ": %s\n", old_out_name, Strerror(errno));
502aca56
TC
869 }
870 }
d8015975
NC
871 /* This very long block ends with return IoIFP(GvIOp(gv));
872 Both this block and the block above fall through on open
873 failure to the warning code, and then the while loop above tries
874 the next entry. */
875 if (do_open_raw(gv, PL_oldname, oldlen, O_RDONLY, 0)) {
1fa0529f
NC
876#ifndef FLEXFILENAMES
877 int filedev;
878 int fileino;
879#endif
880 Uid_t fileuid;
881 Gid_t filegid;
882
79072805 883 TAINT_PROPER("inplace open");
3280af22 884 if (oldlen == 1 && *PL_oldname == '-') {
fafc274c
NC
885 setdefout(gv_fetchpvs("STDOUT", GV_ADD|GV_NOTQUAL,
886 SVt_PVIO));
a0d0e21e 887 return IoIFP(GvIOp(gv));
c623bd54 888 }
99b89507 889#ifndef FLEXFILENAMES
b28d0864
NIS
890 filedev = PL_statbuf.st_dev;
891 fileino = PL_statbuf.st_ino;
99b89507 892#endif
3280af22
NIS
893 PL_filemode = PL_statbuf.st_mode;
894 fileuid = PL_statbuf.st_uid;
895 filegid = PL_statbuf.st_gid;
896 if (!S_ISREG(PL_filemode)) {
9b387841
NC
897 Perl_ck_warner_d(aTHX_ packWARN(WARN_INPLACE),
898 "Can't do inplace edit: %s is not a regular file",
899 PL_oldname );
79072805 900 do_close(gv,FALSE);
c623bd54
LW
901 continue;
902 }
c9930541 903 if (*PL_inplace && strNE(PL_inplace, "*")) {
2d03de9c 904 const char *star = strchr(PL_inplace, '*');
2d259d92 905 if (star) {
2d03de9c 906 const char *begin = PL_inplace;
8062ff11 907 SvPVCLEAR(sv);
2d259d92
CK
908 do {
909 sv_catpvn(sv, begin, star - begin);
3280af22 910 sv_catpvn(sv, PL_oldname, oldlen);
2d259d92
CK
911 begin = ++star;
912 } while ((star = strchr(begin, '*')));
3d66d7bb
GS
913 if (*begin)
914 sv_catpv(sv,begin);
2d259d92
CK
915 }
916 else {
3280af22 917 sv_catpv(sv,PL_inplace);
2d259d92 918 }
c623bd54 919#ifndef FLEXFILENAMES
95a20fc0 920 if ((PerlLIO_stat(SvPVX_const(sv),&PL_statbuf) >= 0
5f74f29c
JH
921 && PL_statbuf.st_dev == filedev
922 && PL_statbuf.st_ino == fileino)
39e571d4 923#ifdef DJGPP
5f74f29c 924 || ((_djstat_fail_bits & _STFAIL_TRUENAME)!=0)
39e571d4 925#endif
f248d071
GS
926 )
927 {
9b387841 928 Perl_ck_warner_d(aTHX_ packWARN(WARN_INPLACE),
147e3846
KW
929 "Can't do inplace edit: %"
930 SVf " would not be unique",
9b387841 931 SVfARG(sv));
79072805 932 do_close(gv,FALSE);
c623bd54
LW
933 continue;
934 }
935#endif
fe14fcc3 936#ifdef HAS_RENAME
739a0b84 937#if !defined(DOSISH) && !defined(__CYGWIN__)
95a20fc0 938 if (PerlLIO_rename(PL_oldname,SvPVX_const(sv)) < 0) {
9b387841 939 Perl_ck_warner_d(aTHX_ packWARN(WARN_INPLACE),
147e3846
KW
940 "Can't rename %s to %" SVf
941 ": %s, skipping file",
942 PL_oldname, SVfARG(sv),
943 Strerror(errno));
79072805 944 do_close(gv,FALSE);
c623bd54
LW
945 continue;
946 }
a687059c 947#else
79072805 948 do_close(gv,FALSE);
95a20fc0
SP
949 (void)PerlLIO_unlink(SvPVX_const(sv));
950 (void)PerlLIO_rename(PL_oldname,SvPVX_const(sv));
d5eb9a46 951 do_open_raw(gv, SvPVX_const(sv), SvCUR(sv), O_RDONLY, 0);
55497cff 952#endif /* DOSISH */
ff8e2863 953#else
95a20fc0
SP
954 (void)UNLINK(SvPVX_const(sv));
955 if (link(PL_oldname,SvPVX_const(sv)) < 0) {
9b387841 956 Perl_ck_warner_d(aTHX_ packWARN(WARN_INPLACE),
147e3846 957 "Can't rename %s to %" SVf ": %s, skipping file",
9b387841 958 PL_oldname, SVfARG(sv), Strerror(errno) );
79072805 959 do_close(gv,FALSE);
c623bd54
LW
960 continue;
961 }
b28d0864 962 (void)UNLINK(PL_oldname);
a687059c
LW
963#endif
964 }
965 else {
91e4b3b3 966#if !defined(DOSISH) && !defined(__amigaos4__)
edc7bc49 967# ifndef VMS /* Don't delete; use automatic file versioning */
3280af22 968 if (UNLINK(PL_oldname) < 0) {
9b387841
NC
969 Perl_ck_warner_d(aTHX_ packWARN(WARN_INPLACE),
970 "Can't remove %s: %s, skipping file",
971 PL_oldname, Strerror(errno) );
79072805 972 do_close(gv,FALSE);
fe14fcc3
LW
973 continue;
974 }
edc7bc49 975# endif
ff8e2863 976#else
cea2e8a9 977 Perl_croak(aTHX_ "Can't do inplace edit without backup");
ff8e2863 978#endif
a687059c
LW
979 }
980
30fc4309 981 sv_setpvn(sv,PL_oldname,oldlen);
748a9306 982 SETERRNO(0,0); /* in case sprintf set errno */
d5eb9a46
NC
983 if (!Perl_do_open_raw(aTHX_ PL_argvoutgv, SvPVX_const(sv),
984 SvCUR(sv),
4119ab01 985#ifdef VMS
d5eb9a46 986 O_WRONLY|O_CREAT|O_TRUNC, 0
4119ab01 987#else
d5eb9a46 988 O_WRONLY|O_CREAT|OPEN_EXCL, 0600
4119ab01 989#endif
d5eb9a46 990 )) {
9b387841
NC
991 Perl_ck_warner_d(aTHX_ packWARN(WARN_INPLACE), "Can't do inplace edit on %s: %s",
992 PL_oldname, Strerror(errno) );
79072805 993 do_close(gv,FALSE);
fe14fcc3
LW
994 continue;
995 }
3280af22
NIS
996 setdefout(PL_argvoutgv);
997 PL_lastfd = PerlIO_fileno(IoIFP(GvIOp(PL_argvoutgv)));
375ed12a 998 if (PL_lastfd >= 0) {
45a23732 999 (void)PerlLIO_fstat(PL_lastfd,&statbuf);
fe14fcc3 1000#ifdef HAS_FCHMOD
375ed12a 1001 (void)fchmod(PL_lastfd,PL_filemode);
a687059c 1002#else
375ed12a 1003 (void)PerlLIO_chmod(PL_oldname,PL_filemode);
a687059c 1004#endif
45a23732 1005 if (fileuid != statbuf.st_uid || filegid != statbuf.st_gid) {
b469f1e0 1006 /* XXX silently ignore failures */
fe14fcc3 1007#ifdef HAS_FCHOWN
b469f1e0 1008 PERL_UNUSED_RESULT(fchown(PL_lastfd,fileuid,filegid));
a687059c 1009#else
fe14fcc3 1010#ifdef HAS_CHOWN
b469f1e0 1011 PERL_UNUSED_RESULT(PerlLIO_chown(PL_oldname,fileuid,filegid));
a687059c 1012#endif
b1248f16 1013#endif
375ed12a 1014 }
fe14fcc3 1015 }
d8015975 1016 return IoIFP(GvIOp(gv));
a687059c 1017 }
d8015975
NC
1018 } /* successful do_open_raw(), PL_inplace non-NULL */
1019
1020 if (ckWARN_d(WARN_INPLACE)) {
1021 const int eno = errno;
45a23732
DD
1022 if (PerlLIO_stat(PL_oldname, &statbuf) >= 0
1023 && !S_ISREG(statbuf.st_mode)) {
d8015975
NC
1024 Perl_warner(aTHX_ packWARN(WARN_INPLACE),
1025 "Can't do inplace edit: %s is not a regular file",
1026 PL_oldname);
1027 }
1028 else {
1029 Perl_warner(aTHX_ packWARN(WARN_INPLACE), "Can't open %s: %s",
1030 PL_oldname, Strerror(eno));
1031 }
4d61ec05 1032 }
a687059c 1033 }
18708f5a
GS
1034 if (io && (IoFLAGS(io) & IOf_ARGV))
1035 IoFLAGS(io) |= IOf_START;
3280af22 1036 if (PL_inplace) {
502aca56
TC
1037 if (old_out_name) {
1038 IO * const io = GvIOp(PL_argvoutgv);
1039 if (io && IoIFP(io) && !io_close(io, PL_argvoutgv, FALSE, FALSE)) {
147e3846 1040 Perl_croak(aTHX_ "Failed to close in-place edit file %" SVf ": %s\n",
502aca56
TC
1041 old_out_name, Strerror(errno));
1042 }
1043 }
1044 else {
1045 /* maybe this is no longer wanted */
1046 (void)do_close(PL_argvoutgv,FALSE);
1047 }
7a1c5554
GS
1048 if (io && (IoFLAGS(io) & IOf_ARGV)
1049 && PL_argvout_stack && AvFILLp(PL_argvout_stack) >= 0)
1050 {
159b6efe 1051 GV * const oldout = MUTABLE_GV(av_pop(PL_argvout_stack));
18708f5a 1052 setdefout(oldout);
8e217d4a 1053 SvREFCNT_dec_NN(oldout);
4608196e 1054 return NULL;
18708f5a 1055 }
fafc274c 1056 setdefout(gv_fetchpvs("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO));
a687059c 1057 }
4608196e 1058 return NULL;
a687059c
LW
1059}
1060
517844ec 1061/* explicit renamed to avoid C++ conflict -- kja */
a687059c 1062bool
864dbfa3 1063Perl_do_close(pTHX_ GV *gv, bool not_implicit)
a687059c 1064{
1193dd27
IZ
1065 bool retval;
1066 IO *io;
a687059c 1067
79072805 1068 if (!gv)
3280af22 1069 gv = PL_argvgv;
6e592b3a 1070 if (!gv || !isGV_with_GP(gv)) {
1d2dff63 1071 if (not_implicit)
93189314 1072 SETERRNO(EBADF,SS_IVCHAN);
c2ab57d4 1073 return FALSE;
99b89507 1074 }
79072805
LW
1075 io = GvIO(gv);
1076 if (!io) { /* never opened */
1d2dff63 1077 if (not_implicit) {
51087808 1078 report_evil_fh(gv);
93189314 1079 SETERRNO(EBADF,SS_IVCHAN);
1d2dff63 1080 }
a687059c
LW
1081 return FALSE;
1082 }
96d7c888 1083 retval = io_close(io, NULL, not_implicit, FALSE);
517844ec 1084 if (not_implicit) {
1193dd27
IZ
1085 IoLINES(io) = 0;
1086 IoPAGE(io) = 0;
1087 IoLINES_LEFT(io) = IoPAGE_LEN(io);
1088 }
50952442 1089 IoTYPE(io) = IoTYPE_CLOSED;
1193dd27
IZ
1090 return retval;
1091}
1092
1093bool
96d7c888 1094Perl_io_close(pTHX_ IO *io, GV *gv, bool not_implicit, bool warn_on_fail)
1193dd27
IZ
1095{
1096 bool retval = FALSE;
1193dd27 1097
7918f24d
NC
1098 PERL_ARGS_ASSERT_IO_CLOSE;
1099
8990e307 1100 if (IoIFP(io)) {
50952442 1101 if (IoTYPE(io) == IoTYPE_PIPE) {
4373e329 1102 const int status = PerlProc_pclose(IoIFP(io));
f2b5be74 1103 if (not_implicit) {
37038d91 1104 STATUS_NATIVE_CHILD_SET(status);
e5218da5 1105 retval = (STATUS_UNIX == 0);
f2b5be74
GS
1106 }
1107 else {
1108 retval = (status != -1);
1109 }
a687059c 1110 }
50952442 1111 else if (IoTYPE(io) == IoTYPE_STD)
a687059c
LW
1112 retval = TRUE;
1113 else {
8990e307 1114 if (IoOFP(io) && IoOFP(io) != IoIFP(io)) { /* a socket */
0bcc34c2 1115 const bool prev_err = PerlIO_error(IoOFP(io));
f4725fad
FC
1116#ifdef USE_PERLIO
1117 if (prev_err)
1118 PerlIO_restore_errno(IoOFP(io));
1119#endif
e199e3be 1120 retval = (PerlIO_close(IoOFP(io)) != EOF && !prev_err);
760ac839 1121 PerlIO_close(IoIFP(io)); /* clear stdio, fd already closed */
c2ab57d4 1122 }
e199e3be 1123 else {
0bcc34c2 1124 const bool prev_err = PerlIO_error(IoIFP(io));
f4725fad
FC
1125#ifdef USE_PERLIO
1126 if (prev_err)
1127 PerlIO_restore_errno(IoIFP(io));
1128#endif
e199e3be
RGS
1129 retval = (PerlIO_close(IoIFP(io)) != EOF && !prev_err);
1130 }
a687059c 1131 }
4608196e 1132 IoOFP(io) = IoIFP(io) = NULL;
96d7c888
FC
1133
1134 if (warn_on_fail && !retval) {
1135 if (gv)
1136 Perl_ck_warner_d(aTHX_ packWARN(WARN_IO),
1137 "Warning: unable to close filehandle %"
147e3846 1138 HEKf " properly: %" SVf,
ac892e4a
DM
1139 HEKfARG(GvNAME_HEK(gv)),
1140 SVfARG(get_sv("!",GV_ADD)));
96d7c888
FC
1141 else
1142 Perl_ck_warner_d(aTHX_ packWARN(WARN_IO),
1143 "Warning: unable to close filehandle "
147e3846 1144 "properly: %" SVf,
ac892e4a 1145 SVfARG(get_sv("!",GV_ADD)));
96d7c888 1146 }
79072805 1147 }
f2b5be74 1148 else if (not_implicit) {
93189314 1149 SETERRNO(EBADF,SS_IVCHAN);
20408e3c 1150 }
1193dd27 1151
a687059c
LW
1152 return retval;
1153}
1154
1155bool
864dbfa3 1156Perl_do_eof(pTHX_ GV *gv)
a687059c 1157{
eb578fdb 1158 IO * const io = GvIO(gv);
a687059c 1159
7918f24d
NC
1160 PERL_ARGS_ASSERT_DO_EOF;
1161
79072805 1162 if (!io)
a687059c 1163 return TRUE;
7716c5c5 1164 else if (IoTYPE(io) == IoTYPE_WRONLY)
a5390457 1165 report_wrongway_fh(gv, '>');
a687059c 1166
8990e307 1167 while (IoIFP(io)) {
760ac839 1168 if (PerlIO_has_cntptr(IoIFP(io))) { /* (the code works without this) */
a20bf0c3 1169 if (PerlIO_get_cnt(IoIFP(io)) > 0) /* cheat a little, since */
760ac839
LW
1170 return FALSE; /* this is the most usual case */
1171 }
a687059c 1172
79852593
NC
1173 {
1174 /* getc and ungetc can stomp on errno */
4ee39169 1175 dSAVE_ERRNO;
79852593
NC
1176 const int ch = PerlIO_getc(IoIFP(io));
1177 if (ch != EOF) {
1178 (void)PerlIO_ungetc(IoIFP(io),ch);
4ee39169 1179 RESTORE_ERRNO;
79852593
NC
1180 return FALSE;
1181 }
4ee39169 1182 RESTORE_ERRNO;
a687059c 1183 }
fab3f3a7 1184
760ac839 1185 if (PerlIO_has_cntptr(IoIFP(io)) && PerlIO_canset_cnt(IoIFP(io))) {
a20bf0c3
JH
1186 if (PerlIO_get_cnt(IoIFP(io)) < -1)
1187 PerlIO_set_cnt(IoIFP(io),-1);
760ac839 1188 }
533c011a 1189 if (PL_op->op_flags & OPf_SPECIAL) { /* not necessarily a real EOF yet? */
157fb5a1 1190 if (gv != PL_argvgv || !nextargv(gv, FALSE)) /* get another fp handy */
a687059c
LW
1191 return TRUE;
1192 }
1193 else
1194 return TRUE; /* normal fp, definitely end of file */
1195 }
1196 return TRUE;
1197}
1198
5ff3f7a4 1199Off_t
864dbfa3 1200Perl_do_tell(pTHX_ GV *gv)
a687059c 1201{
9c9f25b8 1202 IO *const io = GvIO(gv);
eb578fdb 1203 PerlIO *fp;
a687059c 1204
7918f24d
NC
1205 PERL_ARGS_ASSERT_DO_TELL;
1206
9c9f25b8 1207 if (io && (fp = IoIFP(io))) {
8903cb82 1208 return PerlIO_tell(fp);
96e4d5b1 1209 }
51087808 1210 report_evil_fh(gv);
93189314 1211 SETERRNO(EBADF,RMS_IFI);
5ff3f7a4 1212 return (Off_t)-1;
a687059c
LW
1213}
1214
1215bool
864dbfa3 1216Perl_do_seek(pTHX_ GV *gv, Off_t pos, int whence)
a687059c 1217{
9c9f25b8 1218 IO *const io = GvIO(gv);
eb578fdb 1219 PerlIO *fp;
a687059c 1220
9c9f25b8 1221 if (io && (fp = IoIFP(io))) {
8903cb82 1222 return PerlIO_seek(fp, pos, whence) >= 0;
137443ea 1223 }
51087808 1224 report_evil_fh(gv);
93189314 1225 SETERRNO(EBADF,RMS_IFI);
a687059c
LW
1226 return FALSE;
1227}
1228
97cc44eb 1229Off_t
864dbfa3 1230Perl_do_sysseek(pTHX_ GV *gv, Off_t pos, int whence)
8903cb82 1231{
9c9f25b8 1232 IO *const io = GvIO(gv);
eb578fdb 1233 PerlIO *fp;
8903cb82 1234
7918f24d
NC
1235 PERL_ARGS_ASSERT_DO_SYSSEEK;
1236
375ed12a
JH
1237 if (io && (fp = IoIFP(io))) {
1238 int fd = PerlIO_fileno(fp);
07bd88da
JH
1239 if (fd < 0 || (whence == SEEK_SET && pos < 0)) {
1240 SETERRNO(EINVAL,LIB_INVARG);
1241 return -1;
1242 } else {
375ed12a
JH
1243 return PerlLIO_lseek(fd, pos, whence);
1244 }
1245 }
51087808 1246 report_evil_fh(gv);
93189314 1247 SETERRNO(EBADF,RMS_IFI);
d9b3e12d 1248 return (Off_t)-1;
8903cb82 1249}
1250
6ff81951 1251int
a79b25b7 1252Perl_mode_from_discipline(pTHX_ const char *s, STRLEN len)
16fe6d59
GS
1253{
1254 int mode = O_BINARY;
81611534 1255 PERL_UNUSED_CONTEXT;
a79b25b7 1256 if (s) {
16fe6d59
GS
1257 while (*s) {
1258 if (*s == ':') {
1259 switch (s[1]) {
1260 case 'r':
e963d6d2 1261 if (s[2] == 'a' && s[3] == 'w'
16fe6d59
GS
1262 && (!s[4] || s[4] == ':' || isSPACE(s[4])))
1263 {
1264 mode = O_BINARY;
1265 s += 4;
1266 len -= 4;
1267 break;
1268 }
924ba076 1269 /* FALLTHROUGH */
16fe6d59 1270 case 'c':
e963d6d2 1271 if (s[2] == 'r' && s[3] == 'l' && s[4] == 'f'
16fe6d59
GS
1272 && (!s[5] || s[5] == ':' || isSPACE(s[5])))
1273 {
1274 mode = O_TEXT;
1275 s += 5;
1276 len -= 5;
1277 break;
1278 }
924ba076 1279 /* FALLTHROUGH */
16fe6d59
GS
1280 default:
1281 goto fail_discipline;
1282 }
1283 }
1284 else if (isSPACE(*s)) {
1285 ++s;
1286 --len;
1287 }
1288 else {
4373e329 1289 const char *end;
7b52d656 1290 fail_discipline:
16fe6d59
GS
1291 end = strchr(s+1, ':');
1292 if (!end)
1293 end = s+len;
60382766 1294#ifndef PERLIO_LAYERS
363c40c4 1295 Perl_croak(aTHX_ "IO layers (like '%.*s') unavailable", end-s, s);
60382766 1296#else
18a33fb5 1297 len -= end-s;
60382766
NIS
1298 s = end;
1299#endif
16fe6d59
GS
1300 }
1301 }
1302 }
1303 return mode;
1304}
1305
58e24eff 1306#if !defined(HAS_TRUNCATE) && !defined(HAS_CHSIZE)
27da23d5
JH
1307I32
1308my_chsize(int fd, Off_t length)
6eb13c3b 1309{
58e24eff
SH
1310#ifdef F_FREESP
1311 /* code courtesy of William Kucharski */
1312#define HAS_CHSIZE
1313
c623ac67 1314 Stat_t filebuf;
6eb13c3b 1315
3028581b 1316 if (PerlLIO_fstat(fd, &filebuf) < 0)
6eb13c3b
LW
1317 return -1;
1318
1319 if (filebuf.st_size < length) {
1320
1321 /* extend file length */
1322
3028581b 1323 if ((PerlLIO_lseek(fd, (length - 1), 0)) < 0)
6eb13c3b
LW
1324 return -1;
1325
1326 /* write a "0" byte */
1327
3028581b 1328 if ((PerlLIO_write(fd, "", 1)) != 1)
6eb13c3b
LW
1329 return -1;
1330 }
1331 else {
1332 /* truncate length */
35da51f7 1333 struct flock fl;
6eb13c3b
LW
1334 fl.l_whence = 0;
1335 fl.l_len = 0;
1336 fl.l_start = length;
a0d0e21e 1337 fl.l_type = F_WRLCK; /* write lock on file space */
6eb13c3b
LW
1338
1339 /*
a0d0e21e 1340 * This relies on the UNDOCUMENTED F_FREESP argument to
6eb13c3b
LW
1341 * fcntl(2), which truncates the file so that it ends at the
1342 * position indicated by fl.l_start.
1343 *
1344 * Will minor miracles never cease?
1345 */
1346
a0d0e21e 1347 if (fcntl(fd, F_FREESP, &fl) < 0)
6eb13c3b
LW
1348 return -1;
1349
1350 }
6eb13c3b 1351 return 0;
58e24eff 1352#else
27da23d5 1353 Perl_croak_nocontext("truncate not implemented");
a0d0e21e 1354#endif /* F_FREESP */
27da23d5 1355 return -1;
58e24eff
SH
1356}
1357#endif /* !HAS_TRUNCATE && !HAS_CHSIZE */
ff8e2863 1358
a687059c 1359bool
5aaab254 1360Perl_do_print(pTHX_ SV *sv, PerlIO *fp)
a687059c 1361{
7918f24d
NC
1362 PERL_ARGS_ASSERT_DO_PRINT;
1363
79072805
LW
1364 /* assuming fp is checked earlier */
1365 if (!sv)
1366 return TRUE;
e9950d3b
NC
1367 if (SvTYPE(sv) == SVt_IV && SvIOK(sv)) {
1368 assert(!SvGMAGICAL(sv));
1369 if (SvIsUV(sv))
147e3846 1370 PerlIO_printf(fp, "%" UVuf, (UV)SvUVX(sv));
e9950d3b 1371 else
147e3846 1372 PerlIO_printf(fp, "%" IVdf, (IV)SvIVX(sv));
e9950d3b
NC
1373 return !PerlIO_error(fp);
1374 }
1375 else {
1376 STRLEN len;
676f44e7 1377 /* Do this first to trigger any overloading. */
e9950d3b
NC
1378 const char *tmps = SvPV_const(sv, len);
1379 U8 *tmpbuf = NULL;
1380 bool happy = TRUE;
1381
d791f93f
KW
1382 if (PerlIO_isutf8(fp)) { /* If the stream is utf8 ... */
1383 if (!SvUTF8(sv)) { /* Convert to utf8 if necessary */
676f44e7
NC
1384 /* We don't modify the original scalar. */
1385 tmpbuf = bytes_to_utf8((const U8*) tmps, &len);
1386 tmps = (char *) tmpbuf;
1387 }
a099aed4 1388 else if (ckWARN4_d(WARN_UTF8, WARN_SURROGATE, WARN_NON_UNICODE, WARN_NONCHAR)) {
0876b9a0
KW
1389 (void) check_utf8_print((const U8*) tmps, len);
1390 }
d791f93f
KW
1391 } /* else stream isn't utf8 */
1392 else if (DO_UTF8(sv)) { /* But if is utf8 internally, attempt to
1393 convert to bytes */
676f44e7
NC
1394 STRLEN tmplen = len;
1395 bool utf8 = TRUE;
35da51f7 1396 U8 * const result = bytes_from_utf8((const U8*) tmps, &tmplen, &utf8);
676f44e7 1397 if (!utf8) {
d791f93f
KW
1398
1399 /* Here, succeeded in downgrading from utf8. Set up to below
1400 * output the converted value */
676f44e7
NC
1401 tmpbuf = result;
1402 tmps = (char *) tmpbuf;
1403 len = tmplen;
1404 }
d791f93f
KW
1405 else { /* Non-utf8 output stream, but string only representable in
1406 utf8 */
676f44e7 1407 assert((char *)result == tmps);
9b387841 1408 Perl_ck_warner_d(aTHX_ packWARN(WARN_UTF8),
21630838
FC
1409 "Wide character in %s",
1410 PL_op ? OP_DESC(PL_op) : "print"
1411 );
0876b9a0
KW
1412 /* Could also check that isn't one of the things to avoid
1413 * in utf8 by using check_utf8_print(), but not doing so,
1414 * since the stream isn't a UTF8 stream */
ae798467
NIS
1415 }
1416 }
e9950d3b
NC
1417 /* To detect whether the process is about to overstep its
1418 * filesize limit we would need getrlimit(). We could then
1419 * also transparently raise the limit with setrlimit() --
1420 * but only until the system hard limit/the filesystem limit,
1421 * at which we would get EPERM. Note that when using buffered
1422 * io the write failure can be delayed until the flush/close. --jhi */
1423 if (len && (PerlIO_write(fp,tmps,len) == 0))
1424 happy = FALSE;
1425 Safefree(tmpbuf);
1426 return happy ? !PerlIO_error(fp) : FALSE;
ff8e2863 1427 }
a687059c
LW
1428}
1429
79072805 1430I32
0d7d409d 1431Perl_my_stat_flags(pTHX_ const U32 flags)
a687059c 1432{
39644a26 1433 dSP;
79072805 1434 IO *io;
2dd78f96 1435 GV* gv;
79072805 1436
533c011a 1437 if (PL_op->op_flags & OPf_REF) {
2dd78f96 1438 gv = cGVOP_gv;
748a9306 1439 do_fstat:
5228a96c
SP
1440 if (gv == PL_defgv)
1441 return PL_laststatval;
2dd78f96 1442 io = GvIO(gv);
ad02613c 1443 do_fstat_have_io:
5228a96c 1444 PL_laststype = OP_STAT;
bd5f6c01 1445 PL_statgv = gv ? gv : (GV *)io;
8062ff11 1446 SvPVCLEAR(PL_statname);
77616968 1447 if (io) {
5228a96c 1448 if (IoIFP(io)) {
375ed12a 1449 int fd = PerlIO_fileno(IoIFP(io));
77616968
JH
1450 if (fd < 0) {
1451 /* E.g. PerlIO::scalar has no real fd. */
1452 return (PL_laststatval = -1);
1453 } else {
375ed12a
JH
1454 return (PL_laststatval = PerlLIO_fstat(fd, &PL_statcache));
1455 }
5228a96c 1456 } else if (IoDIRP(io)) {
3497a01f 1457 return (PL_laststatval = PerlLIO_fstat(my_dirfd(IoDIRP(io)), &PL_statcache));
5228a96c 1458 }
5228a96c 1459 }
3888144c
FC
1460 PL_laststatval = -1;
1461 report_evil_fh(gv);
1462 return -1;
a687059c 1463 }
d2c4d2d1 1464 else if ((PL_op->op_private & (OPpFT_STACKED|OPpFT_AFTER_t))
8db8f6b6
FC
1465 == OPpFT_STACKED)
1466 return PL_laststatval;
d2c4d2d1
FC
1467 else {
1468 SV* const sv = TOPs;
4373e329 1469 const char *s;
4ecd490c 1470 STRLEN len;
094a3eec 1471 if ((gv = MAYBE_DEREF_GV_flags(sv,flags))) {
748a9306
LW
1472 goto do_fstat;
1473 }
ad02613c 1474 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVIO) {
a45c7426 1475 io = MUTABLE_IO(SvRV(sv));
7f39519f 1476 gv = NULL;
ad02613c
SP
1477 goto do_fstat_have_io;
1478 }
748a9306 1479
0d7d409d 1480 s = SvPV_flags_const(sv, len, flags);
a0714e2c 1481 PL_statgv = NULL;
4ecd490c 1482 sv_setpvn(PL_statname, s, len);
95a20fc0 1483 s = SvPVX_const(PL_statname); /* s now NUL-terminated */
3280af22
NIS
1484 PL_laststype = OP_STAT;
1485 PL_laststatval = PerlLIO_stat(s, &PL_statcache);
7cb3f959 1486 if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && should_warn_nl(s)) {
5d37acd6 1487 GCC_DIAG_IGNORE(-Wformat-nonliteral); /* PL_warn_nl is constant */
9014280d 1488 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "stat");
5d37acd6
DM
1489 GCC_DIAG_RESTORE;
1490 }
3280af22 1491 return PL_laststatval;
a687059c
LW
1492 }
1493}
1494
fbb0b3b3 1495
79072805 1496I32
0d7d409d 1497Perl_my_lstat_flags(pTHX_ const U32 flags)
c623bd54 1498{
a1894d81 1499 static const char* const no_prev_lstat = "The stat preceding -l _ wasn't an lstat";
39644a26 1500 dSP;
b16276bb 1501 const char *file;
cd22fad3 1502 SV* const sv = TOPs;
5840701a 1503 bool isio = FALSE;
533c011a 1504 if (PL_op->op_flags & OPf_REF) {
638eceb6 1505 if (cGVOP_gv == PL_defgv) {
3280af22 1506 if (PL_laststype != OP_LSTAT)
0157ef98 1507 Perl_croak(aTHX_ "%s", no_prev_lstat);
3280af22 1508 return PL_laststatval;
fe14fcc3 1509 }
31b139ba 1510 PL_laststatval = -1;
5d3e98de 1511 if (ckWARN(WARN_IO)) {
5840701a 1512 /* diag_listed_as: Use of -l on filehandle%s */
d0c0e7dd 1513 Perl_warner(aTHX_ packWARN(WARN_IO),
147e3846 1514 "Use of -l on filehandle %" HEKf,
d0c0e7dd 1515 HEKfARG(GvENAME_HEK(cGVOP_gv)));
5d3e98de 1516 }
31b139ba 1517 return -1;
fe14fcc3 1518 }
8db8f6b6
FC
1519 if ((PL_op->op_private & (OPpFT_STACKED|OPpFT_AFTER_t))
1520 == OPpFT_STACKED) {
1f26655e 1521 if (PL_laststype != OP_LSTAT)
0157ef98 1522 Perl_croak(aTHX_ "%s", no_prev_lstat);
1f26655e 1523 return PL_laststatval;
cd22fad3 1524 }
c623bd54 1525
3280af22 1526 PL_laststype = OP_LSTAT;
a0714e2c 1527 PL_statgv = NULL;
5840701a
FC
1528 if ( ( (SvROK(sv) && ( isGV_with_GP(SvRV(sv))
1529 || (isio = SvTYPE(SvRV(sv)) == SVt_PVIO) )
1530 )
1531 || isGV_with_GP(sv)
1532 )
1533 && ckWARN(WARN_IO)) {
1534 if (isio)
1535 /* diag_listed_as: Use of -l on filehandle%s */
1536 Perl_warner(aTHX_ packWARN(WARN_IO),
1537 "Use of -l on filehandle");
1538 else
1539 /* diag_listed_as: Use of -l on filehandle%s */
1540 Perl_warner(aTHX_ packWARN(WARN_IO),
147e3846 1541 "Use of -l on filehandle %" HEKf,
10bafe90
BF
1542 HEKfARG(GvENAME_HEK((const GV *)
1543 (SvROK(sv) ? SvRV(sv) : sv))));
cd22fad3
RS
1544 }
1545 file = SvPV_flags_const_nolen(sv, flags);
b16276bb
VP
1546 sv_setpv(PL_statname,file);
1547 PL_laststatval = PerlLIO_lstat(file,&PL_statcache);
7cb3f959 1548 if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && should_warn_nl(file)) {
5d37acd6
DM
1549 GCC_DIAG_IGNORE(-Wformat-nonliteral); /* PL_warn_nl is constant */
1550 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "lstat");
1551 GCC_DIAG_RESTORE;
1552 }
3280af22 1553 return PL_laststatval;
c623bd54
LW
1554}
1555
a0f2c8ec
JD
1556static void
1557S_exec_failed(pTHX_ const char *cmd, int fd, int do_report)
1558{
1559 const int e = errno;
7918f24d 1560 PERL_ARGS_ASSERT_EXEC_FAILED;
738ab09f
AB
1561
1562 if (ckWARN(WARN_EXEC))
1563 Perl_warner(aTHX_ packWARN(WARN_EXEC), "Can't exec \"%s\": %s",
1564 cmd, Strerror(e));
a0f2c8ec 1565 if (do_report) {
b469f1e0
JH
1566 /* XXX silently ignore failures */
1567 PERL_UNUSED_RESULT(PerlLIO_write(fd, (void*)&e, sizeof(int)));
a0f2c8ec
JD
1568 PerlLIO_close(fd);
1569 }
1570}
1571
738ab09f 1572bool
5aaab254 1573Perl_do_aexec5(pTHX_ SV *really, SV **mark, SV **sp,
2aa1486d 1574 int fd, int do_report)
d5a9bfb0 1575{
27da23d5 1576 dVAR;
7918f24d 1577 PERL_ARGS_ASSERT_DO_AEXEC5;
e37778c2 1578#if defined(__SYMBIAN32__) || defined(__LIBCATAMOUNT__)
cd39f2b6
JH
1579 Perl_croak(aTHX_ "exec? I'm not *that* kind of operating system");
1580#else
79072805 1581 if (sp > mark) {
360ea906 1582 const char **a;
6136c704 1583 const char *tmps = NULL;
360ea906 1584 Newx(PL_Argv, sp - mark + 1, const char*);
c3c5fad7 1585 a = PL_Argv;
890ce7af 1586
79072805
LW
1587 while (++mark <= sp) {
1588 if (*mark)
360ea906 1589 *a++ = SvPV_nolen_const(*mark);
a687059c
LW
1590 else
1591 *a++ = "";
1592 }
6136c704 1593 *a = NULL;
91b2752f 1594 if (really)
e62f0680 1595 tmps = SvPV_nolen_const(really);
91b2752f
RG
1596 if ((!really && *PL_Argv[0] != '/') ||
1597 (really && *tmps != '/')) /* will execvp use PATH? */
79072805 1598 TAINT_ENV(); /* testing IFS here is overkill, probably */
b35112e7 1599 PERL_FPU_PRE_EXEC
839a9f02 1600 if (really && *tmps) {
738ab09f 1601 PerlProc_execvp(tmps,EXEC_ARGV_CAST(PL_Argv));
839a9f02 1602 } else {
738ab09f 1603 PerlProc_execvp(PL_Argv[0],EXEC_ARGV_CAST(PL_Argv));
839a9f02 1604 }
b35112e7 1605 PERL_FPU_POST_EXEC
a0f2c8ec 1606 S_exec_failed(aTHX_ (really ? tmps : PL_Argv[0]), fd, do_report);
a687059c 1607 }
bee1dbe2 1608 do_execfree();
cd39f2b6 1609#endif
738ab09f 1610 return FALSE;
a687059c
LW
1611}
1612
fe14fcc3 1613void
864dbfa3 1614Perl_do_execfree(pTHX)
ff8e2863 1615{
43c5f42d 1616 Safefree(PL_Argv);
4608196e 1617 PL_Argv = NULL;
43c5f42d 1618 Safefree(PL_Cmd);
bd61b366 1619 PL_Cmd = NULL;
ff8e2863
LW
1620}
1621
9555a685 1622#ifdef PERL_DEFAULT_DO_EXEC3_IMPLEMENTATION
e446cec8 1623
738ab09f 1624bool
2fbb330f 1625Perl_do_exec3(pTHX_ const char *incmd, int fd, int do_report)
e446cec8 1626{
27da23d5 1627 dVAR;
eb578fdb
KW
1628 const char **a;
1629 char *s;
15db3ae2 1630 char *buf;
2fbb330f 1631 char *cmd;
2fbb330f 1632 /* Make a copy so we can change it */
6fca0082 1633 const Size_t cmdlen = strlen(incmd) + 1;
7918f24d
NC
1634
1635 PERL_ARGS_ASSERT_DO_EXEC3;
1636
15db3ae2
DM
1637 Newx(buf, cmdlen, char);
1638 cmd = buf;
cfff9797 1639 memcpy(cmd, incmd, cmdlen);
a687059c 1640
748a9306
LW
1641 while (*cmd && isSPACE(*cmd))
1642 cmd++;
1643
a687059c
LW
1644 /* save an extra exec if possible */
1645
bf38876a 1646#ifdef CSH
d05c1ba0 1647 {
0c19750d 1648 char flags[PERL_FLAGS_MAX];
d05c1ba0 1649 if (strnEQ(cmd,PL_cshname,PL_cshlen) &&
8062ff11 1650 strEQs(cmd+PL_cshlen," -c")) {
28f0d0ec 1651 my_strlcpy(flags, "-c", PERL_FLAGS_MAX);
d05c1ba0
JH
1652 s = cmd+PL_cshlen+3;
1653 if (*s == 'f') {
1654 s++;
28f0d0ec 1655 my_strlcat(flags, "f", PERL_FLAGS_MAX - 2);
d05c1ba0
JH
1656 }
1657 if (*s == ' ')
1658 s++;
1659 if (*s++ == '\'') {
0bcc34c2 1660 char * const ncmd = s;
d05c1ba0
JH
1661
1662 while (*s)
1663 s++;
1664 if (s[-1] == '\n')
1665 *--s = '\0';
1666 if (s[-1] == '\'') {
1667 *--s = '\0';
b35112e7 1668 PERL_FPU_PRE_EXEC
738ab09f 1669 PerlProc_execl(PL_cshname, "csh", flags, ncmd, (char*)NULL);
b35112e7 1670 PERL_FPU_POST_EXEC
d05c1ba0 1671 *s = '\'';
a0f2c8ec 1672 S_exec_failed(aTHX_ PL_cshname, fd, do_report);
15db3ae2 1673 Safefree(buf);
738ab09f 1674 return FALSE;
d05c1ba0
JH
1675 }
1676 }
a687059c
LW
1677 }
1678 }
bf38876a 1679#endif /* CSH */
a687059c
LW
1680
1681 /* see if there are shell metacharacters in it */
1682
748a9306
LW
1683 if (*cmd == '.' && isSPACE(cmd[1]))
1684 goto doshell;
1685
8062ff11 1686 if (strEQs(cmd,"exec") && isSPACE(cmd[4]))
748a9306
LW
1687 goto doshell;
1688
294b3b39 1689 s = cmd;
0eb30aeb 1690 while (isWORDCHAR(*s))
294b3b39 1691 s++; /* catch VAR=val gizmo */
63f2c1e1
LW
1692 if (*s == '=')
1693 goto doshell;
748a9306 1694
a687059c 1695 for (s = cmd; *s; s++) {
d05c1ba0
JH
1696 if (*s != ' ' && !isALPHA(*s) &&
1697 strchr("$&*(){}[]'\";\\|?<>~`\n",*s)) {
a687059c
LW
1698 if (*s == '\n' && !s[1]) {
1699 *s = '\0';
1700 break;
1701 }
603a98b0
IZ
1702 /* handle the 2>&1 construct at the end */
1703 if (*s == '>' && s[1] == '&' && s[2] == '1'
1704 && s > cmd + 1 && s[-1] == '2' && isSPACE(s[-2])
1705 && (!s[3] || isSPACE(s[3])))
1706 {
6867be6d 1707 const char *t = s + 3;
603a98b0
IZ
1708
1709 while (*t && isSPACE(*t))
1710 ++t;
943bbd07 1711 if (!*t && (PerlLIO_dup2(1,2) != -1)) {
603a98b0
IZ
1712 s[-2] = '\0';
1713 break;
1714 }
1715 }
a687059c 1716 doshell:
b35112e7 1717 PERL_FPU_PRE_EXEC
738ab09f 1718 PerlProc_execl(PL_sh_path, "sh", "-c", cmd, (char *)NULL);
b35112e7 1719 PERL_FPU_POST_EXEC
a0f2c8ec 1720 S_exec_failed(aTHX_ PL_sh_path, fd, do_report);
15db3ae2 1721 Safefree(buf);
738ab09f 1722 return FALSE;
a687059c
LW
1723 }
1724 }
748a9306 1725
360ea906 1726 Newx(PL_Argv, (s - cmd) / 2 + 2, const char*);
3280af22
NIS
1727 PL_Cmd = savepvn(cmd, s-cmd);
1728 a = PL_Argv;
1729 for (s = PL_Cmd; *s;) {
294b3b39
AL
1730 while (isSPACE(*s))
1731 s++;
a687059c
LW
1732 if (*s)
1733 *(a++) = s;
294b3b39
AL
1734 while (*s && !isSPACE(*s))
1735 s++;
a687059c
LW
1736 if (*s)
1737 *s++ = '\0';
1738 }
6136c704 1739 *a = NULL;
3280af22 1740 if (PL_Argv[0]) {
b35112e7 1741 PERL_FPU_PRE_EXEC
738ab09f 1742 PerlProc_execvp(PL_Argv[0],EXEC_ARGV_CAST(PL_Argv));
b35112e7 1743 PERL_FPU_POST_EXEC
b1248f16 1744 if (errno == ENOEXEC) { /* for system V NIH syndrome */
ff8e2863 1745 do_execfree();
a687059c 1746 goto doshell;
b1248f16 1747 }
a0f2c8ec 1748 S_exec_failed(aTHX_ PL_Argv[0], fd, do_report);
a687059c 1749 }
ff8e2863 1750 do_execfree();
15db3ae2 1751 Safefree(buf);
738ab09f 1752 return FALSE;
a687059c
LW
1753}
1754
6890e559 1755#endif /* OS2 || WIN32 */
760ac839 1756
79072805 1757I32
5aaab254 1758Perl_apply(pTHX_ I32 type, SV **mark, SV **sp)
a687059c 1759{
eb578fdb
KW
1760 I32 val;
1761 I32 tot = 0;
4634a855 1762 const char *const what = PL_op_name[type];
5c144d81 1763 const char *s;
84c7b88c 1764 STRLEN len;
890ce7af 1765 SV ** const oldmark = mark;
885b4b39 1766 bool killgp = FALSE;
a687059c 1767
7918f24d
NC
1768 PERL_ARGS_ASSERT_APPLY;
1769
9a9b5ec9
DM
1770 PERL_UNUSED_VAR(what); /* may not be used depending on compile options */
1771
1444765e
NC
1772 /* Doing this ahead of the switch statement preserves the old behaviour,
1773 where attempting to use kill as a taint test test would fail on
1774 platforms where kill was not defined. */
1775#ifndef HAS_KILL
1776 if (type == OP_KILL)
4634a855 1777 Perl_die(aTHX_ PL_no_func, what);
1444765e
NC
1778#endif
1779#ifndef HAS_CHOWN
1780 if (type == OP_CHOWN)
4634a855 1781 Perl_die(aTHX_ PL_no_func, what);
1444765e
NC
1782#endif
1783
1784
20408e3c 1785#define APPLY_TAINT_PROPER() \
3280af22 1786 STMT_START { \
284167a5 1787 if (TAINT_get) { TAINT_PROPER(what); } \
873ef191 1788 } STMT_END
20408e3c
GS
1789
1790 /* This is a first heuristic; it doesn't catch tainting magic. */
284167a5 1791 if (TAINTING_get) {
463ee0b2 1792 while (++mark <= sp) {
bbce6d69 1793 if (SvTAINTED(*mark)) {
1794 TAINT;
1795 break;
1796 }
463ee0b2
LW
1797 }
1798 mark = oldmark;
1799 }
a687059c 1800 switch (type) {
79072805 1801 case OP_CHMOD:
20408e3c 1802 APPLY_TAINT_PROPER();
79072805 1803 if (++mark <= sp) {
4ea561bc 1804 val = SvIV(*mark);
20408e3c
GS
1805 APPLY_TAINT_PROPER();
1806 tot = sp - mark;
79072805 1807 while (++mark <= sp) {
c4aca7d0 1808 GV* gv;
2ea1cce7 1809 if ((gv = MAYBE_DEREF_GV(*mark))) {
c4aca7d0
GA
1810 if (GvIO(gv) && IoIFP(GvIOp(gv))) {
1811#ifdef HAS_FCHMOD
375ed12a 1812 int fd = PerlIO_fileno(IoIFP(GvIOn(gv)));
c4aca7d0 1813 APPLY_TAINT_PROPER();
375ed12a
JH
1814 if (fd < 0) {
1815 SETERRNO(EBADF,RMS_IFI);
1816 tot--;
1817 } else if (fchmod(fd, val))
1818 tot--;
c4aca7d0 1819#else
b9c6780e 1820 Perl_die(aTHX_ PL_no_func, "fchmod");
c4aca7d0
GA
1821#endif
1822 }
1823 else {
8334cae6 1824 SETERRNO(EBADF,RMS_IFI);
c4aca7d0
GA
1825 tot--;
1826 }
1827 }
c4aca7d0 1828 else {
41188aa0 1829 const char *name = SvPV_nomg_const(*mark, len);
c4aca7d0 1830 APPLY_TAINT_PROPER();
41188aa0 1831 if (!IS_SAFE_PATHNAME(name, len, "chmod") ||
c8028aa6
TC
1832 PerlLIO_chmod(name, val)) {
1833 tot--;
1834 }
c4aca7d0 1835 }
a687059c
LW
1836 }
1837 }
1838 break;
fe14fcc3 1839#ifdef HAS_CHOWN
79072805 1840 case OP_CHOWN:
20408e3c 1841 APPLY_TAINT_PROPER();
79072805 1842 if (sp - mark > 2) {
eb578fdb 1843 I32 val2;
463ee0b2
LW
1844 val = SvIVx(*++mark);
1845 val2 = SvIVx(*++mark);
20408e3c 1846 APPLY_TAINT_PROPER();
a0d0e21e 1847 tot = sp - mark;
79072805 1848 while (++mark <= sp) {
c4aca7d0 1849 GV* gv;
2ea1cce7 1850 if ((gv = MAYBE_DEREF_GV(*mark))) {
c4aca7d0
GA
1851 if (GvIO(gv) && IoIFP(GvIOp(gv))) {
1852#ifdef HAS_FCHOWN
375ed12a 1853 int fd = PerlIO_fileno(IoIFP(GvIOn(gv)));
c4aca7d0 1854 APPLY_TAINT_PROPER();
375ed12a 1855 if (fd < 0) {
dd1dbff0 1856 SETERRNO(EBADF,RMS_IFI);
f95ba548 1857 tot--;
375ed12a 1858 } else if (fchown(fd, val, val2))
c4aca7d0
GA
1859 tot--;
1860#else
b9c6780e 1861 Perl_die(aTHX_ PL_no_func, "fchown");
c4aca7d0
GA
1862#endif
1863 }
1864 else {
8334cae6 1865 SETERRNO(EBADF,RMS_IFI);
c4aca7d0
GA
1866 tot--;
1867 }
1868 }
c4aca7d0 1869 else {
41188aa0 1870 const char *name = SvPV_nomg_const(*mark, len);
c4aca7d0 1871 APPLY_TAINT_PROPER();
41188aa0 1872 if (!IS_SAFE_PATHNAME(name, len, "chown") ||
c8028aa6 1873 PerlLIO_chown(name, val, val2)) {
c4aca7d0 1874 tot--;
c8028aa6 1875 }
c4aca7d0 1876 }
a687059c
LW
1877 }
1878 }
1879 break;
b1248f16 1880#endif
a1d180c4 1881/*
dd64f1c3
AD
1882XXX Should we make lchown() directly available from perl?
1883For now, we'll let Configure test for HAS_LCHOWN, but do
1884nothing in the core.
1885 --AD 5/1998
1886*/
fe14fcc3 1887#ifdef HAS_KILL
79072805 1888 case OP_KILL:
20408e3c 1889 APPLY_TAINT_PROPER();
55497cff 1890 if (mark == sp)
1891 break;
84c7b88c 1892 s = SvPVx_const(*++mark, len);
c2fd40cb
DM
1893 if (*s == '-' && isALPHA(s[1]))
1894 {
1895 s++;
1896 len--;
885b4b39 1897 killgp = TRUE;
c2fd40cb 1898 }
e02bfb16 1899 if (isALPHA(*s)) {
84c7b88c 1900 if (*s == 'S' && s[1] == 'I' && s[2] == 'G') {
79072805 1901 s += 3;
84c7b88c
BF
1902 len -= 3;
1903 }
1904 if ((val = whichsig_pvn(s, len)) < 0)
147e3846
KW
1905 Perl_croak(aTHX_ "Unrecognized signal name \"%" SVf "\"",
1906 SVfARG(*mark));
79072805
LW
1907 }
1908 else
c2fd40cb 1909 {
4ea561bc 1910 val = SvIV(*mark);
c2fd40cb
DM
1911 if (val < 0)
1912 {
885b4b39 1913 killgp = TRUE;
c2fd40cb
DM
1914 val = -val;
1915 }
1916 }
20408e3c
GS
1917 APPLY_TAINT_PROPER();
1918 tot = sp - mark;
fbcd93f0 1919
c2fd40cb 1920 while (++mark <= sp) {
60082291 1921 Pid_t proc;
c2fd40cb 1922 SvGETMAGIC(*mark);
60082291 1923 if (!(SvNIOK(*mark) || looks_like_number(*mark)))
c2fd40cb
DM
1924 Perl_croak(aTHX_ "Can't kill a non-numeric process ID");
1925 proc = SvIV_nomg(*mark);
c2fd40cb 1926 APPLY_TAINT_PROPER();
111f73b5
DM
1927#ifdef HAS_KILLPG
1928 /* use killpg in preference, as the killpg() wrapper for Win32
1929 * understands process groups, but the kill() wrapper doesn't */
1930 if (killgp ? PerlProc_killpg(proc, val)
1931 : PerlProc_kill(proc, val))
1932#else
1933 if (PerlProc_kill(killgp ? -proc: proc, val))
1934#endif
c2fd40cb 1935 tot--;
a687059c 1936 }
8165faea 1937 PERL_ASYNC_CHECK();
a687059c 1938 break;
b1248f16 1939#endif
79072805 1940 case OP_UNLINK:
20408e3c 1941 APPLY_TAINT_PROPER();
79072805
LW
1942 tot = sp - mark;
1943 while (++mark <= sp) {
41188aa0 1944 s = SvPV_const(*mark, len);
20408e3c 1945 APPLY_TAINT_PROPER();
41188aa0 1946 if (!IS_SAFE_PATHNAME(s, len, "unlink")) {
c8028aa6
TC
1947 tot--;
1948 }
f0d85c30 1949 else if (PL_unsafe) {
b8ffc8df 1950 if (UNLINK(s))
5cdd1fc2 1951 {
a687059c 1952 tot--;
5cdd1fc2
AB
1953 }
1954#if defined(__amigaos4__) && defined(NEWLIB)
1955 else
1956 {
1957 /* Under AmigaOS4 unlink only 'fails' if the
1958 * filename is invalid. It may not remove the file
1959 * if it's locked, so check if it's still around. */
1960 if ((access(s,F_OK) != -1))
1961 {
1962 tot--;
1963 }
1964 }
1965#endif
a687059c
LW
1966 }
1967 else { /* don't let root wipe out directories without -U */
45a23732
DD
1968 Stat_t statbuf;
1969 if (PerlLIO_lstat(s, &statbuf) < 0)
1dcae8b8 1970 tot--;
45a23732 1971 else if (S_ISDIR(statbuf.st_mode)) {
cd52bc19 1972 SETERRNO(EISDIR, SS_NOPRIV);
45a23732 1973 tot--;
1dcae8b8 1974 }
a687059c 1975 else {
b8ffc8df 1976 if (UNLINK(s))
5cdd1fc2
AB
1977 {
1978 tot--;
1979 }
1980#if defined(__amigaos4__) && defined(NEWLIB)
1981 else
1982 {
1983 /* Under AmigaOS4 unlink only 'fails' if the filename is invalid */
1984 /* It may not remove the file if it's Locked, so check if it's still */
1985 /* arround */
1986 if((access(s,F_OK) != -1))
1987 {
1988 tot--;
1989 }
1990 }
1991#endif
a687059c
LW
1992 }
1993 }
1994 }
1995 break;
e96b369d 1996#if defined(HAS_UTIME) || defined(HAS_FUTIMES)
79072805 1997 case OP_UTIME:
20408e3c 1998 APPLY_TAINT_PROPER();
79072805 1999 if (sp - mark > 2) {
e96b369d
GA
2000#if defined(HAS_FUTIMES)
2001 struct timeval utbuf[2];
2002 void *utbufp = utbuf;
2003#elif defined(I_UTIME) || defined(VMS)
663a0e37 2004 struct utimbuf utbuf;
07409e01 2005 struct utimbuf *utbufp = &utbuf;
663a0e37 2006#else
a687059c 2007 struct {
dd2821f6
GS
2008 Time_t actime;
2009 Time_t modtime;
a687059c 2010 } utbuf;
07409e01 2011 void *utbufp = &utbuf;
663a0e37 2012#endif
a687059c 2013
0bcc34c2
AL
2014 SV* const accessed = *++mark;
2015 SV* const modified = *++mark;
c6f7b413 2016
6ec06612
SB
2017 /* Be like C, and if both times are undefined, let the C
2018 * library figure out what to do. This usually means
2019 * "current time". */
c6f7b413
RS
2020
2021 if ( accessed == &PL_sv_undef && modified == &PL_sv_undef )
6ec06612
SB
2022 utbufp = NULL;
2023 else {
2024 Zero(&utbuf, sizeof utbuf, char);
e96b369d 2025#ifdef HAS_FUTIMES
4ea561bc 2026 utbuf[0].tv_sec = (long)SvIV(accessed); /* time accessed */
e96b369d 2027 utbuf[0].tv_usec = 0;
4ea561bc 2028 utbuf[1].tv_sec = (long)SvIV(modified); /* time modified */
e96b369d
GA
2029 utbuf[1].tv_usec = 0;
2030#elif defined(BIG_TIME)
4ea561bc
NC
2031 utbuf.actime = (Time_t)SvNV(accessed); /* time accessed */
2032 utbuf.modtime = (Time_t)SvNV(modified); /* time modified */
517844ec 2033#else
4ea561bc
NC
2034 utbuf.actime = (Time_t)SvIV(accessed); /* time accessed */
2035 utbuf.modtime = (Time_t)SvIV(modified); /* time modified */
517844ec 2036#endif
6ec06612 2037 }
4373e329 2038 APPLY_TAINT_PROPER();
79072805
LW
2039 tot = sp - mark;
2040 while (++mark <= sp) {
e96b369d 2041 GV* gv;
64617da9 2042 if ((gv = MAYBE_DEREF_GV(*mark))) {
e96b369d
GA
2043 if (GvIO(gv) && IoIFP(GvIOp(gv))) {
2044#ifdef HAS_FUTIMES
375ed12a 2045 int fd = PerlIO_fileno(IoIFP(GvIOn(gv)));
e96b369d 2046 APPLY_TAINT_PROPER();
375ed12a
JH
2047 if (fd < 0) {
2048 SETERRNO(EBADF,RMS_IFI);
2049 tot--;
2050 } else if (futimes(fd, (struct timeval *) utbufp))
e96b369d
GA
2051 tot--;
2052#else
2053 Perl_die(aTHX_ PL_no_func, "futimes");
2054#endif
2055 }
2056 else {
2057 tot--;
2058 }
2059 }
e96b369d 2060 else {
41188aa0 2061 const char * const name = SvPV_nomg_const(*mark, len);
e96b369d 2062 APPLY_TAINT_PROPER();
41188aa0 2063 if (!IS_SAFE_PATHNAME(name, len, "utime")) {
c8028aa6
TC
2064 tot--;
2065 }
2066 else
e96b369d 2067#ifdef HAS_FUTIMES
8b7231d9 2068 if (utimes(name, (struct timeval *)utbufp))
e96b369d
GA
2069#else
2070 if (PerlLIO_utime(name, utbufp))
2071#endif
2072 tot--;
2073 }
2074
a687059c 2075 }
a687059c
LW
2076 }
2077 else
79072805 2078 tot = 0;
a687059c 2079 break;
a0d0e21e 2080#endif
a687059c
LW
2081 }
2082 return tot;
20408e3c 2083
20408e3c 2084#undef APPLY_TAINT_PROPER
a687059c
LW
2085}
2086
bd93adf5 2087/* Do the permissions in *statbufp allow some operation? */
a0d0e21e 2088#ifndef VMS /* VMS' cando is in vms.c */
7f4774ae 2089bool
5aaab254 2090Perl_cando(pTHX_ Mode_t mode, bool effective, const Stat_t *statbufp)
ae1951c1
NC
2091/* effective is a flag, true for EUID, or for checking if the effective gid
2092 * is in the list of groups returned from getgroups().
2093 */
a687059c 2094{
7918f24d 2095 PERL_ARGS_ASSERT_CANDO;
81611534 2096 PERL_UNUSED_CONTEXT;
7918f24d 2097
bee1dbe2 2098#ifdef DOSISH
fe14fcc3
LW
2099 /* [Comments and code from Len Reed]
2100 * MS-DOS "user" is similar to UNIX's "superuser," but can't write
2101 * to write-protected files. The execute permission bit is set
486ec47a 2102 * by the Microsoft C library stat() function for the following:
fe14fcc3
LW
2103 * .exe files
2104 * .com files
2105 * .bat files
2106 * directories
2107 * All files and directories are readable.
2108 * Directories and special files, e.g. "CON", cannot be
2109 * write-protected.
2110 * [Comment by Tom Dinger -- a directory can have the write-protect
2111 * bit set in the file system, but DOS permits changes to
2112 * the directory anyway. In addition, all bets are off
2113 * here for networked software, such as Novell and
2114 * Sun's PC-NFS.]
2115 */
2116
bee1dbe2
LW
2117 /* Atari stat() does pretty much the same thing. we set x_bit_set_in_stat
2118 * too so it will actually look into the files for magic numbers
2119 */
8298454c 2120 return cBOOL(mode & statbufp->st_mode);
fe14fcc3 2121
55497cff 2122#else /* ! DOSISH */
b595cd4b
RU
2123# ifdef __CYGWIN__
2124 if (ingroup(544,effective)) { /* member of Administrators */
2125# else
985213f2 2126 if ((effective ? PerlProc_geteuid() : PerlProc_getuid()) == 0) { /* root is special */
b595cd4b 2127# endif
7f4774ae 2128 if (mode == S_IXUSR) {
c623bd54 2129 if (statbufp->st_mode & 0111 || S_ISDIR(statbufp->st_mode))
a687059c
LW
2130 return TRUE;
2131 }
2132 else
2133 return TRUE; /* root reads and writes anything */
2134 return FALSE;
2135 }
985213f2 2136 if (statbufp->st_uid == (effective ? PerlProc_geteuid() : PerlProc_getuid()) ) {
7f4774ae 2137 if (statbufp->st_mode & mode)
a687059c
LW
2138 return TRUE; /* ok as "user" */
2139 }
d8eceb89 2140 else if (ingroup(statbufp->st_gid,effective)) {
7f4774ae 2141 if (statbufp->st_mode & mode >> 3)
a687059c
LW
2142 return TRUE; /* ok as "group" */
2143 }
7f4774ae 2144 else if (statbufp->st_mode & mode >> 6)
a687059c
LW
2145 return TRUE; /* ok as "other" */
2146 return FALSE;
55497cff 2147#endif /* ! DOSISH */
a687059c 2148}
a0d0e21e 2149#endif /* ! VMS */
a687059c 2150
1f676739 2151static bool
0da8eb3a 2152S_ingroup(pTHX_ Gid_t testgid, bool effective)
a687059c 2153{
81611534
JH
2154#ifndef PERL_IMPLICIT_SYS
2155 /* PERL_IMPLICIT_SYS like Win32: getegid() etc. require the context. */
2156 PERL_UNUSED_CONTEXT;
2157#endif
985213f2 2158 if (testgid == (effective ? PerlProc_getegid() : PerlProc_getgid()))
a687059c 2159 return TRUE;
fe14fcc3 2160#ifdef HAS_GETGROUPS
a687059c 2161 {
331b57bc 2162 Groups_t *gary = NULL;
79072805 2163 I32 anum;
331b57bc 2164 bool rc = FALSE;
a687059c 2165
331b57bc 2166 anum = getgroups(0, gary);
375ed12a
JH
2167 if (anum > 0) {
2168 Newx(gary, anum, Groups_t);
2169 anum = getgroups(anum, gary);
2170 while (--anum >= 0)
2171 if (gary[anum] == testgid) {
2172 rc = TRUE;
2173 break;
2174 }
331b57bc 2175
375ed12a
JH
2176 Safefree(gary);
2177 }
331b57bc 2178 return rc;
a687059c 2179 }
c685562b 2180#else
a687059c 2181 return FALSE;
cd39f2b6 2182#endif
a687059c 2183}
c2ab57d4 2184
fe14fcc3 2185#if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
c2ab57d4 2186
79072805 2187I32
864dbfa3 2188Perl_do_ipcget(pTHX_ I32 optype, SV **mark, SV **sp)
c2ab57d4 2189{
0bcc34c2 2190 const key_t key = (key_t)SvNVx(*++mark);
c3312966 2191 SV *nsv = optype == OP_MSGGET ? NULL : *++mark;
6867be6d 2192 const I32 flags = SvIVx(*++mark);
294a48e9 2193
7918f24d 2194 PERL_ARGS_ASSERT_DO_IPCGET;
294a48e9 2195 PERL_UNUSED_ARG(sp);
c2ab57d4 2196
748a9306 2197 SETERRNO(0,0);
c2ab57d4
LW
2198 switch (optype)
2199 {
fe14fcc3 2200#ifdef HAS_MSG
79072805 2201 case OP_MSGGET:
c2ab57d4 2202 return msgget(key, flags);
e5d73d77 2203#endif
fe14fcc3 2204#ifdef HAS_SEM
79072805 2205 case OP_SEMGET:
c3312966 2206 return semget(key, (int) SvIV(nsv), flags);
e5d73d77 2207#endif
fe14fcc3 2208#ifdef HAS_SHM
79072805 2209 case OP_SHMGET:
c3312966 2210 return shmget(key, (size_t) SvUV(nsv), flags);
e5d73d77 2211#endif
fe14fcc3 2212#if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
e5d73d77 2213 default:
fe13d51d 2214 /* diag_listed_as: msg%s not implemented */
cea2e8a9 2215 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
e5d73d77 2216#endif
c2ab57d4
LW
2217 }
2218 return -1; /* should never happen */
2219}
2220
79072805 2221I32
864dbfa3 2222Perl_do_ipcctl(pTHX_ I32 optype, SV **mark, SV **sp)
c2ab57d4 2223{
c2ab57d4 2224 char *a;
a0d0e21e 2225 I32 ret = -1;
6867be6d 2226 const I32 id = SvIVx(*++mark);
95b63a38 2227#ifdef Semctl
6867be6d 2228 const I32 n = (optype == OP_SEMCTL) ? SvIVx(*++mark) : 0;
95b63a38 2229#endif
6867be6d 2230 const I32 cmd = SvIVx(*++mark);
0bcc34c2
AL
2231 SV * const astr = *++mark;
2232 STRLEN infosize = 0;
2233 I32 getinfo = (cmd == IPC_STAT);
c2ab57d4 2234
7918f24d 2235 PERL_ARGS_ASSERT_DO_IPCCTL;
0bcc34c2 2236 PERL_UNUSED_ARG(sp);
c2ab57d4
LW
2237
2238 switch (optype)
2239 {
fe14fcc3 2240#ifdef HAS_MSG
79072805 2241 case OP_MSGCTL:
c2ab57d4
LW
2242 if (cmd == IPC_STAT || cmd == IPC_SET)
2243 infosize = sizeof(struct msqid_ds);
2244 break;
e5d73d77 2245#endif
fe14fcc3 2246#ifdef HAS_SHM
79072805 2247 case OP_SHMCTL:
c2ab57d4
LW
2248 if (cmd == IPC_STAT || cmd == IPC_SET)
2249 infosize = sizeof(struct shmid_ds);
2250 break;
e5d73d77 2251#endif
fe14fcc3 2252#ifdef HAS_SEM
79072805 2253 case OP_SEMCTL:
39398f3f 2254#ifdef Semctl
c2ab57d4
LW
2255 if (cmd == IPC_STAT || cmd == IPC_SET)
2256 infosize = sizeof(struct semid_ds);
2257 else if (cmd == GETALL || cmd == SETALL)
2258 {
8e591e46 2259 struct semid_ds semds;
bd89102f 2260 union semun semun;
e6f0bdd6
GS
2261#ifdef EXTRA_F_IN_SEMUN_BUF
2262 semun.buff = &semds;
2263#else
84902520 2264 semun.buf = &semds;
e6f0bdd6 2265#endif
c2ab57d4 2266 getinfo = (cmd == GETALL);
9b89d93d
GB
2267 if (Semctl(id, 0, IPC_STAT, semun) == -1)
2268 return -1;
6e21c824
LW
2269 infosize = semds.sem_nsems * sizeof(short);
2270 /* "short" is technically wrong but much more portable
2271 than guessing about u_?short(_t)? */
c2ab57d4 2272 }
39398f3f 2273#else
fe13d51d 2274 /* diag_listed_as: sem%s not implemented */
cea2e8a9 2275 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
39398f3f 2276#endif
c2ab57d4 2277 break;
e5d73d77 2278#endif
fe14fcc3 2279#if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
e5d73d77 2280 default:
fe13d51d 2281 /* diag_listed_as: shm%s not implemented */
cea2e8a9 2282 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
e5d73d77 2283#endif
c2ab57d4
LW
2284 }
2285
2286 if (infosize)
2287 {
2288 if (getinfo)
2289 {
93524f2b 2290 SvPV_force_nolen(astr);
a0d0e21e 2291 a = SvGROW(astr, infosize+1);
c2ab57d4
LW
2292 }
2293 else
2294 {
93524f2b 2295 STRLEN len;
463ee0b2
LW
2296 a = SvPV(astr, len);
2297 if (len != infosize)
cea2e8a9 2298 Perl_croak(aTHX_ "Bad arg length for %s, is %lu, should be %ld",
4ec43091
JH
2299 PL_op_desc[optype],
2300 (unsigned long)len,
2301 (long)infosize);
c2ab57d4
LW
2302 }
2303 }
2304 else
2305 {
0bcc34c2 2306 const IV i = SvIV(astr);
56431972 2307 a = INT2PTR(char *,i); /* ouch */
c2ab57d4 2308 }
748a9306 2309 SETERRNO(0,0);
c2ab57d4
LW
2310 switch (optype)
2311 {
fe14fcc3 2312#ifdef HAS_MSG
79072805 2313 case OP_MSGCTL:
bee1dbe2 2314 ret = msgctl(id, cmd, (struct msqid_ds *)a);
c2ab57d4 2315 break;
e5d73d77 2316#endif
fe14fcc3 2317#ifdef HAS_SEM
bd89102f 2318 case OP_SEMCTL: {
39398f3f 2319#ifdef Semctl
bd89102f
AD
2320 union semun unsemds;
2321
64d76282
BC
2322 if(cmd == SETVAL) {
2323 unsemds.val = PTR2nat(a);
2324 }
2325 else {
e6f0bdd6 2326#ifdef EXTRA_F_IN_SEMUN_BUF
64d76282 2327 unsemds.buff = (struct semid_ds *)a;
e6f0bdd6 2328#else
64d76282 2329 unsemds.buf = (struct semid_ds *)a;
e6f0bdd6 2330#endif
64d76282 2331 }
bd89102f 2332 ret = Semctl(id, n, cmd, unsemds);
39398f3f 2333#else
fe13d51d 2334 /* diag_listed_as: sem%s not implemented */
cea2e8a9 2335 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
39398f3f 2336#endif
bd89102f 2337 }
c2ab57d4 2338 break;
e5d73d77 2339#endif
fe14fcc3 2340#ifdef HAS_SHM
79072805 2341 case OP_SHMCTL:
bee1dbe2 2342 ret = shmctl(id, cmd, (struct shmid_ds *)a);
c2ab57d4 2343 break;
e5d73d77 2344#endif
c2ab57d4
LW
2345 }
2346 if (getinfo && ret >= 0) {
79072805
LW
2347 SvCUR_set(astr, infosize);
2348 *SvEND(astr) = '\0';
a0d0e21e 2349 SvSETMAGIC(astr);
c2ab57d4
LW
2350 }
2351 return ret;
2352}
2353
79072805 2354I32
864dbfa3 2355Perl_do_msgsnd(pTHX_ SV **mark, SV **sp)
c2ab57d4 2356{
fe14fcc3 2357#ifdef HAS_MSG
463ee0b2 2358 STRLEN len;
6867be6d 2359 const I32 id = SvIVx(*++mark);
0bcc34c2
AL
2360 SV * const mstr = *++mark;
2361 const I32 flags = SvIVx(*++mark);
2362 const char * const mbuf = SvPV_const(mstr, len);
2363 const I32 msize = len - sizeof(long);
2364
7918f24d 2365 PERL_ARGS_ASSERT_DO_MSGSND;
890ce7af 2366 PERL_UNUSED_ARG(sp);
c2ab57d4 2367
0bcc34c2 2368 if (msize < 0)
cea2e8a9 2369 Perl_croak(aTHX_ "Arg too short for msgsnd");
748a9306 2370 SETERRNO(0,0);
681fb693
JH
2371 if (id >= 0 && flags >= 0) {
2372 return msgsnd(id, (struct msgbuf *)mbuf, msize, flags);
2373 } else {
2374 SETERRNO(EINVAL,LIB_INVARG);
2375 return -1;
2376 }
e5d73d77 2377#else
2d51fa4d
RGS
2378 PERL_UNUSED_ARG(sp);
2379 PERL_UNUSED_ARG(mark);
fe13d51d 2380 /* diag_listed_as: msg%s not implemented */
cea2e8a9 2381 Perl_croak(aTHX_ "msgsnd not implemented");
7c522378 2382 return -1;
e5d73d77 2383#endif
c2ab57d4
LW
2384}
2385
79072805 2386I32
864dbfa3 2387Perl_do_msgrcv(pTHX_ SV **mark, SV **sp)
c2ab57d4 2388{
fe14fcc3 2389#ifdef HAS_MSG
c2ab57d4
LW
2390 char *mbuf;
2391 long mtype;
6867be6d 2392 I32 msize, flags, ret;
6867be6d 2393 const I32 id = SvIVx(*++mark);
0bcc34c2 2394 SV * const mstr = *++mark;
7918f24d
NC
2395
2396 PERL_ARGS_ASSERT_DO_MSGRCV;
890ce7af 2397 PERL_UNUSED_ARG(sp);
79072805 2398
c2e66d9e
GS
2399 /* suppress warning when reading into undef var --jhi */
2400 if (! SvOK(mstr))
8062ff11 2401 SvPVCLEAR(mstr);
463ee0b2
LW
2402 msize = SvIVx(*++mark);
2403 mtype = (long)SvIVx(*++mark);
2404 flags = SvIVx(*++mark);
93524f2b 2405 SvPV_force_nolen(mstr);
a0d0e21e 2406 mbuf = SvGROW(mstr, sizeof(long)+msize+1);
a1d180c4 2407
748a9306 2408 SETERRNO(0,0);
d2607e1e
JH
2409 if (id >= 0 && msize >= 0 && flags >= 0) {
2410 ret = msgrcv(id, (struct msgbuf *)mbuf, msize, mtype, flags);
2411 } else {
2412 SETERRNO(EINVAL,LIB_INVARG);
2413 ret = -1;
2414 }
c2ab57d4 2415 if (ret >= 0) {
79072805
LW
2416 SvCUR_set(mstr, sizeof(long)+ret);
2417 *SvEND(mstr) = '\0';
41d6edb2
JH
2418 /* who knows who has been playing with this message? */
2419 SvTAINTED_on(mstr);
c2ab57d4
LW
2420 }
2421 return ret;
e5d73d77 2422#else
2d51fa4d
RGS
2423 PERL_UNUSED_ARG(sp);
2424 PERL_UNUSED_ARG(mark);
fe13d51d 2425 /* diag_listed_as: msg%s not implemented */
cea2e8a9 2426 Perl_croak(aTHX_ "msgrcv not implemented");
7c522378 2427 return -1;
e5d73d77 2428#endif
c2ab57d4
LW
2429}
2430
79072805 2431I32
864dbfa3 2432Perl_do_semop(pTHX_ SV **mark, SV **sp)
c2ab57d4 2433{
fe14fcc3 2434#ifdef HAS_SEM
463ee0b2 2435 STRLEN opsize;
6867be6d 2436 const I32 id = SvIVx(*++mark);
0bcc34c2
AL
2437 SV * const opstr = *++mark;
2438 const char * const opbuf = SvPV_const(opstr, opsize);
7918f24d
NC
2439
2440 PERL_ARGS_ASSERT_DO_SEMOP;
890ce7af 2441 PERL_UNUSED_ARG(sp);
c2ab57d4 2442
248ff010
NC
2443 if (opsize < 3 * SHORTSIZE
2444 || (opsize % (3 * SHORTSIZE))) {
93189314 2445 SETERRNO(EINVAL,LIB_INVARG);
c2ab57d4
LW
2446 return -1;
2447 }
748a9306 2448 SETERRNO(0,0);
248ff010
NC
2449 /* We can't assume that sizeof(struct sembuf) == 3 * sizeof(short). */
2450 {
6867be6d 2451 const int nsops = opsize / (3 * sizeof (short));
248ff010 2452 int i = nsops;
0bcc34c2 2453 short * const ops = (short *) opbuf;
248ff010
NC
2454 short *o = ops;
2455 struct sembuf *temps, *t;
2456 I32 result;
2457
a02a5408 2458 Newx (temps, nsops, struct sembuf);
248ff010
NC
2459 t = temps;
2460 while (i--) {
2461 t->sem_num = *o++;
2462 t->sem_op = *o++;
2463 t->sem_flg = *o++;
2464 t++;
2465 }
2466 result = semop(id, temps, nsops);
248ff010
NC
2467 Safefree(temps);
2468 return result;
2469 }
e5d73d77 2470#else
fe13d51d 2471 /* diag_listed_as: sem%s not implemented */
cea2e8a9 2472 Perl_croak(aTHX_ "semop not implemented");
e5d73d77 2473#endif
c2ab57d4
LW
2474}
2475
79072805 2476I32
864dbfa3 2477Perl_do_shmio(pTHX_ I32 optype, SV **mark, SV **sp)
c2ab57d4 2478{
fe14fcc3 2479#ifdef HAS_SHM
4373e329 2480 char *shm;
c2ab57d4 2481 struct shmid_ds shmds;
6867be6d 2482 const I32 id = SvIVx(*++mark);
0bcc34c2
AL
2483 SV * const mstr = *++mark;
2484 const I32 mpos = SvIVx(*++mark);
2485 const I32 msize = SvIVx(*++mark);
7918f24d
NC
2486
2487 PERL_ARGS_ASSERT_DO_SHMIO;
890ce7af 2488 PERL_UNUSED_ARG(sp);
c2ab57d4 2489
748a9306 2490 SETERRNO(0,0);
c2ab57d4
LW
2491 if (shmctl(id, IPC_STAT, &shmds) == -1)
2492 return -1;
7f39519f
NC
2493 if (mpos < 0 || msize < 0
2494 || (size_t)mpos + msize > (size_t)shmds.shm_segsz) {
93189314 2495 SETERRNO(EFAULT,SS_ACCVIO); /* can't do as caller requested */
c2ab57d4
LW
2496 return -1;
2497 }
568fc267
JH
2498 if (id >= 0) {
2499 shm = (char *)shmat(id, NULL, (optype == OP_SHMREAD) ? SHM_RDONLY : 0);
2500 } else {
2501 SETERRNO(EINVAL,LIB_INVARG);
2502 return -1;
2503 }
c2ab57d4
LW
2504 if (shm == (char *)-1) /* I hate System V IPC, I really do */
2505 return -1;
79072805 2506 if (optype == OP_SHMREAD) {
c8ae91a8 2507 char *mbuf;
9f538c04 2508 /* suppress warning when reading into undef var (tchrist 3/Mar/00) */
b399897d
CS
2509 SvGETMAGIC(mstr);
2510 SvUPGRADE(mstr, SVt_PV);
9f538c04 2511 if (! SvOK(mstr))
8062ff11 2512 SvPVCLEAR(mstr);
af8ff727 2513 SvPOK_only(mstr);
bb7a0f54 2514 mbuf = SvGROW(mstr, (STRLEN)msize+1);
a0d0e21e 2515
bee1dbe2 2516 Copy(shm + mpos, mbuf, msize, char);
79072805
LW
2517 SvCUR_set(mstr, msize);
2518 *SvEND(mstr) = '\0';
a0d0e21e 2519 SvSETMAGIC(mstr);
d929ce6f
JH
2520 /* who knows who has been playing with this shared memory? */
2521 SvTAINTED_on(mstr);
c2ab57d4
LW
2522 }
2523 else {
93524f2b 2524 STRLEN len;
c2ab57d4 2525
93524f2b 2526 const char *mbuf = SvPV_const(mstr, len);
027aa12d 2527 const I32 n = ((I32)len > msize) ? msize : (I32)len;
bee1dbe2 2528 Copy(mbuf, shm + mpos, n, char);
c2ab57d4 2529 if (n < msize)
bee1dbe2 2530 memzero(shm + mpos + n, msize - n);
c2ab57d4
LW
2531 }
2532 return shmdt(shm);
e5d73d77 2533#else
fe13d51d 2534 /* diag_listed_as: shm%s not implemented */
cea2e8a9 2535 Perl_croak(aTHX_ "shm I/O not implemented");
7c522378 2536 return -1;
e5d73d77 2537#endif
c2ab57d4
LW
2538}
2539
fe14fcc3 2540#endif /* SYSV IPC */
4e35701f 2541
0d44d22b 2542/*
ccfc67b7
JH
2543=head1 IO Functions
2544
0d44d22b
NC
2545=for apidoc start_glob
2546
2547Function called by C<do_readline> to spawn a glob (or do the glob inside
154e47c8 2548perl on VMS). This code used to be inline, but now perl uses C<File::Glob>
25047fde
KW
2549this glob starter is only used by miniperl during the build process,
2550or when PERL_EXTERNAL_GLOB is defined.
75af9d73 2551Moving it away shrinks F<pp_hot.c>; shrinking F<pp_hot.c> helps speed perl up.
fab3f3a7 2552
0d44d22b
NC
2553=cut
2554*/
2555
2556PerlIO *
2557Perl_start_glob (pTHX_ SV *tmpglob, IO *io)
2558{
561b68a9 2559 SV * const tmpcmd = newSV(0);
0d44d22b 2560 PerlIO *fp;
41188aa0
TC
2561 STRLEN len;
2562 const char *s = SvPV(tmpglob, len);
7918f24d
NC
2563
2564 PERL_ARGS_ASSERT_START_GLOB;
2565
41188aa0 2566 if (!IS_SAFE_SYSCALL(s, len, "pattern", "glob"))
c8028aa6
TC
2567 return NULL;
2568
0d44d22b
NC
2569 ENTER;
2570 SAVEFREESV(tmpcmd);
2571#ifdef VMS /* expand the wildcards right here, rather than opening a pipe, */
2572 /* since spawning off a process is a real performance hit */
dca5a913
JM
2573
2574PerlIO *
2575Perl_vms_start_glob
2576 (pTHX_ SV *tmpglob,
2577 IO *io);
2578
49a7a762 2579 fp = Perl_vms_start_glob(aTHX_ tmpglob, io);
dca5a913 2580
0d44d22b 2581#else /* !VMS */
0d44d22b
NC
2582#ifdef DOSISH
2583#ifdef OS2
2584 sv_setpv(tmpcmd, "for a in ");
2585 sv_catsv(tmpcmd, tmpglob);
2586 sv_catpv(tmpcmd, "; do echo \"$a\\0\\c\"; done |");
2587#else
2588#ifdef DJGPP
2589 sv_setpv(tmpcmd, "/dev/dosglob/"); /* File System Extension */
2590 sv_catsv(tmpcmd, tmpglob);
2591#else
2592 sv_setpv(tmpcmd, "perlglob ");
2593 sv_catsv(tmpcmd, tmpglob);
2594 sv_catpv(tmpcmd, " |");
2595#endif /* !DJGPP */
2596#endif /* !OS2 */
2597#else /* !DOSISH */
2598#if defined(CSH)
2599 sv_setpvn(tmpcmd, PL_cshname, PL_cshlen);
2600 sv_catpv(tmpcmd, " -cf 'set nonomatch; glob ");
2601 sv_catsv(tmpcmd, tmpglob);
2602 sv_catpv(tmpcmd, "' 2>/dev/null |");
2603#else
2604 sv_setpv(tmpcmd, "echo ");
2605 sv_catsv(tmpcmd, tmpglob);
0d44d22b 2606 sv_catpv(tmpcmd, "|tr -s ' \t\f\r' '\\n\\n\\n\\n'|");
0d44d22b
NC
2607#endif /* !CSH */
2608#endif /* !DOSISH */
93b2dae1 2609 {
acffc8af
FC
2610 SV ** const svp = hv_fetchs(GvHVn(PL_envgv), "LS_COLORS", 0);
2611 if (svp && *svp)
2612 save_helem_flags(GvHV(PL_envgv),
2613 newSVpvs_flags("LS_COLORS", SVs_TEMP), svp,
2614 SAVEf_SETMAGIC);
93b2dae1 2615 }
d5eb9a46
NC
2616 (void)do_open6(PL_last_in_gv, SvPVX_const(tmpcmd), SvCUR(tmpcmd),
2617 NULL, NULL, 0);
0d44d22b
NC
2618 fp = IoIFP(io);
2619#endif /* !VMS */
2620 LEAVE;
de7dabb6
TC
2621
2622 if (!fp && ckWARN(WARN_GLOB)) {
2623 Perl_warner(aTHX_ packWARN(WARN_GLOB), "glob failed (can't start child: %s)",
2624 Strerror(errno));
2625 }
2626
0d44d22b
NC
2627 return fp;
2628}
66610fdd
RGS
2629
2630/*
14d04a33 2631 * ex: set ts=8 sts=4 sw=4 et:
37442d52 2632 */