Don't skip too much of the locale error message if no environ array,
[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)) {
f9a63242 1172 if (!SvUTF8(sv))
1173 sv_utf8_upgrade(sv = sv_mortalcopy(sv));
7d59b7e4 1174 }
f9a63242 1175 else if (DO_UTF8(sv))
1176 sv_utf8_downgrade((sv = sv_mortalcopy(sv)), FALSE);
1177 tmps = SvPV(sv, len);
79072805 1178 break;
ff8e2863 1179 }
94e4c244 1180 /* To detect whether the process is about to overstep its
1181 * filesize limit we would need getrlimit(). We could then
1182 * also transparently raise the limit with setrlimit() --
1183 * but only until the system hard limit/the filesystem limit,
c5dd3cdd 1184 * at which we would get EPERM. Note that when using buffered
1185 * io the write failure can be delayed until the flush/close. --jhi */
a21ac455 1186 if (len && (PerlIO_write(fp,tmps,len) == 0))
a687059c 1187 return FALSE;
760ac839 1188 return !PerlIO_error(fp);
a687059c 1189}
1190
79072805 1191I32
cea2e8a9 1192Perl_my_stat(pTHX)
a687059c 1193{
4e35701f 1194 djSP;
79072805 1195 IO *io;
2dd78f96 1196 GV* gv;
79072805 1197
533c011a 1198 if (PL_op->op_flags & OPf_REF) {
924508f0 1199 EXTEND(SP,1);
2dd78f96 1200 gv = cGVOP_gv;
748a9306 1201 do_fstat:
2dd78f96 1202 io = GvIO(gv);
8990e307 1203 if (io && IoIFP(io)) {
2dd78f96 1204 PL_statgv = gv;
3280af22 1205 sv_setpv(PL_statname,"");
1206 PL_laststype = OP_STAT;
1207 return (PL_laststatval = PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache));
a687059c 1208 }
1209 else {
2dd78f96 1210 if (gv == PL_defgv)
3280af22 1211 return PL_laststatval;
2dd78f96 1212 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1213 report_evil_fh(gv, io, PL_op->op_type);
3280af22 1214 PL_statgv = Nullgv;
1215 sv_setpv(PL_statname,"");
1216 return (PL_laststatval = -1);
a687059c 1217 }
1218 }
1219 else {
748a9306 1220 SV* sv = POPs;
4b74e3fb 1221 char *s;
2d8e6c8d 1222 STRLEN n_a;
79072805 1223 PUTBACK;
748a9306 1224 if (SvTYPE(sv) == SVt_PVGV) {
2dd78f96 1225 gv = (GV*)sv;
748a9306 1226 goto do_fstat;
1227 }
1228 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
2dd78f96 1229 gv = (GV*)SvRV(sv);
748a9306 1230 goto do_fstat;
1231 }
1232
2d8e6c8d 1233 s = SvPV(sv, n_a);
3280af22 1234 PL_statgv = Nullgv;
1235 sv_setpv(PL_statname, s);
1236 PL_laststype = OP_STAT;
1237 PL_laststatval = PerlLIO_stat(s, &PL_statcache);
599cee73 1238 if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(s, '\n'))
cea2e8a9 1239 Perl_warner(aTHX_ WARN_NEWLINE, PL_warn_nl, "stat");
3280af22 1240 return PL_laststatval;
a687059c 1241 }
1242}
1243
79072805 1244I32
cea2e8a9 1245Perl_my_lstat(pTHX)
c623bd54 1246{
4e35701f 1247 djSP;
79072805 1248 SV *sv;
2d8e6c8d 1249 STRLEN n_a;
533c011a 1250 if (PL_op->op_flags & OPf_REF) {
924508f0 1251 EXTEND(SP,1);
638eceb6 1252 if (cGVOP_gv == PL_defgv) {
3280af22 1253 if (PL_laststype != OP_LSTAT)
cea2e8a9 1254 Perl_croak(aTHX_ "The stat preceding -l _ wasn't an lstat");
3280af22 1255 return PL_laststatval;
fe14fcc3 1256 }
cea2e8a9 1257 Perl_croak(aTHX_ "You can't use -l on a filehandle");
fe14fcc3 1258 }
c623bd54 1259
3280af22 1260 PL_laststype = OP_LSTAT;
1261 PL_statgv = Nullgv;
79072805 1262 sv = POPs;
1263 PUTBACK;
2d8e6c8d 1264 sv_setpv(PL_statname,SvPV(sv, n_a));
2d8e6c8d 1265 PL_laststatval = PerlLIO_lstat(SvPV(sv, n_a),&PL_statcache);
2d8e6c8d 1266 if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(SvPV(sv, n_a), '\n'))
cea2e8a9 1267 Perl_warner(aTHX_ WARN_NEWLINE, PL_warn_nl, "lstat");
3280af22 1268 return PL_laststatval;
c623bd54 1269}
1270
a687059c 1271bool
864dbfa3 1272Perl_do_aexec(pTHX_ SV *really, register SV **mark, register SV **sp)
a687059c 1273{
d5a9bfb0 1274 return do_aexec5(really, mark, sp, 0, 0);
1275}
1276
1277bool
2aa1486d 1278Perl_do_aexec5(pTHX_ SV *really, register SV **mark, register SV **sp,
1279 int fd, int do_report)
d5a9bfb0 1280{
cd39f2b6 1281#ifdef MACOS_TRADITIONAL
1282 Perl_croak(aTHX_ "exec? I'm not *that* kind of operating system");
1283#else
a687059c 1284 register char **a;
a687059c 1285 char *tmps;
2d8e6c8d 1286 STRLEN n_a;
a687059c 1287
79072805 1288 if (sp > mark) {
3280af22 1289 New(401,PL_Argv, sp - mark + 1, char*);
1290 a = PL_Argv;
79072805 1291 while (++mark <= sp) {
1292 if (*mark)
2d8e6c8d 1293 *a++ = SvPVx(*mark, n_a);
a687059c 1294 else
1295 *a++ = "";
1296 }
1297 *a = Nullch;
3280af22 1298 if (*PL_Argv[0] != '/') /* will execvp use PATH? */
79072805 1299 TAINT_ENV(); /* testing IFS here is overkill, probably */
2d8e6c8d 1300 if (really && *(tmps = SvPV(really, n_a)))
b4748376 1301 PerlProc_execvp(tmps,EXEC_ARGV_CAST(PL_Argv));
a687059c 1302 else
b4748376 1303 PerlProc_execvp(PL_Argv[0],EXEC_ARGV_CAST(PL_Argv));
599cee73 1304 if (ckWARN(WARN_EXEC))
a1d180c4 1305 Perl_warner(aTHX_ WARN_EXEC, "Can't exec \"%s\": %s",
599cee73 1306 PL_Argv[0], Strerror(errno));
d5a9bfb0 1307 if (do_report) {
1308 int e = errno;
1309
1310 PerlLIO_write(fd, (void*)&e, sizeof(int));
1311 PerlLIO_close(fd);
1312 }
a687059c 1313 }
bee1dbe2 1314 do_execfree();
cd39f2b6 1315#endif
a687059c 1316 return FALSE;
1317}
1318
fe14fcc3 1319void
864dbfa3 1320Perl_do_execfree(pTHX)
ff8e2863 1321{
3280af22 1322 if (PL_Argv) {
1323 Safefree(PL_Argv);
1324 PL_Argv = Null(char **);
ff8e2863 1325 }
3280af22 1326 if (PL_Cmd) {
1327 Safefree(PL_Cmd);
1328 PL_Cmd = Nullch;
ff8e2863 1329 }
1330}
1331
cd39f2b6 1332#if !defined(OS2) && !defined(WIN32) && !defined(DJGPP) && !defined(EPOC) && !defined(MACOS_TRADITIONAL)
760ac839 1333
a687059c 1334bool
864dbfa3 1335Perl_do_exec(pTHX_ char *cmd)
a687059c 1336{
e446cec8 1337 return do_exec3(cmd,0,0);
1338}
1339
1340bool
864dbfa3 1341Perl_do_exec3(pTHX_ char *cmd, int fd, int do_report)
e446cec8 1342{
a687059c 1343 register char **a;
1344 register char *s;
a687059c 1345 char flags[10];
1346
748a9306 1347 while (*cmd && isSPACE(*cmd))
1348 cmd++;
1349
a687059c 1350 /* save an extra exec if possible */
1351
bf38876a 1352#ifdef CSH
3280af22 1353 if (strnEQ(cmd,PL_cshname,PL_cshlen) && strnEQ(cmd+PL_cshlen," -c",3)) {
a687059c 1354 strcpy(flags,"-c");
3280af22 1355 s = cmd+PL_cshlen+3;
a687059c 1356 if (*s == 'f') {
1357 s++;
1358 strcat(flags,"f");
1359 }
1360 if (*s == ' ')
1361 s++;
1362 if (*s++ == '\'') {
1363 char *ncmd = s;
1364
1365 while (*s)
1366 s++;
1367 if (s[-1] == '\n')
1368 *--s = '\0';
1369 if (s[-1] == '\'') {
1370 *--s = '\0';
3280af22 1371 PerlProc_execl(PL_cshname,"csh", flags,ncmd,(char*)0);
a687059c 1372 *s = '\'';
1373 return FALSE;
1374 }
1375 }
1376 }
bf38876a 1377#endif /* CSH */
a687059c 1378
1379 /* see if there are shell metacharacters in it */
1380
748a9306 1381 if (*cmd == '.' && isSPACE(cmd[1]))
1382 goto doshell;
1383
1384 if (strnEQ(cmd,"exec",4) && isSPACE(cmd[4]))
1385 goto doshell;
1386
c170e444 1387 for (s = cmd; *s && isALNUM(*s); s++) ; /* catch VAR=val gizmo */
63f2c1e1 1388 if (*s == '=')
1389 goto doshell;
748a9306 1390
a687059c 1391 for (s = cmd; *s; s++) {
93a17b20 1392 if (*s != ' ' && !isALPHA(*s) && strchr("$&*(){}[]'\";\\|?<>~`\n",*s)) {
a687059c 1393 if (*s == '\n' && !s[1]) {
1394 *s = '\0';
1395 break;
1396 }
603a98b0 1397 /* handle the 2>&1 construct at the end */
1398 if (*s == '>' && s[1] == '&' && s[2] == '1'
1399 && s > cmd + 1 && s[-1] == '2' && isSPACE(s[-2])
1400 && (!s[3] || isSPACE(s[3])))
1401 {
1402 char *t = s + 3;
1403
1404 while (*t && isSPACE(*t))
1405 ++t;
1406 if (!*t && (dup2(1,2) != -1)) {
1407 s[-2] = '\0';
1408 break;
1409 }
1410 }
a687059c 1411 doshell:
3280af22 1412 PerlProc_execl(PL_sh_path, "sh", "-c", cmd, (char*)0);
a687059c 1413 return FALSE;
1414 }
1415 }
748a9306 1416
3280af22 1417 New(402,PL_Argv, (s - cmd) / 2 + 2, char*);
1418 PL_Cmd = savepvn(cmd, s-cmd);
1419 a = PL_Argv;
1420 for (s = PL_Cmd; *s;) {
99b89507 1421 while (*s && isSPACE(*s)) s++;
a687059c 1422 if (*s)
1423 *(a++) = s;
99b89507 1424 while (*s && !isSPACE(*s)) s++;
a687059c 1425 if (*s)
1426 *s++ = '\0';
1427 }
1428 *a = Nullch;
3280af22 1429 if (PL_Argv[0]) {
1430 PerlProc_execvp(PL_Argv[0],PL_Argv);
b1248f16 1431 if (errno == ENOEXEC) { /* for system V NIH syndrome */
ff8e2863 1432 do_execfree();
a687059c 1433 goto doshell;
b1248f16 1434 }
d008e5eb 1435 {
e446cec8 1436 int e = errno;
1437
d008e5eb 1438 if (ckWARN(WARN_EXEC))
a1d180c4 1439 Perl_warner(aTHX_ WARN_EXEC, "Can't exec \"%s\": %s",
d008e5eb 1440 PL_Argv[0], Strerror(errno));
e446cec8 1441 if (do_report) {
1442 PerlLIO_write(fd, (void*)&e, sizeof(int));
1443 PerlLIO_close(fd);
1444 }
d008e5eb 1445 }
a687059c 1446 }
ff8e2863 1447 do_execfree();
a687059c 1448 return FALSE;
1449}
1450
6890e559 1451#endif /* OS2 || WIN32 */
760ac839 1452
79072805 1453I32
864dbfa3 1454Perl_apply(pTHX_ I32 type, register SV **mark, register SV **sp)
a687059c 1455{
79072805 1456 register I32 val;
1457 register I32 val2;
1458 register I32 tot = 0;
20408e3c 1459 char *what;
a687059c 1460 char *s;
79072805 1461 SV **oldmark = mark;
2d8e6c8d 1462 STRLEN n_a;
a687059c 1463
20408e3c 1464#define APPLY_TAINT_PROPER() \
3280af22 1465 STMT_START { \
17406bd6 1466 if (PL_tainted) { TAINT_PROPER(what); } \
873ef191 1467 } STMT_END
20408e3c 1468
1469 /* This is a first heuristic; it doesn't catch tainting magic. */
3280af22 1470 if (PL_tainting) {
463ee0b2 1471 while (++mark <= sp) {
bbce6d69 1472 if (SvTAINTED(*mark)) {
1473 TAINT;
1474 break;
1475 }
463ee0b2 1476 }
1477 mark = oldmark;
1478 }
a687059c 1479 switch (type) {
79072805 1480 case OP_CHMOD:
20408e3c 1481 what = "chmod";
1482 APPLY_TAINT_PROPER();
79072805 1483 if (++mark <= sp) {
463ee0b2 1484 val = SvIVx(*mark);
20408e3c 1485 APPLY_TAINT_PROPER();
1486 tot = sp - mark;
79072805 1487 while (++mark <= sp) {
2d8e6c8d 1488 char *name = SvPVx(*mark, n_a);
20408e3c 1489 APPLY_TAINT_PROPER();
1490 if (PerlLIO_chmod(name, val))
a687059c 1491 tot--;
1492 }
1493 }
1494 break;
fe14fcc3 1495#ifdef HAS_CHOWN
79072805 1496 case OP_CHOWN:
20408e3c 1497 what = "chown";
1498 APPLY_TAINT_PROPER();
79072805 1499 if (sp - mark > 2) {
463ee0b2 1500 val = SvIVx(*++mark);
1501 val2 = SvIVx(*++mark);
20408e3c 1502 APPLY_TAINT_PROPER();
a0d0e21e 1503 tot = sp - mark;
79072805 1504 while (++mark <= sp) {
2d8e6c8d 1505 char *name = SvPVx(*mark, n_a);
20408e3c 1506 APPLY_TAINT_PROPER();
36660982 1507 if (PerlLIO_chown(name, val, val2))
a687059c 1508 tot--;
1509 }
1510 }
1511 break;
b1248f16 1512#endif
a1d180c4 1513/*
dd64f1c3 1514XXX Should we make lchown() directly available from perl?
1515For now, we'll let Configure test for HAS_LCHOWN, but do
1516nothing in the core.
1517 --AD 5/1998
1518*/
fe14fcc3 1519#ifdef HAS_KILL
79072805 1520 case OP_KILL:
20408e3c 1521 what = "kill";
1522 APPLY_TAINT_PROPER();
55497cff 1523 if (mark == sp)
1524 break;
2d8e6c8d 1525 s = SvPVx(*++mark, n_a);
79072805 1526 if (isUPPER(*s)) {
1527 if (*s == 'S' && s[1] == 'I' && s[2] == 'G')
1528 s += 3;
1529 if (!(val = whichsig(s)))
cea2e8a9 1530 Perl_croak(aTHX_ "Unrecognized signal name \"%s\"",s);
79072805 1531 }
1532 else
463ee0b2 1533 val = SvIVx(*mark);
20408e3c 1534 APPLY_TAINT_PROPER();
1535 tot = sp - mark;
3595fcef 1536#ifdef VMS
1537 /* kill() doesn't do process groups (job trees?) under VMS */
1538 if (val < 0) val = -val;
1539 if (val == SIGKILL) {
1540# include <starlet.h>
1541 /* Use native sys$delprc() to insure that target process is
1542 * deleted; supervisor-mode images don't pay attention to
1543 * CRTL's emulation of Unix-style signals and kill()
1544 */
1545 while (++mark <= sp) {
1546 I32 proc = SvIVx(*mark);
1547 register unsigned long int __vmssts;
20408e3c 1548 APPLY_TAINT_PROPER();
3595fcef 1549 if (!((__vmssts = sys$delprc(&proc,0)) & 1)) {
1550 tot--;
1551 switch (__vmssts) {
1552 case SS$_NONEXPR:
1553 case SS$_NOSUCHNODE:
1554 SETERRNO(ESRCH,__vmssts);
1555 break;
1556 case SS$_NOPRIV:
1557 SETERRNO(EPERM,__vmssts);
1558 break;
1559 default:
1560 SETERRNO(EVMSERR,__vmssts);
1561 }
1562 }
1563 }
1564 break;
1565 }
1566#endif
79072805 1567 if (val < 0) {
1568 val = -val;
1569 while (++mark <= sp) {
463ee0b2 1570 I32 proc = SvIVx(*mark);
20408e3c 1571 APPLY_TAINT_PROPER();
fe14fcc3 1572#ifdef HAS_KILLPG
3028581b 1573 if (PerlProc_killpg(proc,val)) /* BSD */
a687059c 1574#else
3028581b 1575 if (PerlProc_kill(-proc,val)) /* SYSV */
a687059c 1576#endif
79072805 1577 tot--;
a687059c 1578 }
79072805 1579 }
1580 else {
1581 while (++mark <= sp) {
20408e3c 1582 I32 proc = SvIVx(*mark);
1583 APPLY_TAINT_PROPER();
1584 if (PerlProc_kill(proc, val))
79072805 1585 tot--;
a687059c 1586 }
1587 }
1588 break;
b1248f16 1589#endif
79072805 1590 case OP_UNLINK:
20408e3c 1591 what = "unlink";
1592 APPLY_TAINT_PROPER();
79072805 1593 tot = sp - mark;
1594 while (++mark <= sp) {
2d8e6c8d 1595 s = SvPVx(*mark, n_a);
20408e3c 1596 APPLY_TAINT_PROPER();
3280af22 1597 if (PL_euid || PL_unsafe) {
a687059c 1598 if (UNLINK(s))
1599 tot--;
1600 }
1601 else { /* don't let root wipe out directories without -U */
3280af22 1602 if (PerlLIO_lstat(s,&PL_statbuf) < 0 || S_ISDIR(PL_statbuf.st_mode))
a687059c 1603 tot--;
1604 else {
1605 if (UNLINK(s))
1606 tot--;
1607 }
1608 }
1609 }
1610 break;
a0d0e21e 1611#ifdef HAS_UTIME
79072805 1612 case OP_UTIME:
20408e3c 1613 what = "utime";
1614 APPLY_TAINT_PROPER();
79072805 1615 if (sp - mark > 2) {
748a9306 1616#if defined(I_UTIME) || defined(VMS)
663a0e37 1617 struct utimbuf utbuf;
1618#else
a687059c 1619 struct {
dd2821f6 1620 Time_t actime;
1621 Time_t modtime;
a687059c 1622 } utbuf;
663a0e37 1623#endif
a687059c 1624
afd9f252 1625 Zero(&utbuf, sizeof utbuf, char);
517844ec 1626#ifdef BIG_TIME
dd2821f6 1627 utbuf.actime = (Time_t)SvNVx(*++mark); /* time accessed */
1628 utbuf.modtime = (Time_t)SvNVx(*++mark); /* time modified */
517844ec 1629#else
dd2821f6 1630 utbuf.actime = (Time_t)SvIVx(*++mark); /* time accessed */
1631 utbuf.modtime = (Time_t)SvIVx(*++mark); /* time modified */
517844ec 1632#endif
20408e3c 1633 APPLY_TAINT_PROPER();
79072805 1634 tot = sp - mark;
1635 while (++mark <= sp) {
2d8e6c8d 1636 char *name = SvPVx(*mark, n_a);
20408e3c 1637 APPLY_TAINT_PROPER();
1638 if (PerlLIO_utime(name, &utbuf))
a687059c 1639 tot--;
1640 }
a687059c 1641 }
1642 else
79072805 1643 tot = 0;
a687059c 1644 break;
a0d0e21e 1645#endif
a687059c 1646 }
1647 return tot;
20408e3c 1648
20408e3c 1649#undef APPLY_TAINT_PROPER
a687059c 1650}
1651
1652/* Do the permissions allow some operation? Assumes statcache already set. */
a0d0e21e 1653#ifndef VMS /* VMS' cando is in vms.c */
7f4774ae 1654bool
1655Perl_cando(pTHX_ Mode_t mode, Uid_t effective, register Stat_t *statbufp)
1656/* Note: we use `effective' both for uids and gids.
1657 * Here we are betting on Uid_t being equal or wider than Gid_t. */
a687059c 1658{
bee1dbe2 1659#ifdef DOSISH
fe14fcc3 1660 /* [Comments and code from Len Reed]
1661 * MS-DOS "user" is similar to UNIX's "superuser," but can't write
1662 * to write-protected files. The execute permission bit is set
1663 * by the Miscrosoft C library stat() function for the following:
1664 * .exe files
1665 * .com files
1666 * .bat files
1667 * directories
1668 * All files and directories are readable.
1669 * Directories and special files, e.g. "CON", cannot be
1670 * write-protected.
1671 * [Comment by Tom Dinger -- a directory can have the write-protect
1672 * bit set in the file system, but DOS permits changes to
1673 * the directory anyway. In addition, all bets are off
1674 * here for networked software, such as Novell and
1675 * Sun's PC-NFS.]
1676 */
1677
bee1dbe2 1678 /* Atari stat() does pretty much the same thing. we set x_bit_set_in_stat
1679 * too so it will actually look into the files for magic numbers
1680 */
7f4774ae 1681 return (mode & statbufp->st_mode) ? TRUE : FALSE;
fe14fcc3 1682
55497cff 1683#else /* ! DOSISH */
3280af22 1684 if ((effective ? PL_euid : PL_uid) == 0) { /* root is special */
7f4774ae 1685 if (mode == S_IXUSR) {
c623bd54 1686 if (statbufp->st_mode & 0111 || S_ISDIR(statbufp->st_mode))
a687059c 1687 return TRUE;
1688 }
1689 else
1690 return TRUE; /* root reads and writes anything */
1691 return FALSE;
1692 }
3280af22 1693 if (statbufp->st_uid == (effective ? PL_euid : PL_uid) ) {
7f4774ae 1694 if (statbufp->st_mode & mode)
a687059c 1695 return TRUE; /* ok as "user" */
1696 }
d8eceb89 1697 else if (ingroup(statbufp->st_gid,effective)) {
7f4774ae 1698 if (statbufp->st_mode & mode >> 3)
a687059c 1699 return TRUE; /* ok as "group" */
1700 }
7f4774ae 1701 else if (statbufp->st_mode & mode >> 6)
a687059c 1702 return TRUE; /* ok as "other" */
1703 return FALSE;
55497cff 1704#endif /* ! DOSISH */
a687059c 1705}
a0d0e21e 1706#endif /* ! VMS */
a687059c 1707
d8eceb89 1708bool
1709Perl_ingroup(pTHX_ Gid_t testgid, Uid_t effective)
a687059c 1710{
cd39f2b6 1711#ifdef MACOS_TRADITIONAL
1712 /* This is simply not correct for AppleShare, but fix it yerself. */
1713 return TRUE;
1714#else
3280af22 1715 if (testgid == (effective ? PL_egid : PL_gid))
a687059c 1716 return TRUE;
fe14fcc3 1717#ifdef HAS_GETGROUPS
a687059c 1718#ifndef NGROUPS
1719#define NGROUPS 32
1720#endif
1721 {
a0d0e21e 1722 Groups_t gary[NGROUPS];
79072805 1723 I32 anum;
a687059c 1724
1725 anum = getgroups(NGROUPS,gary);
1726 while (--anum >= 0)
1727 if (gary[anum] == testgid)
1728 return TRUE;
1729 }
1730#endif
1731 return FALSE;
cd39f2b6 1732#endif
a687059c 1733}
c2ab57d4 1734
fe14fcc3 1735#if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
c2ab57d4 1736
79072805 1737I32
864dbfa3 1738Perl_do_ipcget(pTHX_ I32 optype, SV **mark, SV **sp)
c2ab57d4 1739{
c2ab57d4 1740 key_t key;
79072805 1741 I32 n, flags;
c2ab57d4 1742
463ee0b2 1743 key = (key_t)SvNVx(*++mark);
1744 n = (optype == OP_MSGGET) ? 0 : SvIVx(*++mark);
1745 flags = SvIVx(*++mark);
748a9306 1746 SETERRNO(0,0);
c2ab57d4 1747 switch (optype)
1748 {
fe14fcc3 1749#ifdef HAS_MSG
79072805 1750 case OP_MSGGET:
c2ab57d4 1751 return msgget(key, flags);
e5d73d77 1752#endif
fe14fcc3 1753#ifdef HAS_SEM
79072805 1754 case OP_SEMGET:
c2ab57d4 1755 return semget(key, n, flags);
e5d73d77 1756#endif
fe14fcc3 1757#ifdef HAS_SHM
79072805 1758 case OP_SHMGET:
c2ab57d4 1759 return shmget(key, n, flags);
e5d73d77 1760#endif
fe14fcc3 1761#if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
e5d73d77 1762 default:
cea2e8a9 1763 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
e5d73d77 1764#endif
c2ab57d4 1765 }
1766 return -1; /* should never happen */
1767}
1768
79072805 1769I32
864dbfa3 1770Perl_do_ipcctl(pTHX_ I32 optype, SV **mark, SV **sp)
c2ab57d4 1771{
79072805 1772 SV *astr;
c2ab57d4 1773 char *a;
a0d0e21e 1774 I32 id, n, cmd, infosize, getinfo;
1775 I32 ret = -1;
c2ab57d4 1776
463ee0b2 1777 id = SvIVx(*++mark);
1778 n = (optype == OP_SEMCTL) ? SvIVx(*++mark) : 0;
1779 cmd = SvIVx(*++mark);
79072805 1780 astr = *++mark;
c2ab57d4 1781 infosize = 0;
1782 getinfo = (cmd == IPC_STAT);
1783
1784 switch (optype)
1785 {
fe14fcc3 1786#ifdef HAS_MSG
79072805 1787 case OP_MSGCTL:
c2ab57d4 1788 if (cmd == IPC_STAT || cmd == IPC_SET)
1789 infosize = sizeof(struct msqid_ds);
1790 break;
e5d73d77 1791#endif
fe14fcc3 1792#ifdef HAS_SHM
79072805 1793 case OP_SHMCTL:
c2ab57d4 1794 if (cmd == IPC_STAT || cmd == IPC_SET)
1795 infosize = sizeof(struct shmid_ds);
1796 break;
e5d73d77 1797#endif
fe14fcc3 1798#ifdef HAS_SEM
79072805 1799 case OP_SEMCTL:
39398f3f 1800#ifdef Semctl
c2ab57d4 1801 if (cmd == IPC_STAT || cmd == IPC_SET)
1802 infosize = sizeof(struct semid_ds);
1803 else if (cmd == GETALL || cmd == SETALL)
1804 {
8e591e46 1805 struct semid_ds semds;
bd89102f 1806 union semun semun;
e6f0bdd6 1807#ifdef EXTRA_F_IN_SEMUN_BUF
1808 semun.buff = &semds;
1809#else
84902520 1810 semun.buf = &semds;
e6f0bdd6 1811#endif
c2ab57d4 1812 getinfo = (cmd == GETALL);
9b89d93d 1813 if (Semctl(id, 0, IPC_STAT, semun) == -1)
1814 return -1;
6e21c824 1815 infosize = semds.sem_nsems * sizeof(short);
1816 /* "short" is technically wrong but much more portable
1817 than guessing about u_?short(_t)? */
c2ab57d4 1818 }
39398f3f 1819#else
cea2e8a9 1820 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
39398f3f 1821#endif
c2ab57d4 1822 break;
e5d73d77 1823#endif
fe14fcc3 1824#if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
e5d73d77 1825 default:
cea2e8a9 1826 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
e5d73d77 1827#endif
c2ab57d4 1828 }
1829
1830 if (infosize)
1831 {
a0d0e21e 1832 STRLEN len;
c2ab57d4 1833 if (getinfo)
1834 {
a0d0e21e 1835 SvPV_force(astr, len);
1836 a = SvGROW(astr, infosize+1);
c2ab57d4 1837 }
1838 else
1839 {
463ee0b2 1840 a = SvPV(astr, len);
1841 if (len != infosize)
cea2e8a9 1842 Perl_croak(aTHX_ "Bad arg length for %s, is %lu, should be %ld",
4ec43091 1843 PL_op_desc[optype],
1844 (unsigned long)len,
1845 (long)infosize);
c2ab57d4 1846 }
1847 }
1848 else
1849 {
c030ccd9 1850 IV i = SvIV(astr);
56431972 1851 a = INT2PTR(char *,i); /* ouch */
c2ab57d4 1852 }
748a9306 1853 SETERRNO(0,0);
c2ab57d4 1854 switch (optype)
1855 {
fe14fcc3 1856#ifdef HAS_MSG
79072805 1857 case OP_MSGCTL:
bee1dbe2 1858 ret = msgctl(id, cmd, (struct msqid_ds *)a);
c2ab57d4 1859 break;
e5d73d77 1860#endif
fe14fcc3 1861#ifdef HAS_SEM
bd89102f 1862 case OP_SEMCTL: {
39398f3f 1863#ifdef Semctl
bd89102f 1864 union semun unsemds;
1865
e6f0bdd6 1866#ifdef EXTRA_F_IN_SEMUN_BUF
1867 unsemds.buff = (struct semid_ds *)a;
1868#else
bd89102f 1869 unsemds.buf = (struct semid_ds *)a;
e6f0bdd6 1870#endif
bd89102f 1871 ret = Semctl(id, n, cmd, unsemds);
39398f3f 1872#else
cea2e8a9 1873 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
39398f3f 1874#endif
bd89102f 1875 }
c2ab57d4 1876 break;
e5d73d77 1877#endif
fe14fcc3 1878#ifdef HAS_SHM
79072805 1879 case OP_SHMCTL:
bee1dbe2 1880 ret = shmctl(id, cmd, (struct shmid_ds *)a);
c2ab57d4 1881 break;
e5d73d77 1882#endif
c2ab57d4 1883 }
1884 if (getinfo && ret >= 0) {
79072805 1885 SvCUR_set(astr, infosize);
1886 *SvEND(astr) = '\0';
a0d0e21e 1887 SvSETMAGIC(astr);
c2ab57d4 1888 }
1889 return ret;
1890}
1891
79072805 1892I32
864dbfa3 1893Perl_do_msgsnd(pTHX_ SV **mark, SV **sp)
c2ab57d4 1894{
fe14fcc3 1895#ifdef HAS_MSG
79072805 1896 SV *mstr;
c2ab57d4 1897 char *mbuf;
79072805 1898 I32 id, msize, flags;
463ee0b2 1899 STRLEN len;
c2ab57d4 1900
463ee0b2 1901 id = SvIVx(*++mark);
79072805 1902 mstr = *++mark;
463ee0b2 1903 flags = SvIVx(*++mark);
1904 mbuf = SvPV(mstr, len);
1905 if ((msize = len - sizeof(long)) < 0)
cea2e8a9 1906 Perl_croak(aTHX_ "Arg too short for msgsnd");
748a9306 1907 SETERRNO(0,0);
bee1dbe2 1908 return msgsnd(id, (struct msgbuf *)mbuf, msize, flags);
e5d73d77 1909#else
cea2e8a9 1910 Perl_croak(aTHX_ "msgsnd not implemented");
e5d73d77 1911#endif
c2ab57d4 1912}
1913
79072805 1914I32
864dbfa3 1915Perl_do_msgrcv(pTHX_ SV **mark, SV **sp)
c2ab57d4 1916{
fe14fcc3 1917#ifdef HAS_MSG
79072805 1918 SV *mstr;
c2ab57d4 1919 char *mbuf;
1920 long mtype;
79072805 1921 I32 id, msize, flags, ret;
463ee0b2 1922 STRLEN len;
79072805 1923
463ee0b2 1924 id = SvIVx(*++mark);
79072805 1925 mstr = *++mark;
c2e66d9e 1926 /* suppress warning when reading into undef var --jhi */
1927 if (! SvOK(mstr))
1928 sv_setpvn(mstr, "", 0);
463ee0b2 1929 msize = SvIVx(*++mark);
1930 mtype = (long)SvIVx(*++mark);
1931 flags = SvIVx(*++mark);
a0d0e21e 1932 SvPV_force(mstr, len);
1933 mbuf = SvGROW(mstr, sizeof(long)+msize+1);
a1d180c4 1934
748a9306 1935 SETERRNO(0,0);
bee1dbe2 1936 ret = msgrcv(id, (struct msgbuf *)mbuf, msize, mtype, flags);
c2ab57d4 1937 if (ret >= 0) {
79072805 1938 SvCUR_set(mstr, sizeof(long)+ret);
1939 *SvEND(mstr) = '\0';
41d6edb2 1940#ifndef INCOMPLETE_TAINTS
1941 /* who knows who has been playing with this message? */
1942 SvTAINTED_on(mstr);
1943#endif
c2ab57d4 1944 }
1945 return ret;
e5d73d77 1946#else
cea2e8a9 1947 Perl_croak(aTHX_ "msgrcv not implemented");
e5d73d77 1948#endif
c2ab57d4 1949}
1950
79072805 1951I32
864dbfa3 1952Perl_do_semop(pTHX_ SV **mark, SV **sp)
c2ab57d4 1953{
fe14fcc3 1954#ifdef HAS_SEM
79072805 1955 SV *opstr;
c2ab57d4 1956 char *opbuf;
463ee0b2 1957 I32 id;
1958 STRLEN opsize;
c2ab57d4 1959
463ee0b2 1960 id = SvIVx(*++mark);
79072805 1961 opstr = *++mark;
463ee0b2 1962 opbuf = SvPV(opstr, opsize);
c2ab57d4 1963 if (opsize < sizeof(struct sembuf)
1964 || (opsize % sizeof(struct sembuf)) != 0) {
748a9306 1965 SETERRNO(EINVAL,LIB$_INVARG);
c2ab57d4 1966 return -1;
1967 }
748a9306 1968 SETERRNO(0,0);
6e21c824 1969 return semop(id, (struct sembuf *)opbuf, opsize/sizeof(struct sembuf));
e5d73d77 1970#else
cea2e8a9 1971 Perl_croak(aTHX_ "semop not implemented");
e5d73d77 1972#endif
c2ab57d4 1973}
1974
79072805 1975I32
864dbfa3 1976Perl_do_shmio(pTHX_ I32 optype, SV **mark, SV **sp)
c2ab57d4 1977{
fe14fcc3 1978#ifdef HAS_SHM
79072805 1979 SV *mstr;
c2ab57d4 1980 char *mbuf, *shm;
79072805 1981 I32 id, mpos, msize;
463ee0b2 1982 STRLEN len;
c2ab57d4 1983 struct shmid_ds shmds;
c2ab57d4 1984
463ee0b2 1985 id = SvIVx(*++mark);
79072805 1986 mstr = *++mark;
463ee0b2 1987 mpos = SvIVx(*++mark);
1988 msize = SvIVx(*++mark);
748a9306 1989 SETERRNO(0,0);
c2ab57d4 1990 if (shmctl(id, IPC_STAT, &shmds) == -1)
1991 return -1;
1992 if (mpos < 0 || msize < 0 || mpos + msize > shmds.shm_segsz) {
748a9306 1993 SETERRNO(EFAULT,SS$_ACCVIO); /* can't do as caller requested */
c2ab57d4 1994 return -1;
1995 }
8ac85365 1996 shm = (char *)shmat(id, (char*)NULL, (optype == OP_SHMREAD) ? SHM_RDONLY : 0);
c2ab57d4 1997 if (shm == (char *)-1) /* I hate System V IPC, I really do */
1998 return -1;
79072805 1999 if (optype == OP_SHMREAD) {
9f538c04 2000 /* suppress warning when reading into undef var (tchrist 3/Mar/00) */
2001 if (! SvOK(mstr))
2002 sv_setpvn(mstr, "", 0);
a0d0e21e 2003 SvPV_force(mstr, len);
2004 mbuf = SvGROW(mstr, msize+1);
2005
bee1dbe2 2006 Copy(shm + mpos, mbuf, msize, char);
79072805 2007 SvCUR_set(mstr, msize);
2008 *SvEND(mstr) = '\0';
a0d0e21e 2009 SvSETMAGIC(mstr);
d929ce6f 2010#ifndef INCOMPLETE_TAINTS
2011 /* who knows who has been playing with this shared memory? */
2012 SvTAINTED_on(mstr);
2013#endif
c2ab57d4 2014 }
2015 else {
79072805 2016 I32 n;
c2ab57d4 2017
a0d0e21e 2018 mbuf = SvPV(mstr, len);
463ee0b2 2019 if ((n = len) > msize)
c2ab57d4 2020 n = msize;
bee1dbe2 2021 Copy(mbuf, shm + mpos, n, char);
c2ab57d4 2022 if (n < msize)
bee1dbe2 2023 memzero(shm + mpos + n, msize - n);
c2ab57d4 2024 }
2025 return shmdt(shm);
e5d73d77 2026#else
cea2e8a9 2027 Perl_croak(aTHX_ "shm I/O not implemented");
e5d73d77 2028#endif
c2ab57d4 2029}
2030
fe14fcc3 2031#endif /* SYSV IPC */
4e35701f 2032
0d44d22b 2033/*
2034=for apidoc start_glob
2035
2036Function called by C<do_readline> to spawn a glob (or do the glob inside
2037perl on VMS). This code used to be inline, but now perl uses C<File::Glob>
2038this glob starter is only used by miniperl during the build proccess.
2039Moving it away shrinks pp_hot.c; shrinking pp_hot.c helps speed perl up.
fab3f3a7 2040
0d44d22b 2041=cut
2042*/
2043
2044PerlIO *
2045Perl_start_glob (pTHX_ SV *tmpglob, IO *io)
2046{
2047 SV *tmpcmd = NEWSV(55, 0);
2048 PerlIO *fp;
2049 ENTER;
2050 SAVEFREESV(tmpcmd);
2051#ifdef VMS /* expand the wildcards right here, rather than opening a pipe, */
2052 /* since spawning off a process is a real performance hit */
2053 {
2054#include <descrip.h>
2055#include <lib$routines.h>
2056#include <nam.h>
2057#include <rmsdef.h>
2058 char rslt[NAM$C_MAXRSS+1+sizeof(unsigned short int)] = {'\0','\0'};
2059 char vmsspec[NAM$C_MAXRSS+1];
2060 char *rstr = rslt + sizeof(unsigned short int), *begin, *end, *cp;
2061 char tmpfnam[L_tmpnam] = "SYS$SCRATCH:";
2062 $DESCRIPTOR(dfltdsc,"SYS$DISK:[]*.*;");
2063 PerlIO *tmpfp;
2064 STRLEN i;
2065 struct dsc$descriptor_s wilddsc
2066 = {0, DSC$K_DTYPE_T, DSC$K_CLASS_S, 0};
2067 struct dsc$descriptor_vs rsdsc
2068 = {sizeof rslt, DSC$K_DTYPE_VT, DSC$K_CLASS_VS, rslt};
2069 unsigned long int cxt = 0, sts = 0, ok = 1, hasdir = 0, hasver = 0, isunix = 0;
2070
2071 /* We could find out if there's an explicit dev/dir or version
2072 by peeking into lib$find_file's internal context at
2073 ((struct NAM *)((struct FAB *)cxt)->fab$l_nam)->nam$l_fnb
2074 but that's unsupported, so I don't want to do it now and
2075 have it bite someone in the future. */
2076 strcat(tmpfnam,PerlLIO_tmpnam(NULL));
2077 cp = SvPV(tmpglob,i);
2078 for (; i; i--) {
2079 if (cp[i] == ';') hasver = 1;
2080 if (cp[i] == '.') {
2081 if (sts) hasver = 1;
2082 else sts = 1;
2083 }
2084 if (cp[i] == '/') {
2085 hasdir = isunix = 1;
2086 break;
2087 }
2088 if (cp[i] == ']' || cp[i] == '>' || cp[i] == ':') {
2089 hasdir = 1;
2090 break;
2091 }
2092 }
2093 if ((tmpfp = PerlIO_open(tmpfnam,"w+","fop=dlt")) != NULL) {
2094 Stat_t st;
2095 if (!PerlLIO_stat(SvPVX(tmpglob),&st) && S_ISDIR(st.st_mode))
2096 ok = ((wilddsc.dsc$a_pointer = tovmspath(SvPVX(tmpglob),vmsspec)) != NULL);
2097 else ok = ((wilddsc.dsc$a_pointer = tovmsspec(SvPVX(tmpglob),vmsspec)) != NULL);
2098 if (ok) wilddsc.dsc$w_length = (unsigned short int) strlen(wilddsc.dsc$a_pointer);
2099 while (ok && ((sts = lib$find_file(&wilddsc,&rsdsc,&cxt,
2100 &dfltdsc,NULL,NULL,NULL))&1)) {
2101 end = rstr + (unsigned long int) *rslt;
2102 if (!hasver) while (*end != ';') end--;
2103 *(end++) = '\n'; *end = '\0';
2104 for (cp = rstr; *cp; cp++) *cp = _tolower(*cp);
2105 if (hasdir) {
2106 if (isunix) trim_unixpath(rstr,SvPVX(tmpglob),1);
2107 begin = rstr;
2108 }
2109 else {
2110 begin = end;
2111 while (*(--begin) != ']' && *begin != '>') ;
2112 ++begin;
2113 }
2114 ok = (PerlIO_puts(tmpfp,begin) != EOF);
2115 }
2116 if (cxt) (void)lib$find_file_end(&cxt);
2117 if (ok && sts != RMS$_NMF &&
2118 sts != RMS$_DNF && sts != RMS$_FNF) ok = 0;
2119 if (!ok) {
2120 if (!(sts & 1)) {
2121 SETERRNO((sts == RMS$_SYN ? EINVAL : EVMSERR),sts);
2122 }
2123 PerlIO_close(tmpfp);
2124 fp = NULL;
2125 }
2126 else {
2127 PerlIO_rewind(tmpfp);
2128 IoTYPE(io) = IoTYPE_RDONLY;
2129 IoIFP(io) = fp = tmpfp;
2130 IoFLAGS(io) &= ~IOf_UNTAINT; /* maybe redundant */
2131 }
2132 }
2133 }
2134#else /* !VMS */
2135#ifdef MACOS_TRADITIONAL
2136 sv_setpv(tmpcmd, "glob ");
2137 sv_catsv(tmpcmd, tmpglob);
2138 sv_catpv(tmpcmd, " |");
2139#else
2140#ifdef DOSISH
2141#ifdef OS2
2142 sv_setpv(tmpcmd, "for a in ");
2143 sv_catsv(tmpcmd, tmpglob);
2144 sv_catpv(tmpcmd, "; do echo \"$a\\0\\c\"; done |");
2145#else
2146#ifdef DJGPP
2147 sv_setpv(tmpcmd, "/dev/dosglob/"); /* File System Extension */
2148 sv_catsv(tmpcmd, tmpglob);
2149#else
2150 sv_setpv(tmpcmd, "perlglob ");
2151 sv_catsv(tmpcmd, tmpglob);
2152 sv_catpv(tmpcmd, " |");
2153#endif /* !DJGPP */
2154#endif /* !OS2 */
2155#else /* !DOSISH */
2156#if defined(CSH)
2157 sv_setpvn(tmpcmd, PL_cshname, PL_cshlen);
2158 sv_catpv(tmpcmd, " -cf 'set nonomatch; glob ");
2159 sv_catsv(tmpcmd, tmpglob);
2160 sv_catpv(tmpcmd, "' 2>/dev/null |");
2161#else
2162 sv_setpv(tmpcmd, "echo ");
2163 sv_catsv(tmpcmd, tmpglob);
2164#if 'z' - 'a' == 25
2165 sv_catpv(tmpcmd, "|tr -s ' \t\f\r' '\\012\\012\\012\\012'|");
2166#else
2167 sv_catpv(tmpcmd, "|tr -s ' \t\f\r' '\\n\\n\\n\\n'|");
2168#endif
2169#endif /* !CSH */
2170#endif /* !DOSISH */
2171#endif /* MACOS_TRADITIONAL */
2172 (void)do_open(PL_last_in_gv, SvPVX(tmpcmd), SvCUR(tmpcmd),
2173 FALSE, O_RDONLY, 0, Nullfp);
2174 fp = IoIFP(io);
2175#endif /* !VMS */
2176 LEAVE;
2177 return fp;
2178}