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