gutsupport for C++ exceptions
[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);
25da4f38 916 if (SvIsUV(sv)) /* XXXX 64-bit? */
917 PerlIO_printf(fp, "%lu", (unsigned long)SvUVX(sv));
918 else
919 PerlIO_printf(fp, "%ld", (long)SvIVX(sv));
760ac839 920 return !PerlIO_error(fp);
a0d0e21e 921 }
922 /* FALL THROUGH */
79072805 923 default:
463ee0b2 924 tmps = SvPV(sv, len);
79072805 925 break;
ff8e2863 926 }
760ac839 927 if (len && (PerlIO_write(fp,tmps,len) == 0 || PerlIO_error(fp)))
a687059c 928 return FALSE;
760ac839 929 return !PerlIO_error(fp);
a687059c 930}
931
79072805 932I32
8ac85365 933my_stat(ARGSproto)
a687059c 934{
4e35701f 935 djSP;
79072805 936 IO *io;
748a9306 937 GV* tmpgv;
79072805 938
533c011a 939 if (PL_op->op_flags & OPf_REF) {
924508f0 940 EXTEND(SP,1);
748a9306 941 tmpgv = cGVOP->op_gv;
942 do_fstat:
943 io = GvIO(tmpgv);
8990e307 944 if (io && IoIFP(io)) {
3280af22 945 PL_statgv = tmpgv;
946 sv_setpv(PL_statname,"");
947 PL_laststype = OP_STAT;
948 return (PL_laststatval = PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache));
a687059c 949 }
950 else {
3280af22 951 if (tmpgv == PL_defgv)
952 return PL_laststatval;
599cee73 953 if (ckWARN(WARN_UNOPENED))
954 warner(WARN_UNOPENED, "Stat on unopened file <%s>",
748a9306 955 GvENAME(tmpgv));
3280af22 956 PL_statgv = Nullgv;
957 sv_setpv(PL_statname,"");
958 return (PL_laststatval = -1);
a687059c 959 }
960 }
961 else {
748a9306 962 SV* sv = POPs;
4b74e3fb 963 char *s;
2d8e6c8d 964 STRLEN n_a;
79072805 965 PUTBACK;
748a9306 966 if (SvTYPE(sv) == SVt_PVGV) {
967 tmpgv = (GV*)sv;
968 goto do_fstat;
969 }
970 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
971 tmpgv = (GV*)SvRV(sv);
972 goto do_fstat;
973 }
974
2d8e6c8d 975 s = SvPV(sv, n_a);
3280af22 976 PL_statgv = Nullgv;
977 sv_setpv(PL_statname, s);
978 PL_laststype = OP_STAT;
979 PL_laststatval = PerlLIO_stat(s, &PL_statcache);
599cee73 980 if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(s, '\n'))
22c35a8c 981 warner(WARN_NEWLINE, PL_warn_nl, "stat");
3280af22 982 return PL_laststatval;
a687059c 983 }
984}
985
79072805 986I32
8ac85365 987my_lstat(ARGSproto)
c623bd54 988{
4e35701f 989 djSP;
79072805 990 SV *sv;
2d8e6c8d 991 STRLEN n_a;
533c011a 992 if (PL_op->op_flags & OPf_REF) {
924508f0 993 EXTEND(SP,1);
3280af22 994 if (cGVOP->op_gv == PL_defgv) {
995 if (PL_laststype != OP_LSTAT)
463ee0b2 996 croak("The stat preceding -l _ wasn't an lstat");
3280af22 997 return PL_laststatval;
fe14fcc3 998 }
463ee0b2 999 croak("You can't use -l on a filehandle");
fe14fcc3 1000 }
c623bd54 1001
3280af22 1002 PL_laststype = OP_LSTAT;
1003 PL_statgv = Nullgv;
79072805 1004 sv = POPs;
1005 PUTBACK;
2d8e6c8d 1006 sv_setpv(PL_statname,SvPV(sv, n_a));
fe14fcc3 1007#ifdef HAS_LSTAT
2d8e6c8d 1008 PL_laststatval = PerlLIO_lstat(SvPV(sv, n_a),&PL_statcache);
c623bd54 1009#else
2d8e6c8d 1010 PL_laststatval = PerlLIO_stat(SvPV(sv, n_a),&PL_statcache);
c623bd54 1011#endif
2d8e6c8d 1012 if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(SvPV(sv, n_a), '\n'))
22c35a8c 1013 warner(WARN_NEWLINE, PL_warn_nl, "lstat");
3280af22 1014 return PL_laststatval;
c623bd54 1015}
1016
a687059c 1017bool
8ac85365 1018do_aexec(SV *really, register SV **mark, register SV **sp)
a687059c 1019{
a687059c 1020 register char **a;
a687059c 1021 char *tmps;
2d8e6c8d 1022 STRLEN n_a;
a687059c 1023
79072805 1024 if (sp > mark) {
11343788 1025 dTHR;
3280af22 1026 New(401,PL_Argv, sp - mark + 1, char*);
1027 a = PL_Argv;
79072805 1028 while (++mark <= sp) {
1029 if (*mark)
2d8e6c8d 1030 *a++ = SvPVx(*mark, n_a);
a687059c 1031 else
1032 *a++ = "";
1033 }
1034 *a = Nullch;
3280af22 1035 if (*PL_Argv[0] != '/') /* will execvp use PATH? */
79072805 1036 TAINT_ENV(); /* testing IFS here is overkill, probably */
2d8e6c8d 1037 if (really && *(tmps = SvPV(really, n_a)))
3280af22 1038 PerlProc_execvp(tmps,PL_Argv);
a687059c 1039 else
3280af22 1040 PerlProc_execvp(PL_Argv[0],PL_Argv);
599cee73 1041 if (ckWARN(WARN_EXEC))
1042 warner(WARN_EXEC, "Can't exec \"%s\": %s",
1043 PL_Argv[0], Strerror(errno));
a687059c 1044 }
bee1dbe2 1045 do_execfree();
a687059c 1046 return FALSE;
1047}
1048
fe14fcc3 1049void
8ac85365 1050do_execfree(void)
ff8e2863 1051{
3280af22 1052 if (PL_Argv) {
1053 Safefree(PL_Argv);
1054 PL_Argv = Null(char **);
ff8e2863 1055 }
3280af22 1056 if (PL_Cmd) {
1057 Safefree(PL_Cmd);
1058 PL_Cmd = Nullch;
ff8e2863 1059 }
1060}
1061
39e571d4 1062#if !defined(OS2) && !defined(WIN32) && !defined(DJGPP)
760ac839 1063
a687059c 1064bool
8ac85365 1065do_exec(char *cmd)
a687059c 1066{
e446cec8 1067 return do_exec3(cmd,0,0);
1068}
1069
1070bool
1071do_exec3(char *cmd, int fd, int do_report)
1072{
a687059c 1073 register char **a;
1074 register char *s;
a687059c 1075 char flags[10];
1076
748a9306 1077 while (*cmd && isSPACE(*cmd))
1078 cmd++;
1079
a687059c 1080 /* save an extra exec if possible */
1081
bf38876a 1082#ifdef CSH
3280af22 1083 if (strnEQ(cmd,PL_cshname,PL_cshlen) && strnEQ(cmd+PL_cshlen," -c",3)) {
a687059c 1084 strcpy(flags,"-c");
3280af22 1085 s = cmd+PL_cshlen+3;
a687059c 1086 if (*s == 'f') {
1087 s++;
1088 strcat(flags,"f");
1089 }
1090 if (*s == ' ')
1091 s++;
1092 if (*s++ == '\'') {
1093 char *ncmd = s;
1094
1095 while (*s)
1096 s++;
1097 if (s[-1] == '\n')
1098 *--s = '\0';
1099 if (s[-1] == '\'') {
1100 *--s = '\0';
3280af22 1101 PerlProc_execl(PL_cshname,"csh", flags,ncmd,(char*)0);
a687059c 1102 *s = '\'';
1103 return FALSE;
1104 }
1105 }
1106 }
bf38876a 1107#endif /* CSH */
a687059c 1108
1109 /* see if there are shell metacharacters in it */
1110
748a9306 1111 if (*cmd == '.' && isSPACE(cmd[1]))
1112 goto doshell;
1113
1114 if (strnEQ(cmd,"exec",4) && isSPACE(cmd[4]))
1115 goto doshell;
1116
99b89507 1117 for (s = cmd; *s && isALPHA(*s); s++) ; /* catch VAR=val gizmo */
63f2c1e1 1118 if (*s == '=')
1119 goto doshell;
748a9306 1120
a687059c 1121 for (s = cmd; *s; s++) {
93a17b20 1122 if (*s != ' ' && !isALPHA(*s) && strchr("$&*(){}[]'\";\\|?<>~`\n",*s)) {
a687059c 1123 if (*s == '\n' && !s[1]) {
1124 *s = '\0';
1125 break;
1126 }
1127 doshell:
3280af22 1128 PerlProc_execl(PL_sh_path, "sh", "-c", cmd, (char*)0);
a687059c 1129 return FALSE;
1130 }
1131 }
748a9306 1132
3280af22 1133 New(402,PL_Argv, (s - cmd) / 2 + 2, char*);
1134 PL_Cmd = savepvn(cmd, s-cmd);
1135 a = PL_Argv;
1136 for (s = PL_Cmd; *s;) {
99b89507 1137 while (*s && isSPACE(*s)) s++;
a687059c 1138 if (*s)
1139 *(a++) = s;
99b89507 1140 while (*s && !isSPACE(*s)) s++;
a687059c 1141 if (*s)
1142 *s++ = '\0';
1143 }
1144 *a = Nullch;
3280af22 1145 if (PL_Argv[0]) {
1146 PerlProc_execvp(PL_Argv[0],PL_Argv);
b1248f16 1147 if (errno == ENOEXEC) { /* for system V NIH syndrome */
ff8e2863 1148 do_execfree();
a687059c 1149 goto doshell;
b1248f16 1150 }
d008e5eb 1151 {
1152 dTHR;
e446cec8 1153 int e = errno;
1154
d008e5eb 1155 if (ckWARN(WARN_EXEC))
1156 warner(WARN_EXEC, "Can't exec \"%s\": %s",
1157 PL_Argv[0], Strerror(errno));
e446cec8 1158 if (do_report) {
1159 PerlLIO_write(fd, (void*)&e, sizeof(int));
1160 PerlLIO_close(fd);
1161 }
d008e5eb 1162 }
a687059c 1163 }
ff8e2863 1164 do_execfree();
a687059c 1165 return FALSE;
1166}
1167
6890e559 1168#endif /* OS2 || WIN32 */
760ac839 1169
79072805 1170I32
8ac85365 1171apply(I32 type, register SV **mark, register SV **sp)
a687059c 1172{
11343788 1173 dTHR;
79072805 1174 register I32 val;
1175 register I32 val2;
1176 register I32 tot = 0;
20408e3c 1177 char *what;
a687059c 1178 char *s;
79072805 1179 SV **oldmark = mark;
2d8e6c8d 1180 STRLEN n_a;
a687059c 1181
20408e3c 1182#define APPLY_TAINT_PROPER() \
3280af22 1183 STMT_START { \
17406bd6 1184 if (PL_tainted) { TAINT_PROPER(what); } \
873ef191 1185 } STMT_END
20408e3c 1186
1187 /* This is a first heuristic; it doesn't catch tainting magic. */
3280af22 1188 if (PL_tainting) {
463ee0b2 1189 while (++mark <= sp) {
bbce6d69 1190 if (SvTAINTED(*mark)) {
1191 TAINT;
1192 break;
1193 }
463ee0b2 1194 }
1195 mark = oldmark;
1196 }
a687059c 1197 switch (type) {
79072805 1198 case OP_CHMOD:
20408e3c 1199 what = "chmod";
1200 APPLY_TAINT_PROPER();
79072805 1201 if (++mark <= sp) {
463ee0b2 1202 val = SvIVx(*mark);
20408e3c 1203 APPLY_TAINT_PROPER();
1204 tot = sp - mark;
79072805 1205 while (++mark <= sp) {
2d8e6c8d 1206 char *name = SvPVx(*mark, n_a);
20408e3c 1207 APPLY_TAINT_PROPER();
1208 if (PerlLIO_chmod(name, val))
a687059c 1209 tot--;
1210 }
1211 }
1212 break;
fe14fcc3 1213#ifdef HAS_CHOWN
79072805 1214 case OP_CHOWN:
20408e3c 1215 what = "chown";
1216 APPLY_TAINT_PROPER();
79072805 1217 if (sp - mark > 2) {
463ee0b2 1218 val = SvIVx(*++mark);
1219 val2 = SvIVx(*++mark);
20408e3c 1220 APPLY_TAINT_PROPER();
a0d0e21e 1221 tot = sp - mark;
79072805 1222 while (++mark <= sp) {
2d8e6c8d 1223 char *name = SvPVx(*mark, n_a);
20408e3c 1224 APPLY_TAINT_PROPER();
36660982 1225 if (PerlLIO_chown(name, val, val2))
a687059c 1226 tot--;
1227 }
1228 }
1229 break;
b1248f16 1230#endif
dd64f1c3 1231/*
1232XXX Should we make lchown() directly available from perl?
1233For now, we'll let Configure test for HAS_LCHOWN, but do
1234nothing in the core.
1235 --AD 5/1998
1236*/
fe14fcc3 1237#ifdef HAS_KILL
79072805 1238 case OP_KILL:
20408e3c 1239 what = "kill";
1240 APPLY_TAINT_PROPER();
55497cff 1241 if (mark == sp)
1242 break;
2d8e6c8d 1243 s = SvPVx(*++mark, n_a);
79072805 1244 if (isUPPER(*s)) {
1245 if (*s == 'S' && s[1] == 'I' && s[2] == 'G')
1246 s += 3;
1247 if (!(val = whichsig(s)))
463ee0b2 1248 croak("Unrecognized signal name \"%s\"",s);
79072805 1249 }
1250 else
463ee0b2 1251 val = SvIVx(*mark);
20408e3c 1252 APPLY_TAINT_PROPER();
1253 tot = sp - mark;
3595fcef 1254#ifdef VMS
1255 /* kill() doesn't do process groups (job trees?) under VMS */
1256 if (val < 0) val = -val;
1257 if (val == SIGKILL) {
1258# include <starlet.h>
1259 /* Use native sys$delprc() to insure that target process is
1260 * deleted; supervisor-mode images don't pay attention to
1261 * CRTL's emulation of Unix-style signals and kill()
1262 */
1263 while (++mark <= sp) {
1264 I32 proc = SvIVx(*mark);
1265 register unsigned long int __vmssts;
20408e3c 1266 APPLY_TAINT_PROPER();
3595fcef 1267 if (!((__vmssts = sys$delprc(&proc,0)) & 1)) {
1268 tot--;
1269 switch (__vmssts) {
1270 case SS$_NONEXPR:
1271 case SS$_NOSUCHNODE:
1272 SETERRNO(ESRCH,__vmssts);
1273 break;
1274 case SS$_NOPRIV:
1275 SETERRNO(EPERM,__vmssts);
1276 break;
1277 default:
1278 SETERRNO(EVMSERR,__vmssts);
1279 }
1280 }
1281 }
1282 break;
1283 }
1284#endif
79072805 1285 if (val < 0) {
1286 val = -val;
1287 while (++mark <= sp) {
463ee0b2 1288 I32 proc = SvIVx(*mark);
20408e3c 1289 APPLY_TAINT_PROPER();
fe14fcc3 1290#ifdef HAS_KILLPG
3028581b 1291 if (PerlProc_killpg(proc,val)) /* BSD */
a687059c 1292#else
3028581b 1293 if (PerlProc_kill(-proc,val)) /* SYSV */
a687059c 1294#endif
79072805 1295 tot--;
a687059c 1296 }
79072805 1297 }
1298 else {
1299 while (++mark <= sp) {
20408e3c 1300 I32 proc = SvIVx(*mark);
1301 APPLY_TAINT_PROPER();
1302 if (PerlProc_kill(proc, val))
79072805 1303 tot--;
a687059c 1304 }
1305 }
1306 break;
b1248f16 1307#endif
79072805 1308 case OP_UNLINK:
20408e3c 1309 what = "unlink";
1310 APPLY_TAINT_PROPER();
79072805 1311 tot = sp - mark;
1312 while (++mark <= sp) {
2d8e6c8d 1313 s = SvPVx(*mark, n_a);
20408e3c 1314 APPLY_TAINT_PROPER();
3280af22 1315 if (PL_euid || PL_unsafe) {
a687059c 1316 if (UNLINK(s))
1317 tot--;
1318 }
1319 else { /* don't let root wipe out directories without -U */
fe14fcc3 1320#ifdef HAS_LSTAT
3280af22 1321 if (PerlLIO_lstat(s,&PL_statbuf) < 0 || S_ISDIR(PL_statbuf.st_mode))
a687059c 1322#else
b28d0864 1323 if (PerlLIO_stat(s,&PL_statbuf) < 0 || S_ISDIR(PL_statbuf.st_mode))
a687059c 1324#endif
a687059c 1325 tot--;
1326 else {
1327 if (UNLINK(s))
1328 tot--;
1329 }
1330 }
1331 }
1332 break;
a0d0e21e 1333#ifdef HAS_UTIME
79072805 1334 case OP_UTIME:
20408e3c 1335 what = "utime";
1336 APPLY_TAINT_PROPER();
79072805 1337 if (sp - mark > 2) {
748a9306 1338#if defined(I_UTIME) || defined(VMS)
663a0e37 1339 struct utimbuf utbuf;
1340#else
a687059c 1341 struct {
dd2821f6 1342 Time_t actime;
1343 Time_t modtime;
a687059c 1344 } utbuf;
663a0e37 1345#endif
a687059c 1346
afd9f252 1347 Zero(&utbuf, sizeof utbuf, char);
517844ec 1348#ifdef BIG_TIME
dd2821f6 1349 utbuf.actime = (Time_t)SvNVx(*++mark); /* time accessed */
1350 utbuf.modtime = (Time_t)SvNVx(*++mark); /* time modified */
517844ec 1351#else
dd2821f6 1352 utbuf.actime = (Time_t)SvIVx(*++mark); /* time accessed */
1353 utbuf.modtime = (Time_t)SvIVx(*++mark); /* time modified */
517844ec 1354#endif
20408e3c 1355 APPLY_TAINT_PROPER();
79072805 1356 tot = sp - mark;
1357 while (++mark <= sp) {
2d8e6c8d 1358 char *name = SvPVx(*mark, n_a);
20408e3c 1359 APPLY_TAINT_PROPER();
1360 if (PerlLIO_utime(name, &utbuf))
a687059c 1361 tot--;
1362 }
a687059c 1363 }
1364 else
79072805 1365 tot = 0;
a687059c 1366 break;
a0d0e21e 1367#endif
a687059c 1368 }
1369 return tot;
20408e3c 1370
20408e3c 1371#undef APPLY_TAINT_PROPER
a687059c 1372}
1373
1374/* Do the permissions allow some operation? Assumes statcache already set. */
a0d0e21e 1375#ifndef VMS /* VMS' cando is in vms.c */
79072805 1376I32
8ac85365 1377cando(I32 bit, I32 effective, register struct stat *statbufp)
a687059c 1378{
bee1dbe2 1379#ifdef DOSISH
fe14fcc3 1380 /* [Comments and code from Len Reed]
1381 * MS-DOS "user" is similar to UNIX's "superuser," but can't write
1382 * to write-protected files. The execute permission bit is set
1383 * by the Miscrosoft C library stat() function for the following:
1384 * .exe files
1385 * .com files
1386 * .bat files
1387 * directories
1388 * All files and directories are readable.
1389 * Directories and special files, e.g. "CON", cannot be
1390 * write-protected.
1391 * [Comment by Tom Dinger -- a directory can have the write-protect
1392 * bit set in the file system, but DOS permits changes to
1393 * the directory anyway. In addition, all bets are off
1394 * here for networked software, such as Novell and
1395 * Sun's PC-NFS.]
1396 */
1397
bee1dbe2 1398 /* Atari stat() does pretty much the same thing. we set x_bit_set_in_stat
1399 * too so it will actually look into the files for magic numbers
1400 */
fe14fcc3 1401 return (bit & statbufp->st_mode) ? TRUE : FALSE;
1402
55497cff 1403#else /* ! DOSISH */
3280af22 1404 if ((effective ? PL_euid : PL_uid) == 0) { /* root is special */
c623bd54 1405 if (bit == S_IXUSR) {
1406 if (statbufp->st_mode & 0111 || S_ISDIR(statbufp->st_mode))
a687059c 1407 return TRUE;
1408 }
1409 else
1410 return TRUE; /* root reads and writes anything */
1411 return FALSE;
1412 }
3280af22 1413 if (statbufp->st_uid == (effective ? PL_euid : PL_uid) ) {
a687059c 1414 if (statbufp->st_mode & bit)
1415 return TRUE; /* ok as "user" */
1416 }
79072805 1417 else if (ingroup((I32)statbufp->st_gid,effective)) {
a687059c 1418 if (statbufp->st_mode & bit >> 3)
1419 return TRUE; /* ok as "group" */
1420 }
1421 else if (statbufp->st_mode & bit >> 6)
1422 return TRUE; /* ok as "other" */
1423 return FALSE;
55497cff 1424#endif /* ! DOSISH */
a687059c 1425}
a0d0e21e 1426#endif /* ! VMS */
a687059c 1427
79072805 1428I32
8ac85365 1429ingroup(I32 testgid, I32 effective)
a687059c 1430{
3280af22 1431 if (testgid == (effective ? PL_egid : PL_gid))
a687059c 1432 return TRUE;
fe14fcc3 1433#ifdef HAS_GETGROUPS
a687059c 1434#ifndef NGROUPS
1435#define NGROUPS 32
1436#endif
1437 {
a0d0e21e 1438 Groups_t gary[NGROUPS];
79072805 1439 I32 anum;
a687059c 1440
1441 anum = getgroups(NGROUPS,gary);
1442 while (--anum >= 0)
1443 if (gary[anum] == testgid)
1444 return TRUE;
1445 }
1446#endif
1447 return FALSE;
1448}
c2ab57d4 1449
fe14fcc3 1450#if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
c2ab57d4 1451
79072805 1452I32
8ac85365 1453do_ipcget(I32 optype, SV **mark, SV **sp)
c2ab57d4 1454{
11343788 1455 dTHR;
c2ab57d4 1456 key_t key;
79072805 1457 I32 n, flags;
c2ab57d4 1458
463ee0b2 1459 key = (key_t)SvNVx(*++mark);
1460 n = (optype == OP_MSGGET) ? 0 : SvIVx(*++mark);
1461 flags = SvIVx(*++mark);
748a9306 1462 SETERRNO(0,0);
c2ab57d4 1463 switch (optype)
1464 {
fe14fcc3 1465#ifdef HAS_MSG
79072805 1466 case OP_MSGGET:
c2ab57d4 1467 return msgget(key, flags);
e5d73d77 1468#endif
fe14fcc3 1469#ifdef HAS_SEM
79072805 1470 case OP_SEMGET:
c2ab57d4 1471 return semget(key, n, flags);
e5d73d77 1472#endif
fe14fcc3 1473#ifdef HAS_SHM
79072805 1474 case OP_SHMGET:
c2ab57d4 1475 return shmget(key, n, flags);
e5d73d77 1476#endif
fe14fcc3 1477#if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
e5d73d77 1478 default:
4369b173 1479 croak("%s not implemented", PL_op_desc[optype]);
e5d73d77 1480#endif
c2ab57d4 1481 }
1482 return -1; /* should never happen */
1483}
1484
79072805 1485I32
8ac85365 1486do_ipcctl(I32 optype, SV **mark, SV **sp)
c2ab57d4 1487{
11343788 1488 dTHR;
79072805 1489 SV *astr;
c2ab57d4 1490 char *a;
a0d0e21e 1491 I32 id, n, cmd, infosize, getinfo;
1492 I32 ret = -1;
c2ab57d4 1493
463ee0b2 1494 id = SvIVx(*++mark);
1495 n = (optype == OP_SEMCTL) ? SvIVx(*++mark) : 0;
1496 cmd = SvIVx(*++mark);
79072805 1497 astr = *++mark;
c2ab57d4 1498 infosize = 0;
1499 getinfo = (cmd == IPC_STAT);
1500
1501 switch (optype)
1502 {
fe14fcc3 1503#ifdef HAS_MSG
79072805 1504 case OP_MSGCTL:
c2ab57d4 1505 if (cmd == IPC_STAT || cmd == IPC_SET)
1506 infosize = sizeof(struct msqid_ds);
1507 break;
e5d73d77 1508#endif
fe14fcc3 1509#ifdef HAS_SHM
79072805 1510 case OP_SHMCTL:
c2ab57d4 1511 if (cmd == IPC_STAT || cmd == IPC_SET)
1512 infosize = sizeof(struct shmid_ds);
1513 break;
e5d73d77 1514#endif
fe14fcc3 1515#ifdef HAS_SEM
79072805 1516 case OP_SEMCTL:
39398f3f 1517#ifdef Semctl
c2ab57d4 1518 if (cmd == IPC_STAT || cmd == IPC_SET)
1519 infosize = sizeof(struct semid_ds);
1520 else if (cmd == GETALL || cmd == SETALL)
1521 {
8e591e46 1522 struct semid_ds semds;
bd89102f 1523 union semun semun;
1524
84902520 1525 semun.buf = &semds;
c2ab57d4 1526 getinfo = (cmd == GETALL);
9b89d93d 1527 if (Semctl(id, 0, IPC_STAT, semun) == -1)
1528 return -1;
6e21c824 1529 infosize = semds.sem_nsems * sizeof(short);
1530 /* "short" is technically wrong but much more portable
1531 than guessing about u_?short(_t)? */
c2ab57d4 1532 }
39398f3f 1533#else
1534 croak("%s not implemented", PL_op_desc[optype]);
1535#endif
c2ab57d4 1536 break;
e5d73d77 1537#endif
fe14fcc3 1538#if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
e5d73d77 1539 default:
4369b173 1540 croak("%s not implemented", PL_op_desc[optype]);
e5d73d77 1541#endif
c2ab57d4 1542 }
1543
1544 if (infosize)
1545 {
a0d0e21e 1546 STRLEN len;
c2ab57d4 1547 if (getinfo)
1548 {
a0d0e21e 1549 SvPV_force(astr, len);
1550 a = SvGROW(astr, infosize+1);
c2ab57d4 1551 }
1552 else
1553 {
463ee0b2 1554 a = SvPV(astr, len);
1555 if (len != infosize)
9607fc9c 1556 croak("Bad arg length for %s, is %lu, should be %ld",
4ec43091 1557 PL_op_desc[optype],
1558 (unsigned long)len,
1559 (long)infosize);
c2ab57d4 1560 }
1561 }
1562 else
1563 {
c030ccd9 1564 IV i = SvIV(astr);
c2ab57d4 1565 a = (char *)i; /* ouch */
1566 }
748a9306 1567 SETERRNO(0,0);
c2ab57d4 1568 switch (optype)
1569 {
fe14fcc3 1570#ifdef HAS_MSG
79072805 1571 case OP_MSGCTL:
bee1dbe2 1572 ret = msgctl(id, cmd, (struct msqid_ds *)a);
c2ab57d4 1573 break;
e5d73d77 1574#endif
fe14fcc3 1575#ifdef HAS_SEM
bd89102f 1576 case OP_SEMCTL: {
39398f3f 1577#ifdef Semctl
bd89102f 1578 union semun unsemds;
1579
1580 unsemds.buf = (struct semid_ds *)a;
1581 ret = Semctl(id, n, cmd, unsemds);
39398f3f 1582#else
1583 croak("%s not implemented", PL_op_desc[optype]);
1584#endif
bd89102f 1585 }
c2ab57d4 1586 break;
e5d73d77 1587#endif
fe14fcc3 1588#ifdef HAS_SHM
79072805 1589 case OP_SHMCTL:
bee1dbe2 1590 ret = shmctl(id, cmd, (struct shmid_ds *)a);
c2ab57d4 1591 break;
e5d73d77 1592#endif
c2ab57d4 1593 }
1594 if (getinfo && ret >= 0) {
79072805 1595 SvCUR_set(astr, infosize);
1596 *SvEND(astr) = '\0';
a0d0e21e 1597 SvSETMAGIC(astr);
c2ab57d4 1598 }
1599 return ret;
1600}
1601
79072805 1602I32
8ac85365 1603do_msgsnd(SV **mark, SV **sp)
c2ab57d4 1604{
fe14fcc3 1605#ifdef HAS_MSG
11343788 1606 dTHR;
79072805 1607 SV *mstr;
c2ab57d4 1608 char *mbuf;
79072805 1609 I32 id, msize, flags;
463ee0b2 1610 STRLEN len;
c2ab57d4 1611
463ee0b2 1612 id = SvIVx(*++mark);
79072805 1613 mstr = *++mark;
463ee0b2 1614 flags = SvIVx(*++mark);
1615 mbuf = SvPV(mstr, len);
1616 if ((msize = len - sizeof(long)) < 0)
1617 croak("Arg too short for msgsnd");
748a9306 1618 SETERRNO(0,0);
bee1dbe2 1619 return msgsnd(id, (struct msgbuf *)mbuf, msize, flags);
e5d73d77 1620#else
463ee0b2 1621 croak("msgsnd not implemented");
e5d73d77 1622#endif
c2ab57d4 1623}
1624
79072805 1625I32
8ac85365 1626do_msgrcv(SV **mark, SV **sp)
c2ab57d4 1627{
fe14fcc3 1628#ifdef HAS_MSG
11343788 1629 dTHR;
79072805 1630 SV *mstr;
c2ab57d4 1631 char *mbuf;
1632 long mtype;
79072805 1633 I32 id, msize, flags, ret;
463ee0b2 1634 STRLEN len;
79072805 1635
463ee0b2 1636 id = SvIVx(*++mark);
79072805 1637 mstr = *++mark;
463ee0b2 1638 msize = SvIVx(*++mark);
1639 mtype = (long)SvIVx(*++mark);
1640 flags = SvIVx(*++mark);
a0d0e21e 1641 SvPV_force(mstr, len);
1642 mbuf = SvGROW(mstr, sizeof(long)+msize+1);
1643
748a9306 1644 SETERRNO(0,0);
bee1dbe2 1645 ret = msgrcv(id, (struct msgbuf *)mbuf, msize, mtype, flags);
c2ab57d4 1646 if (ret >= 0) {
79072805 1647 SvCUR_set(mstr, sizeof(long)+ret);
1648 *SvEND(mstr) = '\0';
c2ab57d4 1649 }
1650 return ret;
e5d73d77 1651#else
463ee0b2 1652 croak("msgrcv not implemented");
e5d73d77 1653#endif
c2ab57d4 1654}
1655
79072805 1656I32
8ac85365 1657do_semop(SV **mark, SV **sp)
c2ab57d4 1658{
fe14fcc3 1659#ifdef HAS_SEM
11343788 1660 dTHR;
79072805 1661 SV *opstr;
c2ab57d4 1662 char *opbuf;
463ee0b2 1663 I32 id;
1664 STRLEN opsize;
c2ab57d4 1665
463ee0b2 1666 id = SvIVx(*++mark);
79072805 1667 opstr = *++mark;
463ee0b2 1668 opbuf = SvPV(opstr, opsize);
c2ab57d4 1669 if (opsize < sizeof(struct sembuf)
1670 || (opsize % sizeof(struct sembuf)) != 0) {
748a9306 1671 SETERRNO(EINVAL,LIB$_INVARG);
c2ab57d4 1672 return -1;
1673 }
748a9306 1674 SETERRNO(0,0);
6e21c824 1675 return semop(id, (struct sembuf *)opbuf, opsize/sizeof(struct sembuf));
e5d73d77 1676#else
463ee0b2 1677 croak("semop not implemented");
e5d73d77 1678#endif
c2ab57d4 1679}
1680
79072805 1681I32
8ac85365 1682do_shmio(I32 optype, SV **mark, SV **sp)
c2ab57d4 1683{
fe14fcc3 1684#ifdef HAS_SHM
11343788 1685 dTHR;
79072805 1686 SV *mstr;
c2ab57d4 1687 char *mbuf, *shm;
79072805 1688 I32 id, mpos, msize;
463ee0b2 1689 STRLEN len;
c2ab57d4 1690 struct shmid_ds shmds;
c2ab57d4 1691
463ee0b2 1692 id = SvIVx(*++mark);
79072805 1693 mstr = *++mark;
463ee0b2 1694 mpos = SvIVx(*++mark);
1695 msize = SvIVx(*++mark);
748a9306 1696 SETERRNO(0,0);
c2ab57d4 1697 if (shmctl(id, IPC_STAT, &shmds) == -1)
1698 return -1;
1699 if (mpos < 0 || msize < 0 || mpos + msize > shmds.shm_segsz) {
748a9306 1700 SETERRNO(EFAULT,SS$_ACCVIO); /* can't do as caller requested */
c2ab57d4 1701 return -1;
1702 }
8ac85365 1703 shm = (char *)shmat(id, (char*)NULL, (optype == OP_SHMREAD) ? SHM_RDONLY : 0);
c2ab57d4 1704 if (shm == (char *)-1) /* I hate System V IPC, I really do */
1705 return -1;
79072805 1706 if (optype == OP_SHMREAD) {
a0d0e21e 1707 SvPV_force(mstr, len);
1708 mbuf = SvGROW(mstr, msize+1);
1709
bee1dbe2 1710 Copy(shm + mpos, mbuf, msize, char);
79072805 1711 SvCUR_set(mstr, msize);
1712 *SvEND(mstr) = '\0';
a0d0e21e 1713 SvSETMAGIC(mstr);
c2ab57d4 1714 }
1715 else {
79072805 1716 I32 n;
c2ab57d4 1717
a0d0e21e 1718 mbuf = SvPV(mstr, len);
463ee0b2 1719 if ((n = len) > msize)
c2ab57d4 1720 n = msize;
bee1dbe2 1721 Copy(mbuf, shm + mpos, n, char);
c2ab57d4 1722 if (n < msize)
bee1dbe2 1723 memzero(shm + mpos + n, msize - n);
c2ab57d4 1724 }
1725 return shmdt(shm);
e5d73d77 1726#else
463ee0b2 1727 croak("shm I/O not implemented");
e5d73d77 1728#endif
c2ab57d4 1729}
1730
fe14fcc3 1731#endif /* SYSV IPC */
4e35701f 1732