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