dTHR is a nop in 5.6.0 onwards. Ergo, it can go.
[p5sagit/p5-mst-13.2.git] / doio.c
CommitLineData
a0d0e21e 1/* doio.c
a687059c 2 *
3818b22b 3 * Copyright (c) 1991-2000, Larry Wall
a687059c 4 *
6e21c824 5 * You may distribute under the terms of either the GNU General Public
6 * License or the Artistic License, as specified in the README file.
a687059c 7 *
a0d0e21e 8 */
9
10/*
11 * "Far below them they saw the white waters pour into a foaming bowl, and
12 * then swirl darkly about a deep oval basin in the rocks, until they found
13 * their way out again through a narrow gate, and flowed away, fuming and
14 * chattering, into calmer and more level reaches."
a687059c 15 */
16
17#include "EXTERN.h"
864dbfa3 18#define PERL_IN_DOIO_C
a687059c 19#include "perl.h"
20
fe14fcc3 21#if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
aec308ec 22#ifndef HAS_SEM
c2ab57d4 23#include <sys/ipc.h>
aec308ec 24#endif
fe14fcc3 25#ifdef HAS_MSG
c2ab57d4 26#include <sys/msg.h>
e5d73d77 27#endif
fe14fcc3 28#ifdef HAS_SHM
c2ab57d4 29#include <sys/shm.h>
a0d0e21e 30# ifndef HAS_SHMAT_PROTOTYPE
20ce7b12 31 extern Shmat_t shmat (int, char *, int);
a0d0e21e 32# endif
c2ab57d4 33#endif
e5d73d77 34#endif
c2ab57d4 35
663a0e37 36#ifdef I_UTIME
3730b96e 37# if defined(_MSC_VER) || defined(__MINGW32__)
3fe9a6f1 38# include <sys/utime.h>
39# else
40# include <utime.h>
41# endif
663a0e37 42#endif
85aff577 43
85aff577 44#ifdef O_EXCL
45# define OPEN_EXCL O_EXCL
46#else
47# define OPEN_EXCL 0
48#endif
a687059c 49
76121258 50#if !defined(NSIG) || defined(M_UNIX) || defined(M_XENIX)
51#include <signal.h>
52#endif
53
a687059c 54bool
6170680b 55Perl_do_open(pTHX_ GV *gv, register char *name, I32 len, int as_raw,
56 int rawmode, int rawperm, PerlIO *supplied_fp)
57{
58 return do_open9(gv, name, len, as_raw, rawmode, rawperm,
59 supplied_fp, Nullsv, 0);
60}
61
62bool
63Perl_do_open9(pTHX_ GV *gv, register char *name, I32 len, int as_raw,
64 int rawmode, int rawperm, PerlIO *supplied_fp, SV *svs,
65 I32 num_svs)
a687059c 66{
a0d0e21e 67 register IO *io = GvIOn(gv);
760ac839 68 PerlIO *saveifp = Nullfp;
69 PerlIO *saveofp = Nullfp;
9f37169a 70 char savetype = IoTYPE_CLOSED;
c07a80fd 71 int writing = 0;
760ac839 72 PerlIO *fp;
c07a80fd 73 int fd;
74 int result;
3500f679 75 bool was_fdopen = FALSE;
16fe6d59 76 bool in_raw = 0, in_crlf = 0, out_raw = 0, out_crlf = 0;
b931b1d9 77 char *type = NULL;
78 char *deftype = NULL;
79 char mode[4]; /* stdio file mode ("r\0", "rb\0", "r+b\0" etc.) */
a687059c 80
b931b1d9 81 Zero(mode,sizeof(mode),char);
3280af22 82 PL_forkprocess = 1; /* assume true if no fork */
c07a80fd 83
b931b1d9 84 /* Collect default raw/crlf info from the op */
16fe6d59 85 if (PL_op && PL_op->op_type == OP_OPEN) {
86 /* set up disciplines */
87 U8 flags = PL_op->op_private;
88 in_raw = (flags & OPpOPEN_IN_RAW);
89 in_crlf = (flags & OPpOPEN_IN_CRLF);
90 out_raw = (flags & OPpOPEN_OUT_RAW);
91 out_crlf = (flags & OPpOPEN_OUT_CRLF);
92 }
93
b931b1d9 94 /* If currently open - close before we re-open */
a0d0e21e 95 if (IoIFP(io)) {
760ac839 96 fd = PerlIO_fileno(IoIFP(io));
50952442 97 if (IoTYPE(io) == IoTYPE_STD)
c2ab57d4 98 result = 0;
3280af22 99 else if (fd <= PL_maxsysfd) {
8990e307 100 saveifp = IoIFP(io);
101 saveofp = IoOFP(io);
102 savetype = IoTYPE(io);
6e21c824 103 result = 0;
104 }
50952442 105 else if (IoTYPE(io) == IoTYPE_PIPE)
3028581b 106 result = PerlProc_pclose(IoIFP(io));
8990e307 107 else if (IoIFP(io) != IoOFP(io)) {
108 if (IoOFP(io)) {
760ac839 109 result = PerlIO_close(IoOFP(io));
6170680b 110 PerlIO_close(IoIFP(io)); /* clear stdio, fd already closed */
c2ab57d4 111 }
112 else
760ac839 113 result = PerlIO_close(IoIFP(io));
a687059c 114 }
a687059c 115 else
760ac839 116 result = PerlIO_close(IoIFP(io));
3280af22 117 if (result == EOF && fd > PL_maxsysfd)
bf49b057 118 PerlIO_printf(Perl_error_log,
6170680b 119 "Warning: unable to close filehandle %s properly.\n",
120 GvENAME(gv));
8990e307 121 IoOFP(io) = IoIFP(io) = Nullfp;
a687059c 122 }
c07a80fd 123
124 if (as_raw) {
b931b1d9 125 /* sysopen style args, i.e. integer mode and permissions */
126
09458382 127#if defined(USE_64_BIT_RAWIO) && defined(O_LARGEFILE)
5ff3f7a4 128 rawmode |= O_LARGEFILE;
129#endif
130
9d116dd7 131#ifndef O_ACCMODE
132#define O_ACCMODE 3 /* Assume traditional implementation */
133#endif
5ff3f7a4 134
9d116dd7 135 switch (result = rawmode & O_ACCMODE) {
136 case O_RDONLY:
50952442 137 IoTYPE(io) = IoTYPE_RDONLY;
9d116dd7 138 break;
139 case O_WRONLY:
50952442 140 IoTYPE(io) = IoTYPE_WRONLY;
9d116dd7 141 break;
142 case O_RDWR:
143 default:
50952442 144 IoTYPE(io) = IoTYPE_RDWR;
9d116dd7 145 break;
146 }
147
c07a80fd 148 writing = (result > 0);
3028581b 149 fd = PerlLIO_open3(name, rawmode, rawperm);
9d116dd7 150
c07a80fd 151 if (fd == -1)
152 fp = NULL;
153 else {
16fe6d59 154 STRLEN ix = 0;
b931b1d9 155 if (result == O_RDONLY) {
156 mode[ix++] = 'r';
157 }
360e5741 158#ifdef O_APPEND
16fe6d59 159 else if (rawmode & O_APPEND) {
b931b1d9 160 mode[ix++] = 'a';
16fe6d59 161 if (result != O_WRONLY)
b931b1d9 162 mode[ix++] = '+';
16fe6d59 163 }
360e5741 164#endif
16fe6d59 165 else {
166 if (result == O_WRONLY)
b931b1d9 167 mode[ix++] = 'w';
16fe6d59 168 else {
b931b1d9 169 mode[ix++] = 'r';
170 mode[ix++] = '+';
16fe6d59 171 }
172 }
173 if (rawmode & O_BINARY)
b931b1d9 174 mode[ix++] = 'b';
175 mode[ix] = '\0';
176 fp = PerlIO_fdopen(fd, mode);
c07a80fd 177 if (!fp)
3028581b 178 PerlLIO_close(fd);
c07a80fd 179 }
a687059c 180 }
c07a80fd 181 else {
b931b1d9 182 /* Regular (non-sys) open */
faecd977 183 char *oname = name;
faecd977 184 STRLEN olen = len;
b931b1d9 185 char *tend;
186 int dodup = 0;
c07a80fd 187
faecd977 188 type = savepvn(name, len);
b931b1d9 189 tend = type+len;
faecd977 190 SAVEFREEPV(type);
b931b1d9 191 /* Loose trailing white space */
192 while (tend > type && isSPACE(tend[-1]))
193 *tend-- = '\0';
6170680b 194 if (num_svs) {
b931b1d9 195 /* New style explict name, type is just mode and discipline/layer info */
faecd977 196 STRLEN l;
197 name = SvPV(svs, l) ;
198 len = (I32)l;
199 name = savepvn(name, len);
200 SAVEFREEPV(name);
b931b1d9 201 /*SUPPRESS 530*/
202 for (; isSPACE(*type); type++) ;
6170680b 203 }
faecd977 204 else {
faecd977 205 name = type;
b931b1d9 206 len = tend-type;
faecd977 207 }
6170680b 208 IoTYPE(io) = *type;
b931b1d9 209 if (*type == IoTYPE_RDWR && (!num_svs || tend > type+1 && tend[-1] != IoTYPE_PIPE)) { /* scary */
6170680b 210 mode[1] = *type++;
c07a80fd 211 writing = 1;
a687059c 212 }
c07a80fd 213
9f37169a 214 if (*type == IoTYPE_PIPE) {
b931b1d9 215 if (num_svs) {
216 if (type[1] != IoTYPE_STD) {
217 unknown_desr:
218 Perl_croak(aTHX_ "Unknown open() mode '%.*s'", (int)olen, oname);
219 }
220 type++;
6170680b 221 }
c07a80fd 222 /*SUPPRESS 530*/
b931b1d9 223 for (type++; isSPACE(*type); type++) ;
faecd977 224 if (!num_svs) {
6170680b 225 name = type;
b931b1d9 226 len = tend-type;
faecd977 227 }
06eaf0bc 228 if (*name == '\0') { /* command is missing 19990114 */
06eaf0bc 229 if (ckWARN(WARN_PIPE))
cea2e8a9 230 Perl_warner(aTHX_ WARN_PIPE, "Missing command in piped open");
06eaf0bc 231 errno = EPIPE;
232 goto say_false;
233 }
6170680b 234 if (strNE(name,"-") || num_svs)
c07a80fd 235 TAINT_ENV();
236 TAINT_PROPER("piped open");
b931b1d9 237 if (!num_svs && name[len-1] == '|') {
faecd977 238 name[--len] = '\0' ;
599cee73 239 if (ckWARN(WARN_PIPE))
9a7dcd9c 240 Perl_warner(aTHX_ WARN_PIPE, "Can't open bidirectional pipe");
7b8d334a 241 }
a1d180c4 242 mode[0] = 'w';
c07a80fd 243 writing = 1;
a1d180c4 244 if (out_raw)
245 strcat(mode, "b");
246 else if (out_crlf)
247 strcat(mode, "t");
248 fp = PerlProc_popen(name,mode);
c07a80fd 249 }
9f37169a 250 else if (*type == IoTYPE_WRONLY) {
c07a80fd 251 TAINT_PROPER("open");
6170680b 252 type++;
9f37169a 253 if (*type == IoTYPE_WRONLY) {
254 /* Two IoTYPE_WRONLYs in a row make for an IoTYPE_APPEND. */
50952442 255 mode[0] = IoTYPE(io) = IoTYPE_APPEND;
6170680b 256 type++;
a0d0e21e 257 }
c07a80fd 258 else
259 mode[0] = 'w';
260 writing = 1;
261
16fe6d59 262 if (out_raw)
263 strcat(mode, "b");
264 else if (out_crlf)
265 strcat(mode, "t");
266
6170680b 267 if (*type == '&') {
268 name = type;
c07a80fd 269 duplicity:
b931b1d9 270 if (num_svs)
271 goto unknown_desr;
c07a80fd 272 dodup = 1;
273 name++;
274 if (*name == '=') {
275 dodup = 0;
a0d0e21e 276 name++;
c07a80fd 277 }
278 if (!*name && supplied_fp)
279 fp = supplied_fp;
a0d0e21e 280 else {
c07a80fd 281 /*SUPPRESS 530*/
282 for (; isSPACE(*name); name++) ;
283 if (isDIGIT(*name))
284 fd = atoi(name);
285 else {
286 IO* thatio;
287 gv = gv_fetchpv(name,FALSE,SVt_PVIO);
288 thatio = GvIO(gv);
289 if (!thatio) {
6e21c824 290#ifdef EINVAL
c07a80fd 291 SETERRNO(EINVAL,SS$_IVCHAN);
6e21c824 292#endif
c07a80fd 293 goto say_false;
294 }
295 if (IoIFP(thatio)) {
54195c32 296 PerlIO *fp = IoIFP(thatio);
7211d486 297 /* Flush stdio buffer before dup. --mjd
298 * Unfortunately SEEK_CURing 0 seems to
299 * be optimized away on most platforms;
300 * only Solaris and Linux seem to flush
301 * on that. --jhi */
2c534a3f 302#ifdef USE_SFIO
303 /* sfio fails to clear error on next
304 sfwrite, contrary to documentation.
305 -- Nick Clark */
306 if (PerlIO_seek(fp, 0, SEEK_CUR) == -1)
307 PerlIO_clearerr(fp);
308#endif
7211d486 309 /* On the other hand, do all platforms
310 * take gracefully to flushing a read-only
311 * filehandle? Perhaps we should do
312 * fsetpos(src)+fgetpos(dst)? --nik */
313 PerlIO_flush(fp);
54195c32 314 fd = PerlIO_fileno(fp);
0759c907 315 /* When dup()ing STDIN, STDOUT or STDERR
316 * explicitly set appropriate access mode */
317 if (IoIFP(thatio) == PerlIO_stdout()
318 || IoIFP(thatio) == PerlIO_stderr())
319 IoTYPE(io) = IoTYPE_WRONLY;
320 else if (IoIFP(thatio) == PerlIO_stdin())
321 IoTYPE(io) = IoTYPE_RDONLY;
322 /* When dup()ing a socket, say result is
323 * one as well */
324 else if (IoTYPE(thatio) == IoTYPE_SOCKET)
50952442 325 IoTYPE(io) = IoTYPE_SOCKET;
c07a80fd 326 }
327 else
328 fd = -1;
a0d0e21e 329 }
fec02dd3 330 if (dodup)
3028581b 331 fd = PerlLIO_dup(fd);
3500f679 332 else
333 was_fdopen = TRUE;
760ac839 334 if (!(fp = PerlIO_fdopen(fd,mode))) {
c07a80fd 335 if (dodup)
3028581b 336 PerlLIO_close(fd);
faecd977 337 }
c07a80fd 338 }
bf38876a 339 }
c07a80fd 340 else {
341 /*SUPPRESS 530*/
6170680b 342 for (; isSPACE(*type); type++) ;
b931b1d9 343 if (*type == IoTYPE_STD && (!type[1] || isSPACE(type[1]) || type[1] == ':')) {
344 /*SUPPRESS 530*/
345 type++;
760ac839 346 fp = PerlIO_stdout();
50952442 347 IoTYPE(io) = IoTYPE_STD;
c07a80fd 348 }
349 else {
6170680b 350 fp = PerlIO_open((num_svs ? name : type), mode);
c07a80fd 351 }
bf38876a 352 }
353 }
9f37169a 354 else if (*type == IoTYPE_RDONLY) {
c07a80fd 355 /*SUPPRESS 530*/
6170680b 356 for (type++; isSPACE(*type); type++) ;
bf38876a 357 mode[0] = 'r';
16fe6d59 358 if (in_raw)
359 strcat(mode, "b");
360 else if (in_crlf)
361 strcat(mode, "t");
362
6170680b 363 if (*type == '&') {
364 name = type;
bf38876a 365 goto duplicity;
6170680b 366 }
b931b1d9 367 if (*type == IoTYPE_STD && (!type[1] || isSPACE(type[1]) || type[1] == ':')) {
368 /*SUPPRESS 530*/
369 type++;
760ac839 370 fp = PerlIO_stdin();
50952442 371 IoTYPE(io) = IoTYPE_STD;
a687059c 372 }
bf38876a 373 else
6170680b 374 fp = PerlIO_open((num_svs ? name : type), mode);
a687059c 375 }
b931b1d9 376 else if ((num_svs && type[0] == IoTYPE_STD && type[1] == IoTYPE_PIPE) ||
377 (!num_svs && tend > type+1 && tend[-1] == IoTYPE_PIPE)) {
6170680b 378 if (num_svs) {
b931b1d9 379 type += 2; /* skip over '-|' */
6170680b 380 }
381 else {
b931b1d9 382 *--tend = '\0';
383 while (tend > type && isSPACE(tend[-1]))
384 *--tend = '\0';
6170680b 385 /*SUPPRESS 530*/
386 for (; isSPACE(*type); type++) ;
387 name = type;
b931b1d9 388 len = tend-type;
6170680b 389 }
06eaf0bc 390 if (*name == '\0') { /* command is missing 19990114 */
06eaf0bc 391 if (ckWARN(WARN_PIPE))
cea2e8a9 392 Perl_warner(aTHX_ WARN_PIPE, "Missing command in piped open");
06eaf0bc 393 errno = EPIPE;
394 goto say_false;
395 }
6170680b 396 if (strNE(name,"-") || num_svs)
79072805 397 TAINT_ENV();
398 TAINT_PROPER("piped open");
a1d180c4 399 mode[0] = 'r';
400 if (in_raw)
401 strcat(mode, "b");
402 else if (in_crlf)
403 strcat(mode, "t");
404 fp = PerlProc_popen(name,mode);
50952442 405 IoTYPE(io) = IoTYPE_PIPE;
a687059c 406 }
407 else {
6170680b 408 if (num_svs)
409 goto unknown_desr;
410 name = type;
50952442 411 IoTYPE(io) = IoTYPE_RDONLY;
99b89507 412 /*SUPPRESS 530*/
413 for (; isSPACE(*name); name++) ;
88b61e10 414 mode[0] = 'r';
415 if (in_raw)
416 strcat(mode, "b");
417 else if (in_crlf)
418 strcat(mode, "t");
a687059c 419 if (strEQ(name,"-")) {
760ac839 420 fp = PerlIO_stdin();
50952442 421 IoTYPE(io) = IoTYPE_STD;
a687059c 422 }
16fe6d59 423 else {
16fe6d59 424 fp = PerlIO_open(name,mode);
425 }
a687059c 426 }
427 }
bee1dbe2 428 if (!fp) {
50952442 429 if (ckWARN(WARN_NEWLINE) && IoTYPE(io) == IoTYPE_RDONLY && strchr(name, '\n'))
cea2e8a9 430 Perl_warner(aTHX_ WARN_NEWLINE, PL_warn_nl, "open");
6e21c824 431 goto say_false;
bee1dbe2 432 }
88b61e10 433 if (IoTYPE(io) && IoTYPE(io) != IoTYPE_PIPE && IoTYPE(io) != IoTYPE_STD) {
3280af22 434 if (PerlLIO_fstat(PerlIO_fileno(fp),&PL_statbuf) < 0) {
760ac839 435 (void)PerlIO_close(fp);
6e21c824 436 goto say_false;
a687059c 437 }
3280af22 438 if (S_ISSOCK(PL_statbuf.st_mode))
50952442 439 IoTYPE(io) = IoTYPE_SOCKET; /* in case a socket was passed in to us */
99b89507 440#ifdef HAS_SOCKET
441 else if (
c623bd54 442#ifdef S_IFMT
3280af22 443 !(PL_statbuf.st_mode & S_IFMT)
99b89507 444#else
b28d0864 445 !PL_statbuf.st_mode
99b89507 446#endif
0759c907 447 && IoTYPE(io) != IoTYPE_WRONLY /* Dups of STD* filehandles already have */
448 && IoTYPE(io) != IoTYPE_RDONLY /* type so they aren't marked as sockets */
449 ) { /* on OS's that return 0 on fstat()ed pipe */
96827780 450 char tmpbuf[256];
451 Sock_size_t buflen = sizeof tmpbuf;
3028581b 452 if (PerlSock_getsockname(PerlIO_fileno(fp), (struct sockaddr *)tmpbuf,
d574b85e 453 &buflen) >= 0
454 || errno != ENOTSOCK)
50952442 455 IoTYPE(io) = IoTYPE_SOCKET; /* some OS's return 0 on fstat()ed socket */
99b89507 456 /* but some return 0 for streams too, sigh */
457 }
bf38876a 458#endif
a687059c 459 }
6e21c824 460 if (saveifp) { /* must use old fp? */
f5b9d040 461 /* If fd is less that PL_maxsysfd i.e. STDIN..STDERR
24c23ab4 462 then dup the new fileno down
f5b9d040 463 */
760ac839 464 fd = PerlIO_fileno(saveifp);
6e21c824 465 if (saveofp) {
f5b9d040 466 PerlIO_flush(saveofp); /* emulate PerlIO_close() */
6e21c824 467 if (saveofp != saveifp) { /* was a socket? */
760ac839 468 PerlIO_close(saveofp);
24c23ab4 469 /* This looks very suspect - NI-S 24 Nov 2000 */
99b89507 470 if (fd > 2)
f5b9d040 471 Safefree(saveofp); /* ??? */
6e21c824 472 }
473 }
760ac839 474 if (fd != PerlIO_fileno(fp)) {
d8a83dd3 475 Pid_t pid;
79072805 476 SV *sv;
bee1dbe2 477
3028581b 478 PerlLIO_dup2(PerlIO_fileno(fp), fd);
4755096e 479 LOCK_FDPID_MUTEX;
3280af22 480 sv = *av_fetch(PL_fdpid,PerlIO_fileno(fp),TRUE);
a0d0e21e 481 (void)SvUPGRADE(sv, SVt_IV);
463ee0b2 482 pid = SvIVX(sv);
483 SvIVX(sv) = 0;
3280af22 484 sv = *av_fetch(PL_fdpid,fd,TRUE);
4755096e 485 UNLOCK_FDPID_MUTEX;
a0d0e21e 486 (void)SvUPGRADE(sv, SVt_IV);
463ee0b2 487 SvIVX(sv) = pid;
3500f679 488 if (!was_fdopen)
489 PerlIO_close(fp);
bee1dbe2 490
6e21c824 491 }
492 fp = saveifp;
760ac839 493 PerlIO_clearerr(fp);
6e21c824 494 }
a0d0e21e 495#if defined(HAS_FCNTL) && defined(F_SETFD)
a8710ca1 496 {
497 int save_errno = errno;
498 fd = PerlIO_fileno(fp);
499 fcntl(fd,F_SETFD,fd > PL_maxsysfd); /* can change errno */
500 errno = save_errno;
501 }
1462b684 502#endif
8990e307 503 IoIFP(io) = fp;
b931b1d9 504 if (!num_svs) {
505 /* Need to supply default type info from open.pm */
ac27b0f5 506 SV *layers = PL_curcop->cop_io;
b931b1d9 507 type = NULL;
ac27b0f5 508 if (layers) {
509 STRLEN len;
510 type = SvPV(layers,len);
511 if (type && mode[0] != 'r') {
512 /* Skip to write part */
513 char *s = strchr(type,0);
514 if (s && (s-type) < len) {
515 type = s+1;
516 }
517 }
518 }
b931b1d9 519 }
520 if (type) {
521 while (isSPACE(*type)) type++;
522 if (*type) {
ac27b0f5 523 if (PerlIO_apply_layers(aTHX_ IoIFP(io),mode,type) != 0) {
524 goto say_false;
525 }
b931b1d9 526 }
527 }
528
684bef36 529 IoFLAGS(io) &= ~IOf_NOLINE;
bf38876a 530 if (writing) {
50952442 531 if (IoTYPE(io) == IoTYPE_SOCKET
532 || (IoTYPE(io) == IoTYPE_WRONLY && S_ISCHR(PL_statbuf.st_mode)) )
16fe6d59 533 {
b931b1d9 534 mode[0] = 'w';
16fe6d59 535 if (!(IoOFP(io) = PerlIO_fdopen(PerlIO_fileno(fp),mode))) {
760ac839 536 PerlIO_close(fp);
8990e307 537 IoIFP(io) = Nullfp;
6e21c824 538 goto say_false;
fe14fcc3 539 }
ac27b0f5 540 if (type && *type) {
541 if (PerlIO_apply_layers(aTHX_ IoOFP(io),mode,type) != 0) {
542 PerlIO_close(IoOFP(io));
543 PerlIO_close(fp);
544 IoIFP(io) = Nullfp;
545 IoOFP(io) = Nullfp;
546 goto say_false;
547 }
548 }
1462b684 549 }
550 else
8990e307 551 IoOFP(io) = fp;
bf38876a 552 }
a687059c 553 return TRUE;
6e21c824 554
555say_false:
8990e307 556 IoIFP(io) = saveifp;
557 IoOFP(io) = saveofp;
558 IoTYPE(io) = savetype;
6e21c824 559 return FALSE;
a687059c 560}
561
760ac839 562PerlIO *
864dbfa3 563Perl_nextargv(pTHX_ register GV *gv)
a687059c 564{
79072805 565 register SV *sv;
99b89507 566#ifndef FLEXFILENAMES
c623bd54 567 int filedev;
568 int fileino;
99b89507 569#endif
761237fe 570 Uid_t fileuid;
571 Gid_t filegid;
18708f5a 572 IO *io = GvIOp(gv);
fe14fcc3 573
3280af22 574 if (!PL_argvoutgv)
575 PL_argvoutgv = gv_fetchpv("ARGVOUT",TRUE,SVt_PVIO);
18708f5a 576 if (io && (IoFLAGS(io) & IOf_ARGV) && (IoFLAGS(io) & IOf_START)) {
577 IoFLAGS(io) &= ~IOf_START;
7a1c5554 578 if (PL_inplace) {
579 if (!PL_argvout_stack)
580 PL_argvout_stack = newAV();
18708f5a 581 av_push(PL_argvout_stack, SvREFCNT_inc(PL_defoutgv));
7a1c5554 582 }
18708f5a 583 }
3280af22 584 if (PL_filemode & (S_ISUID|S_ISGID)) {
585 PerlIO_flush(IoIFP(GvIOn(PL_argvoutgv))); /* chmod must follow last write */
fe14fcc3 586#ifdef HAS_FCHMOD
3280af22 587 (void)fchmod(PL_lastfd,PL_filemode);
fe14fcc3 588#else
b28d0864 589 (void)PerlLIO_chmod(PL_oldname,PL_filemode);
fe14fcc3 590#endif
591 }
3280af22 592 PL_filemode = 0;
79072805 593 while (av_len(GvAV(gv)) >= 0) {
85aff577 594 STRLEN oldlen;
79072805 595 sv = av_shift(GvAV(gv));
8990e307 596 SAVEFREESV(sv);
79072805 597 sv_setsv(GvSV(gv),sv);
598 SvSETMAGIC(GvSV(gv));
3280af22 599 PL_oldname = SvPVx(GvSV(gv), oldlen);
9d116dd7 600 if (do_open(gv,PL_oldname,oldlen,PL_inplace!=0,O_RDONLY,0,Nullfp)) {
3280af22 601 if (PL_inplace) {
79072805 602 TAINT_PROPER("inplace open");
3280af22 603 if (oldlen == 1 && *PL_oldname == '-') {
4633a7c4 604 setdefout(gv_fetchpv("STDOUT",TRUE,SVt_PVIO));
a0d0e21e 605 return IoIFP(GvIOp(gv));
c623bd54 606 }
99b89507 607#ifndef FLEXFILENAMES
b28d0864 608 filedev = PL_statbuf.st_dev;
609 fileino = PL_statbuf.st_ino;
99b89507 610#endif
3280af22 611 PL_filemode = PL_statbuf.st_mode;
612 fileuid = PL_statbuf.st_uid;
613 filegid = PL_statbuf.st_gid;
614 if (!S_ISREG(PL_filemode)) {
0453d815 615 if (ckWARN_d(WARN_INPLACE))
616 Perl_warner(aTHX_ WARN_INPLACE,
617 "Can't do inplace edit: %s is not a regular file",
618 PL_oldname );
79072805 619 do_close(gv,FALSE);
c623bd54 620 continue;
621 }
3280af22 622 if (*PL_inplace) {
623 char *star = strchr(PL_inplace, '*');
2d259d92 624 if (star) {
3280af22 625 char *begin = PL_inplace;
2d259d92 626 sv_setpvn(sv, "", 0);
627 do {
628 sv_catpvn(sv, begin, star - begin);
3280af22 629 sv_catpvn(sv, PL_oldname, oldlen);
2d259d92 630 begin = ++star;
631 } while ((star = strchr(begin, '*')));
3d66d7bb 632 if (*begin)
633 sv_catpv(sv,begin);
2d259d92 634 }
635 else {
3280af22 636 sv_catpv(sv,PL_inplace);
2d259d92 637 }
c623bd54 638#ifndef FLEXFILENAMES
b28d0864 639 if (PerlLIO_stat(SvPVX(sv),&PL_statbuf) >= 0
640 && PL_statbuf.st_dev == filedev
641 && PL_statbuf.st_ino == fileino
39e571d4 642#ifdef DJGPP
643 || (_djstat_fail_bits & _STFAIL_TRUENAME)!=0
644#endif
f248d071 645 )
646 {
647 if (ckWARN_d(WARN_INPLACE))
648 Perl_warner(aTHX_ WARN_INPLACE,
649 "Can't do inplace edit: %s would not be unique",
650 SvPVX(sv));
79072805 651 do_close(gv,FALSE);
c623bd54 652 continue;
653 }
654#endif
fe14fcc3 655#ifdef HAS_RENAME
d308986b 656#if !defined(DOSISH) && !defined(__CYGWIN__)
3280af22 657 if (PerlLIO_rename(PL_oldname,SvPVX(sv)) < 0) {
0453d815 658 if (ckWARN_d(WARN_INPLACE))
a1d180c4 659 Perl_warner(aTHX_ WARN_INPLACE,
0453d815 660 "Can't rename %s to %s: %s, skipping file",
661 PL_oldname, SvPVX(sv), Strerror(errno) );
79072805 662 do_close(gv,FALSE);
c623bd54 663 continue;
664 }
a687059c 665#else
79072805 666 do_close(gv,FALSE);
3028581b 667 (void)PerlLIO_unlink(SvPVX(sv));
b28d0864 668 (void)PerlLIO_rename(PL_oldname,SvPVX(sv));
9d116dd7 669 do_open(gv,SvPVX(sv),SvCUR(sv),PL_inplace!=0,O_RDONLY,0,Nullfp);
55497cff 670#endif /* DOSISH */
ff8e2863 671#else
463ee0b2 672 (void)UNLINK(SvPVX(sv));
b28d0864 673 if (link(PL_oldname,SvPVX(sv)) < 0) {
0453d815 674 if (ckWARN_d(WARN_INPLACE))
675 Perl_warner(aTHX_ WARN_INPLACE,
676 "Can't rename %s to %s: %s, skipping file",
677 PL_oldname, SvPVX(sv), Strerror(errno) );
79072805 678 do_close(gv,FALSE);
c623bd54 679 continue;
680 }
b28d0864 681 (void)UNLINK(PL_oldname);
a687059c 682#endif
683 }
684 else {
a8c18271 685#if !defined(DOSISH) && !defined(AMIGAOS)
edc7bc49 686# ifndef VMS /* Don't delete; use automatic file versioning */
3280af22 687 if (UNLINK(PL_oldname) < 0) {
0453d815 688 if (ckWARN_d(WARN_INPLACE))
689 Perl_warner(aTHX_ WARN_INPLACE,
690 "Can't remove %s: %s, skipping file",
691 PL_oldname, Strerror(errno) );
79072805 692 do_close(gv,FALSE);
fe14fcc3 693 continue;
694 }
edc7bc49 695# endif
ff8e2863 696#else
cea2e8a9 697 Perl_croak(aTHX_ "Can't do inplace edit without backup");
ff8e2863 698#endif
a687059c 699 }
700
3280af22 701 sv_setpvn(sv,">",!PL_inplace);
702 sv_catpvn(sv,PL_oldname,oldlen);
748a9306 703 SETERRNO(0,0); /* in case sprintf set errno */
4119ab01 704#ifdef VMS
705 if (!do_open(PL_argvoutgv,SvPVX(sv),SvCUR(sv),PL_inplace!=0,
18708f5a 706 O_WRONLY|O_CREAT|O_TRUNC,0,Nullfp))
4119ab01 707#else
3280af22 708 if (!do_open(PL_argvoutgv,SvPVX(sv),SvCUR(sv),PL_inplace!=0,
18708f5a 709 O_WRONLY|O_CREAT|OPEN_EXCL,0666,Nullfp))
4119ab01 710#endif
18708f5a 711 {
0453d815 712 if (ckWARN_d(WARN_INPLACE))
713 Perl_warner(aTHX_ WARN_INPLACE, "Can't do inplace edit on %s: %s",
714 PL_oldname, Strerror(errno) );
79072805 715 do_close(gv,FALSE);
fe14fcc3 716 continue;
717 }
3280af22 718 setdefout(PL_argvoutgv);
719 PL_lastfd = PerlIO_fileno(IoIFP(GvIOp(PL_argvoutgv)));
720 (void)PerlLIO_fstat(PL_lastfd,&PL_statbuf);
fe14fcc3 721#ifdef HAS_FCHMOD
3280af22 722 (void)fchmod(PL_lastfd,PL_filemode);
a687059c 723#else
3e3baf6d 724# if !(defined(WIN32) && defined(__BORLANDC__))
725 /* Borland runtime creates a readonly file! */
b28d0864 726 (void)PerlLIO_chmod(PL_oldname,PL_filemode);
3e3baf6d 727# endif
a687059c 728#endif
3280af22 729 if (fileuid != PL_statbuf.st_uid || filegid != PL_statbuf.st_gid) {
fe14fcc3 730#ifdef HAS_FCHOWN
3280af22 731 (void)fchown(PL_lastfd,fileuid,filegid);
a687059c 732#else
fe14fcc3 733#ifdef HAS_CHOWN
b28d0864 734 (void)PerlLIO_chown(PL_oldname,fileuid,filegid);
a687059c 735#endif
b1248f16 736#endif
fe14fcc3 737 }
a687059c 738 }
a0d0e21e 739 return IoIFP(GvIOp(gv));
a687059c 740 }
4d61ec05 741 else {
4d61ec05 742 if (ckWARN_d(WARN_INPLACE)) {
6af84f9f 743 int eno = errno;
744 if (PerlLIO_stat(PL_oldname, &PL_statbuf) >= 0
745 && !S_ISREG(PL_statbuf.st_mode))
746 {
4d61ec05 747 Perl_warner(aTHX_ WARN_INPLACE,
748 "Can't do inplace edit: %s is not a regular file",
9a7dcd9c 749 PL_oldname);
6af84f9f 750 }
4d61ec05 751 else
9a7dcd9c 752 Perl_warner(aTHX_ WARN_INPLACE, "Can't open %s: %s",
6af84f9f 753 PL_oldname, Strerror(eno));
4d61ec05 754 }
755 }
a687059c 756 }
18708f5a 757 if (io && (IoFLAGS(io) & IOf_ARGV))
758 IoFLAGS(io) |= IOf_START;
3280af22 759 if (PL_inplace) {
760 (void)do_close(PL_argvoutgv,FALSE);
7a1c5554 761 if (io && (IoFLAGS(io) & IOf_ARGV)
762 && PL_argvout_stack && AvFILLp(PL_argvout_stack) >= 0)
763 {
18708f5a 764 GV *oldout = (GV*)av_pop(PL_argvout_stack);
765 setdefout(oldout);
766 SvREFCNT_dec(oldout);
767 return Nullfp;
768 }
4633a7c4 769 setdefout(gv_fetchpv("STDOUT",TRUE,SVt_PVIO));
a687059c 770 }
771 return Nullfp;
772}
773
fe14fcc3 774#ifdef HAS_PIPE
afd9f252 775void
864dbfa3 776Perl_do_pipe(pTHX_ SV *sv, GV *rgv, GV *wgv)
afd9f252 777{
79072805 778 register IO *rstio;
779 register IO *wstio;
afd9f252 780 int fd[2];
781
79072805 782 if (!rgv)
afd9f252 783 goto badexit;
79072805 784 if (!wgv)
afd9f252 785 goto badexit;
786
a0d0e21e 787 rstio = GvIOn(rgv);
788 wstio = GvIOn(wgv);
afd9f252 789
a0d0e21e 790 if (IoIFP(rstio))
79072805 791 do_close(rgv,FALSE);
a0d0e21e 792 if (IoIFP(wstio))
79072805 793 do_close(wgv,FALSE);
afd9f252 794
3028581b 795 if (PerlProc_pipe(fd) < 0)
afd9f252 796 goto badexit;
760ac839 797 IoIFP(rstio) = PerlIO_fdopen(fd[0], "r");
798 IoOFP(wstio) = PerlIO_fdopen(fd[1], "w");
8990e307 799 IoIFP(wstio) = IoOFP(wstio);
50952442 800 IoTYPE(rstio) = IoTYPE_RDONLY;
801 IoTYPE(wstio) = IoTYPE_WRONLY;
8990e307 802 if (!IoIFP(rstio) || !IoOFP(wstio)) {
760ac839 803 if (IoIFP(rstio)) PerlIO_close(IoIFP(rstio));
3028581b 804 else PerlLIO_close(fd[0]);
760ac839 805 if (IoOFP(wstio)) PerlIO_close(IoOFP(wstio));
3028581b 806 else PerlLIO_close(fd[1]);
fe14fcc3 807 goto badexit;
808 }
afd9f252 809
3280af22 810 sv_setsv(sv,&PL_sv_yes);
afd9f252 811 return;
812
813badexit:
3280af22 814 sv_setsv(sv,&PL_sv_undef);
afd9f252 815 return;
816}
b1248f16 817#endif
afd9f252 818
517844ec 819/* explicit renamed to avoid C++ conflict -- kja */
a687059c 820bool
864dbfa3 821Perl_do_close(pTHX_ GV *gv, bool not_implicit)
a687059c 822{
1193dd27 823 bool retval;
824 IO *io;
a687059c 825
79072805 826 if (!gv)
3280af22 827 gv = PL_argvgv;
a0d0e21e 828 if (!gv || SvTYPE(gv) != SVt_PVGV) {
1d2dff63 829 if (not_implicit)
830 SETERRNO(EBADF,SS$_IVCHAN);
c2ab57d4 831 return FALSE;
99b89507 832 }
79072805 833 io = GvIO(gv);
834 if (!io) { /* never opened */
1d2dff63 835 if (not_implicit) {
2dd78f96 836 if (ckWARN(WARN_UNOPENED)) /* no check for closed here */
837 report_evil_fh(gv, io, PL_op->op_type);
1d2dff63 838 SETERRNO(EBADF,SS$_IVCHAN);
839 }
a687059c 840 return FALSE;
841 }
f2b5be74 842 retval = io_close(io, not_implicit);
517844ec 843 if (not_implicit) {
1193dd27 844 IoLINES(io) = 0;
845 IoPAGE(io) = 0;
846 IoLINES_LEFT(io) = IoPAGE_LEN(io);
847 }
50952442 848 IoTYPE(io) = IoTYPE_CLOSED;
1193dd27 849 return retval;
850}
851
852bool
f2b5be74 853Perl_io_close(pTHX_ IO *io, bool not_implicit)
1193dd27 854{
855 bool retval = FALSE;
856 int status;
857
8990e307 858 if (IoIFP(io)) {
50952442 859 if (IoTYPE(io) == IoTYPE_PIPE) {
3028581b 860 status = PerlProc_pclose(IoIFP(io));
f2b5be74 861 if (not_implicit) {
862 STATUS_NATIVE_SET(status);
863 retval = (STATUS_POSIX == 0);
864 }
865 else {
866 retval = (status != -1);
867 }
a687059c 868 }
50952442 869 else if (IoTYPE(io) == IoTYPE_STD)
a687059c 870 retval = TRUE;
871 else {
8990e307 872 if (IoOFP(io) && IoOFP(io) != IoIFP(io)) { /* a socket */
760ac839 873 retval = (PerlIO_close(IoOFP(io)) != EOF);
874 PerlIO_close(IoIFP(io)); /* clear stdio, fd already closed */
c2ab57d4 875 }
876 else
760ac839 877 retval = (PerlIO_close(IoIFP(io)) != EOF);
a687059c 878 }
8990e307 879 IoOFP(io) = IoIFP(io) = Nullfp;
79072805 880 }
f2b5be74 881 else if (not_implicit) {
20408e3c 882 SETERRNO(EBADF,SS$_IVCHAN);
883 }
1193dd27 884
a687059c 885 return retval;
886}
887
888bool
864dbfa3 889Perl_do_eof(pTHX_ GV *gv)
a687059c 890{
79072805 891 register IO *io;
a687059c 892 int ch;
893
79072805 894 io = GvIO(gv);
a687059c 895
79072805 896 if (!io)
a687059c 897 return TRUE;
af8c498a 898 else if (ckWARN(WARN_IO)
50952442 899 && (IoTYPE(io) == IoTYPE_WRONLY || IoIFP(io) == PerlIO_stdout()
af8c498a 900 || IoIFP(io) == PerlIO_stderr()))
901 {
2dd78f96 902 /* integrate to report_evil_fh()? */
a1d180c4 903 char *name = NULL;
2dd78f96 904 if (isGV(gv)) {
905 SV* sv = sv_newmortal();
906 gv_efullname4(sv, gv, Nullch, FALSE);
907 name = SvPV_nolen(sv);
908 }
909 if (name && *name)
910 Perl_warner(aTHX_ WARN_IO,
911 "Filehandle %s opened only for output", name);
912 else
913 Perl_warner(aTHX_ WARN_IO,
914 "Filehandle opened only for output");
af8c498a 915 }
a687059c 916
8990e307 917 while (IoIFP(io)) {
a687059c 918
760ac839 919 if (PerlIO_has_cntptr(IoIFP(io))) { /* (the code works without this) */
a20bf0c3 920 if (PerlIO_get_cnt(IoIFP(io)) > 0) /* cheat a little, since */
760ac839 921 return FALSE; /* this is the most usual case */
922 }
a687059c 923
760ac839 924 ch = PerlIO_getc(IoIFP(io));
a687059c 925 if (ch != EOF) {
760ac839 926 (void)PerlIO_ungetc(IoIFP(io),ch);
a687059c 927 return FALSE;
928 }
fab3f3a7 929
760ac839 930 if (PerlIO_has_cntptr(IoIFP(io)) && PerlIO_canset_cnt(IoIFP(io))) {
a20bf0c3 931 if (PerlIO_get_cnt(IoIFP(io)) < -1)
932 PerlIO_set_cnt(IoIFP(io),-1);
760ac839 933 }
533c011a 934 if (PL_op->op_flags & OPf_SPECIAL) { /* not necessarily a real EOF yet? */
3280af22 935 if (!nextargv(PL_argvgv)) /* get another fp handy */
a687059c 936 return TRUE;
937 }
938 else
939 return TRUE; /* normal fp, definitely end of file */
940 }
941 return TRUE;
942}
943
5ff3f7a4 944Off_t
864dbfa3 945Perl_do_tell(pTHX_ GV *gv)
a687059c 946{
79072805 947 register IO *io;
96e4d5b1 948 register PerlIO *fp;
a687059c 949
96e4d5b1 950 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io))) {
bee1dbe2 951#ifdef ULTRIX_STDIO_BOTCH
96e4d5b1 952 if (PerlIO_eof(fp))
953 (void)PerlIO_seek(fp, 0L, 2); /* ultrix 1.2 workaround */
bee1dbe2 954#endif
8903cb82 955 return PerlIO_tell(fp);
96e4d5b1 956 }
411caa50 957 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
958 report_evil_fh(gv, io, PL_op->op_type);
748a9306 959 SETERRNO(EBADF,RMS$_IFI);
5ff3f7a4 960 return (Off_t)-1;
a687059c 961}
962
963bool
864dbfa3 964Perl_do_seek(pTHX_ GV *gv, Off_t pos, int whence)
a687059c 965{
79072805 966 register IO *io;
137443ea 967 register PerlIO *fp;
a687059c 968
137443ea 969 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io))) {
bee1dbe2 970#ifdef ULTRIX_STDIO_BOTCH
137443ea 971 if (PerlIO_eof(fp))
972 (void)PerlIO_seek(fp, 0L, 2); /* ultrix 1.2 workaround */
bee1dbe2 973#endif
8903cb82 974 return PerlIO_seek(fp, pos, whence) >= 0;
137443ea 975 }
411caa50 976 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
977 report_evil_fh(gv, io, PL_op->op_type);
748a9306 978 SETERRNO(EBADF,RMS$_IFI);
a687059c 979 return FALSE;
980}
981
97cc44eb 982Off_t
864dbfa3 983Perl_do_sysseek(pTHX_ GV *gv, Off_t pos, int whence)
8903cb82 984{
985 register IO *io;
986 register PerlIO *fp;
987
988 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io)))
3028581b 989 return PerlLIO_lseek(PerlIO_fileno(fp), pos, whence);
411caa50 990 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
991 report_evil_fh(gv, io, PL_op->op_type);
8903cb82 992 SETERRNO(EBADF,RMS$_IFI);
d9b3e12d 993 return (Off_t)-1;
8903cb82 994}
995
6ff81951 996int
16fe6d59 997Perl_mode_from_discipline(pTHX_ SV *discp)
998{
999 int mode = O_BINARY;
1000 if (discp) {
1001 STRLEN len;
1002 char *s = SvPV(discp,len);
1003 while (*s) {
1004 if (*s == ':') {
1005 switch (s[1]) {
1006 case 'r':
1007 if (len > 3 && strnEQ(s+1, "raw", 3)
1008 && (!s[4] || s[4] == ':' || isSPACE(s[4])))
1009 {
1010 mode = O_BINARY;
1011 s += 4;
1012 len -= 4;
1013 break;
1014 }
1015 /* FALL THROUGH */
1016 case 'c':
1017 if (len > 4 && strnEQ(s+1, "crlf", 4)
1018 && (!s[5] || s[5] == ':' || isSPACE(s[5])))
1019 {
1020 mode = O_TEXT;
1021 s += 5;
1022 len -= 5;
1023 break;
1024 }
1025 /* FALL THROUGH */
1026 default:
1027 goto fail_discipline;
1028 }
1029 }
1030 else if (isSPACE(*s)) {
1031 ++s;
1032 --len;
1033 }
1034 else {
1035 char *end;
1036fail_discipline:
1037 end = strchr(s+1, ':');
1038 if (!end)
1039 end = s+len;
60382766 1040#ifndef PERLIO_LAYERS
16fe6d59 1041 Perl_croak(aTHX_ "Unknown discipline '%.*s'", end-s, s);
60382766 1042#else
1043 s = end;
1044#endif
16fe6d59 1045 }
1046 }
1047 }
1048 return mode;
1049}
1050
1051int
1052Perl_do_binmode(pTHX_ PerlIO *fp, int iotype, int mode)
6ff81951 1053{
60382766 1054 /* The old body of this is now in non-LAYER part of perlio.c
1055 * This is a stub for any XS code which might have been calling it.
1056 */
1057 char *name = (O_BINARY != O_TEXT && !(mode & O_BINARY)) ? ":crlf" : ":raw";
1058 return PerlIO_binmode(aTHX_ fp, iotype, mode, name);
6ff81951 1059}
1060
a0d0e21e 1061#if !defined(HAS_TRUNCATE) && !defined(HAS_CHSIZE) && defined(F_FREESP)
c2ab57d4 1062 /* code courtesy of William Kucharski */
fe14fcc3 1063#define HAS_CHSIZE
6eb13c3b 1064
517844ec 1065I32 my_chsize(fd, length)
79072805 1066I32 fd; /* file descriptor */
85e6fe83 1067Off_t length; /* length to set file to */
6eb13c3b 1068{
6eb13c3b 1069 struct flock fl;
1070 struct stat filebuf;
1071
3028581b 1072 if (PerlLIO_fstat(fd, &filebuf) < 0)
6eb13c3b 1073 return -1;
1074
1075 if (filebuf.st_size < length) {
1076
1077 /* extend file length */
1078
3028581b 1079 if ((PerlLIO_lseek(fd, (length - 1), 0)) < 0)
6eb13c3b 1080 return -1;
1081
1082 /* write a "0" byte */
1083
3028581b 1084 if ((PerlLIO_write(fd, "", 1)) != 1)
6eb13c3b 1085 return -1;
1086 }
1087 else {
1088 /* truncate length */
1089
1090 fl.l_whence = 0;
1091 fl.l_len = 0;
1092 fl.l_start = length;
a0d0e21e 1093 fl.l_type = F_WRLCK; /* write lock on file space */
6eb13c3b 1094
1095 /*
a0d0e21e 1096 * This relies on the UNDOCUMENTED F_FREESP argument to
6eb13c3b 1097 * fcntl(2), which truncates the file so that it ends at the
1098 * position indicated by fl.l_start.
1099 *
1100 * Will minor miracles never cease?
1101 */
1102
a0d0e21e 1103 if (fcntl(fd, F_FREESP, &fl) < 0)
6eb13c3b 1104 return -1;
1105
1106 }
1107
1108 return 0;
1109}
a0d0e21e 1110#endif /* F_FREESP */
ff8e2863 1111
a687059c 1112bool
864dbfa3 1113Perl_do_print(pTHX_ register SV *sv, PerlIO *fp)
a687059c 1114{
1115 register char *tmps;
463ee0b2 1116 STRLEN len;
a687059c 1117
79072805 1118 /* assuming fp is checked earlier */
1119 if (!sv)
1120 return TRUE;
3280af22 1121 if (PL_ofmt) {
8990e307 1122 if (SvGMAGICAL(sv))
79072805 1123 mg_get(sv);
463ee0b2 1124 if (SvIOK(sv) && SvIVX(sv) != 0) {
65202027 1125 PerlIO_printf(fp, PL_ofmt, (NV)SvIVX(sv));
760ac839 1126 return !PerlIO_error(fp);
79072805 1127 }
463ee0b2 1128 if ( (SvNOK(sv) && SvNVX(sv) != 0.0)
79072805 1129 || (looks_like_number(sv) && sv_2nv(sv) != 0.0) ) {
3280af22 1130 PerlIO_printf(fp, PL_ofmt, SvNVX(sv));
760ac839 1131 return !PerlIO_error(fp);
79072805 1132 }
a687059c 1133 }
79072805 1134 switch (SvTYPE(sv)) {
1135 case SVt_NULL:
411caa50 1136 if (ckWARN(WARN_UNINITIALIZED))
1137 report_uninit();
ff8e2863 1138 return TRUE;
79072805 1139 case SVt_IV:
a0d0e21e 1140 if (SvIOK(sv)) {
1141 if (SvGMAGICAL(sv))
1142 mg_get(sv);
cf2093f6 1143 if (SvIsUV(sv))
57def98f 1144 PerlIO_printf(fp, "%"UVuf, (UV)SvUVX(sv));
cf2093f6 1145 else
57def98f 1146 PerlIO_printf(fp, "%"IVdf, (IV)SvIVX(sv));
760ac839 1147 return !PerlIO_error(fp);
a0d0e21e 1148 }
1149 /* FALL THROUGH */
79072805 1150 default:
fba9e08b 1151#if 0
4b3603a4 1152 /* XXX Fix this when the I/O disciplines arrive. XXX */
1153 if (DO_UTF8(sv))
1154 sv_utf8_downgrade(sv, FALSE);
fba9e08b 1155#endif
463ee0b2 1156 tmps = SvPV(sv, len);
79072805 1157 break;
ff8e2863 1158 }
94e4c244 1159 /* To detect whether the process is about to overstep its
1160 * filesize limit we would need getrlimit(). We could then
1161 * also transparently raise the limit with setrlimit() --
1162 * but only until the system hard limit/the filesystem limit,
c5dd3cdd 1163 * at which we would get EPERM. Note that when using buffered
1164 * io the write failure can be delayed until the flush/close. --jhi */
a21ac455 1165 if (len && (PerlIO_write(fp,tmps,len) == 0))
a687059c 1166 return FALSE;
760ac839 1167 return !PerlIO_error(fp);
a687059c 1168}
1169
79072805 1170I32
cea2e8a9 1171Perl_my_stat(pTHX)
a687059c 1172{
4e35701f 1173 djSP;
79072805 1174 IO *io;
2dd78f96 1175 GV* gv;
79072805 1176
533c011a 1177 if (PL_op->op_flags & OPf_REF) {
924508f0 1178 EXTEND(SP,1);
2dd78f96 1179 gv = cGVOP_gv;
748a9306 1180 do_fstat:
2dd78f96 1181 io = GvIO(gv);
8990e307 1182 if (io && IoIFP(io)) {
2dd78f96 1183 PL_statgv = gv;
3280af22 1184 sv_setpv(PL_statname,"");
1185 PL_laststype = OP_STAT;
1186 return (PL_laststatval = PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache));
a687059c 1187 }
1188 else {
2dd78f96 1189 if (gv == PL_defgv)
3280af22 1190 return PL_laststatval;
2dd78f96 1191 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1192 report_evil_fh(gv, io, PL_op->op_type);
3280af22 1193 PL_statgv = Nullgv;
1194 sv_setpv(PL_statname,"");
1195 return (PL_laststatval = -1);
a687059c 1196 }
1197 }
1198 else {
748a9306 1199 SV* sv = POPs;
4b74e3fb 1200 char *s;
2d8e6c8d 1201 STRLEN n_a;
79072805 1202 PUTBACK;
748a9306 1203 if (SvTYPE(sv) == SVt_PVGV) {
2dd78f96 1204 gv = (GV*)sv;
748a9306 1205 goto do_fstat;
1206 }
1207 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
2dd78f96 1208 gv = (GV*)SvRV(sv);
748a9306 1209 goto do_fstat;
1210 }
1211
2d8e6c8d 1212 s = SvPV(sv, n_a);
3280af22 1213 PL_statgv = Nullgv;
1214 sv_setpv(PL_statname, s);
1215 PL_laststype = OP_STAT;
1216 PL_laststatval = PerlLIO_stat(s, &PL_statcache);
599cee73 1217 if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(s, '\n'))
cea2e8a9 1218 Perl_warner(aTHX_ WARN_NEWLINE, PL_warn_nl, "stat");
3280af22 1219 return PL_laststatval;
a687059c 1220 }
1221}
1222
79072805 1223I32
cea2e8a9 1224Perl_my_lstat(pTHX)
c623bd54 1225{
4e35701f 1226 djSP;
79072805 1227 SV *sv;
2d8e6c8d 1228 STRLEN n_a;
533c011a 1229 if (PL_op->op_flags & OPf_REF) {
924508f0 1230 EXTEND(SP,1);
638eceb6 1231 if (cGVOP_gv == PL_defgv) {
3280af22 1232 if (PL_laststype != OP_LSTAT)
cea2e8a9 1233 Perl_croak(aTHX_ "The stat preceding -l _ wasn't an lstat");
3280af22 1234 return PL_laststatval;
fe14fcc3 1235 }
cea2e8a9 1236 Perl_croak(aTHX_ "You can't use -l on a filehandle");
fe14fcc3 1237 }
c623bd54 1238
3280af22 1239 PL_laststype = OP_LSTAT;
1240 PL_statgv = Nullgv;
79072805 1241 sv = POPs;
1242 PUTBACK;
2d8e6c8d 1243 sv_setpv(PL_statname,SvPV(sv, n_a));
2d8e6c8d 1244 PL_laststatval = PerlLIO_lstat(SvPV(sv, n_a),&PL_statcache);
2d8e6c8d 1245 if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(SvPV(sv, n_a), '\n'))
cea2e8a9 1246 Perl_warner(aTHX_ WARN_NEWLINE, PL_warn_nl, "lstat");
3280af22 1247 return PL_laststatval;
c623bd54 1248}
1249
a687059c 1250bool
864dbfa3 1251Perl_do_aexec(pTHX_ SV *really, register SV **mark, register SV **sp)
a687059c 1252{
d5a9bfb0 1253 return do_aexec5(really, mark, sp, 0, 0);
1254}
1255
1256bool
2aa1486d 1257Perl_do_aexec5(pTHX_ SV *really, register SV **mark, register SV **sp,
1258 int fd, int do_report)
d5a9bfb0 1259{
cd39f2b6 1260#ifdef MACOS_TRADITIONAL
1261 Perl_croak(aTHX_ "exec? I'm not *that* kind of operating system");
1262#else
a687059c 1263 register char **a;
a687059c 1264 char *tmps;
2d8e6c8d 1265 STRLEN n_a;
a687059c 1266
79072805 1267 if (sp > mark) {
3280af22 1268 New(401,PL_Argv, sp - mark + 1, char*);
1269 a = PL_Argv;
79072805 1270 while (++mark <= sp) {
1271 if (*mark)
2d8e6c8d 1272 *a++ = SvPVx(*mark, n_a);
a687059c 1273 else
1274 *a++ = "";
1275 }
1276 *a = Nullch;
3280af22 1277 if (*PL_Argv[0] != '/') /* will execvp use PATH? */
79072805 1278 TAINT_ENV(); /* testing IFS here is overkill, probably */
2d8e6c8d 1279 if (really && *(tmps = SvPV(really, n_a)))
b4748376 1280 PerlProc_execvp(tmps,EXEC_ARGV_CAST(PL_Argv));
a687059c 1281 else
b4748376 1282 PerlProc_execvp(PL_Argv[0],EXEC_ARGV_CAST(PL_Argv));
599cee73 1283 if (ckWARN(WARN_EXEC))
a1d180c4 1284 Perl_warner(aTHX_ WARN_EXEC, "Can't exec \"%s\": %s",
599cee73 1285 PL_Argv[0], Strerror(errno));
d5a9bfb0 1286 if (do_report) {
1287 int e = errno;
1288
1289 PerlLIO_write(fd, (void*)&e, sizeof(int));
1290 PerlLIO_close(fd);
1291 }
a687059c 1292 }
bee1dbe2 1293 do_execfree();
cd39f2b6 1294#endif
a687059c 1295 return FALSE;
1296}
1297
fe14fcc3 1298void
864dbfa3 1299Perl_do_execfree(pTHX)
ff8e2863 1300{
3280af22 1301 if (PL_Argv) {
1302 Safefree(PL_Argv);
1303 PL_Argv = Null(char **);
ff8e2863 1304 }
3280af22 1305 if (PL_Cmd) {
1306 Safefree(PL_Cmd);
1307 PL_Cmd = Nullch;
ff8e2863 1308 }
1309}
1310
cd39f2b6 1311#if !defined(OS2) && !defined(WIN32) && !defined(DJGPP) && !defined(EPOC) && !defined(MACOS_TRADITIONAL)
760ac839 1312
a687059c 1313bool
864dbfa3 1314Perl_do_exec(pTHX_ char *cmd)
a687059c 1315{
e446cec8 1316 return do_exec3(cmd,0,0);
1317}
1318
1319bool
864dbfa3 1320Perl_do_exec3(pTHX_ char *cmd, int fd, int do_report)
e446cec8 1321{
a687059c 1322 register char **a;
1323 register char *s;
a687059c 1324 char flags[10];
1325
748a9306 1326 while (*cmd && isSPACE(*cmd))
1327 cmd++;
1328
a687059c 1329 /* save an extra exec if possible */
1330
bf38876a 1331#ifdef CSH
3280af22 1332 if (strnEQ(cmd,PL_cshname,PL_cshlen) && strnEQ(cmd+PL_cshlen," -c",3)) {
a687059c 1333 strcpy(flags,"-c");
3280af22 1334 s = cmd+PL_cshlen+3;
a687059c 1335 if (*s == 'f') {
1336 s++;
1337 strcat(flags,"f");
1338 }
1339 if (*s == ' ')
1340 s++;
1341 if (*s++ == '\'') {
1342 char *ncmd = s;
1343
1344 while (*s)
1345 s++;
1346 if (s[-1] == '\n')
1347 *--s = '\0';
1348 if (s[-1] == '\'') {
1349 *--s = '\0';
3280af22 1350 PerlProc_execl(PL_cshname,"csh", flags,ncmd,(char*)0);
a687059c 1351 *s = '\'';
1352 return FALSE;
1353 }
1354 }
1355 }
bf38876a 1356#endif /* CSH */
a687059c 1357
1358 /* see if there are shell metacharacters in it */
1359
748a9306 1360 if (*cmd == '.' && isSPACE(cmd[1]))
1361 goto doshell;
1362
1363 if (strnEQ(cmd,"exec",4) && isSPACE(cmd[4]))
1364 goto doshell;
1365
c170e444 1366 for (s = cmd; *s && isALNUM(*s); s++) ; /* catch VAR=val gizmo */
63f2c1e1 1367 if (*s == '=')
1368 goto doshell;
748a9306 1369
a687059c 1370 for (s = cmd; *s; s++) {
93a17b20 1371 if (*s != ' ' && !isALPHA(*s) && strchr("$&*(){}[]'\";\\|?<>~`\n",*s)) {
a687059c 1372 if (*s == '\n' && !s[1]) {
1373 *s = '\0';
1374 break;
1375 }
603a98b0 1376 /* handle the 2>&1 construct at the end */
1377 if (*s == '>' && s[1] == '&' && s[2] == '1'
1378 && s > cmd + 1 && s[-1] == '2' && isSPACE(s[-2])
1379 && (!s[3] || isSPACE(s[3])))
1380 {
1381 char *t = s + 3;
1382
1383 while (*t && isSPACE(*t))
1384 ++t;
1385 if (!*t && (dup2(1,2) != -1)) {
1386 s[-2] = '\0';
1387 break;
1388 }
1389 }
a687059c 1390 doshell:
3280af22 1391 PerlProc_execl(PL_sh_path, "sh", "-c", cmd, (char*)0);
a687059c 1392 return FALSE;
1393 }
1394 }
748a9306 1395
3280af22 1396 New(402,PL_Argv, (s - cmd) / 2 + 2, char*);
1397 PL_Cmd = savepvn(cmd, s-cmd);
1398 a = PL_Argv;
1399 for (s = PL_Cmd; *s;) {
99b89507 1400 while (*s && isSPACE(*s)) s++;
a687059c 1401 if (*s)
1402 *(a++) = s;
99b89507 1403 while (*s && !isSPACE(*s)) s++;
a687059c 1404 if (*s)
1405 *s++ = '\0';
1406 }
1407 *a = Nullch;
3280af22 1408 if (PL_Argv[0]) {
1409 PerlProc_execvp(PL_Argv[0],PL_Argv);
b1248f16 1410 if (errno == ENOEXEC) { /* for system V NIH syndrome */
ff8e2863 1411 do_execfree();
a687059c 1412 goto doshell;
b1248f16 1413 }
d008e5eb 1414 {
e446cec8 1415 int e = errno;
1416
d008e5eb 1417 if (ckWARN(WARN_EXEC))
a1d180c4 1418 Perl_warner(aTHX_ WARN_EXEC, "Can't exec \"%s\": %s",
d008e5eb 1419 PL_Argv[0], Strerror(errno));
e446cec8 1420 if (do_report) {
1421 PerlLIO_write(fd, (void*)&e, sizeof(int));
1422 PerlLIO_close(fd);
1423 }
d008e5eb 1424 }
a687059c 1425 }
ff8e2863 1426 do_execfree();
a687059c 1427 return FALSE;
1428}
1429
6890e559 1430#endif /* OS2 || WIN32 */
760ac839 1431
79072805 1432I32
864dbfa3 1433Perl_apply(pTHX_ I32 type, register SV **mark, register SV **sp)
a687059c 1434{
79072805 1435 register I32 val;
1436 register I32 val2;
1437 register I32 tot = 0;
20408e3c 1438 char *what;
a687059c 1439 char *s;
79072805 1440 SV **oldmark = mark;
2d8e6c8d 1441 STRLEN n_a;
a687059c 1442
20408e3c 1443#define APPLY_TAINT_PROPER() \
3280af22 1444 STMT_START { \
17406bd6 1445 if (PL_tainted) { TAINT_PROPER(what); } \
873ef191 1446 } STMT_END
20408e3c 1447
1448 /* This is a first heuristic; it doesn't catch tainting magic. */
3280af22 1449 if (PL_tainting) {
463ee0b2 1450 while (++mark <= sp) {
bbce6d69 1451 if (SvTAINTED(*mark)) {
1452 TAINT;
1453 break;
1454 }
463ee0b2 1455 }
1456 mark = oldmark;
1457 }
a687059c 1458 switch (type) {
79072805 1459 case OP_CHMOD:
20408e3c 1460 what = "chmod";
1461 APPLY_TAINT_PROPER();
79072805 1462 if (++mark <= sp) {
463ee0b2 1463 val = SvIVx(*mark);
20408e3c 1464 APPLY_TAINT_PROPER();
1465 tot = sp - mark;
79072805 1466 while (++mark <= sp) {
2d8e6c8d 1467 char *name = SvPVx(*mark, n_a);
20408e3c 1468 APPLY_TAINT_PROPER();
1469 if (PerlLIO_chmod(name, val))
a687059c 1470 tot--;
1471 }
1472 }
1473 break;
fe14fcc3 1474#ifdef HAS_CHOWN
79072805 1475 case OP_CHOWN:
20408e3c 1476 what = "chown";
1477 APPLY_TAINT_PROPER();
79072805 1478 if (sp - mark > 2) {
463ee0b2 1479 val = SvIVx(*++mark);
1480 val2 = SvIVx(*++mark);
20408e3c 1481 APPLY_TAINT_PROPER();
a0d0e21e 1482 tot = sp - mark;
79072805 1483 while (++mark <= sp) {
2d8e6c8d 1484 char *name = SvPVx(*mark, n_a);
20408e3c 1485 APPLY_TAINT_PROPER();
36660982 1486 if (PerlLIO_chown(name, val, val2))
a687059c 1487 tot--;
1488 }
1489 }
1490 break;
b1248f16 1491#endif
a1d180c4 1492/*
dd64f1c3 1493XXX Should we make lchown() directly available from perl?
1494For now, we'll let Configure test for HAS_LCHOWN, but do
1495nothing in the core.
1496 --AD 5/1998
1497*/
fe14fcc3 1498#ifdef HAS_KILL
79072805 1499 case OP_KILL:
20408e3c 1500 what = "kill";
1501 APPLY_TAINT_PROPER();
55497cff 1502 if (mark == sp)
1503 break;
2d8e6c8d 1504 s = SvPVx(*++mark, n_a);
79072805 1505 if (isUPPER(*s)) {
1506 if (*s == 'S' && s[1] == 'I' && s[2] == 'G')
1507 s += 3;
1508 if (!(val = whichsig(s)))
cea2e8a9 1509 Perl_croak(aTHX_ "Unrecognized signal name \"%s\"",s);
79072805 1510 }
1511 else
463ee0b2 1512 val = SvIVx(*mark);
20408e3c 1513 APPLY_TAINT_PROPER();
1514 tot = sp - mark;
3595fcef 1515#ifdef VMS
1516 /* kill() doesn't do process groups (job trees?) under VMS */
1517 if (val < 0) val = -val;
1518 if (val == SIGKILL) {
1519# include <starlet.h>
1520 /* Use native sys$delprc() to insure that target process is
1521 * deleted; supervisor-mode images don't pay attention to
1522 * CRTL's emulation of Unix-style signals and kill()
1523 */
1524 while (++mark <= sp) {
1525 I32 proc = SvIVx(*mark);
1526 register unsigned long int __vmssts;
20408e3c 1527 APPLY_TAINT_PROPER();
3595fcef 1528 if (!((__vmssts = sys$delprc(&proc,0)) & 1)) {
1529 tot--;
1530 switch (__vmssts) {
1531 case SS$_NONEXPR:
1532 case SS$_NOSUCHNODE:
1533 SETERRNO(ESRCH,__vmssts);
1534 break;
1535 case SS$_NOPRIV:
1536 SETERRNO(EPERM,__vmssts);
1537 break;
1538 default:
1539 SETERRNO(EVMSERR,__vmssts);
1540 }
1541 }
1542 }
1543 break;
1544 }
1545#endif
79072805 1546 if (val < 0) {
1547 val = -val;
1548 while (++mark <= sp) {
463ee0b2 1549 I32 proc = SvIVx(*mark);
20408e3c 1550 APPLY_TAINT_PROPER();
fe14fcc3 1551#ifdef HAS_KILLPG
3028581b 1552 if (PerlProc_killpg(proc,val)) /* BSD */
a687059c 1553#else
3028581b 1554 if (PerlProc_kill(-proc,val)) /* SYSV */
a687059c 1555#endif
79072805 1556 tot--;
a687059c 1557 }
79072805 1558 }
1559 else {
1560 while (++mark <= sp) {
20408e3c 1561 I32 proc = SvIVx(*mark);
1562 APPLY_TAINT_PROPER();
1563 if (PerlProc_kill(proc, val))
79072805 1564 tot--;
a687059c 1565 }
1566 }
1567 break;
b1248f16 1568#endif
79072805 1569 case OP_UNLINK:
20408e3c 1570 what = "unlink";
1571 APPLY_TAINT_PROPER();
79072805 1572 tot = sp - mark;
1573 while (++mark <= sp) {
2d8e6c8d 1574 s = SvPVx(*mark, n_a);
20408e3c 1575 APPLY_TAINT_PROPER();
3280af22 1576 if (PL_euid || PL_unsafe) {
a687059c 1577 if (UNLINK(s))
1578 tot--;
1579 }
1580 else { /* don't let root wipe out directories without -U */
3280af22 1581 if (PerlLIO_lstat(s,&PL_statbuf) < 0 || S_ISDIR(PL_statbuf.st_mode))
a687059c 1582 tot--;
1583 else {
1584 if (UNLINK(s))
1585 tot--;
1586 }
1587 }
1588 }
1589 break;
a0d0e21e 1590#ifdef HAS_UTIME
79072805 1591 case OP_UTIME:
20408e3c 1592 what = "utime";
1593 APPLY_TAINT_PROPER();
79072805 1594 if (sp - mark > 2) {
748a9306 1595#if defined(I_UTIME) || defined(VMS)
663a0e37 1596 struct utimbuf utbuf;
1597#else
a687059c 1598 struct {
dd2821f6 1599 Time_t actime;
1600 Time_t modtime;
a687059c 1601 } utbuf;
663a0e37 1602#endif
a687059c 1603
afd9f252 1604 Zero(&utbuf, sizeof utbuf, char);
517844ec 1605#ifdef BIG_TIME
dd2821f6 1606 utbuf.actime = (Time_t)SvNVx(*++mark); /* time accessed */
1607 utbuf.modtime = (Time_t)SvNVx(*++mark); /* time modified */
517844ec 1608#else
dd2821f6 1609 utbuf.actime = (Time_t)SvIVx(*++mark); /* time accessed */
1610 utbuf.modtime = (Time_t)SvIVx(*++mark); /* time modified */
517844ec 1611#endif
20408e3c 1612 APPLY_TAINT_PROPER();
79072805 1613 tot = sp - mark;
1614 while (++mark <= sp) {
2d8e6c8d 1615 char *name = SvPVx(*mark, n_a);
20408e3c 1616 APPLY_TAINT_PROPER();
1617 if (PerlLIO_utime(name, &utbuf))
a687059c 1618 tot--;
1619 }
a687059c 1620 }
1621 else
79072805 1622 tot = 0;
a687059c 1623 break;
a0d0e21e 1624#endif
a687059c 1625 }
1626 return tot;
20408e3c 1627
20408e3c 1628#undef APPLY_TAINT_PROPER
a687059c 1629}
1630
1631/* Do the permissions allow some operation? Assumes statcache already set. */
a0d0e21e 1632#ifndef VMS /* VMS' cando is in vms.c */
7f4774ae 1633bool
1634Perl_cando(pTHX_ Mode_t mode, Uid_t effective, register Stat_t *statbufp)
1635/* Note: we use `effective' both for uids and gids.
1636 * Here we are betting on Uid_t being equal or wider than Gid_t. */
a687059c 1637{
bee1dbe2 1638#ifdef DOSISH
fe14fcc3 1639 /* [Comments and code from Len Reed]
1640 * MS-DOS "user" is similar to UNIX's "superuser," but can't write
1641 * to write-protected files. The execute permission bit is set
1642 * by the Miscrosoft C library stat() function for the following:
1643 * .exe files
1644 * .com files
1645 * .bat files
1646 * directories
1647 * All files and directories are readable.
1648 * Directories and special files, e.g. "CON", cannot be
1649 * write-protected.
1650 * [Comment by Tom Dinger -- a directory can have the write-protect
1651 * bit set in the file system, but DOS permits changes to
1652 * the directory anyway. In addition, all bets are off
1653 * here for networked software, such as Novell and
1654 * Sun's PC-NFS.]
1655 */
1656
bee1dbe2 1657 /* Atari stat() does pretty much the same thing. we set x_bit_set_in_stat
1658 * too so it will actually look into the files for magic numbers
1659 */
7f4774ae 1660 return (mode & statbufp->st_mode) ? TRUE : FALSE;
fe14fcc3 1661
55497cff 1662#else /* ! DOSISH */
3280af22 1663 if ((effective ? PL_euid : PL_uid) == 0) { /* root is special */
7f4774ae 1664 if (mode == S_IXUSR) {
c623bd54 1665 if (statbufp->st_mode & 0111 || S_ISDIR(statbufp->st_mode))
a687059c 1666 return TRUE;
1667 }
1668 else
1669 return TRUE; /* root reads and writes anything */
1670 return FALSE;
1671 }
3280af22 1672 if (statbufp->st_uid == (effective ? PL_euid : PL_uid) ) {
7f4774ae 1673 if (statbufp->st_mode & mode)
a687059c 1674 return TRUE; /* ok as "user" */
1675 }
d8eceb89 1676 else if (ingroup(statbufp->st_gid,effective)) {
7f4774ae 1677 if (statbufp->st_mode & mode >> 3)
a687059c 1678 return TRUE; /* ok as "group" */
1679 }
7f4774ae 1680 else if (statbufp->st_mode & mode >> 6)
a687059c 1681 return TRUE; /* ok as "other" */
1682 return FALSE;
55497cff 1683#endif /* ! DOSISH */
a687059c 1684}
a0d0e21e 1685#endif /* ! VMS */
a687059c 1686
d8eceb89 1687bool
1688Perl_ingroup(pTHX_ Gid_t testgid, Uid_t effective)
a687059c 1689{
cd39f2b6 1690#ifdef MACOS_TRADITIONAL
1691 /* This is simply not correct for AppleShare, but fix it yerself. */
1692 return TRUE;
1693#else
3280af22 1694 if (testgid == (effective ? PL_egid : PL_gid))
a687059c 1695 return TRUE;
fe14fcc3 1696#ifdef HAS_GETGROUPS
a687059c 1697#ifndef NGROUPS
1698#define NGROUPS 32
1699#endif
1700 {
a0d0e21e 1701 Groups_t gary[NGROUPS];
79072805 1702 I32 anum;
a687059c 1703
1704 anum = getgroups(NGROUPS,gary);
1705 while (--anum >= 0)
1706 if (gary[anum] == testgid)
1707 return TRUE;
1708 }
1709#endif
1710 return FALSE;
cd39f2b6 1711#endif
a687059c 1712}
c2ab57d4 1713
fe14fcc3 1714#if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
c2ab57d4 1715
79072805 1716I32
864dbfa3 1717Perl_do_ipcget(pTHX_ I32 optype, SV **mark, SV **sp)
c2ab57d4 1718{
c2ab57d4 1719 key_t key;
79072805 1720 I32 n, flags;
c2ab57d4 1721
463ee0b2 1722 key = (key_t)SvNVx(*++mark);
1723 n = (optype == OP_MSGGET) ? 0 : SvIVx(*++mark);
1724 flags = SvIVx(*++mark);
748a9306 1725 SETERRNO(0,0);
c2ab57d4 1726 switch (optype)
1727 {
fe14fcc3 1728#ifdef HAS_MSG
79072805 1729 case OP_MSGGET:
c2ab57d4 1730 return msgget(key, flags);
e5d73d77 1731#endif
fe14fcc3 1732#ifdef HAS_SEM
79072805 1733 case OP_SEMGET:
c2ab57d4 1734 return semget(key, n, flags);
e5d73d77 1735#endif
fe14fcc3 1736#ifdef HAS_SHM
79072805 1737 case OP_SHMGET:
c2ab57d4 1738 return shmget(key, n, flags);
e5d73d77 1739#endif
fe14fcc3 1740#if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
e5d73d77 1741 default:
cea2e8a9 1742 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
e5d73d77 1743#endif
c2ab57d4 1744 }
1745 return -1; /* should never happen */
1746}
1747
79072805 1748I32
864dbfa3 1749Perl_do_ipcctl(pTHX_ I32 optype, SV **mark, SV **sp)
c2ab57d4 1750{
79072805 1751 SV *astr;
c2ab57d4 1752 char *a;
a0d0e21e 1753 I32 id, n, cmd, infosize, getinfo;
1754 I32 ret = -1;
c2ab57d4 1755
463ee0b2 1756 id = SvIVx(*++mark);
1757 n = (optype == OP_SEMCTL) ? SvIVx(*++mark) : 0;
1758 cmd = SvIVx(*++mark);
79072805 1759 astr = *++mark;
c2ab57d4 1760 infosize = 0;
1761 getinfo = (cmd == IPC_STAT);
1762
1763 switch (optype)
1764 {
fe14fcc3 1765#ifdef HAS_MSG
79072805 1766 case OP_MSGCTL:
c2ab57d4 1767 if (cmd == IPC_STAT || cmd == IPC_SET)
1768 infosize = sizeof(struct msqid_ds);
1769 break;
e5d73d77 1770#endif
fe14fcc3 1771#ifdef HAS_SHM
79072805 1772 case OP_SHMCTL:
c2ab57d4 1773 if (cmd == IPC_STAT || cmd == IPC_SET)
1774 infosize = sizeof(struct shmid_ds);
1775 break;
e5d73d77 1776#endif
fe14fcc3 1777#ifdef HAS_SEM
79072805 1778 case OP_SEMCTL:
39398f3f 1779#ifdef Semctl
c2ab57d4 1780 if (cmd == IPC_STAT || cmd == IPC_SET)
1781 infosize = sizeof(struct semid_ds);
1782 else if (cmd == GETALL || cmd == SETALL)
1783 {
8e591e46 1784 struct semid_ds semds;
bd89102f 1785 union semun semun;
e6f0bdd6 1786#ifdef EXTRA_F_IN_SEMUN_BUF
1787 semun.buff = &semds;
1788#else
84902520 1789 semun.buf = &semds;
e6f0bdd6 1790#endif
c2ab57d4 1791 getinfo = (cmd == GETALL);
9b89d93d 1792 if (Semctl(id, 0, IPC_STAT, semun) == -1)
1793 return -1;
6e21c824 1794 infosize = semds.sem_nsems * sizeof(short);
1795 /* "short" is technically wrong but much more portable
1796 than guessing about u_?short(_t)? */
c2ab57d4 1797 }
39398f3f 1798#else
cea2e8a9 1799 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
39398f3f 1800#endif
c2ab57d4 1801 break;
e5d73d77 1802#endif
fe14fcc3 1803#if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
e5d73d77 1804 default:
cea2e8a9 1805 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
e5d73d77 1806#endif
c2ab57d4 1807 }
1808
1809 if (infosize)
1810 {
a0d0e21e 1811 STRLEN len;
c2ab57d4 1812 if (getinfo)
1813 {
a0d0e21e 1814 SvPV_force(astr, len);
1815 a = SvGROW(astr, infosize+1);
c2ab57d4 1816 }
1817 else
1818 {
463ee0b2 1819 a = SvPV(astr, len);
1820 if (len != infosize)
cea2e8a9 1821 Perl_croak(aTHX_ "Bad arg length for %s, is %lu, should be %ld",
4ec43091 1822 PL_op_desc[optype],
1823 (unsigned long)len,
1824 (long)infosize);
c2ab57d4 1825 }
1826 }
1827 else
1828 {
c030ccd9 1829 IV i = SvIV(astr);
56431972 1830 a = INT2PTR(char *,i); /* ouch */
c2ab57d4 1831 }
748a9306 1832 SETERRNO(0,0);
c2ab57d4 1833 switch (optype)
1834 {
fe14fcc3 1835#ifdef HAS_MSG
79072805 1836 case OP_MSGCTL:
bee1dbe2 1837 ret = msgctl(id, cmd, (struct msqid_ds *)a);
c2ab57d4 1838 break;
e5d73d77 1839#endif
fe14fcc3 1840#ifdef HAS_SEM
bd89102f 1841 case OP_SEMCTL: {
39398f3f 1842#ifdef Semctl
bd89102f 1843 union semun unsemds;
1844
e6f0bdd6 1845#ifdef EXTRA_F_IN_SEMUN_BUF
1846 unsemds.buff = (struct semid_ds *)a;
1847#else
bd89102f 1848 unsemds.buf = (struct semid_ds *)a;
e6f0bdd6 1849#endif
bd89102f 1850 ret = Semctl(id, n, cmd, unsemds);
39398f3f 1851#else
cea2e8a9 1852 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
39398f3f 1853#endif
bd89102f 1854 }
c2ab57d4 1855 break;
e5d73d77 1856#endif
fe14fcc3 1857#ifdef HAS_SHM
79072805 1858 case OP_SHMCTL:
bee1dbe2 1859 ret = shmctl(id, cmd, (struct shmid_ds *)a);
c2ab57d4 1860 break;
e5d73d77 1861#endif
c2ab57d4 1862 }
1863 if (getinfo && ret >= 0) {
79072805 1864 SvCUR_set(astr, infosize);
1865 *SvEND(astr) = '\0';
a0d0e21e 1866 SvSETMAGIC(astr);
c2ab57d4 1867 }
1868 return ret;
1869}
1870
79072805 1871I32
864dbfa3 1872Perl_do_msgsnd(pTHX_ SV **mark, SV **sp)
c2ab57d4 1873{
fe14fcc3 1874#ifdef HAS_MSG
79072805 1875 SV *mstr;
c2ab57d4 1876 char *mbuf;
79072805 1877 I32 id, msize, flags;
463ee0b2 1878 STRLEN len;
c2ab57d4 1879
463ee0b2 1880 id = SvIVx(*++mark);
79072805 1881 mstr = *++mark;
463ee0b2 1882 flags = SvIVx(*++mark);
1883 mbuf = SvPV(mstr, len);
1884 if ((msize = len - sizeof(long)) < 0)
cea2e8a9 1885 Perl_croak(aTHX_ "Arg too short for msgsnd");
748a9306 1886 SETERRNO(0,0);
bee1dbe2 1887 return msgsnd(id, (struct msgbuf *)mbuf, msize, flags);
e5d73d77 1888#else
cea2e8a9 1889 Perl_croak(aTHX_ "msgsnd not implemented");
e5d73d77 1890#endif
c2ab57d4 1891}
1892
79072805 1893I32
864dbfa3 1894Perl_do_msgrcv(pTHX_ SV **mark, SV **sp)
c2ab57d4 1895{
fe14fcc3 1896#ifdef HAS_MSG
79072805 1897 SV *mstr;
c2ab57d4 1898 char *mbuf;
1899 long mtype;
79072805 1900 I32 id, msize, flags, ret;
463ee0b2 1901 STRLEN len;
79072805 1902
463ee0b2 1903 id = SvIVx(*++mark);
79072805 1904 mstr = *++mark;
c2e66d9e 1905 /* suppress warning when reading into undef var --jhi */
1906 if (! SvOK(mstr))
1907 sv_setpvn(mstr, "", 0);
463ee0b2 1908 msize = SvIVx(*++mark);
1909 mtype = (long)SvIVx(*++mark);
1910 flags = SvIVx(*++mark);
a0d0e21e 1911 SvPV_force(mstr, len);
1912 mbuf = SvGROW(mstr, sizeof(long)+msize+1);
a1d180c4 1913
748a9306 1914 SETERRNO(0,0);
bee1dbe2 1915 ret = msgrcv(id, (struct msgbuf *)mbuf, msize, mtype, flags);
c2ab57d4 1916 if (ret >= 0) {
79072805 1917 SvCUR_set(mstr, sizeof(long)+ret);
1918 *SvEND(mstr) = '\0';
41d6edb2 1919#ifndef INCOMPLETE_TAINTS
1920 /* who knows who has been playing with this message? */
1921 SvTAINTED_on(mstr);
1922#endif
c2ab57d4 1923 }
1924 return ret;
e5d73d77 1925#else
cea2e8a9 1926 Perl_croak(aTHX_ "msgrcv not implemented");
e5d73d77 1927#endif
c2ab57d4 1928}
1929
79072805 1930I32
864dbfa3 1931Perl_do_semop(pTHX_ SV **mark, SV **sp)
c2ab57d4 1932{
fe14fcc3 1933#ifdef HAS_SEM
79072805 1934 SV *opstr;
c2ab57d4 1935 char *opbuf;
463ee0b2 1936 I32 id;
1937 STRLEN opsize;
c2ab57d4 1938
463ee0b2 1939 id = SvIVx(*++mark);
79072805 1940 opstr = *++mark;
463ee0b2 1941 opbuf = SvPV(opstr, opsize);
c2ab57d4 1942 if (opsize < sizeof(struct sembuf)
1943 || (opsize % sizeof(struct sembuf)) != 0) {
748a9306 1944 SETERRNO(EINVAL,LIB$_INVARG);
c2ab57d4 1945 return -1;
1946 }
748a9306 1947 SETERRNO(0,0);
6e21c824 1948 return semop(id, (struct sembuf *)opbuf, opsize/sizeof(struct sembuf));
e5d73d77 1949#else
cea2e8a9 1950 Perl_croak(aTHX_ "semop not implemented");
e5d73d77 1951#endif
c2ab57d4 1952}
1953
79072805 1954I32
864dbfa3 1955Perl_do_shmio(pTHX_ I32 optype, SV **mark, SV **sp)
c2ab57d4 1956{
fe14fcc3 1957#ifdef HAS_SHM
79072805 1958 SV *mstr;
c2ab57d4 1959 char *mbuf, *shm;
79072805 1960 I32 id, mpos, msize;
463ee0b2 1961 STRLEN len;
c2ab57d4 1962 struct shmid_ds shmds;
c2ab57d4 1963
463ee0b2 1964 id = SvIVx(*++mark);
79072805 1965 mstr = *++mark;
463ee0b2 1966 mpos = SvIVx(*++mark);
1967 msize = SvIVx(*++mark);
748a9306 1968 SETERRNO(0,0);
c2ab57d4 1969 if (shmctl(id, IPC_STAT, &shmds) == -1)
1970 return -1;
1971 if (mpos < 0 || msize < 0 || mpos + msize > shmds.shm_segsz) {
748a9306 1972 SETERRNO(EFAULT,SS$_ACCVIO); /* can't do as caller requested */
c2ab57d4 1973 return -1;
1974 }
8ac85365 1975 shm = (char *)shmat(id, (char*)NULL, (optype == OP_SHMREAD) ? SHM_RDONLY : 0);
c2ab57d4 1976 if (shm == (char *)-1) /* I hate System V IPC, I really do */
1977 return -1;
79072805 1978 if (optype == OP_SHMREAD) {
9f538c04 1979 /* suppress warning when reading into undef var (tchrist 3/Mar/00) */
1980 if (! SvOK(mstr))
1981 sv_setpvn(mstr, "", 0);
a0d0e21e 1982 SvPV_force(mstr, len);
1983 mbuf = SvGROW(mstr, msize+1);
1984
bee1dbe2 1985 Copy(shm + mpos, mbuf, msize, char);
79072805 1986 SvCUR_set(mstr, msize);
1987 *SvEND(mstr) = '\0';
a0d0e21e 1988 SvSETMAGIC(mstr);
d929ce6f 1989#ifndef INCOMPLETE_TAINTS
1990 /* who knows who has been playing with this shared memory? */
1991 SvTAINTED_on(mstr);
1992#endif
c2ab57d4 1993 }
1994 else {
79072805 1995 I32 n;
c2ab57d4 1996
a0d0e21e 1997 mbuf = SvPV(mstr, len);
463ee0b2 1998 if ((n = len) > msize)
c2ab57d4 1999 n = msize;
bee1dbe2 2000 Copy(mbuf, shm + mpos, n, char);
c2ab57d4 2001 if (n < msize)
bee1dbe2 2002 memzero(shm + mpos + n, msize - n);
c2ab57d4 2003 }
2004 return shmdt(shm);
e5d73d77 2005#else
cea2e8a9 2006 Perl_croak(aTHX_ "shm I/O not implemented");
e5d73d77 2007#endif
c2ab57d4 2008}
2009
fe14fcc3 2010#endif /* SYSV IPC */
4e35701f 2011
fab3f3a7 2012