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