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