Cosmetic: data-driven REx-dump
[p5sagit/p5-mst-13.2.git] / doio.c
CommitLineData
a0d0e21e 1/* doio.c
a687059c 2 *
4eb8286e 3 * Copyright (c) 1991-1999, 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"
18#include "perl.h"
19
fe14fcc3 20#if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
aec308ec 21#ifndef HAS_SEM
c2ab57d4 22#include <sys/ipc.h>
aec308ec 23#endif
fe14fcc3 24#ifdef HAS_MSG
c2ab57d4 25#include <sys/msg.h>
e5d73d77 26#endif
fe14fcc3 27#ifdef HAS_SHM
c2ab57d4 28#include <sys/shm.h>
a0d0e21e 29# ifndef HAS_SHMAT_PROTOTYPE
30 extern Shmat_t shmat _((int, char *, int));
31# endif
c2ab57d4 32#endif
e5d73d77 33#endif
c2ab57d4 34
663a0e37 35#ifdef I_UTIME
3730b96e 36# if defined(_MSC_VER) || defined(__MINGW32__)
3fe9a6f1 37# include <sys/utime.h>
38# else
39# include <utime.h>
40# endif
663a0e37 41#endif
85aff577 42
ff8e2863 43#ifdef I_FCNTL
44#include <fcntl.h>
45#endif
fe14fcc3 46#ifdef I_SYS_FILE
47#include <sys/file.h>
48#endif
85aff577 49#ifdef O_EXCL
50# define OPEN_EXCL O_EXCL
51#else
52# define OPEN_EXCL 0
53#endif
a687059c 54
76121258 55#if !defined(NSIG) || defined(M_UNIX) || defined(M_XENIX)
56#include <signal.h>
57#endif
58
59/* XXX If this causes problems, set i_unistd=undef in the hint file. */
60#ifdef I_UNISTD
61# include <unistd.h>
62#endif
63
232e078e 64#if defined(HAS_SOCKET) && !defined(VMS) /* VMS handles sockets via vmsish.h */
65# include <sys/socket.h>
66# include <netdb.h>
67# ifndef ENOTSOCK
68# ifdef I_NET_ERRNO
69# include <net/errno.h>
70# endif
71# endif
72#endif
73
d574b85e 74/* Put this after #includes because <unistd.h> defines _XOPEN_*. */
75#ifndef Sock_size_t
137443ea 76# if _XOPEN_VERSION >= 5 || defined(_XOPEN_SOURCE_EXTENDED) || defined(__GLIBC__)
d574b85e 77# define Sock_size_t Size_t
78# else
79# define Sock_size_t int
80# endif
81#endif
82
a687059c 83bool
6acef3b7 84do_open(GV *gv, register char *name, I32 len, int as_raw, int rawmode, int rawperm, PerlIO *supplied_fp)
a687059c 85{
a0d0e21e 86 register IO *io = GvIOn(gv);
760ac839 87 PerlIO *saveifp = Nullfp;
88 PerlIO *saveofp = Nullfp;
6e21c824 89 char savetype = ' ';
c07a80fd 90 int writing = 0;
760ac839 91 PerlIO *fp;
c07a80fd 92 int fd;
93 int result;
3500f679 94 bool was_fdopen = FALSE;
a687059c 95
3280af22 96 PL_forkprocess = 1; /* assume true if no fork */
c07a80fd 97
a0d0e21e 98 if (IoIFP(io)) {
760ac839 99 fd = PerlIO_fileno(IoIFP(io));
8990e307 100 if (IoTYPE(io) == '-')
c2ab57d4 101 result = 0;
3280af22 102 else if (fd <= PL_maxsysfd) {
8990e307 103 saveifp = IoIFP(io);
104 saveofp = IoOFP(io);
105 savetype = IoTYPE(io);
6e21c824 106 result = 0;
107 }
8990e307 108 else if (IoTYPE(io) == '|')
3028581b 109 result = PerlProc_pclose(IoIFP(io));
8990e307 110 else if (IoIFP(io) != IoOFP(io)) {
111 if (IoOFP(io)) {
760ac839 112 result = PerlIO_close(IoOFP(io));
113 PerlIO_close(IoIFP(io)); /* clear stdio, fd already closed */
c2ab57d4 114 }
115 else
760ac839 116 result = PerlIO_close(IoIFP(io));
a687059c 117 }
a687059c 118 else
760ac839 119 result = PerlIO_close(IoIFP(io));
3280af22 120 if (result == EOF && fd > PL_maxsysfd)
760ac839 121 PerlIO_printf(PerlIO_stderr(), "Warning: unable to close filehandle %s properly.\n",
79072805 122 GvENAME(gv));
8990e307 123 IoOFP(io) = IoIFP(io) = Nullfp;
a687059c 124 }
c07a80fd 125
126 if (as_raw) {
5ff3f7a4 127#if defined(O_LARGEFILE)
128 rawmode |= O_LARGEFILE;
129#endif
130
9d116dd7 131#ifndef O_ACCMODE
132#define O_ACCMODE 3 /* Assume traditional implementation */
133#endif
5ff3f7a4 134
9d116dd7 135 switch (result = rawmode & O_ACCMODE) {
136 case O_RDONLY:
137 IoTYPE(io) = '<';
138 break;
139 case O_WRONLY:
140 IoTYPE(io) = '>';
141 break;
142 case O_RDWR:
143 default:
144 IoTYPE(io) = '+';
145 break;
146 }
147
c07a80fd 148 writing = (result > 0);
3028581b 149 fd = PerlLIO_open3(name, rawmode, rawperm);
9d116dd7 150
c07a80fd 151 if (fd == -1)
152 fp = NULL;
153 else {
360e5741 154 char *fpmode;
9d116dd7 155 if (result == O_RDONLY)
360e5741 156 fpmode = "r";
157#ifdef O_APPEND
158 else if (rawmode & O_APPEND)
9d116dd7 159 fpmode = (result == O_WRONLY) ? "a" : "a+";
360e5741 160#endif
161 else
9d116dd7 162 fpmode = (result == O_WRONLY) ? "w" : "r+";
360e5741 163 fp = PerlIO_fdopen(fd, fpmode);
c07a80fd 164 if (!fp)
3028581b 165 PerlLIO_close(fd);
c07a80fd 166 }
a687059c 167 }
c07a80fd 168 else {
169 char *myname;
170 char mode[3]; /* stdio file mode ("r\0" or "r+\0") */
171 int dodup;
172
173 myname = savepvn(name, len);
174 SAVEFREEPV(myname);
175 name = myname;
176 while (len && isSPACE(name[len-1]))
177 name[--len] = '\0';
178
179 mode[0] = mode[1] = mode[2] = '\0';
180 IoTYPE(io) = *name;
181 if (*name == '+' && len > 1 && name[len-1] != '|') { /* scary */
182 mode[1] = *name++;
183 --len;
184 writing = 1;
a687059c 185 }
c07a80fd 186
187 if (*name == '|') {
188 /*SUPPRESS 530*/
189 for (name++; isSPACE(*name); name++) ;
06eaf0bc 190 if (*name == '\0') { /* command is missing 19990114 */
191 dTHR;
192 if (ckWARN(WARN_PIPE))
193 warner(WARN_PIPE, "Missing command in piped open");
194 errno = EPIPE;
195 goto say_false;
196 }
c07a80fd 197 if (strNE(name,"-"))
198 TAINT_ENV();
199 TAINT_PROPER("piped open");
7b8d334a 200 if (name[strlen(name)-1] == '|') {
d008e5eb 201 dTHR;
7b8d334a 202 name[strlen(name)-1] = '\0' ;
599cee73 203 if (ckWARN(WARN_PIPE))
204 warner(WARN_PIPE, "Can't do bidirectional pipe");
7b8d334a 205 }
3028581b 206 fp = PerlProc_popen(name,"w");
c07a80fd 207 writing = 1;
208 }
209 else if (*name == '>') {
210 TAINT_PROPER("open");
bf38876a 211 name++;
c07a80fd 212 if (*name == '>') {
213 mode[0] = IoTYPE(io) = 'a';
bf38876a 214 name++;
a0d0e21e 215 }
c07a80fd 216 else
217 mode[0] = 'w';
218 writing = 1;
219
220 if (*name == '&') {
221 duplicity:
222 dodup = 1;
223 name++;
224 if (*name == '=') {
225 dodup = 0;
a0d0e21e 226 name++;
c07a80fd 227 }
228 if (!*name && supplied_fp)
229 fp = supplied_fp;
a0d0e21e 230 else {
c07a80fd 231 /*SUPPRESS 530*/
232 for (; isSPACE(*name); name++) ;
233 if (isDIGIT(*name))
234 fd = atoi(name);
235 else {
236 IO* thatio;
237 gv = gv_fetchpv(name,FALSE,SVt_PVIO);
238 thatio = GvIO(gv);
239 if (!thatio) {
6e21c824 240#ifdef EINVAL
c07a80fd 241 SETERRNO(EINVAL,SS$_IVCHAN);
6e21c824 242#endif
c07a80fd 243 goto say_false;
244 }
245 if (IoIFP(thatio)) {
760ac839 246 fd = PerlIO_fileno(IoIFP(thatio));
c07a80fd 247 if (IoTYPE(thatio) == 's')
248 IoTYPE(io) = 's';
249 }
250 else
251 fd = -1;
a0d0e21e 252 }
fec02dd3 253 if (dodup)
3028581b 254 fd = PerlLIO_dup(fd);
3500f679 255 else
256 was_fdopen = TRUE;
760ac839 257 if (!(fp = PerlIO_fdopen(fd,mode))) {
c07a80fd 258 if (dodup)
3028581b 259 PerlLIO_close(fd);
517844ec 260 }
c07a80fd 261 }
bf38876a 262 }
c07a80fd 263 else {
264 /*SUPPRESS 530*/
265 for (; isSPACE(*name); name++) ;
266 if (strEQ(name,"-")) {
760ac839 267 fp = PerlIO_stdout();
c07a80fd 268 IoTYPE(io) = '-';
269 }
270 else {
760ac839 271 fp = PerlIO_open(name,mode);
c07a80fd 272 }
bf38876a 273 }
274 }
c07a80fd 275 else if (*name == '<') {
276 /*SUPPRESS 530*/
277 for (name++; isSPACE(*name); name++) ;
bf38876a 278 mode[0] = 'r';
bf38876a 279 if (*name == '&')
280 goto duplicity;
a687059c 281 if (strEQ(name,"-")) {
760ac839 282 fp = PerlIO_stdin();
8990e307 283 IoTYPE(io) = '-';
a687059c 284 }
bf38876a 285 else
760ac839 286 fp = PerlIO_open(name,mode);
a687059c 287 }
00db4c45 288 else if (len > 1 && name[len-1] == '|') {
a687059c 289 name[--len] = '\0';
99b89507 290 while (len && isSPACE(name[len-1]))
a687059c 291 name[--len] = '\0';
99b89507 292 /*SUPPRESS 530*/
293 for (; isSPACE(*name); name++) ;
06eaf0bc 294 if (*name == '\0') { /* command is missing 19990114 */
295 dTHR;
296 if (ckWARN(WARN_PIPE))
297 warner(WARN_PIPE, "Missing command in piped open");
298 errno = EPIPE;
299 goto say_false;
300 }
79072805 301 if (strNE(name,"-"))
302 TAINT_ENV();
303 TAINT_PROPER("piped open");
3028581b 304 fp = PerlProc_popen(name,"r");
8990e307 305 IoTYPE(io) = '|';
a687059c 306 }
307 else {
8990e307 308 IoTYPE(io) = '<';
99b89507 309 /*SUPPRESS 530*/
310 for (; isSPACE(*name); name++) ;
a687059c 311 if (strEQ(name,"-")) {
760ac839 312 fp = PerlIO_stdin();
8990e307 313 IoTYPE(io) = '-';
a687059c 314 }
315 else
760ac839 316 fp = PerlIO_open(name,"r");
a687059c 317 }
318 }
bee1dbe2 319 if (!fp) {
d008e5eb 320 dTHR;
599cee73 321 if (ckWARN(WARN_NEWLINE) && IoTYPE(io) == '<' && strchr(name, '\n'))
22c35a8c 322 warner(WARN_NEWLINE, PL_warn_nl, "open");
6e21c824 323 goto say_false;
bee1dbe2 324 }
8990e307 325 if (IoTYPE(io) &&
326 IoTYPE(io) != '|' && IoTYPE(io) != '-') {
96827780 327 dTHR;
3280af22 328 if (PerlLIO_fstat(PerlIO_fileno(fp),&PL_statbuf) < 0) {
760ac839 329 (void)PerlIO_close(fp);
6e21c824 330 goto say_false;
a687059c 331 }
3280af22 332 if (S_ISSOCK(PL_statbuf.st_mode))
8990e307 333 IoTYPE(io) = 's'; /* in case a socket was passed in to us */
99b89507 334#ifdef HAS_SOCKET
335 else if (
c623bd54 336#ifdef S_IFMT
3280af22 337 !(PL_statbuf.st_mode & S_IFMT)
99b89507 338#else
b28d0864 339 !PL_statbuf.st_mode
99b89507 340#endif
341 ) {
96827780 342 char tmpbuf[256];
343 Sock_size_t buflen = sizeof tmpbuf;
3028581b 344 if (PerlSock_getsockname(PerlIO_fileno(fp), (struct sockaddr *)tmpbuf,
d574b85e 345 &buflen) >= 0
346 || errno != ENOTSOCK)
8990e307 347 IoTYPE(io) = 's'; /* some OS's return 0 on fstat()ed socket */
99b89507 348 /* but some return 0 for streams too, sigh */
349 }
bf38876a 350#endif
a687059c 351 }
6e21c824 352 if (saveifp) { /* must use old fp? */
760ac839 353 fd = PerlIO_fileno(saveifp);
6e21c824 354 if (saveofp) {
760ac839 355 PerlIO_flush(saveofp); /* emulate PerlIO_close() */
6e21c824 356 if (saveofp != saveifp) { /* was a socket? */
760ac839 357 PerlIO_close(saveofp);
99b89507 358 if (fd > 2)
359 Safefree(saveofp);
6e21c824 360 }
361 }
760ac839 362 if (fd != PerlIO_fileno(fp)) {
bee1dbe2 363 int pid;
79072805 364 SV *sv;
bee1dbe2 365
3028581b 366 PerlLIO_dup2(PerlIO_fileno(fp), fd);
3280af22 367 sv = *av_fetch(PL_fdpid,PerlIO_fileno(fp),TRUE);
a0d0e21e 368 (void)SvUPGRADE(sv, SVt_IV);
463ee0b2 369 pid = SvIVX(sv);
370 SvIVX(sv) = 0;
3280af22 371 sv = *av_fetch(PL_fdpid,fd,TRUE);
a0d0e21e 372 (void)SvUPGRADE(sv, SVt_IV);
463ee0b2 373 SvIVX(sv) = pid;
3500f679 374 if (!was_fdopen)
375 PerlIO_close(fp);
bee1dbe2 376
6e21c824 377 }
378 fp = saveifp;
760ac839 379 PerlIO_clearerr(fp);
6e21c824 380 }
a0d0e21e 381#if defined(HAS_FCNTL) && defined(F_SETFD)
a8710ca1 382 {
383 int save_errno = errno;
384 fd = PerlIO_fileno(fp);
385 fcntl(fd,F_SETFD,fd > PL_maxsysfd); /* can change errno */
386 errno = save_errno;
387 }
1462b684 388#endif
8990e307 389 IoIFP(io) = fp;
bf38876a 390 if (writing) {
96827780 391 dTHR;
8990e307 392 if (IoTYPE(io) == 's'
3280af22 393 || (IoTYPE(io) == '>' && S_ISCHR(PL_statbuf.st_mode)) ) {
760ac839 394 if (!(IoOFP(io) = PerlIO_fdopen(PerlIO_fileno(fp),"w"))) {
395 PerlIO_close(fp);
8990e307 396 IoIFP(io) = Nullfp;
6e21c824 397 goto say_false;
fe14fcc3 398 }
1462b684 399 }
400 else
8990e307 401 IoOFP(io) = fp;
bf38876a 402 }
a687059c 403 return TRUE;
6e21c824 404
405say_false:
8990e307 406 IoIFP(io) = saveifp;
407 IoOFP(io) = saveofp;
408 IoTYPE(io) = savetype;
6e21c824 409 return FALSE;
a687059c 410}
411
760ac839 412PerlIO *
8ac85365 413nextargv(register GV *gv)
a687059c 414{
79072805 415 register SV *sv;
99b89507 416#ifndef FLEXFILENAMES
c623bd54 417 int filedev;
418 int fileino;
99b89507 419#endif
761237fe 420 Uid_t fileuid;
421 Gid_t filegid;
fe14fcc3 422
3280af22 423 if (!PL_argvoutgv)
424 PL_argvoutgv = gv_fetchpv("ARGVOUT",TRUE,SVt_PVIO);
425 if (PL_filemode & (S_ISUID|S_ISGID)) {
426 PerlIO_flush(IoIFP(GvIOn(PL_argvoutgv))); /* chmod must follow last write */
fe14fcc3 427#ifdef HAS_FCHMOD
3280af22 428 (void)fchmod(PL_lastfd,PL_filemode);
fe14fcc3 429#else
b28d0864 430 (void)PerlLIO_chmod(PL_oldname,PL_filemode);
fe14fcc3 431#endif
432 }
3280af22 433 PL_filemode = 0;
79072805 434 while (av_len(GvAV(gv)) >= 0) {
11343788 435 dTHR;
85aff577 436 STRLEN oldlen;
79072805 437 sv = av_shift(GvAV(gv));
8990e307 438 SAVEFREESV(sv);
79072805 439 sv_setsv(GvSV(gv),sv);
440 SvSETMAGIC(GvSV(gv));
3280af22 441 PL_oldname = SvPVx(GvSV(gv), oldlen);
9d116dd7 442 if (do_open(gv,PL_oldname,oldlen,PL_inplace!=0,O_RDONLY,0,Nullfp)) {
3280af22 443 if (PL_inplace) {
79072805 444 TAINT_PROPER("inplace open");
3280af22 445 if (oldlen == 1 && *PL_oldname == '-') {
4633a7c4 446 setdefout(gv_fetchpv("STDOUT",TRUE,SVt_PVIO));
a0d0e21e 447 return IoIFP(GvIOp(gv));
c623bd54 448 }
99b89507 449#ifndef FLEXFILENAMES
b28d0864 450 filedev = PL_statbuf.st_dev;
451 fileino = PL_statbuf.st_ino;
99b89507 452#endif
3280af22 453 PL_filemode = PL_statbuf.st_mode;
454 fileuid = PL_statbuf.st_uid;
455 filegid = PL_statbuf.st_gid;
456 if (!S_ISREG(PL_filemode)) {
c623bd54 457 warn("Can't do inplace edit: %s is not a regular file",
3280af22 458 PL_oldname );
79072805 459 do_close(gv,FALSE);
c623bd54 460 continue;
461 }
3280af22 462 if (*PL_inplace) {
463 char *star = strchr(PL_inplace, '*');
2d259d92 464 if (star) {
3280af22 465 char *begin = PL_inplace;
2d259d92 466 sv_setpvn(sv, "", 0);
467 do {
468 sv_catpvn(sv, begin, star - begin);
3280af22 469 sv_catpvn(sv, PL_oldname, oldlen);
2d259d92 470 begin = ++star;
471 } while ((star = strchr(begin, '*')));
3d66d7bb 472 if (*begin)
473 sv_catpv(sv,begin);
2d259d92 474 }
475 else {
3280af22 476 sv_catpv(sv,PL_inplace);
2d259d92 477 }
c623bd54 478#ifndef FLEXFILENAMES
b28d0864 479 if (PerlLIO_stat(SvPVX(sv),&PL_statbuf) >= 0
480 && PL_statbuf.st_dev == filedev
481 && PL_statbuf.st_ino == fileino
39e571d4 482#ifdef DJGPP
483 || (_djstat_fail_bits & _STFAIL_TRUENAME)!=0
484#endif
485 ) {
752ebe84 486 warn("Can't do inplace edit: %s would not be unique",
463ee0b2 487 SvPVX(sv) );
79072805 488 do_close(gv,FALSE);
c623bd54 489 continue;
490 }
491#endif
fe14fcc3 492#ifdef HAS_RENAME
bee1dbe2 493#ifndef DOSISH
3280af22 494 if (PerlLIO_rename(PL_oldname,SvPVX(sv)) < 0) {
c623bd54 495 warn("Can't rename %s to %s: %s, skipping file",
3280af22 496 PL_oldname, SvPVX(sv), Strerror(errno) );
79072805 497 do_close(gv,FALSE);
c623bd54 498 continue;
499 }
a687059c 500#else
79072805 501 do_close(gv,FALSE);
3028581b 502 (void)PerlLIO_unlink(SvPVX(sv));
b28d0864 503 (void)PerlLIO_rename(PL_oldname,SvPVX(sv));
9d116dd7 504 do_open(gv,SvPVX(sv),SvCUR(sv),PL_inplace!=0,O_RDONLY,0,Nullfp);
55497cff 505#endif /* DOSISH */
ff8e2863 506#else
463ee0b2 507 (void)UNLINK(SvPVX(sv));
b28d0864 508 if (link(PL_oldname,SvPVX(sv)) < 0) {
c623bd54 509 warn("Can't rename %s to %s: %s, skipping file",
b28d0864 510 PL_oldname, SvPVX(sv), Strerror(errno) );
79072805 511 do_close(gv,FALSE);
c623bd54 512 continue;
513 }
b28d0864 514 (void)UNLINK(PL_oldname);
a687059c 515#endif
516 }
517 else {
a8c18271 518#if !defined(DOSISH) && !defined(AMIGAOS)
edc7bc49 519# ifndef VMS /* Don't delete; use automatic file versioning */
3280af22 520 if (UNLINK(PL_oldname) < 0) {
85aff577 521 warn("Can't remove %s: %s, skipping file",
3280af22 522 PL_oldname, Strerror(errno) );
79072805 523 do_close(gv,FALSE);
fe14fcc3 524 continue;
525 }
edc7bc49 526# endif
ff8e2863 527#else
463ee0b2 528 croak("Can't do inplace edit without backup");
ff8e2863 529#endif
a687059c 530 }
531
3280af22 532 sv_setpvn(sv,">",!PL_inplace);
533 sv_catpvn(sv,PL_oldname,oldlen);
748a9306 534 SETERRNO(0,0); /* in case sprintf set errno */
4119ab01 535#ifdef VMS
536 if (!do_open(PL_argvoutgv,SvPVX(sv),SvCUR(sv),PL_inplace!=0,
537 O_WRONLY|O_CREAT|O_TRUNC,0,Nullfp)) {
538#else
3280af22 539 if (!do_open(PL_argvoutgv,SvPVX(sv),SvCUR(sv),PL_inplace!=0,
85aff577 540 O_WRONLY|O_CREAT|OPEN_EXCL,0666,Nullfp)) {
4119ab01 541#endif
c623bd54 542 warn("Can't do inplace edit on %s: %s",
3280af22 543 PL_oldname, Strerror(errno) );
79072805 544 do_close(gv,FALSE);
fe14fcc3 545 continue;
546 }
3280af22 547 setdefout(PL_argvoutgv);
548 PL_lastfd = PerlIO_fileno(IoIFP(GvIOp(PL_argvoutgv)));
549 (void)PerlLIO_fstat(PL_lastfd,&PL_statbuf);
fe14fcc3 550#ifdef HAS_FCHMOD
3280af22 551 (void)fchmod(PL_lastfd,PL_filemode);
a687059c 552#else
3e3baf6d 553# if !(defined(WIN32) && defined(__BORLANDC__))
554 /* Borland runtime creates a readonly file! */
b28d0864 555 (void)PerlLIO_chmod(PL_oldname,PL_filemode);
3e3baf6d 556# endif
a687059c 557#endif
3280af22 558 if (fileuid != PL_statbuf.st_uid || filegid != PL_statbuf.st_gid) {
fe14fcc3 559#ifdef HAS_FCHOWN
3280af22 560 (void)fchown(PL_lastfd,fileuid,filegid);
a687059c 561#else
fe14fcc3 562#ifdef HAS_CHOWN
b28d0864 563 (void)PerlLIO_chown(PL_oldname,fileuid,filegid);
a687059c 564#endif
b1248f16 565#endif
fe14fcc3 566 }
a687059c 567 }
a0d0e21e 568 return IoIFP(GvIOp(gv));
a687059c 569 }
570 else
22fae026 571 PerlIO_printf(PerlIO_stderr(), "Can't open %s: %s\n",
2d8e6c8d 572 SvPV(sv, oldlen), Strerror(errno));
a687059c 573 }
3280af22 574 if (PL_inplace) {
575 (void)do_close(PL_argvoutgv,FALSE);
4633a7c4 576 setdefout(gv_fetchpv("STDOUT",TRUE,SVt_PVIO));
a687059c 577 }
578 return Nullfp;
579}
580
fe14fcc3 581#ifdef HAS_PIPE
afd9f252 582void
8ac85365 583do_pipe(SV *sv, GV *rgv, GV *wgv)
afd9f252 584{
79072805 585 register IO *rstio;
586 register IO *wstio;
afd9f252 587 int fd[2];
588
79072805 589 if (!rgv)
afd9f252 590 goto badexit;
79072805 591 if (!wgv)
afd9f252 592 goto badexit;
593
a0d0e21e 594 rstio = GvIOn(rgv);
595 wstio = GvIOn(wgv);
afd9f252 596
a0d0e21e 597 if (IoIFP(rstio))
79072805 598 do_close(rgv,FALSE);
a0d0e21e 599 if (IoIFP(wstio))
79072805 600 do_close(wgv,FALSE);
afd9f252 601
3028581b 602 if (PerlProc_pipe(fd) < 0)
afd9f252 603 goto badexit;
760ac839 604 IoIFP(rstio) = PerlIO_fdopen(fd[0], "r");
605 IoOFP(wstio) = PerlIO_fdopen(fd[1], "w");
8990e307 606 IoIFP(wstio) = IoOFP(wstio);
607 IoTYPE(rstio) = '<';
608 IoTYPE(wstio) = '>';
609 if (!IoIFP(rstio) || !IoOFP(wstio)) {
760ac839 610 if (IoIFP(rstio)) PerlIO_close(IoIFP(rstio));
3028581b 611 else PerlLIO_close(fd[0]);
760ac839 612 if (IoOFP(wstio)) PerlIO_close(IoOFP(wstio));
3028581b 613 else PerlLIO_close(fd[1]);
fe14fcc3 614 goto badexit;
615 }
afd9f252 616
3280af22 617 sv_setsv(sv,&PL_sv_yes);
afd9f252 618 return;
619
620badexit:
3280af22 621 sv_setsv(sv,&PL_sv_undef);
afd9f252 622 return;
623}
b1248f16 624#endif
afd9f252 625
517844ec 626/* explicit renamed to avoid C++ conflict -- kja */
a687059c 627bool
517844ec 628do_close(GV *gv, bool not_implicit)
a687059c 629{
1193dd27 630 bool retval;
631 IO *io;
a687059c 632
79072805 633 if (!gv)
3280af22 634 gv = PL_argvgv;
a0d0e21e 635 if (!gv || SvTYPE(gv) != SVt_PVGV) {
1d2dff63 636 if (not_implicit)
637 SETERRNO(EBADF,SS$_IVCHAN);
c2ab57d4 638 return FALSE;
99b89507 639 }
79072805 640 io = GvIO(gv);
641 if (!io) { /* never opened */
1d2dff63 642 if (not_implicit) {
d008e5eb 643 dTHR;
599cee73 644 if (ckWARN(WARN_UNOPENED))
645 warner(WARN_UNOPENED,
646 "Close on unopened file <%s>",GvENAME(gv));
1d2dff63 647 SETERRNO(EBADF,SS$_IVCHAN);
648 }
a687059c 649 return FALSE;
650 }
1193dd27 651 retval = io_close(io);
517844ec 652 if (not_implicit) {
1193dd27 653 IoLINES(io) = 0;
654 IoPAGE(io) = 0;
655 IoLINES_LEFT(io) = IoPAGE_LEN(io);
656 }
657 IoTYPE(io) = ' ';
658 return retval;
659}
660
661bool
8ac85365 662io_close(IO *io)
1193dd27 663{
664 bool retval = FALSE;
665 int status;
666
8990e307 667 if (IoIFP(io)) {
668 if (IoTYPE(io) == '|') {
3028581b 669 status = PerlProc_pclose(IoIFP(io));
f86702cc 670 STATUS_NATIVE_SET(status);
1e422769 671 retval = (STATUS_POSIX == 0);
a687059c 672 }
8990e307 673 else if (IoTYPE(io) == '-')
a687059c 674 retval = TRUE;
675 else {
8990e307 676 if (IoOFP(io) && IoOFP(io) != IoIFP(io)) { /* a socket */
760ac839 677 retval = (PerlIO_close(IoOFP(io)) != EOF);
678 PerlIO_close(IoIFP(io)); /* clear stdio, fd already closed */
c2ab57d4 679 }
680 else
760ac839 681 retval = (PerlIO_close(IoIFP(io)) != EOF);
a687059c 682 }
8990e307 683 IoOFP(io) = IoIFP(io) = Nullfp;
79072805 684 }
20408e3c 685 else {
686 SETERRNO(EBADF,SS$_IVCHAN);
687 }
1193dd27 688
a687059c 689 return retval;
690}
691
692bool
8ac85365 693do_eof(GV *gv)
a687059c 694{
11343788 695 dTHR;
79072805 696 register IO *io;
a687059c 697 int ch;
698
79072805 699 io = GvIO(gv);
a687059c 700
79072805 701 if (!io)
a687059c 702 return TRUE;
703
8990e307 704 while (IoIFP(io)) {
a687059c 705
760ac839 706 if (PerlIO_has_cntptr(IoIFP(io))) { /* (the code works without this) */
707 if (PerlIO_get_cnt(IoIFP(io)) > 0) /* cheat a little, since */
708 return FALSE; /* this is the most usual case */
709 }
a687059c 710
760ac839 711 ch = PerlIO_getc(IoIFP(io));
a687059c 712 if (ch != EOF) {
760ac839 713 (void)PerlIO_ungetc(IoIFP(io),ch);
a687059c 714 return FALSE;
715 }
760ac839 716 if (PerlIO_has_cntptr(IoIFP(io)) && PerlIO_canset_cnt(IoIFP(io))) {
717 if (PerlIO_get_cnt(IoIFP(io)) < -1)
718 PerlIO_set_cnt(IoIFP(io),-1);
719 }
533c011a 720 if (PL_op->op_flags & OPf_SPECIAL) { /* not necessarily a real EOF yet? */
3280af22 721 if (!nextargv(PL_argvgv)) /* get another fp handy */
a687059c 722 return TRUE;
723 }
724 else
725 return TRUE; /* normal fp, definitely end of file */
726 }
727 return TRUE;
728}
729
5ff3f7a4 730Off_t
8ac85365 731do_tell(GV *gv)
a687059c 732{
79072805 733 register IO *io;
96e4d5b1 734 register PerlIO *fp;
a687059c 735
96e4d5b1 736 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io))) {
bee1dbe2 737#ifdef ULTRIX_STDIO_BOTCH
96e4d5b1 738 if (PerlIO_eof(fp))
739 (void)PerlIO_seek(fp, 0L, 2); /* ultrix 1.2 workaround */
bee1dbe2 740#endif
8903cb82 741 return PerlIO_tell(fp);
96e4d5b1 742 }
d008e5eb 743 {
744 dTHR;
745 if (ckWARN(WARN_UNOPENED))
746 warner(WARN_UNOPENED, "tell() on unopened file");
747 }
748a9306 748 SETERRNO(EBADF,RMS$_IFI);
5ff3f7a4 749 return (Off_t)-1;
a687059c 750}
751
752bool
5ff3f7a4 753do_seek(GV *gv, Off_t pos, int whence)
a687059c 754{
79072805 755 register IO *io;
137443ea 756 register PerlIO *fp;
a687059c 757
137443ea 758 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io))) {
bee1dbe2 759#ifdef ULTRIX_STDIO_BOTCH
137443ea 760 if (PerlIO_eof(fp))
761 (void)PerlIO_seek(fp, 0L, 2); /* ultrix 1.2 workaround */
bee1dbe2 762#endif
8903cb82 763 return PerlIO_seek(fp, pos, whence) >= 0;
137443ea 764 }
d008e5eb 765 {
766 dTHR;
767 if (ckWARN(WARN_UNOPENED))
768 warner(WARN_UNOPENED, "seek() on unopened file");
769 }
748a9306 770 SETERRNO(EBADF,RMS$_IFI);
a687059c 771 return FALSE;
772}
773
97cc44eb 774Off_t
775do_sysseek(GV *gv, Off_t pos, int whence)
8903cb82 776{
777 register IO *io;
778 register PerlIO *fp;
779
780 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io)))
3028581b 781 return PerlLIO_lseek(PerlIO_fileno(fp), pos, whence);
d008e5eb 782 {
783 dTHR;
784 if (ckWARN(WARN_UNOPENED))
785 warner(WARN_UNOPENED, "sysseek() on unopened file");
786 }
8903cb82 787 SETERRNO(EBADF,RMS$_IFI);
788 return -1L;
789}
790
6ff81951 791int
792do_binmode(PerlIO *fp, int iotype, int flag)
793{
794 if (flag != TRUE)
795 croak("panic: unsetting binmode"); /* Not implemented yet */
796#ifdef DOSISH
61ae2fbf 797#if defined(atarist) || defined(__MINT__)
6ff81951 798 if (!PerlIO_flush(fp) && (fp->_flag |= _IOBIN))
799 return 1;
800 else
801 return 0;
802#else
803 if (PerlLIO_setmode(PerlIO_fileno(fp), OP_BINARY) != -1) {
804#if defined(WIN32) && defined(__BORLANDC__)
805 /* The translation mode of the stream is maintained independent
806 * of the translation mode of the fd in the Borland RTL (heavy
807 * digging through their runtime sources reveal). User has to
808 * set the mode explicitly for the stream (though they don't
809 * document this anywhere). GSAR 97-5-24
810 */
811 PerlIO_seek(fp,0L,0);
873ef191 812 ((FILE*)fp)->flags |= _F_BIN;
6ff81951 813#endif
814 return 1;
815 }
816 else
817 return 0;
818#endif
819#else
820#if defined(USEMYBINMODE)
821 if (my_binmode(fp,iotype) != NULL)
822 return 1;
823 else
824 return 0;
825#else
826 return 1;
827#endif
828#endif
829}
830
a0d0e21e 831#if !defined(HAS_TRUNCATE) && !defined(HAS_CHSIZE) && defined(F_FREESP)
c2ab57d4 832 /* code courtesy of William Kucharski */
fe14fcc3 833#define HAS_CHSIZE
6eb13c3b 834
517844ec 835I32 my_chsize(fd, length)
79072805 836I32 fd; /* file descriptor */
85e6fe83 837Off_t length; /* length to set file to */
6eb13c3b 838{
6eb13c3b 839 struct flock fl;
840 struct stat filebuf;
841
3028581b 842 if (PerlLIO_fstat(fd, &filebuf) < 0)
6eb13c3b 843 return -1;
844
845 if (filebuf.st_size < length) {
846
847 /* extend file length */
848
3028581b 849 if ((PerlLIO_lseek(fd, (length - 1), 0)) < 0)
6eb13c3b 850 return -1;
851
852 /* write a "0" byte */
853
3028581b 854 if ((PerlLIO_write(fd, "", 1)) != 1)
6eb13c3b 855 return -1;
856 }
857 else {
858 /* truncate length */
859
860 fl.l_whence = 0;
861 fl.l_len = 0;
862 fl.l_start = length;
a0d0e21e 863 fl.l_type = F_WRLCK; /* write lock on file space */
6eb13c3b 864
865 /*
a0d0e21e 866 * This relies on the UNDOCUMENTED F_FREESP argument to
6eb13c3b 867 * fcntl(2), which truncates the file so that it ends at the
868 * position indicated by fl.l_start.
869 *
870 * Will minor miracles never cease?
871 */
872
a0d0e21e 873 if (fcntl(fd, F_FREESP, &fl) < 0)
6eb13c3b 874 return -1;
875
876 }
877
878 return 0;
879}
a0d0e21e 880#endif /* F_FREESP */
ff8e2863 881
a687059c 882bool
6acef3b7 883do_print(register SV *sv, PerlIO *fp)
a687059c 884{
885 register char *tmps;
463ee0b2 886 STRLEN len;
a687059c 887
79072805 888 /* assuming fp is checked earlier */
889 if (!sv)
890 return TRUE;
3280af22 891 if (PL_ofmt) {
8990e307 892 if (SvGMAGICAL(sv))
79072805 893 mg_get(sv);
463ee0b2 894 if (SvIOK(sv) && SvIVX(sv) != 0) {
3280af22 895 PerlIO_printf(fp, PL_ofmt, (double)SvIVX(sv));
760ac839 896 return !PerlIO_error(fp);
79072805 897 }
463ee0b2 898 if ( (SvNOK(sv) && SvNVX(sv) != 0.0)
79072805 899 || (looks_like_number(sv) && sv_2nv(sv) != 0.0) ) {
3280af22 900 PerlIO_printf(fp, PL_ofmt, SvNVX(sv));
760ac839 901 return !PerlIO_error(fp);
79072805 902 }
a687059c 903 }
79072805 904 switch (SvTYPE(sv)) {
905 case SVt_NULL:
d008e5eb 906 {
907 dTHR;
908 if (ckWARN(WARN_UNINITIALIZED))
22c35a8c 909 warner(WARN_UNINITIALIZED, PL_warn_uninit);
d008e5eb 910 }
ff8e2863 911 return TRUE;
79072805 912 case SVt_IV:
a0d0e21e 913 if (SvIOK(sv)) {
914 if (SvGMAGICAL(sv))
915 mg_get(sv);
760ac839 916 PerlIO_printf(fp, "%ld", (long)SvIVX(sv));
917 return !PerlIO_error(fp);
a0d0e21e 918 }
919 /* FALL THROUGH */
79072805 920 default:
463ee0b2 921 tmps = SvPV(sv, len);
79072805 922 break;
ff8e2863 923 }
760ac839 924 if (len && (PerlIO_write(fp,tmps,len) == 0 || PerlIO_error(fp)))
a687059c 925 return FALSE;
760ac839 926 return !PerlIO_error(fp);
a687059c 927}
928
79072805 929I32
8ac85365 930my_stat(ARGSproto)
a687059c 931{
4e35701f 932 djSP;
79072805 933 IO *io;
748a9306 934 GV* tmpgv;
79072805 935
533c011a 936 if (PL_op->op_flags & OPf_REF) {
924508f0 937 EXTEND(SP,1);
748a9306 938 tmpgv = cGVOP->op_gv;
939 do_fstat:
940 io = GvIO(tmpgv);
8990e307 941 if (io && IoIFP(io)) {
3280af22 942 PL_statgv = tmpgv;
943 sv_setpv(PL_statname,"");
944 PL_laststype = OP_STAT;
945 return (PL_laststatval = PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache));
a687059c 946 }
947 else {
3280af22 948 if (tmpgv == PL_defgv)
949 return PL_laststatval;
599cee73 950 if (ckWARN(WARN_UNOPENED))
951 warner(WARN_UNOPENED, "Stat on unopened file <%s>",
748a9306 952 GvENAME(tmpgv));
3280af22 953 PL_statgv = Nullgv;
954 sv_setpv(PL_statname,"");
955 return (PL_laststatval = -1);
a687059c 956 }
957 }
958 else {
748a9306 959 SV* sv = POPs;
4b74e3fb 960 char *s;
2d8e6c8d 961 STRLEN n_a;
79072805 962 PUTBACK;
748a9306 963 if (SvTYPE(sv) == SVt_PVGV) {
964 tmpgv = (GV*)sv;
965 goto do_fstat;
966 }
967 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
968 tmpgv = (GV*)SvRV(sv);
969 goto do_fstat;
970 }
971
2d8e6c8d 972 s = SvPV(sv, n_a);
3280af22 973 PL_statgv = Nullgv;
974 sv_setpv(PL_statname, s);
975 PL_laststype = OP_STAT;
976 PL_laststatval = PerlLIO_stat(s, &PL_statcache);
599cee73 977 if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(s, '\n'))
22c35a8c 978 warner(WARN_NEWLINE, PL_warn_nl, "stat");
3280af22 979 return PL_laststatval;
a687059c 980 }
981}
982
79072805 983I32
8ac85365 984my_lstat(ARGSproto)
c623bd54 985{
4e35701f 986 djSP;
79072805 987 SV *sv;
2d8e6c8d 988 STRLEN n_a;
533c011a 989 if (PL_op->op_flags & OPf_REF) {
924508f0 990 EXTEND(SP,1);
3280af22 991 if (cGVOP->op_gv == PL_defgv) {
992 if (PL_laststype != OP_LSTAT)
463ee0b2 993 croak("The stat preceding -l _ wasn't an lstat");
3280af22 994 return PL_laststatval;
fe14fcc3 995 }
463ee0b2 996 croak("You can't use -l on a filehandle");
fe14fcc3 997 }
c623bd54 998
3280af22 999 PL_laststype = OP_LSTAT;
1000 PL_statgv = Nullgv;
79072805 1001 sv = POPs;
1002 PUTBACK;
2d8e6c8d 1003 sv_setpv(PL_statname,SvPV(sv, n_a));
fe14fcc3 1004#ifdef HAS_LSTAT
2d8e6c8d 1005 PL_laststatval = PerlLIO_lstat(SvPV(sv, n_a),&PL_statcache);
c623bd54 1006#else
2d8e6c8d 1007 PL_laststatval = PerlLIO_stat(SvPV(sv, n_a),&PL_statcache);
c623bd54 1008#endif
2d8e6c8d 1009 if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(SvPV(sv, n_a), '\n'))
22c35a8c 1010 warner(WARN_NEWLINE, PL_warn_nl, "lstat");
3280af22 1011 return PL_laststatval;
c623bd54 1012}
1013
a687059c 1014bool
8ac85365 1015do_aexec(SV *really, register SV **mark, register SV **sp)
a687059c 1016{
a687059c 1017 register char **a;
a687059c 1018 char *tmps;
2d8e6c8d 1019 STRLEN n_a;
a687059c 1020
79072805 1021 if (sp > mark) {
11343788 1022 dTHR;
3280af22 1023 New(401,PL_Argv, sp - mark + 1, char*);
1024 a = PL_Argv;
79072805 1025 while (++mark <= sp) {
1026 if (*mark)
2d8e6c8d 1027 *a++ = SvPVx(*mark, n_a);
a687059c 1028 else
1029 *a++ = "";
1030 }
1031 *a = Nullch;
3280af22 1032 if (*PL_Argv[0] != '/') /* will execvp use PATH? */
79072805 1033 TAINT_ENV(); /* testing IFS here is overkill, probably */
2d8e6c8d 1034 if (really && *(tmps = SvPV(really, n_a)))
3280af22 1035 PerlProc_execvp(tmps,PL_Argv);
a687059c 1036 else
3280af22 1037 PerlProc_execvp(PL_Argv[0],PL_Argv);
599cee73 1038 if (ckWARN(WARN_EXEC))
1039 warner(WARN_EXEC, "Can't exec \"%s\": %s",
1040 PL_Argv[0], Strerror(errno));
a687059c 1041 }
bee1dbe2 1042 do_execfree();
a687059c 1043 return FALSE;
1044}
1045
fe14fcc3 1046void
8ac85365 1047do_execfree(void)
ff8e2863 1048{
3280af22 1049 if (PL_Argv) {
1050 Safefree(PL_Argv);
1051 PL_Argv = Null(char **);
ff8e2863 1052 }
3280af22 1053 if (PL_Cmd) {
1054 Safefree(PL_Cmd);
1055 PL_Cmd = Nullch;
ff8e2863 1056 }
1057}
1058
39e571d4 1059#if !defined(OS2) && !defined(WIN32) && !defined(DJGPP)
760ac839 1060
a687059c 1061bool
8ac85365 1062do_exec(char *cmd)
a687059c 1063{
1064 register char **a;
1065 register char *s;
a687059c 1066 char flags[10];
1067
748a9306 1068 while (*cmd && isSPACE(*cmd))
1069 cmd++;
1070
a687059c 1071 /* save an extra exec if possible */
1072
bf38876a 1073#ifdef CSH
3280af22 1074 if (strnEQ(cmd,PL_cshname,PL_cshlen) && strnEQ(cmd+PL_cshlen," -c",3)) {
a687059c 1075 strcpy(flags,"-c");
3280af22 1076 s = cmd+PL_cshlen+3;
a687059c 1077 if (*s == 'f') {
1078 s++;
1079 strcat(flags,"f");
1080 }
1081 if (*s == ' ')
1082 s++;
1083 if (*s++ == '\'') {
1084 char *ncmd = s;
1085
1086 while (*s)
1087 s++;
1088 if (s[-1] == '\n')
1089 *--s = '\0';
1090 if (s[-1] == '\'') {
1091 *--s = '\0';
3280af22 1092 PerlProc_execl(PL_cshname,"csh", flags,ncmd,(char*)0);
a687059c 1093 *s = '\'';
1094 return FALSE;
1095 }
1096 }
1097 }
bf38876a 1098#endif /* CSH */
a687059c 1099
1100 /* see if there are shell metacharacters in it */
1101
748a9306 1102 if (*cmd == '.' && isSPACE(cmd[1]))
1103 goto doshell;
1104
1105 if (strnEQ(cmd,"exec",4) && isSPACE(cmd[4]))
1106 goto doshell;
1107
99b89507 1108 for (s = cmd; *s && isALPHA(*s); s++) ; /* catch VAR=val gizmo */
63f2c1e1 1109 if (*s == '=')
1110 goto doshell;
748a9306 1111
a687059c 1112 for (s = cmd; *s; s++) {
93a17b20 1113 if (*s != ' ' && !isALPHA(*s) && strchr("$&*(){}[]'\";\\|?<>~`\n",*s)) {
a687059c 1114 if (*s == '\n' && !s[1]) {
1115 *s = '\0';
1116 break;
1117 }
1118 doshell:
3280af22 1119 PerlProc_execl(PL_sh_path, "sh", "-c", cmd, (char*)0);
a687059c 1120 return FALSE;
1121 }
1122 }
748a9306 1123
3280af22 1124 New(402,PL_Argv, (s - cmd) / 2 + 2, char*);
1125 PL_Cmd = savepvn(cmd, s-cmd);
1126 a = PL_Argv;
1127 for (s = PL_Cmd; *s;) {
99b89507 1128 while (*s && isSPACE(*s)) s++;
a687059c 1129 if (*s)
1130 *(a++) = s;
99b89507 1131 while (*s && !isSPACE(*s)) s++;
a687059c 1132 if (*s)
1133 *s++ = '\0';
1134 }
1135 *a = Nullch;
3280af22 1136 if (PL_Argv[0]) {
1137 PerlProc_execvp(PL_Argv[0],PL_Argv);
b1248f16 1138 if (errno == ENOEXEC) { /* for system V NIH syndrome */
ff8e2863 1139 do_execfree();
a687059c 1140 goto doshell;
b1248f16 1141 }
d008e5eb 1142 {
1143 dTHR;
1144 if (ckWARN(WARN_EXEC))
1145 warner(WARN_EXEC, "Can't exec \"%s\": %s",
1146 PL_Argv[0], Strerror(errno));
1147 }
a687059c 1148 }
ff8e2863 1149 do_execfree();
a687059c 1150 return FALSE;
1151}
1152
6890e559 1153#endif /* OS2 || WIN32 */
760ac839 1154
79072805 1155I32
8ac85365 1156apply(I32 type, register SV **mark, register SV **sp)
a687059c 1157{
11343788 1158 dTHR;
79072805 1159 register I32 val;
1160 register I32 val2;
1161 register I32 tot = 0;
20408e3c 1162 char *what;
a687059c 1163 char *s;
79072805 1164 SV **oldmark = mark;
2d8e6c8d 1165 STRLEN n_a;
a687059c 1166
20408e3c 1167#define APPLY_TAINT_PROPER() \
3280af22 1168 STMT_START { \
17406bd6 1169 if (PL_tainted) { TAINT_PROPER(what); } \
873ef191 1170 } STMT_END
20408e3c 1171
1172 /* This is a first heuristic; it doesn't catch tainting magic. */
3280af22 1173 if (PL_tainting) {
463ee0b2 1174 while (++mark <= sp) {
bbce6d69 1175 if (SvTAINTED(*mark)) {
1176 TAINT;
1177 break;
1178 }
463ee0b2 1179 }
1180 mark = oldmark;
1181 }
a687059c 1182 switch (type) {
79072805 1183 case OP_CHMOD:
20408e3c 1184 what = "chmod";
1185 APPLY_TAINT_PROPER();
79072805 1186 if (++mark <= sp) {
463ee0b2 1187 val = SvIVx(*mark);
20408e3c 1188 APPLY_TAINT_PROPER();
1189 tot = sp - mark;
79072805 1190 while (++mark <= sp) {
2d8e6c8d 1191 char *name = SvPVx(*mark, n_a);
20408e3c 1192 APPLY_TAINT_PROPER();
1193 if (PerlLIO_chmod(name, val))
a687059c 1194 tot--;
1195 }
1196 }
1197 break;
fe14fcc3 1198#ifdef HAS_CHOWN
79072805 1199 case OP_CHOWN:
20408e3c 1200 what = "chown";
1201 APPLY_TAINT_PROPER();
79072805 1202 if (sp - mark > 2) {
463ee0b2 1203 val = SvIVx(*++mark);
1204 val2 = SvIVx(*++mark);
20408e3c 1205 APPLY_TAINT_PROPER();
a0d0e21e 1206 tot = sp - mark;
79072805 1207 while (++mark <= sp) {
2d8e6c8d 1208 char *name = SvPVx(*mark, n_a);
20408e3c 1209 APPLY_TAINT_PROPER();
36660982 1210 if (PerlLIO_chown(name, val, val2))
a687059c 1211 tot--;
1212 }
1213 }
1214 break;
b1248f16 1215#endif
dd64f1c3 1216/*
1217XXX Should we make lchown() directly available from perl?
1218For now, we'll let Configure test for HAS_LCHOWN, but do
1219nothing in the core.
1220 --AD 5/1998
1221*/
fe14fcc3 1222#ifdef HAS_KILL
79072805 1223 case OP_KILL:
20408e3c 1224 what = "kill";
1225 APPLY_TAINT_PROPER();
55497cff 1226 if (mark == sp)
1227 break;
2d8e6c8d 1228 s = SvPVx(*++mark, n_a);
79072805 1229 if (isUPPER(*s)) {
1230 if (*s == 'S' && s[1] == 'I' && s[2] == 'G')
1231 s += 3;
1232 if (!(val = whichsig(s)))
463ee0b2 1233 croak("Unrecognized signal name \"%s\"",s);
79072805 1234 }
1235 else
463ee0b2 1236 val = SvIVx(*mark);
20408e3c 1237 APPLY_TAINT_PROPER();
1238 tot = sp - mark;
3595fcef 1239#ifdef VMS
1240 /* kill() doesn't do process groups (job trees?) under VMS */
1241 if (val < 0) val = -val;
1242 if (val == SIGKILL) {
1243# include <starlet.h>
1244 /* Use native sys$delprc() to insure that target process is
1245 * deleted; supervisor-mode images don't pay attention to
1246 * CRTL's emulation of Unix-style signals and kill()
1247 */
1248 while (++mark <= sp) {
1249 I32 proc = SvIVx(*mark);
1250 register unsigned long int __vmssts;
20408e3c 1251 APPLY_TAINT_PROPER();
3595fcef 1252 if (!((__vmssts = sys$delprc(&proc,0)) & 1)) {
1253 tot--;
1254 switch (__vmssts) {
1255 case SS$_NONEXPR:
1256 case SS$_NOSUCHNODE:
1257 SETERRNO(ESRCH,__vmssts);
1258 break;
1259 case SS$_NOPRIV:
1260 SETERRNO(EPERM,__vmssts);
1261 break;
1262 default:
1263 SETERRNO(EVMSERR,__vmssts);
1264 }
1265 }
1266 }
1267 break;
1268 }
1269#endif
79072805 1270 if (val < 0) {
1271 val = -val;
1272 while (++mark <= sp) {
463ee0b2 1273 I32 proc = SvIVx(*mark);
20408e3c 1274 APPLY_TAINT_PROPER();
fe14fcc3 1275#ifdef HAS_KILLPG
3028581b 1276 if (PerlProc_killpg(proc,val)) /* BSD */
a687059c 1277#else
3028581b 1278 if (PerlProc_kill(-proc,val)) /* SYSV */
a687059c 1279#endif
79072805 1280 tot--;
a687059c 1281 }
79072805 1282 }
1283 else {
1284 while (++mark <= sp) {
20408e3c 1285 I32 proc = SvIVx(*mark);
1286 APPLY_TAINT_PROPER();
1287 if (PerlProc_kill(proc, val))
79072805 1288 tot--;
a687059c 1289 }
1290 }
1291 break;
b1248f16 1292#endif
79072805 1293 case OP_UNLINK:
20408e3c 1294 what = "unlink";
1295 APPLY_TAINT_PROPER();
79072805 1296 tot = sp - mark;
1297 while (++mark <= sp) {
2d8e6c8d 1298 s = SvPVx(*mark, n_a);
20408e3c 1299 APPLY_TAINT_PROPER();
3280af22 1300 if (PL_euid || PL_unsafe) {
a687059c 1301 if (UNLINK(s))
1302 tot--;
1303 }
1304 else { /* don't let root wipe out directories without -U */
fe14fcc3 1305#ifdef HAS_LSTAT
3280af22 1306 if (PerlLIO_lstat(s,&PL_statbuf) < 0 || S_ISDIR(PL_statbuf.st_mode))
a687059c 1307#else
b28d0864 1308 if (PerlLIO_stat(s,&PL_statbuf) < 0 || S_ISDIR(PL_statbuf.st_mode))
a687059c 1309#endif
a687059c 1310 tot--;
1311 else {
1312 if (UNLINK(s))
1313 tot--;
1314 }
1315 }
1316 }
1317 break;
a0d0e21e 1318#ifdef HAS_UTIME
79072805 1319 case OP_UTIME:
20408e3c 1320 what = "utime";
1321 APPLY_TAINT_PROPER();
79072805 1322 if (sp - mark > 2) {
748a9306 1323#if defined(I_UTIME) || defined(VMS)
663a0e37 1324 struct utimbuf utbuf;
1325#else
a687059c 1326 struct {
dd2821f6 1327 Time_t actime;
1328 Time_t modtime;
a687059c 1329 } utbuf;
663a0e37 1330#endif
a687059c 1331
afd9f252 1332 Zero(&utbuf, sizeof utbuf, char);
517844ec 1333#ifdef BIG_TIME
dd2821f6 1334 utbuf.actime = (Time_t)SvNVx(*++mark); /* time accessed */
1335 utbuf.modtime = (Time_t)SvNVx(*++mark); /* time modified */
517844ec 1336#else
dd2821f6 1337 utbuf.actime = (Time_t)SvIVx(*++mark); /* time accessed */
1338 utbuf.modtime = (Time_t)SvIVx(*++mark); /* time modified */
517844ec 1339#endif
20408e3c 1340 APPLY_TAINT_PROPER();
79072805 1341 tot = sp - mark;
1342 while (++mark <= sp) {
2d8e6c8d 1343 char *name = SvPVx(*mark, n_a);
20408e3c 1344 APPLY_TAINT_PROPER();
1345 if (PerlLIO_utime(name, &utbuf))
a687059c 1346 tot--;
1347 }
a687059c 1348 }
1349 else
79072805 1350 tot = 0;
a687059c 1351 break;
a0d0e21e 1352#endif
a687059c 1353 }
1354 return tot;
20408e3c 1355
20408e3c 1356#undef APPLY_TAINT_PROPER
a687059c 1357}
1358
1359/* Do the permissions allow some operation? Assumes statcache already set. */
a0d0e21e 1360#ifndef VMS /* VMS' cando is in vms.c */
79072805 1361I32
8ac85365 1362cando(I32 bit, I32 effective, register struct stat *statbufp)
a687059c 1363{
bee1dbe2 1364#ifdef DOSISH
fe14fcc3 1365 /* [Comments and code from Len Reed]
1366 * MS-DOS "user" is similar to UNIX's "superuser," but can't write
1367 * to write-protected files. The execute permission bit is set
1368 * by the Miscrosoft C library stat() function for the following:
1369 * .exe files
1370 * .com files
1371 * .bat files
1372 * directories
1373 * All files and directories are readable.
1374 * Directories and special files, e.g. "CON", cannot be
1375 * write-protected.
1376 * [Comment by Tom Dinger -- a directory can have the write-protect
1377 * bit set in the file system, but DOS permits changes to
1378 * the directory anyway. In addition, all bets are off
1379 * here for networked software, such as Novell and
1380 * Sun's PC-NFS.]
1381 */
1382
bee1dbe2 1383 /* Atari stat() does pretty much the same thing. we set x_bit_set_in_stat
1384 * too so it will actually look into the files for magic numbers
1385 */
fe14fcc3 1386 return (bit & statbufp->st_mode) ? TRUE : FALSE;
1387
55497cff 1388#else /* ! DOSISH */
3280af22 1389 if ((effective ? PL_euid : PL_uid) == 0) { /* root is special */
c623bd54 1390 if (bit == S_IXUSR) {
1391 if (statbufp->st_mode & 0111 || S_ISDIR(statbufp->st_mode))
a687059c 1392 return TRUE;
1393 }
1394 else
1395 return TRUE; /* root reads and writes anything */
1396 return FALSE;
1397 }
3280af22 1398 if (statbufp->st_uid == (effective ? PL_euid : PL_uid) ) {
a687059c 1399 if (statbufp->st_mode & bit)
1400 return TRUE; /* ok as "user" */
1401 }
79072805 1402 else if (ingroup((I32)statbufp->st_gid,effective)) {
a687059c 1403 if (statbufp->st_mode & bit >> 3)
1404 return TRUE; /* ok as "group" */
1405 }
1406 else if (statbufp->st_mode & bit >> 6)
1407 return TRUE; /* ok as "other" */
1408 return FALSE;
55497cff 1409#endif /* ! DOSISH */
a687059c 1410}
a0d0e21e 1411#endif /* ! VMS */
a687059c 1412
79072805 1413I32
8ac85365 1414ingroup(I32 testgid, I32 effective)
a687059c 1415{
3280af22 1416 if (testgid == (effective ? PL_egid : PL_gid))
a687059c 1417 return TRUE;
fe14fcc3 1418#ifdef HAS_GETGROUPS
a687059c 1419#ifndef NGROUPS
1420#define NGROUPS 32
1421#endif
1422 {
a0d0e21e 1423 Groups_t gary[NGROUPS];
79072805 1424 I32 anum;
a687059c 1425
1426 anum = getgroups(NGROUPS,gary);
1427 while (--anum >= 0)
1428 if (gary[anum] == testgid)
1429 return TRUE;
1430 }
1431#endif
1432 return FALSE;
1433}
c2ab57d4 1434
fe14fcc3 1435#if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
c2ab57d4 1436
79072805 1437I32
8ac85365 1438do_ipcget(I32 optype, SV **mark, SV **sp)
c2ab57d4 1439{
11343788 1440 dTHR;
c2ab57d4 1441 key_t key;
79072805 1442 I32 n, flags;
c2ab57d4 1443
463ee0b2 1444 key = (key_t)SvNVx(*++mark);
1445 n = (optype == OP_MSGGET) ? 0 : SvIVx(*++mark);
1446 flags = SvIVx(*++mark);
748a9306 1447 SETERRNO(0,0);
c2ab57d4 1448 switch (optype)
1449 {
fe14fcc3 1450#ifdef HAS_MSG
79072805 1451 case OP_MSGGET:
c2ab57d4 1452 return msgget(key, flags);
e5d73d77 1453#endif
fe14fcc3 1454#ifdef HAS_SEM
79072805 1455 case OP_SEMGET:
c2ab57d4 1456 return semget(key, n, flags);
e5d73d77 1457#endif
fe14fcc3 1458#ifdef HAS_SHM
79072805 1459 case OP_SHMGET:
c2ab57d4 1460 return shmget(key, n, flags);
e5d73d77 1461#endif
fe14fcc3 1462#if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
e5d73d77 1463 default:
4369b173 1464 croak("%s not implemented", PL_op_desc[optype]);
e5d73d77 1465#endif
c2ab57d4 1466 }
1467 return -1; /* should never happen */
1468}
1469
79072805 1470I32
8ac85365 1471do_ipcctl(I32 optype, SV **mark, SV **sp)
c2ab57d4 1472{
11343788 1473 dTHR;
79072805 1474 SV *astr;
c2ab57d4 1475 char *a;
a0d0e21e 1476 I32 id, n, cmd, infosize, getinfo;
1477 I32 ret = -1;
c2ab57d4 1478
463ee0b2 1479 id = SvIVx(*++mark);
1480 n = (optype == OP_SEMCTL) ? SvIVx(*++mark) : 0;
1481 cmd = SvIVx(*++mark);
79072805 1482 astr = *++mark;
c2ab57d4 1483 infosize = 0;
1484 getinfo = (cmd == IPC_STAT);
1485
1486 switch (optype)
1487 {
fe14fcc3 1488#ifdef HAS_MSG
79072805 1489 case OP_MSGCTL:
c2ab57d4 1490 if (cmd == IPC_STAT || cmd == IPC_SET)
1491 infosize = sizeof(struct msqid_ds);
1492 break;
e5d73d77 1493#endif
fe14fcc3 1494#ifdef HAS_SHM
79072805 1495 case OP_SHMCTL:
c2ab57d4 1496 if (cmd == IPC_STAT || cmd == IPC_SET)
1497 infosize = sizeof(struct shmid_ds);
1498 break;
e5d73d77 1499#endif
fe14fcc3 1500#ifdef HAS_SEM
79072805 1501 case OP_SEMCTL:
39398f3f 1502#ifdef Semctl
c2ab57d4 1503 if (cmd == IPC_STAT || cmd == IPC_SET)
1504 infosize = sizeof(struct semid_ds);
1505 else if (cmd == GETALL || cmd == SETALL)
1506 {
8e591e46 1507 struct semid_ds semds;
bd89102f 1508 union semun semun;
1509
84902520 1510 semun.buf = &semds;
c2ab57d4 1511 getinfo = (cmd == GETALL);
9b89d93d 1512 if (Semctl(id, 0, IPC_STAT, semun) == -1)
1513 return -1;
6e21c824 1514 infosize = semds.sem_nsems * sizeof(short);
1515 /* "short" is technically wrong but much more portable
1516 than guessing about u_?short(_t)? */
c2ab57d4 1517 }
39398f3f 1518#else
1519 croak("%s not implemented", PL_op_desc[optype]);
1520#endif
c2ab57d4 1521 break;
e5d73d77 1522#endif
fe14fcc3 1523#if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
e5d73d77 1524 default:
4369b173 1525 croak("%s not implemented", PL_op_desc[optype]);
e5d73d77 1526#endif
c2ab57d4 1527 }
1528
1529 if (infosize)
1530 {
a0d0e21e 1531 STRLEN len;
c2ab57d4 1532 if (getinfo)
1533 {
a0d0e21e 1534 SvPV_force(astr, len);
1535 a = SvGROW(astr, infosize+1);
c2ab57d4 1536 }
1537 else
1538 {
463ee0b2 1539 a = SvPV(astr, len);
1540 if (len != infosize)
9607fc9c 1541 croak("Bad arg length for %s, is %lu, should be %ld",
4ec43091 1542 PL_op_desc[optype],
1543 (unsigned long)len,
1544 (long)infosize);
c2ab57d4 1545 }
1546 }
1547 else
1548 {
c030ccd9 1549 IV i = SvIV(astr);
c2ab57d4 1550 a = (char *)i; /* ouch */
1551 }
748a9306 1552 SETERRNO(0,0);
c2ab57d4 1553 switch (optype)
1554 {
fe14fcc3 1555#ifdef HAS_MSG
79072805 1556 case OP_MSGCTL:
bee1dbe2 1557 ret = msgctl(id, cmd, (struct msqid_ds *)a);
c2ab57d4 1558 break;
e5d73d77 1559#endif
fe14fcc3 1560#ifdef HAS_SEM
bd89102f 1561 case OP_SEMCTL: {
39398f3f 1562#ifdef Semctl
bd89102f 1563 union semun unsemds;
1564
1565 unsemds.buf = (struct semid_ds *)a;
1566 ret = Semctl(id, n, cmd, unsemds);
39398f3f 1567#else
1568 croak("%s not implemented", PL_op_desc[optype]);
1569#endif
bd89102f 1570 }
c2ab57d4 1571 break;
e5d73d77 1572#endif
fe14fcc3 1573#ifdef HAS_SHM
79072805 1574 case OP_SHMCTL:
bee1dbe2 1575 ret = shmctl(id, cmd, (struct shmid_ds *)a);
c2ab57d4 1576 break;
e5d73d77 1577#endif
c2ab57d4 1578 }
1579 if (getinfo && ret >= 0) {
79072805 1580 SvCUR_set(astr, infosize);
1581 *SvEND(astr) = '\0';
a0d0e21e 1582 SvSETMAGIC(astr);
c2ab57d4 1583 }
1584 return ret;
1585}
1586
79072805 1587I32
8ac85365 1588do_msgsnd(SV **mark, SV **sp)
c2ab57d4 1589{
fe14fcc3 1590#ifdef HAS_MSG
11343788 1591 dTHR;
79072805 1592 SV *mstr;
c2ab57d4 1593 char *mbuf;
79072805 1594 I32 id, msize, flags;
463ee0b2 1595 STRLEN len;
c2ab57d4 1596
463ee0b2 1597 id = SvIVx(*++mark);
79072805 1598 mstr = *++mark;
463ee0b2 1599 flags = SvIVx(*++mark);
1600 mbuf = SvPV(mstr, len);
1601 if ((msize = len - sizeof(long)) < 0)
1602 croak("Arg too short for msgsnd");
748a9306 1603 SETERRNO(0,0);
bee1dbe2 1604 return msgsnd(id, (struct msgbuf *)mbuf, msize, flags);
e5d73d77 1605#else
463ee0b2 1606 croak("msgsnd not implemented");
e5d73d77 1607#endif
c2ab57d4 1608}
1609
79072805 1610I32
8ac85365 1611do_msgrcv(SV **mark, SV **sp)
c2ab57d4 1612{
fe14fcc3 1613#ifdef HAS_MSG
11343788 1614 dTHR;
79072805 1615 SV *mstr;
c2ab57d4 1616 char *mbuf;
1617 long mtype;
79072805 1618 I32 id, msize, flags, ret;
463ee0b2 1619 STRLEN len;
79072805 1620
463ee0b2 1621 id = SvIVx(*++mark);
79072805 1622 mstr = *++mark;
463ee0b2 1623 msize = SvIVx(*++mark);
1624 mtype = (long)SvIVx(*++mark);
1625 flags = SvIVx(*++mark);
a0d0e21e 1626 SvPV_force(mstr, len);
1627 mbuf = SvGROW(mstr, sizeof(long)+msize+1);
1628
748a9306 1629 SETERRNO(0,0);
bee1dbe2 1630 ret = msgrcv(id, (struct msgbuf *)mbuf, msize, mtype, flags);
c2ab57d4 1631 if (ret >= 0) {
79072805 1632 SvCUR_set(mstr, sizeof(long)+ret);
1633 *SvEND(mstr) = '\0';
c2ab57d4 1634 }
1635 return ret;
e5d73d77 1636#else
463ee0b2 1637 croak("msgrcv not implemented");
e5d73d77 1638#endif
c2ab57d4 1639}
1640
79072805 1641I32
8ac85365 1642do_semop(SV **mark, SV **sp)
c2ab57d4 1643{
fe14fcc3 1644#ifdef HAS_SEM
11343788 1645 dTHR;
79072805 1646 SV *opstr;
c2ab57d4 1647 char *opbuf;
463ee0b2 1648 I32 id;
1649 STRLEN opsize;
c2ab57d4 1650
463ee0b2 1651 id = SvIVx(*++mark);
79072805 1652 opstr = *++mark;
463ee0b2 1653 opbuf = SvPV(opstr, opsize);
c2ab57d4 1654 if (opsize < sizeof(struct sembuf)
1655 || (opsize % sizeof(struct sembuf)) != 0) {
748a9306 1656 SETERRNO(EINVAL,LIB$_INVARG);
c2ab57d4 1657 return -1;
1658 }
748a9306 1659 SETERRNO(0,0);
6e21c824 1660 return semop(id, (struct sembuf *)opbuf, opsize/sizeof(struct sembuf));
e5d73d77 1661#else
463ee0b2 1662 croak("semop not implemented");
e5d73d77 1663#endif
c2ab57d4 1664}
1665
79072805 1666I32
8ac85365 1667do_shmio(I32 optype, SV **mark, SV **sp)
c2ab57d4 1668{
fe14fcc3 1669#ifdef HAS_SHM
11343788 1670 dTHR;
79072805 1671 SV *mstr;
c2ab57d4 1672 char *mbuf, *shm;
79072805 1673 I32 id, mpos, msize;
463ee0b2 1674 STRLEN len;
c2ab57d4 1675 struct shmid_ds shmds;
c2ab57d4 1676
463ee0b2 1677 id = SvIVx(*++mark);
79072805 1678 mstr = *++mark;
463ee0b2 1679 mpos = SvIVx(*++mark);
1680 msize = SvIVx(*++mark);
748a9306 1681 SETERRNO(0,0);
c2ab57d4 1682 if (shmctl(id, IPC_STAT, &shmds) == -1)
1683 return -1;
1684 if (mpos < 0 || msize < 0 || mpos + msize > shmds.shm_segsz) {
748a9306 1685 SETERRNO(EFAULT,SS$_ACCVIO); /* can't do as caller requested */
c2ab57d4 1686 return -1;
1687 }
8ac85365 1688 shm = (char *)shmat(id, (char*)NULL, (optype == OP_SHMREAD) ? SHM_RDONLY : 0);
c2ab57d4 1689 if (shm == (char *)-1) /* I hate System V IPC, I really do */
1690 return -1;
79072805 1691 if (optype == OP_SHMREAD) {
a0d0e21e 1692 SvPV_force(mstr, len);
1693 mbuf = SvGROW(mstr, msize+1);
1694
bee1dbe2 1695 Copy(shm + mpos, mbuf, msize, char);
79072805 1696 SvCUR_set(mstr, msize);
1697 *SvEND(mstr) = '\0';
a0d0e21e 1698 SvSETMAGIC(mstr);
c2ab57d4 1699 }
1700 else {
79072805 1701 I32 n;
c2ab57d4 1702
a0d0e21e 1703 mbuf = SvPV(mstr, len);
463ee0b2 1704 if ((n = len) > msize)
c2ab57d4 1705 n = msize;
bee1dbe2 1706 Copy(mbuf, shm + mpos, n, char);
c2ab57d4 1707 if (n < msize)
bee1dbe2 1708 memzero(shm + mpos + n, msize - n);
c2ab57d4 1709 }
1710 return shmdt(shm);
e5d73d77 1711#else
463ee0b2 1712 croak("shm I/O not implemented");
e5d73d77 1713#endif
c2ab57d4 1714}
1715
fe14fcc3 1716#endif /* SYSV IPC */
4e35701f 1717