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