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