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