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