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