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