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