3 * Copyright (c) 1991-1999, Larry Wall
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.
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."
18 #define PERL_IN_DOIO_C
21 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
30 # ifndef HAS_SHMAT_PROTOTYPE
31 extern Shmat_t shmat (int, char *, int);
37 # if defined(_MSC_VER) || defined(__MINGW32__)
38 # include <sys/utime.h>
51 # define OPEN_EXCL O_EXCL
56 #if !defined(NSIG) || defined(M_UNIX) || defined(M_XENIX)
60 /* XXX If this causes problems, set i_unistd=undef in the hint file. */
65 #if defined(HAS_SOCKET) && !defined(VMS) /* VMS handles sockets via vmsish.h */
66 # include <sys/socket.h>
67 # if defined(USE_SOCKS) && defined(I_SOCKS)
75 # include <net/errno.h>
80 /* Put this after #includes because <unistd.h> defines _XOPEN_*. */
82 # if _XOPEN_VERSION >= 5 || defined(_XOPEN_SOURCE_EXTENDED) || defined(__GLIBC__)
83 # define Sock_size_t Size_t
85 # define Sock_size_t int
90 Perl_do_open(pTHX_ GV *gv, register char *name, I32 len, int as_raw,
91 int rawmode, int rawperm, PerlIO *supplied_fp)
93 return do_open9(gv, name, len, as_raw, rawmode, rawperm,
94 supplied_fp, Nullsv, 0);
98 Perl_do_open9(pTHX_ GV *gv, register char *name, I32 len, int as_raw,
99 int rawmode, int rawperm, PerlIO *supplied_fp, SV *svs,
102 register IO *io = GvIOn(gv);
103 PerlIO *saveifp = Nullfp;
104 PerlIO *saveofp = Nullfp;
110 bool was_fdopen = FALSE;
112 PL_forkprocess = 1; /* assume true if no fork */
115 fd = PerlIO_fileno(IoIFP(io));
116 if (IoTYPE(io) == '-')
118 else if (fd <= PL_maxsysfd) {
121 savetype = IoTYPE(io);
124 else if (IoTYPE(io) == '|')
125 result = PerlProc_pclose(IoIFP(io));
126 else if (IoIFP(io) != IoOFP(io)) {
128 result = PerlIO_close(IoOFP(io));
129 PerlIO_close(IoIFP(io)); /* clear stdio, fd already closed */
132 result = PerlIO_close(IoIFP(io));
135 result = PerlIO_close(IoIFP(io));
136 if (result == EOF && fd > PL_maxsysfd)
137 PerlIO_printf(Perl_error_log,
138 "Warning: unable to close filehandle %s properly.\n",
140 IoOFP(io) = IoIFP(io) = Nullfp;
144 #if defined(USE_64_BIT_RAWIO) && defined(O_LARGEFILE)
145 rawmode |= O_LARGEFILE;
149 #define O_ACCMODE 3 /* Assume traditional implementation */
152 switch (result = rawmode & O_ACCMODE) {
165 writing = (result > 0);
166 fd = PerlLIO_open3(name, rawmode, rawperm);
172 if (result == O_RDONLY)
175 else if (rawmode & O_APPEND)
176 fpmode = (result == O_WRONLY) ? "a" : "a+";
179 fpmode = (result == O_WRONLY) ? "w" : "r+";
180 fp = PerlIO_fdopen(fd, fpmode);
191 char mode[3]; /* stdio file mode ("r\0" or "r+\0") */
196 name = SvPV(svs, tlen) ;
201 myname = savepvn(name, len);
205 while (tlen && isSPACE(type[tlen-1]))
208 mode[0] = mode[1] = mode[2] = '\0';
210 if (*type == '+' && tlen > 1 && type[tlen-1] != '|') { /* scary */
217 if (num_svs && (tlen != 2 || type[1] != '-')) {
219 Perl_croak(aTHX_ "Unknown open() mode '%.*s'", otlen, otype);
222 for (type++; isSPACE(*type); type++) ;
225 if (*name == '\0') { /* command is missing 19990114 */
227 if (ckWARN(WARN_PIPE))
228 Perl_warner(aTHX_ WARN_PIPE, "Missing command in piped open");
232 if (strNE(name,"-") || num_svs)
234 TAINT_PROPER("piped open");
235 if (name[strlen(name)-1] == '|') {
237 name[strlen(name)-1] = '\0' ;
238 if (ckWARN(WARN_PIPE))
239 Perl_warner(aTHX_ WARN_PIPE, "Can't do bidirectional pipe");
241 fp = PerlProc_popen(name,"w");
244 else if (*type == '>') {
245 TAINT_PROPER("open");
248 mode[0] = IoTYPE(io) = 'a';
256 if (num_svs && tlen != 1)
267 if (!*name && supplied_fp)
271 for (; isSPACE(*name); name++) ;
276 gv = gv_fetchpv(name,FALSE,SVt_PVIO);
280 SETERRNO(EINVAL,SS$_IVCHAN);
285 PerlIO *fp = IoIFP(thatio);
286 /* Flush stdio buffer before dup. --mjd
287 * Unfortunately SEEK_CURing 0 seems to
288 * be optimized away on most platforms;
289 * only Solaris and Linux seem to flush
291 PerlIO_seek(fp, 0, SEEK_CUR);
292 /* On the other hand, do all platforms
293 * take gracefully to flushing a read-only
294 * filehandle? Perhaps we should do
295 * fsetpos(src)+fgetpos(dst)? --nik */
297 fd = PerlIO_fileno(fp);
298 if (IoTYPE(thatio) == 's')
305 fd = PerlLIO_dup(fd);
308 if (!(fp = PerlIO_fdopen(fd,mode))) {
316 for (; isSPACE(*type); type++) ;
317 if (strEQ(type,"-")) {
318 fp = PerlIO_stdout();
322 fp = PerlIO_open((num_svs ? name : type), mode);
326 else if (*type == '<') {
327 if (num_svs && tlen != 1)
330 for (type++; isSPACE(*type); type++) ;
336 if (strEQ(type,"-")) {
341 fp = PerlIO_open((num_svs ? name : type), mode);
343 else if (tlen > 1 && type[tlen-1] == '|') {
345 if (tlen != 2 || type[0] != '-')
350 while (tlen && isSPACE(type[tlen-1]))
353 for (; isSPACE(*type); type++) ;
356 if (*name == '\0') { /* command is missing 19990114 */
358 if (ckWARN(WARN_PIPE))
359 Perl_warner(aTHX_ WARN_PIPE, "Missing command in piped open");
363 if (strNE(name,"-") || num_svs)
365 TAINT_PROPER("piped open");
366 fp = PerlProc_popen(name,"r");
375 for (; isSPACE(*name); name++) ;
376 if (strEQ(name,"-")) {
381 fp = PerlIO_open(name,"r");
386 if (ckWARN(WARN_NEWLINE) && IoTYPE(io) == '<' && strchr(name, '\n'))
387 Perl_warner(aTHX_ WARN_NEWLINE, PL_warn_nl, "open");
391 IoTYPE(io) != '|' && IoTYPE(io) != '-') {
393 if (PerlLIO_fstat(PerlIO_fileno(fp),&PL_statbuf) < 0) {
394 (void)PerlIO_close(fp);
397 if (S_ISSOCK(PL_statbuf.st_mode))
398 IoTYPE(io) = 's'; /* in case a socket was passed in to us */
402 !(PL_statbuf.st_mode & S_IFMT)
408 Sock_size_t buflen = sizeof tmpbuf;
409 if (PerlSock_getsockname(PerlIO_fileno(fp), (struct sockaddr *)tmpbuf,
411 || errno != ENOTSOCK)
412 IoTYPE(io) = 's'; /* some OS's return 0 on fstat()ed socket */
413 /* but some return 0 for streams too, sigh */
417 if (saveifp) { /* must use old fp? */
418 fd = PerlIO_fileno(saveifp);
420 PerlIO_flush(saveofp); /* emulate PerlIO_close() */
421 if (saveofp != saveifp) { /* was a socket? */
422 PerlIO_close(saveofp);
427 if (fd != PerlIO_fileno(fp)) {
431 PerlLIO_dup2(PerlIO_fileno(fp), fd);
432 sv = *av_fetch(PL_fdpid,PerlIO_fileno(fp),TRUE);
433 (void)SvUPGRADE(sv, SVt_IV);
436 sv = *av_fetch(PL_fdpid,fd,TRUE);
437 (void)SvUPGRADE(sv, SVt_IV);
446 #if defined(HAS_FCNTL) && defined(F_SETFD)
448 int save_errno = errno;
449 fd = PerlIO_fileno(fp);
450 fcntl(fd,F_SETFD,fd > PL_maxsysfd); /* can change errno */
457 if (IoTYPE(io) == 's'
458 || (IoTYPE(io) == '>' && S_ISCHR(PL_statbuf.st_mode)) ) {
459 if (!(IoOFP(io) = PerlIO_fdopen(PerlIO_fileno(fp),"w"))) {
473 IoTYPE(io) = savetype;
478 Perl_nextargv(pTHX_ register GV *gv)
481 #ifndef FLEXFILENAMES
490 PL_argvoutgv = gv_fetchpv("ARGVOUT",TRUE,SVt_PVIO);
491 if (io && (IoFLAGS(io) & IOf_ARGV) && (IoFLAGS(io) & IOf_START)) {
492 IoFLAGS(io) &= ~IOf_START;
494 av_push(PL_argvout_stack, SvREFCNT_inc(PL_defoutgv));
496 if (PL_filemode & (S_ISUID|S_ISGID)) {
497 PerlIO_flush(IoIFP(GvIOn(PL_argvoutgv))); /* chmod must follow last write */
499 (void)fchmod(PL_lastfd,PL_filemode);
501 (void)PerlLIO_chmod(PL_oldname,PL_filemode);
505 while (av_len(GvAV(gv)) >= 0) {
508 sv = av_shift(GvAV(gv));
510 sv_setsv(GvSV(gv),sv);
511 SvSETMAGIC(GvSV(gv));
512 PL_oldname = SvPVx(GvSV(gv), oldlen);
513 if (do_open(gv,PL_oldname,oldlen,PL_inplace!=0,O_RDONLY,0,Nullfp)) {
515 TAINT_PROPER("inplace open");
516 if (oldlen == 1 && *PL_oldname == '-') {
517 setdefout(gv_fetchpv("STDOUT",TRUE,SVt_PVIO));
518 return IoIFP(GvIOp(gv));
520 #ifndef FLEXFILENAMES
521 filedev = PL_statbuf.st_dev;
522 fileino = PL_statbuf.st_ino;
524 PL_filemode = PL_statbuf.st_mode;
525 fileuid = PL_statbuf.st_uid;
526 filegid = PL_statbuf.st_gid;
527 if (!S_ISREG(PL_filemode)) {
528 if (ckWARN_d(WARN_INPLACE))
529 Perl_warner(aTHX_ WARN_INPLACE,
530 "Can't do inplace edit: %s is not a regular file",
536 char *star = strchr(PL_inplace, '*');
538 char *begin = PL_inplace;
539 sv_setpvn(sv, "", 0);
541 sv_catpvn(sv, begin, star - begin);
542 sv_catpvn(sv, PL_oldname, oldlen);
544 } while ((star = strchr(begin, '*')));
549 sv_catpv(sv,PL_inplace);
551 #ifndef FLEXFILENAMES
552 if (PerlLIO_stat(SvPVX(sv),&PL_statbuf) >= 0
553 && PL_statbuf.st_dev == filedev
554 && PL_statbuf.st_ino == fileino
556 || (_djstat_fail_bits & _STFAIL_TRUENAME)!=0
560 if (ckWARN_d(WARN_INPLACE))
561 Perl_warner(aTHX_ WARN_INPLACE,
562 "Can't do inplace edit: %s would not be unique",
569 #if !defined(DOSISH) && !defined(CYGWIN)
570 if (PerlLIO_rename(PL_oldname,SvPVX(sv)) < 0) {
571 if (ckWARN_d(WARN_INPLACE))
572 Perl_warner(aTHX_ WARN_INPLACE,
573 "Can't rename %s to %s: %s, skipping file",
574 PL_oldname, SvPVX(sv), Strerror(errno) );
580 (void)PerlLIO_unlink(SvPVX(sv));
581 (void)PerlLIO_rename(PL_oldname,SvPVX(sv));
582 do_open(gv,SvPVX(sv),SvCUR(sv),PL_inplace!=0,O_RDONLY,0,Nullfp);
585 (void)UNLINK(SvPVX(sv));
586 if (link(PL_oldname,SvPVX(sv)) < 0) {
587 if (ckWARN_d(WARN_INPLACE))
588 Perl_warner(aTHX_ WARN_INPLACE,
589 "Can't rename %s to %s: %s, skipping file",
590 PL_oldname, SvPVX(sv), Strerror(errno) );
594 (void)UNLINK(PL_oldname);
598 #if !defined(DOSISH) && !defined(AMIGAOS)
599 # ifndef VMS /* Don't delete; use automatic file versioning */
600 if (UNLINK(PL_oldname) < 0) {
601 if (ckWARN_d(WARN_INPLACE))
602 Perl_warner(aTHX_ WARN_INPLACE,
603 "Can't remove %s: %s, skipping file",
604 PL_oldname, Strerror(errno) );
610 Perl_croak(aTHX_ "Can't do inplace edit without backup");
614 sv_setpvn(sv,">",!PL_inplace);
615 sv_catpvn(sv,PL_oldname,oldlen);
616 SETERRNO(0,0); /* in case sprintf set errno */
618 if (!do_open(PL_argvoutgv,SvPVX(sv),SvCUR(sv),PL_inplace!=0,
619 O_WRONLY|O_CREAT|O_TRUNC,0,Nullfp))
621 if (!do_open(PL_argvoutgv,SvPVX(sv),SvCUR(sv),PL_inplace!=0,
622 O_WRONLY|O_CREAT|OPEN_EXCL,0666,Nullfp))
625 if (ckWARN_d(WARN_INPLACE))
626 Perl_warner(aTHX_ WARN_INPLACE, "Can't do inplace edit on %s: %s",
627 PL_oldname, Strerror(errno) );
631 setdefout(PL_argvoutgv);
632 PL_lastfd = PerlIO_fileno(IoIFP(GvIOp(PL_argvoutgv)));
633 (void)PerlLIO_fstat(PL_lastfd,&PL_statbuf);
635 (void)fchmod(PL_lastfd,PL_filemode);
637 # if !(defined(WIN32) && defined(__BORLANDC__))
638 /* Borland runtime creates a readonly file! */
639 (void)PerlLIO_chmod(PL_oldname,PL_filemode);
642 if (fileuid != PL_statbuf.st_uid || filegid != PL_statbuf.st_gid) {
644 (void)fchown(PL_lastfd,fileuid,filegid);
647 (void)PerlLIO_chown(PL_oldname,fileuid,filegid);
652 return IoIFP(GvIOp(gv));
656 if (ckWARN_d(WARN_INPLACE)) {
657 if (!S_ISREG(PL_statbuf.st_mode))
658 Perl_warner(aTHX_ WARN_INPLACE,
659 "Can't do inplace edit: %s is not a regular file",
662 Perl_warner(aTHX_ WARN_INPLACE, "Can't open %s: %s\n",
663 PL_oldname, Strerror(errno));
667 if (io && (IoFLAGS(io) & IOf_ARGV))
668 IoFLAGS(io) |= IOf_START;
670 (void)do_close(PL_argvoutgv,FALSE);
671 if (io && (IoFLAGS(io) & IOf_ARGV) && AvFILLp(PL_argvout_stack) >= 0) {
672 GV *oldout = (GV*)av_pop(PL_argvout_stack);
674 SvREFCNT_dec(oldout);
677 setdefout(gv_fetchpv("STDOUT",TRUE,SVt_PVIO));
684 Perl_do_pipe(pTHX_ SV *sv, GV *rgv, GV *wgv)
703 if (PerlProc_pipe(fd) < 0)
705 IoIFP(rstio) = PerlIO_fdopen(fd[0], "r");
706 IoOFP(wstio) = PerlIO_fdopen(fd[1], "w");
707 IoIFP(wstio) = IoOFP(wstio);
710 if (!IoIFP(rstio) || !IoOFP(wstio)) {
711 if (IoIFP(rstio)) PerlIO_close(IoIFP(rstio));
712 else PerlLIO_close(fd[0]);
713 if (IoOFP(wstio)) PerlIO_close(IoOFP(wstio));
714 else PerlLIO_close(fd[1]);
718 sv_setsv(sv,&PL_sv_yes);
722 sv_setsv(sv,&PL_sv_undef);
727 /* explicit renamed to avoid C++ conflict -- kja */
729 Perl_do_close(pTHX_ GV *gv, bool not_implicit)
736 if (!gv || SvTYPE(gv) != SVt_PVGV) {
738 SETERRNO(EBADF,SS$_IVCHAN);
742 if (!io) { /* never opened */
745 if (ckWARN(WARN_UNOPENED))
746 Perl_warner(aTHX_ WARN_UNOPENED,
747 "Close on unopened file <%s>",GvENAME(gv));
748 SETERRNO(EBADF,SS$_IVCHAN);
752 retval = io_close(io, not_implicit);
756 IoLINES_LEFT(io) = IoPAGE_LEN(io);
763 Perl_io_close(pTHX_ IO *io, bool not_implicit)
769 if (IoTYPE(io) == '|') {
770 status = PerlProc_pclose(IoIFP(io));
772 STATUS_NATIVE_SET(status);
773 retval = (STATUS_POSIX == 0);
776 retval = (status != -1);
779 else if (IoTYPE(io) == '-')
782 if (IoOFP(io) && IoOFP(io) != IoIFP(io)) { /* a socket */
783 retval = (PerlIO_close(IoOFP(io)) != EOF);
784 PerlIO_close(IoIFP(io)); /* clear stdio, fd already closed */
787 retval = (PerlIO_close(IoIFP(io)) != EOF);
789 IoOFP(io) = IoIFP(io) = Nullfp;
791 else if (not_implicit) {
792 SETERRNO(EBADF,SS$_IVCHAN);
799 Perl_do_eof(pTHX_ GV *gv)
809 else if (ckWARN(WARN_IO)
810 && (IoTYPE(io) == '>' || IoIFP(io) == PerlIO_stdout()
811 || IoIFP(io) == PerlIO_stderr()))
813 SV* sv = sv_newmortal();
814 gv_efullname3(sv, gv, Nullch);
815 Perl_warner(aTHX_ WARN_IO, "Filehandle %s opened only for output",
821 if (PerlIO_has_cntptr(IoIFP(io))) { /* (the code works without this) */
822 if (PerlIO_get_cnt(IoIFP(io)) > 0) /* cheat a little, since */
823 return FALSE; /* this is the most usual case */
826 ch = PerlIO_getc(IoIFP(io));
828 (void)PerlIO_ungetc(IoIFP(io),ch);
831 if (PerlIO_has_cntptr(IoIFP(io)) && PerlIO_canset_cnt(IoIFP(io))) {
832 if (PerlIO_get_cnt(IoIFP(io)) < -1)
833 PerlIO_set_cnt(IoIFP(io),-1);
835 if (PL_op->op_flags & OPf_SPECIAL) { /* not necessarily a real EOF yet? */
836 if (!nextargv(PL_argvgv)) /* get another fp handy */
840 return TRUE; /* normal fp, definitely end of file */
846 Perl_do_tell(pTHX_ GV *gv)
851 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io))) {
852 #ifdef ULTRIX_STDIO_BOTCH
854 (void)PerlIO_seek(fp, 0L, 2); /* ultrix 1.2 workaround */
856 return PerlIO_tell(fp);
860 if (ckWARN(WARN_UNOPENED))
861 Perl_warner(aTHX_ WARN_UNOPENED, "tell() on unopened file");
863 SETERRNO(EBADF,RMS$_IFI);
868 Perl_do_seek(pTHX_ GV *gv, Off_t pos, int whence)
873 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io))) {
874 #ifdef ULTRIX_STDIO_BOTCH
876 (void)PerlIO_seek(fp, 0L, 2); /* ultrix 1.2 workaround */
878 return PerlIO_seek(fp, pos, whence) >= 0;
882 if (ckWARN(WARN_UNOPENED))
883 Perl_warner(aTHX_ WARN_UNOPENED, "seek() on unopened file");
885 SETERRNO(EBADF,RMS$_IFI);
890 Perl_do_sysseek(pTHX_ GV *gv, Off_t pos, int whence)
895 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io)))
896 return PerlLIO_lseek(PerlIO_fileno(fp), pos, whence);
899 if (ckWARN(WARN_UNOPENED))
900 Perl_warner(aTHX_ WARN_UNOPENED, "sysseek() on unopened file");
902 SETERRNO(EBADF,RMS$_IFI);
907 Perl_do_binmode(pTHX_ PerlIO *fp, int iotype, int flag)
910 Perl_croak(aTHX_ "panic: unsetting binmode"); /* Not implemented yet */
912 #if defined(atarist) || defined(__MINT__)
913 if (!PerlIO_flush(fp) && (fp->_flag |= _IOBIN))
918 if (PerlLIO_setmode(PerlIO_fileno(fp), OP_BINARY) != -1) {
919 #if defined(WIN32) && defined(__BORLANDC__)
920 /* The translation mode of the stream is maintained independent
921 * of the translation mode of the fd in the Borland RTL (heavy
922 * digging through their runtime sources reveal). User has to
923 * set the mode explicitly for the stream (though they don't
924 * document this anywhere). GSAR 97-5-24
926 PerlIO_seek(fp,0L,0);
927 ((FILE*)fp)->flags |= _F_BIN;
935 #if defined(USEMYBINMODE)
936 if (my_binmode(fp,iotype) != FALSE)
946 #if !defined(HAS_TRUNCATE) && !defined(HAS_CHSIZE) && defined(F_FREESP)
947 /* code courtesy of William Kucharski */
950 I32 my_chsize(fd, length)
951 I32 fd; /* file descriptor */
952 Off_t length; /* length to set file to */
957 if (PerlLIO_fstat(fd, &filebuf) < 0)
960 if (filebuf.st_size < length) {
962 /* extend file length */
964 if ((PerlLIO_lseek(fd, (length - 1), 0)) < 0)
967 /* write a "0" byte */
969 if ((PerlLIO_write(fd, "", 1)) != 1)
973 /* truncate length */
978 fl.l_type = F_WRLCK; /* write lock on file space */
981 * This relies on the UNDOCUMENTED F_FREESP argument to
982 * fcntl(2), which truncates the file so that it ends at the
983 * position indicated by fl.l_start.
985 * Will minor miracles never cease?
988 if (fcntl(fd, F_FREESP, &fl) < 0)
995 #endif /* F_FREESP */
998 Perl_do_print(pTHX_ register SV *sv, PerlIO *fp)
1000 register char *tmps;
1003 /* assuming fp is checked earlier */
1009 if (SvIOK(sv) && SvIVX(sv) != 0) {
1010 PerlIO_printf(fp, PL_ofmt, (NV)SvIVX(sv));
1011 return !PerlIO_error(fp);
1013 if ( (SvNOK(sv) && SvNVX(sv) != 0.0)
1014 || (looks_like_number(sv) && sv_2nv(sv) != 0.0) ) {
1015 PerlIO_printf(fp, PL_ofmt, SvNVX(sv));
1016 return !PerlIO_error(fp);
1019 switch (SvTYPE(sv)) {
1023 if (ckWARN(WARN_UNINITIALIZED))
1024 Perl_warner(aTHX_ WARN_UNINITIALIZED, PL_warn_uninit);
1032 PerlIO_printf(fp, "%"UVuf, (UV)SvUVX(sv));
1034 PerlIO_printf(fp, "%"IVdf, (IV)SvIVX(sv));
1035 return !PerlIO_error(fp);
1039 tmps = SvPV(sv, len);
1042 /* To detect whether the process is about to overstep its
1043 * filesize limit we would need getrlimit(). We could then
1044 * also transparently raise the limit with setrlimit() --
1045 * but only until the system hard limit/the filesystem limit,
1046 * at which we would get EPERM. Note that when using buffered
1047 * io the write failure can be delayed until the flush/close. --jhi */
1048 if (len && (PerlIO_write(fp,tmps,len) == 0 || PerlIO_error(fp)))
1050 return !PerlIO_error(fp);
1060 if (PL_op->op_flags & OPf_REF) {
1062 tmpgv = (GV*)cSVOP->op_sv;
1065 if (io && IoIFP(io)) {
1067 sv_setpv(PL_statname,"");
1068 PL_laststype = OP_STAT;
1069 return (PL_laststatval = PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache));
1072 if (tmpgv == PL_defgv)
1073 return PL_laststatval;
1074 if (ckWARN(WARN_UNOPENED))
1075 Perl_warner(aTHX_ WARN_UNOPENED, "Stat on unopened file <%s>",
1078 sv_setpv(PL_statname,"");
1079 return (PL_laststatval = -1);
1087 if (SvTYPE(sv) == SVt_PVGV) {
1091 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
1092 tmpgv = (GV*)SvRV(sv);
1098 sv_setpv(PL_statname, s);
1099 PL_laststype = OP_STAT;
1100 PL_laststatval = PerlLIO_stat(s, &PL_statcache);
1101 if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(s, '\n'))
1102 Perl_warner(aTHX_ WARN_NEWLINE, PL_warn_nl, "stat");
1103 return PL_laststatval;
1113 if (PL_op->op_flags & OPf_REF) {
1115 if ((GV*)cSVOP->op_sv == PL_defgv) {
1116 if (PL_laststype != OP_LSTAT)
1117 Perl_croak(aTHX_ "The stat preceding -l _ wasn't an lstat");
1118 return PL_laststatval;
1120 Perl_croak(aTHX_ "You can't use -l on a filehandle");
1123 PL_laststype = OP_LSTAT;
1127 sv_setpv(PL_statname,SvPV(sv, n_a));
1128 PL_laststatval = PerlLIO_lstat(SvPV(sv, n_a),&PL_statcache);
1129 if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(SvPV(sv, n_a), '\n'))
1130 Perl_warner(aTHX_ WARN_NEWLINE, PL_warn_nl, "lstat");
1131 return PL_laststatval;
1135 Perl_do_aexec(pTHX_ SV *really, register SV **mark, register SV **sp)
1137 return do_aexec5(really, mark, sp, 0, 0);
1141 Perl_do_aexec5(pTHX_ SV *really, register SV **mark, register SV **sp,
1142 int fd, int do_report)
1150 New(401,PL_Argv, sp - mark + 1, char*);
1152 while (++mark <= sp) {
1154 *a++ = SvPVx(*mark, n_a);
1159 if (*PL_Argv[0] != '/') /* will execvp use PATH? */
1160 TAINT_ENV(); /* testing IFS here is overkill, probably */
1161 if (really && *(tmps = SvPV(really, n_a)))
1162 PerlProc_execvp(tmps,PL_Argv);
1164 PerlProc_execvp(PL_Argv[0],PL_Argv);
1165 if (ckWARN(WARN_EXEC))
1166 Perl_warner(aTHX_ WARN_EXEC, "Can't exec \"%s\": %s",
1167 PL_Argv[0], Strerror(errno));
1171 PerlLIO_write(fd, (void*)&e, sizeof(int));
1180 Perl_do_execfree(pTHX)
1184 PL_Argv = Null(char **);
1192 #if !defined(OS2) && !defined(WIN32) && !defined(DJGPP) && !defined(EPOC)
1195 Perl_do_exec(pTHX_ char *cmd)
1197 return do_exec3(cmd,0,0);
1201 Perl_do_exec3(pTHX_ char *cmd, int fd, int do_report)
1207 while (*cmd && isSPACE(*cmd))
1210 /* save an extra exec if possible */
1213 if (strnEQ(cmd,PL_cshname,PL_cshlen) && strnEQ(cmd+PL_cshlen," -c",3)) {
1215 s = cmd+PL_cshlen+3;
1229 if (s[-1] == '\'') {
1231 PerlProc_execl(PL_cshname,"csh", flags,ncmd,(char*)0);
1239 /* see if there are shell metacharacters in it */
1241 if (*cmd == '.' && isSPACE(cmd[1]))
1244 if (strnEQ(cmd,"exec",4) && isSPACE(cmd[4]))
1247 for (s = cmd; *s && isALPHA(*s); s++) ; /* catch VAR=val gizmo */
1251 for (s = cmd; *s; s++) {
1252 if (*s != ' ' && !isALPHA(*s) && strchr("$&*(){}[]'\";\\|?<>~`\n",*s)) {
1253 if (*s == '\n' && !s[1]) {
1257 /* handle the 2>&1 construct at the end */
1258 if (*s == '>' && s[1] == '&' && s[2] == '1'
1259 && s > cmd + 1 && s[-1] == '2' && isSPACE(s[-2])
1260 && (!s[3] || isSPACE(s[3])))
1264 while (*t && isSPACE(*t))
1266 if (!*t && (dup2(1,2) != -1)) {
1272 PerlProc_execl(PL_sh_path, "sh", "-c", cmd, (char*)0);
1277 New(402,PL_Argv, (s - cmd) / 2 + 2, char*);
1278 PL_Cmd = savepvn(cmd, s-cmd);
1280 for (s = PL_Cmd; *s;) {
1281 while (*s && isSPACE(*s)) s++;
1284 while (*s && !isSPACE(*s)) s++;
1290 PerlProc_execvp(PL_Argv[0],PL_Argv);
1291 if (errno == ENOEXEC) { /* for system V NIH syndrome */
1299 if (ckWARN(WARN_EXEC))
1300 Perl_warner(aTHX_ WARN_EXEC, "Can't exec \"%s\": %s",
1301 PL_Argv[0], Strerror(errno));
1303 PerlLIO_write(fd, (void*)&e, sizeof(int));
1312 #endif /* OS2 || WIN32 */
1315 Perl_apply(pTHX_ I32 type, register SV **mark, register SV **sp)
1320 register I32 tot = 0;
1323 SV **oldmark = mark;
1326 #define APPLY_TAINT_PROPER() \
1328 if (PL_tainted) { TAINT_PROPER(what); } \
1331 /* This is a first heuristic; it doesn't catch tainting magic. */
1333 while (++mark <= sp) {
1334 if (SvTAINTED(*mark)) {
1344 APPLY_TAINT_PROPER();
1347 APPLY_TAINT_PROPER();
1349 while (++mark <= sp) {
1350 char *name = SvPVx(*mark, n_a);
1351 APPLY_TAINT_PROPER();
1352 if (PerlLIO_chmod(name, val))
1360 APPLY_TAINT_PROPER();
1361 if (sp - mark > 2) {
1362 val = SvIVx(*++mark);
1363 val2 = SvIVx(*++mark);
1364 APPLY_TAINT_PROPER();
1366 while (++mark <= sp) {
1367 char *name = SvPVx(*mark, n_a);
1368 APPLY_TAINT_PROPER();
1369 if (PerlLIO_chown(name, val, val2))
1376 XXX Should we make lchown() directly available from perl?
1377 For now, we'll let Configure test for HAS_LCHOWN, but do
1378 nothing in the core.
1384 APPLY_TAINT_PROPER();
1387 s = SvPVx(*++mark, n_a);
1389 if (*s == 'S' && s[1] == 'I' && s[2] == 'G')
1391 if (!(val = whichsig(s)))
1392 Perl_croak(aTHX_ "Unrecognized signal name \"%s\"",s);
1396 APPLY_TAINT_PROPER();
1399 /* kill() doesn't do process groups (job trees?) under VMS */
1400 if (val < 0) val = -val;
1401 if (val == SIGKILL) {
1402 # include <starlet.h>
1403 /* Use native sys$delprc() to insure that target process is
1404 * deleted; supervisor-mode images don't pay attention to
1405 * CRTL's emulation of Unix-style signals and kill()
1407 while (++mark <= sp) {
1408 I32 proc = SvIVx(*mark);
1409 register unsigned long int __vmssts;
1410 APPLY_TAINT_PROPER();
1411 if (!((__vmssts = sys$delprc(&proc,0)) & 1)) {
1415 case SS$_NOSUCHNODE:
1416 SETERRNO(ESRCH,__vmssts);
1419 SETERRNO(EPERM,__vmssts);
1422 SETERRNO(EVMSERR,__vmssts);
1431 while (++mark <= sp) {
1432 I32 proc = SvIVx(*mark);
1433 APPLY_TAINT_PROPER();
1435 if (PerlProc_killpg(proc,val)) /* BSD */
1437 if (PerlProc_kill(-proc,val)) /* SYSV */
1443 while (++mark <= sp) {
1444 I32 proc = SvIVx(*mark);
1445 APPLY_TAINT_PROPER();
1446 if (PerlProc_kill(proc, val))
1454 APPLY_TAINT_PROPER();
1456 while (++mark <= sp) {
1457 s = SvPVx(*mark, n_a);
1458 APPLY_TAINT_PROPER();
1459 if (PL_euid || PL_unsafe) {
1463 else { /* don't let root wipe out directories without -U */
1464 if (PerlLIO_lstat(s,&PL_statbuf) < 0 || S_ISDIR(PL_statbuf.st_mode))
1476 APPLY_TAINT_PROPER();
1477 if (sp - mark > 2) {
1478 #if defined(I_UTIME) || defined(VMS)
1479 struct utimbuf utbuf;
1487 Zero(&utbuf, sizeof utbuf, char);
1489 utbuf.actime = (Time_t)SvNVx(*++mark); /* time accessed */
1490 utbuf.modtime = (Time_t)SvNVx(*++mark); /* time modified */
1492 utbuf.actime = (Time_t)SvIVx(*++mark); /* time accessed */
1493 utbuf.modtime = (Time_t)SvIVx(*++mark); /* time modified */
1495 APPLY_TAINT_PROPER();
1497 while (++mark <= sp) {
1498 char *name = SvPVx(*mark, n_a);
1499 APPLY_TAINT_PROPER();
1500 if (PerlLIO_utime(name, &utbuf))
1511 #undef APPLY_TAINT_PROPER
1514 /* Do the permissions allow some operation? Assumes statcache already set. */
1515 #ifndef VMS /* VMS' cando is in vms.c */
1517 Perl_cando(pTHX_ Mode_t mode, Uid_t effective, register Stat_t *statbufp)
1518 /* Note: we use `effective' both for uids and gids.
1519 * Here we are betting on Uid_t being equal or wider than Gid_t. */
1522 /* [Comments and code from Len Reed]
1523 * MS-DOS "user" is similar to UNIX's "superuser," but can't write
1524 * to write-protected files. The execute permission bit is set
1525 * by the Miscrosoft C library stat() function for the following:
1530 * All files and directories are readable.
1531 * Directories and special files, e.g. "CON", cannot be
1533 * [Comment by Tom Dinger -- a directory can have the write-protect
1534 * bit set in the file system, but DOS permits changes to
1535 * the directory anyway. In addition, all bets are off
1536 * here for networked software, such as Novell and
1540 /* Atari stat() does pretty much the same thing. we set x_bit_set_in_stat
1541 * too so it will actually look into the files for magic numbers
1543 return (mode & statbufp->st_mode) ? TRUE : FALSE;
1545 #else /* ! DOSISH */
1546 if ((effective ? PL_euid : PL_uid) == 0) { /* root is special */
1547 if (mode == S_IXUSR) {
1548 if (statbufp->st_mode & 0111 || S_ISDIR(statbufp->st_mode))
1552 return TRUE; /* root reads and writes anything */
1555 if (statbufp->st_uid == (effective ? PL_euid : PL_uid) ) {
1556 if (statbufp->st_mode & mode)
1557 return TRUE; /* ok as "user" */
1559 else if (ingroup(statbufp->st_gid,effective)) {
1560 if (statbufp->st_mode & mode >> 3)
1561 return TRUE; /* ok as "group" */
1563 else if (statbufp->st_mode & mode >> 6)
1564 return TRUE; /* ok as "other" */
1566 #endif /* ! DOSISH */
1571 Perl_ingroup(pTHX_ Gid_t testgid, Uid_t effective)
1573 if (testgid == (effective ? PL_egid : PL_gid))
1575 #ifdef HAS_GETGROUPS
1580 Groups_t gary[NGROUPS];
1583 anum = getgroups(NGROUPS,gary);
1585 if (gary[anum] == testgid)
1592 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
1595 Perl_do_ipcget(pTHX_ I32 optype, SV **mark, SV **sp)
1601 key = (key_t)SvNVx(*++mark);
1602 n = (optype == OP_MSGGET) ? 0 : SvIVx(*++mark);
1603 flags = SvIVx(*++mark);
1609 return msgget(key, flags);
1613 return semget(key, n, flags);
1617 return shmget(key, n, flags);
1619 #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
1621 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
1624 return -1; /* should never happen */
1628 Perl_do_ipcctl(pTHX_ I32 optype, SV **mark, SV **sp)
1633 I32 id, n, cmd, infosize, getinfo;
1636 id = SvIVx(*++mark);
1637 n = (optype == OP_SEMCTL) ? SvIVx(*++mark) : 0;
1638 cmd = SvIVx(*++mark);
1641 getinfo = (cmd == IPC_STAT);
1647 if (cmd == IPC_STAT || cmd == IPC_SET)
1648 infosize = sizeof(struct msqid_ds);
1653 if (cmd == IPC_STAT || cmd == IPC_SET)
1654 infosize = sizeof(struct shmid_ds);
1660 if (cmd == IPC_STAT || cmd == IPC_SET)
1661 infosize = sizeof(struct semid_ds);
1662 else if (cmd == GETALL || cmd == SETALL)
1664 struct semid_ds semds;
1668 getinfo = (cmd == GETALL);
1669 if (Semctl(id, 0, IPC_STAT, semun) == -1)
1671 infosize = semds.sem_nsems * sizeof(short);
1672 /* "short" is technically wrong but much more portable
1673 than guessing about u_?short(_t)? */
1676 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
1680 #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
1682 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
1691 SvPV_force(astr, len);
1692 a = SvGROW(astr, infosize+1);
1696 a = SvPV(astr, len);
1697 if (len != infosize)
1698 Perl_croak(aTHX_ "Bad arg length for %s, is %lu, should be %ld",
1707 a = INT2PTR(char *,i); /* ouch */
1714 ret = msgctl(id, cmd, (struct msqid_ds *)a);
1720 union semun unsemds;
1722 unsemds.buf = (struct semid_ds *)a;
1723 ret = Semctl(id, n, cmd, unsemds);
1725 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
1732 ret = shmctl(id, cmd, (struct shmid_ds *)a);
1736 if (getinfo && ret >= 0) {
1737 SvCUR_set(astr, infosize);
1738 *SvEND(astr) = '\0';
1745 Perl_do_msgsnd(pTHX_ SV **mark, SV **sp)
1751 I32 id, msize, flags;
1754 id = SvIVx(*++mark);
1756 flags = SvIVx(*++mark);
1757 mbuf = SvPV(mstr, len);
1758 if ((msize = len - sizeof(long)) < 0)
1759 Perl_croak(aTHX_ "Arg too short for msgsnd");
1761 return msgsnd(id, (struct msgbuf *)mbuf, msize, flags);
1763 Perl_croak(aTHX_ "msgsnd not implemented");
1768 Perl_do_msgrcv(pTHX_ SV **mark, SV **sp)
1775 I32 id, msize, flags, ret;
1778 id = SvIVx(*++mark);
1780 msize = SvIVx(*++mark);
1781 mtype = (long)SvIVx(*++mark);
1782 flags = SvIVx(*++mark);
1783 SvPV_force(mstr, len);
1784 mbuf = SvGROW(mstr, sizeof(long)+msize+1);
1787 ret = msgrcv(id, (struct msgbuf *)mbuf, msize, mtype, flags);
1789 SvCUR_set(mstr, sizeof(long)+ret);
1790 *SvEND(mstr) = '\0';
1794 Perl_croak(aTHX_ "msgrcv not implemented");
1799 Perl_do_semop(pTHX_ SV **mark, SV **sp)
1808 id = SvIVx(*++mark);
1810 opbuf = SvPV(opstr, opsize);
1811 if (opsize < sizeof(struct sembuf)
1812 || (opsize % sizeof(struct sembuf)) != 0) {
1813 SETERRNO(EINVAL,LIB$_INVARG);
1817 return semop(id, (struct sembuf *)opbuf, opsize/sizeof(struct sembuf));
1819 Perl_croak(aTHX_ "semop not implemented");
1824 Perl_do_shmio(pTHX_ I32 optype, SV **mark, SV **sp)
1830 I32 id, mpos, msize;
1832 struct shmid_ds shmds;
1834 id = SvIVx(*++mark);
1836 mpos = SvIVx(*++mark);
1837 msize = SvIVx(*++mark);
1839 if (shmctl(id, IPC_STAT, &shmds) == -1)
1841 if (mpos < 0 || msize < 0 || mpos + msize > shmds.shm_segsz) {
1842 SETERRNO(EFAULT,SS$_ACCVIO); /* can't do as caller requested */
1845 shm = (char *)shmat(id, (char*)NULL, (optype == OP_SHMREAD) ? SHM_RDONLY : 0);
1846 if (shm == (char *)-1) /* I hate System V IPC, I really do */
1848 if (optype == OP_SHMREAD) {
1849 SvPV_force(mstr, len);
1850 mbuf = SvGROW(mstr, msize+1);
1852 Copy(shm + mpos, mbuf, msize, char);
1853 SvCUR_set(mstr, msize);
1854 *SvEND(mstr) = '\0';
1860 mbuf = SvPV(mstr, len);
1861 if ((n = len) > msize)
1863 Copy(mbuf, shm + mpos, n, char);
1865 memzero(shm + mpos + n, msize - n);
1869 Perl_croak(aTHX_ "shm I/O not implemented");
1873 #endif /* SYSV IPC */