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