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