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 if (!PL_argvout_stack)
495 PL_argvout_stack = newAV();
496 av_push(PL_argvout_stack, SvREFCNT_inc(PL_defoutgv));
499 if (PL_filemode & (S_ISUID|S_ISGID)) {
500 PerlIO_flush(IoIFP(GvIOn(PL_argvoutgv))); /* chmod must follow last write */
502 (void)fchmod(PL_lastfd,PL_filemode);
504 (void)PerlLIO_chmod(PL_oldname,PL_filemode);
508 while (av_len(GvAV(gv)) >= 0) {
511 sv = av_shift(GvAV(gv));
513 sv_setsv(GvSV(gv),sv);
514 SvSETMAGIC(GvSV(gv));
515 PL_oldname = SvPVx(GvSV(gv), oldlen);
516 if (do_open(gv,PL_oldname,oldlen,PL_inplace!=0,O_RDONLY,0,Nullfp)) {
518 TAINT_PROPER("inplace open");
519 if (oldlen == 1 && *PL_oldname == '-') {
520 setdefout(gv_fetchpv("STDOUT",TRUE,SVt_PVIO));
521 return IoIFP(GvIOp(gv));
523 #ifndef FLEXFILENAMES
524 filedev = PL_statbuf.st_dev;
525 fileino = PL_statbuf.st_ino;
527 PL_filemode = PL_statbuf.st_mode;
528 fileuid = PL_statbuf.st_uid;
529 filegid = PL_statbuf.st_gid;
530 if (!S_ISREG(PL_filemode)) {
531 if (ckWARN_d(WARN_INPLACE))
532 Perl_warner(aTHX_ WARN_INPLACE,
533 "Can't do inplace edit: %s is not a regular file",
539 char *star = strchr(PL_inplace, '*');
541 char *begin = PL_inplace;
542 sv_setpvn(sv, "", 0);
544 sv_catpvn(sv, begin, star - begin);
545 sv_catpvn(sv, PL_oldname, oldlen);
547 } while ((star = strchr(begin, '*')));
552 sv_catpv(sv,PL_inplace);
554 #ifndef FLEXFILENAMES
555 if (PerlLIO_stat(SvPVX(sv),&PL_statbuf) >= 0
556 && PL_statbuf.st_dev == filedev
557 && PL_statbuf.st_ino == fileino
559 || (_djstat_fail_bits & _STFAIL_TRUENAME)!=0
563 if (ckWARN_d(WARN_INPLACE))
564 Perl_warner(aTHX_ WARN_INPLACE,
565 "Can't do inplace edit: %s would not be unique",
572 #if !defined(DOSISH) && !defined(CYGWIN)
573 if (PerlLIO_rename(PL_oldname,SvPVX(sv)) < 0) {
574 if (ckWARN_d(WARN_INPLACE))
575 Perl_warner(aTHX_ WARN_INPLACE,
576 "Can't rename %s to %s: %s, skipping file",
577 PL_oldname, SvPVX(sv), Strerror(errno) );
583 (void)PerlLIO_unlink(SvPVX(sv));
584 (void)PerlLIO_rename(PL_oldname,SvPVX(sv));
585 do_open(gv,SvPVX(sv),SvCUR(sv),PL_inplace!=0,O_RDONLY,0,Nullfp);
588 (void)UNLINK(SvPVX(sv));
589 if (link(PL_oldname,SvPVX(sv)) < 0) {
590 if (ckWARN_d(WARN_INPLACE))
591 Perl_warner(aTHX_ WARN_INPLACE,
592 "Can't rename %s to %s: %s, skipping file",
593 PL_oldname, SvPVX(sv), Strerror(errno) );
597 (void)UNLINK(PL_oldname);
601 #if !defined(DOSISH) && !defined(AMIGAOS)
602 # ifndef VMS /* Don't delete; use automatic file versioning */
603 if (UNLINK(PL_oldname) < 0) {
604 if (ckWARN_d(WARN_INPLACE))
605 Perl_warner(aTHX_ WARN_INPLACE,
606 "Can't remove %s: %s, skipping file",
607 PL_oldname, Strerror(errno) );
613 Perl_croak(aTHX_ "Can't do inplace edit without backup");
617 sv_setpvn(sv,">",!PL_inplace);
618 sv_catpvn(sv,PL_oldname,oldlen);
619 SETERRNO(0,0); /* in case sprintf set errno */
621 if (!do_open(PL_argvoutgv,SvPVX(sv),SvCUR(sv),PL_inplace!=0,
622 O_WRONLY|O_CREAT|O_TRUNC,0,Nullfp))
624 if (!do_open(PL_argvoutgv,SvPVX(sv),SvCUR(sv),PL_inplace!=0,
625 O_WRONLY|O_CREAT|OPEN_EXCL,0666,Nullfp))
628 if (ckWARN_d(WARN_INPLACE))
629 Perl_warner(aTHX_ WARN_INPLACE, "Can't do inplace edit on %s: %s",
630 PL_oldname, Strerror(errno) );
634 setdefout(PL_argvoutgv);
635 PL_lastfd = PerlIO_fileno(IoIFP(GvIOp(PL_argvoutgv)));
636 (void)PerlLIO_fstat(PL_lastfd,&PL_statbuf);
638 (void)fchmod(PL_lastfd,PL_filemode);
640 # if !(defined(WIN32) && defined(__BORLANDC__))
641 /* Borland runtime creates a readonly file! */
642 (void)PerlLIO_chmod(PL_oldname,PL_filemode);
645 if (fileuid != PL_statbuf.st_uid || filegid != PL_statbuf.st_gid) {
647 (void)fchown(PL_lastfd,fileuid,filegid);
650 (void)PerlLIO_chown(PL_oldname,fileuid,filegid);
655 return IoIFP(GvIOp(gv));
659 if (ckWARN_d(WARN_INPLACE)) {
660 if (!S_ISREG(PL_statbuf.st_mode))
661 Perl_warner(aTHX_ WARN_INPLACE,
662 "Can't do inplace edit: %s is not a regular file",
665 Perl_warner(aTHX_ WARN_INPLACE, "Can't open %s: %s\n",
666 PL_oldname, Strerror(errno));
670 if (io && (IoFLAGS(io) & IOf_ARGV))
671 IoFLAGS(io) |= IOf_START;
673 (void)do_close(PL_argvoutgv,FALSE);
674 if (io && (IoFLAGS(io) & IOf_ARGV)
675 && PL_argvout_stack && AvFILLp(PL_argvout_stack) >= 0)
677 GV *oldout = (GV*)av_pop(PL_argvout_stack);
679 SvREFCNT_dec(oldout);
682 setdefout(gv_fetchpv("STDOUT",TRUE,SVt_PVIO));
689 Perl_do_pipe(pTHX_ SV *sv, GV *rgv, GV *wgv)
708 if (PerlProc_pipe(fd) < 0)
710 IoIFP(rstio) = PerlIO_fdopen(fd[0], "r");
711 IoOFP(wstio) = PerlIO_fdopen(fd[1], "w");
712 IoIFP(wstio) = IoOFP(wstio);
715 if (!IoIFP(rstio) || !IoOFP(wstio)) {
716 if (IoIFP(rstio)) PerlIO_close(IoIFP(rstio));
717 else PerlLIO_close(fd[0]);
718 if (IoOFP(wstio)) PerlIO_close(IoOFP(wstio));
719 else PerlLIO_close(fd[1]);
723 sv_setsv(sv,&PL_sv_yes);
727 sv_setsv(sv,&PL_sv_undef);
732 /* explicit renamed to avoid C++ conflict -- kja */
734 Perl_do_close(pTHX_ GV *gv, bool not_implicit)
741 if (!gv || SvTYPE(gv) != SVt_PVGV) {
743 SETERRNO(EBADF,SS$_IVCHAN);
747 if (!io) { /* never opened */
750 if (ckWARN(WARN_UNOPENED))
751 Perl_warner(aTHX_ WARN_UNOPENED,
752 "Close on unopened file <%s>",GvENAME(gv));
753 SETERRNO(EBADF,SS$_IVCHAN);
757 retval = io_close(io, not_implicit);
761 IoLINES_LEFT(io) = IoPAGE_LEN(io);
768 Perl_io_close(pTHX_ IO *io, bool not_implicit)
774 if (IoTYPE(io) == '|') {
775 status = PerlProc_pclose(IoIFP(io));
777 STATUS_NATIVE_SET(status);
778 retval = (STATUS_POSIX == 0);
781 retval = (status != -1);
784 else if (IoTYPE(io) == '-')
787 if (IoOFP(io) && IoOFP(io) != IoIFP(io)) { /* a socket */
788 retval = (PerlIO_close(IoOFP(io)) != EOF);
789 PerlIO_close(IoIFP(io)); /* clear stdio, fd already closed */
792 retval = (PerlIO_close(IoIFP(io)) != EOF);
794 IoOFP(io) = IoIFP(io) = Nullfp;
796 else if (not_implicit) {
797 SETERRNO(EBADF,SS$_IVCHAN);
804 Perl_do_eof(pTHX_ GV *gv)
814 else if (ckWARN(WARN_IO)
815 && (IoTYPE(io) == '>' || IoIFP(io) == PerlIO_stdout()
816 || IoIFP(io) == PerlIO_stderr()))
818 SV* sv = sv_newmortal();
819 gv_efullname3(sv, gv, Nullch);
820 Perl_warner(aTHX_ WARN_IO, "Filehandle %s opened only for output",
826 if (PerlIO_has_cntptr(IoIFP(io))) { /* (the code works without this) */
827 if (PerlIO_get_cnt(IoIFP(io)) > 0) /* cheat a little, since */
828 return FALSE; /* this is the most usual case */
831 ch = PerlIO_getc(IoIFP(io));
833 (void)PerlIO_ungetc(IoIFP(io),ch);
836 if (PerlIO_has_cntptr(IoIFP(io)) && PerlIO_canset_cnt(IoIFP(io))) {
837 if (PerlIO_get_cnt(IoIFP(io)) < -1)
838 PerlIO_set_cnt(IoIFP(io),-1);
840 if (PL_op->op_flags & OPf_SPECIAL) { /* not necessarily a real EOF yet? */
841 if (!nextargv(PL_argvgv)) /* get another fp handy */
845 return TRUE; /* normal fp, definitely end of file */
851 Perl_do_tell(pTHX_ GV *gv)
856 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io))) {
857 #ifdef ULTRIX_STDIO_BOTCH
859 (void)PerlIO_seek(fp, 0L, 2); /* ultrix 1.2 workaround */
861 return PerlIO_tell(fp);
865 if (ckWARN(WARN_UNOPENED))
866 Perl_warner(aTHX_ WARN_UNOPENED, "tell() on unopened file");
868 SETERRNO(EBADF,RMS$_IFI);
873 Perl_do_seek(pTHX_ GV *gv, Off_t pos, int whence)
878 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io))) {
879 #ifdef ULTRIX_STDIO_BOTCH
881 (void)PerlIO_seek(fp, 0L, 2); /* ultrix 1.2 workaround */
883 return PerlIO_seek(fp, pos, whence) >= 0;
887 if (ckWARN(WARN_UNOPENED))
888 Perl_warner(aTHX_ WARN_UNOPENED, "seek() on unopened file");
890 SETERRNO(EBADF,RMS$_IFI);
895 Perl_do_sysseek(pTHX_ GV *gv, Off_t pos, int whence)
900 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io)))
901 return PerlLIO_lseek(PerlIO_fileno(fp), pos, whence);
904 if (ckWARN(WARN_UNOPENED))
905 Perl_warner(aTHX_ WARN_UNOPENED, "sysseek() on unopened file");
907 SETERRNO(EBADF,RMS$_IFI);
912 Perl_do_binmode(pTHX_ PerlIO *fp, int iotype, int flag)
915 Perl_croak(aTHX_ "panic: unsetting binmode"); /* Not implemented yet */
917 #if defined(atarist) || defined(__MINT__)
918 if (!PerlIO_flush(fp) && (fp->_flag |= _IOBIN))
923 if (PerlLIO_setmode(PerlIO_fileno(fp), OP_BINARY) != -1) {
924 #if defined(WIN32) && defined(__BORLANDC__)
925 /* The translation mode of the stream is maintained independent
926 * of the translation mode of the fd in the Borland RTL (heavy
927 * digging through their runtime sources reveal). User has to
928 * set the mode explicitly for the stream (though they don't
929 * document this anywhere). GSAR 97-5-24
931 PerlIO_seek(fp,0L,0);
932 ((FILE*)fp)->flags |= _F_BIN;
940 #if defined(USEMYBINMODE)
941 if (my_binmode(fp,iotype) != FALSE)
951 #if !defined(HAS_TRUNCATE) && !defined(HAS_CHSIZE) && defined(F_FREESP)
952 /* code courtesy of William Kucharski */
955 I32 my_chsize(fd, length)
956 I32 fd; /* file descriptor */
957 Off_t length; /* length to set file to */
962 if (PerlLIO_fstat(fd, &filebuf) < 0)
965 if (filebuf.st_size < length) {
967 /* extend file length */
969 if ((PerlLIO_lseek(fd, (length - 1), 0)) < 0)
972 /* write a "0" byte */
974 if ((PerlLIO_write(fd, "", 1)) != 1)
978 /* truncate length */
983 fl.l_type = F_WRLCK; /* write lock on file space */
986 * This relies on the UNDOCUMENTED F_FREESP argument to
987 * fcntl(2), which truncates the file so that it ends at the
988 * position indicated by fl.l_start.
990 * Will minor miracles never cease?
993 if (fcntl(fd, F_FREESP, &fl) < 0)
1000 #endif /* F_FREESP */
1003 Perl_do_print(pTHX_ register SV *sv, PerlIO *fp)
1005 register char *tmps;
1008 /* assuming fp is checked earlier */
1014 if (SvIOK(sv) && SvIVX(sv) != 0) {
1015 PerlIO_printf(fp, PL_ofmt, (NV)SvIVX(sv));
1016 return !PerlIO_error(fp);
1018 if ( (SvNOK(sv) && SvNVX(sv) != 0.0)
1019 || (looks_like_number(sv) && sv_2nv(sv) != 0.0) ) {
1020 PerlIO_printf(fp, PL_ofmt, SvNVX(sv));
1021 return !PerlIO_error(fp);
1024 switch (SvTYPE(sv)) {
1028 if (ckWARN(WARN_UNINITIALIZED))
1029 Perl_warner(aTHX_ WARN_UNINITIALIZED, PL_warn_uninit);
1037 PerlIO_printf(fp, "%"UVuf, (UV)SvUVX(sv));
1039 PerlIO_printf(fp, "%"IVdf, (IV)SvIVX(sv));
1040 return !PerlIO_error(fp);
1044 tmps = SvPV(sv, len);
1047 /* To detect whether the process is about to overstep its
1048 * filesize limit we would need getrlimit(). We could then
1049 * also transparently raise the limit with setrlimit() --
1050 * but only until the system hard limit/the filesystem limit,
1051 * at which we would get EPERM. Note that when using buffered
1052 * io the write failure can be delayed until the flush/close. --jhi */
1053 if (len && (PerlIO_write(fp,tmps,len) == 0 || PerlIO_error(fp)))
1055 return !PerlIO_error(fp);
1065 if (PL_op->op_flags & OPf_REF) {
1070 if (io && IoIFP(io)) {
1072 sv_setpv(PL_statname,"");
1073 PL_laststype = OP_STAT;
1074 return (PL_laststatval = PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache));
1077 if (tmpgv == PL_defgv)
1078 return PL_laststatval;
1079 if (ckWARN(WARN_UNOPENED))
1080 Perl_warner(aTHX_ WARN_UNOPENED, "Stat on unopened file <%s>",
1083 sv_setpv(PL_statname,"");
1084 return (PL_laststatval = -1);
1092 if (SvTYPE(sv) == SVt_PVGV) {
1096 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
1097 tmpgv = (GV*)SvRV(sv);
1103 sv_setpv(PL_statname, s);
1104 PL_laststype = OP_STAT;
1105 PL_laststatval = PerlLIO_stat(s, &PL_statcache);
1106 if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(s, '\n'))
1107 Perl_warner(aTHX_ WARN_NEWLINE, PL_warn_nl, "stat");
1108 return PL_laststatval;
1118 if (PL_op->op_flags & OPf_REF) {
1120 if (cGVOP == PL_defgv) {
1121 if (PL_laststype != OP_LSTAT)
1122 Perl_croak(aTHX_ "The stat preceding -l _ wasn't an lstat");
1123 return PL_laststatval;
1125 Perl_croak(aTHX_ "You can't use -l on a filehandle");
1128 PL_laststype = OP_LSTAT;
1132 sv_setpv(PL_statname,SvPV(sv, n_a));
1133 PL_laststatval = PerlLIO_lstat(SvPV(sv, n_a),&PL_statcache);
1134 if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(SvPV(sv, n_a), '\n'))
1135 Perl_warner(aTHX_ WARN_NEWLINE, PL_warn_nl, "lstat");
1136 return PL_laststatval;
1140 Perl_do_aexec(pTHX_ SV *really, register SV **mark, register SV **sp)
1142 return do_aexec5(really, mark, sp, 0, 0);
1146 Perl_do_aexec5(pTHX_ SV *really, register SV **mark, register SV **sp,
1147 int fd, int do_report)
1155 New(401,PL_Argv, sp - mark + 1, char*);
1157 while (++mark <= sp) {
1159 *a++ = SvPVx(*mark, n_a);
1164 if (*PL_Argv[0] != '/') /* will execvp use PATH? */
1165 TAINT_ENV(); /* testing IFS here is overkill, probably */
1166 if (really && *(tmps = SvPV(really, n_a)))
1167 PerlProc_execvp(tmps,PL_Argv);
1169 PerlProc_execvp(PL_Argv[0],PL_Argv);
1170 if (ckWARN(WARN_EXEC))
1171 Perl_warner(aTHX_ WARN_EXEC, "Can't exec \"%s\": %s",
1172 PL_Argv[0], Strerror(errno));
1176 PerlLIO_write(fd, (void*)&e, sizeof(int));
1185 Perl_do_execfree(pTHX)
1189 PL_Argv = Null(char **);
1197 #if !defined(OS2) && !defined(WIN32) && !defined(DJGPP) && !defined(EPOC)
1200 Perl_do_exec(pTHX_ char *cmd)
1202 return do_exec3(cmd,0,0);
1206 Perl_do_exec3(pTHX_ char *cmd, int fd, int do_report)
1212 while (*cmd && isSPACE(*cmd))
1215 /* save an extra exec if possible */
1218 if (strnEQ(cmd,PL_cshname,PL_cshlen) && strnEQ(cmd+PL_cshlen," -c",3)) {
1220 s = cmd+PL_cshlen+3;
1234 if (s[-1] == '\'') {
1236 PerlProc_execl(PL_cshname,"csh", flags,ncmd,(char*)0);
1244 /* see if there are shell metacharacters in it */
1246 if (*cmd == '.' && isSPACE(cmd[1]))
1249 if (strnEQ(cmd,"exec",4) && isSPACE(cmd[4]))
1252 for (s = cmd; *s && isALPHA(*s); s++) ; /* catch VAR=val gizmo */
1256 for (s = cmd; *s; s++) {
1257 if (*s != ' ' && !isALPHA(*s) && strchr("$&*(){}[]'\";\\|?<>~`\n",*s)) {
1258 if (*s == '\n' && !s[1]) {
1262 /* handle the 2>&1 construct at the end */
1263 if (*s == '>' && s[1] == '&' && s[2] == '1'
1264 && s > cmd + 1 && s[-1] == '2' && isSPACE(s[-2])
1265 && (!s[3] || isSPACE(s[3])))
1269 while (*t && isSPACE(*t))
1271 if (!*t && (dup2(1,2) != -1)) {
1277 PerlProc_execl(PL_sh_path, "sh", "-c", cmd, (char*)0);
1282 New(402,PL_Argv, (s - cmd) / 2 + 2, char*);
1283 PL_Cmd = savepvn(cmd, s-cmd);
1285 for (s = PL_Cmd; *s;) {
1286 while (*s && isSPACE(*s)) s++;
1289 while (*s && !isSPACE(*s)) s++;
1295 PerlProc_execvp(PL_Argv[0],PL_Argv);
1296 if (errno == ENOEXEC) { /* for system V NIH syndrome */
1304 if (ckWARN(WARN_EXEC))
1305 Perl_warner(aTHX_ WARN_EXEC, "Can't exec \"%s\": %s",
1306 PL_Argv[0], Strerror(errno));
1308 PerlLIO_write(fd, (void*)&e, sizeof(int));
1317 #endif /* OS2 || WIN32 */
1320 Perl_apply(pTHX_ I32 type, register SV **mark, register SV **sp)
1325 register I32 tot = 0;
1328 SV **oldmark = mark;
1331 #define APPLY_TAINT_PROPER() \
1333 if (PL_tainted) { TAINT_PROPER(what); } \
1336 /* This is a first heuristic; it doesn't catch tainting magic. */
1338 while (++mark <= sp) {
1339 if (SvTAINTED(*mark)) {
1349 APPLY_TAINT_PROPER();
1352 APPLY_TAINT_PROPER();
1354 while (++mark <= sp) {
1355 char *name = SvPVx(*mark, n_a);
1356 APPLY_TAINT_PROPER();
1357 if (PerlLIO_chmod(name, val))
1365 APPLY_TAINT_PROPER();
1366 if (sp - mark > 2) {
1367 val = SvIVx(*++mark);
1368 val2 = SvIVx(*++mark);
1369 APPLY_TAINT_PROPER();
1371 while (++mark <= sp) {
1372 char *name = SvPVx(*mark, n_a);
1373 APPLY_TAINT_PROPER();
1374 if (PerlLIO_chown(name, val, val2))
1381 XXX Should we make lchown() directly available from perl?
1382 For now, we'll let Configure test for HAS_LCHOWN, but do
1383 nothing in the core.
1389 APPLY_TAINT_PROPER();
1392 s = SvPVx(*++mark, n_a);
1394 if (*s == 'S' && s[1] == 'I' && s[2] == 'G')
1396 if (!(val = whichsig(s)))
1397 Perl_croak(aTHX_ "Unrecognized signal name \"%s\"",s);
1401 APPLY_TAINT_PROPER();
1404 /* kill() doesn't do process groups (job trees?) under VMS */
1405 if (val < 0) val = -val;
1406 if (val == SIGKILL) {
1407 # include <starlet.h>
1408 /* Use native sys$delprc() to insure that target process is
1409 * deleted; supervisor-mode images don't pay attention to
1410 * CRTL's emulation of Unix-style signals and kill()
1412 while (++mark <= sp) {
1413 I32 proc = SvIVx(*mark);
1414 register unsigned long int __vmssts;
1415 APPLY_TAINT_PROPER();
1416 if (!((__vmssts = sys$delprc(&proc,0)) & 1)) {
1420 case SS$_NOSUCHNODE:
1421 SETERRNO(ESRCH,__vmssts);
1424 SETERRNO(EPERM,__vmssts);
1427 SETERRNO(EVMSERR,__vmssts);
1436 while (++mark <= sp) {
1437 I32 proc = SvIVx(*mark);
1438 APPLY_TAINT_PROPER();
1440 if (PerlProc_killpg(proc,val)) /* BSD */
1442 if (PerlProc_kill(-proc,val)) /* SYSV */
1448 while (++mark <= sp) {
1449 I32 proc = SvIVx(*mark);
1450 APPLY_TAINT_PROPER();
1451 if (PerlProc_kill(proc, val))
1459 APPLY_TAINT_PROPER();
1461 while (++mark <= sp) {
1462 s = SvPVx(*mark, n_a);
1463 APPLY_TAINT_PROPER();
1464 if (PL_euid || PL_unsafe) {
1468 else { /* don't let root wipe out directories without -U */
1469 if (PerlLIO_lstat(s,&PL_statbuf) < 0 || S_ISDIR(PL_statbuf.st_mode))
1481 APPLY_TAINT_PROPER();
1482 if (sp - mark > 2) {
1483 #if defined(I_UTIME) || defined(VMS)
1484 struct utimbuf utbuf;
1492 Zero(&utbuf, sizeof utbuf, char);
1494 utbuf.actime = (Time_t)SvNVx(*++mark); /* time accessed */
1495 utbuf.modtime = (Time_t)SvNVx(*++mark); /* time modified */
1497 utbuf.actime = (Time_t)SvIVx(*++mark); /* time accessed */
1498 utbuf.modtime = (Time_t)SvIVx(*++mark); /* time modified */
1500 APPLY_TAINT_PROPER();
1502 while (++mark <= sp) {
1503 char *name = SvPVx(*mark, n_a);
1504 APPLY_TAINT_PROPER();
1505 if (PerlLIO_utime(name, &utbuf))
1516 #undef APPLY_TAINT_PROPER
1519 /* Do the permissions allow some operation? Assumes statcache already set. */
1520 #ifndef VMS /* VMS' cando is in vms.c */
1522 Perl_cando(pTHX_ Mode_t mode, Uid_t effective, register Stat_t *statbufp)
1523 /* Note: we use `effective' both for uids and gids.
1524 * Here we are betting on Uid_t being equal or wider than Gid_t. */
1527 /* [Comments and code from Len Reed]
1528 * MS-DOS "user" is similar to UNIX's "superuser," but can't write
1529 * to write-protected files. The execute permission bit is set
1530 * by the Miscrosoft C library stat() function for the following:
1535 * All files and directories are readable.
1536 * Directories and special files, e.g. "CON", cannot be
1538 * [Comment by Tom Dinger -- a directory can have the write-protect
1539 * bit set in the file system, but DOS permits changes to
1540 * the directory anyway. In addition, all bets are off
1541 * here for networked software, such as Novell and
1545 /* Atari stat() does pretty much the same thing. we set x_bit_set_in_stat
1546 * too so it will actually look into the files for magic numbers
1548 return (mode & statbufp->st_mode) ? TRUE : FALSE;
1550 #else /* ! DOSISH */
1551 if ((effective ? PL_euid : PL_uid) == 0) { /* root is special */
1552 if (mode == S_IXUSR) {
1553 if (statbufp->st_mode & 0111 || S_ISDIR(statbufp->st_mode))
1557 return TRUE; /* root reads and writes anything */
1560 if (statbufp->st_uid == (effective ? PL_euid : PL_uid) ) {
1561 if (statbufp->st_mode & mode)
1562 return TRUE; /* ok as "user" */
1564 else if (ingroup(statbufp->st_gid,effective)) {
1565 if (statbufp->st_mode & mode >> 3)
1566 return TRUE; /* ok as "group" */
1568 else if (statbufp->st_mode & mode >> 6)
1569 return TRUE; /* ok as "other" */
1571 #endif /* ! DOSISH */
1576 Perl_ingroup(pTHX_ Gid_t testgid, Uid_t effective)
1578 if (testgid == (effective ? PL_egid : PL_gid))
1580 #ifdef HAS_GETGROUPS
1585 Groups_t gary[NGROUPS];
1588 anum = getgroups(NGROUPS,gary);
1590 if (gary[anum] == testgid)
1597 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
1600 Perl_do_ipcget(pTHX_ I32 optype, SV **mark, SV **sp)
1606 key = (key_t)SvNVx(*++mark);
1607 n = (optype == OP_MSGGET) ? 0 : SvIVx(*++mark);
1608 flags = SvIVx(*++mark);
1614 return msgget(key, flags);
1618 return semget(key, n, flags);
1622 return shmget(key, n, flags);
1624 #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
1626 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
1629 return -1; /* should never happen */
1633 Perl_do_ipcctl(pTHX_ I32 optype, SV **mark, SV **sp)
1638 I32 id, n, cmd, infosize, getinfo;
1641 id = SvIVx(*++mark);
1642 n = (optype == OP_SEMCTL) ? SvIVx(*++mark) : 0;
1643 cmd = SvIVx(*++mark);
1646 getinfo = (cmd == IPC_STAT);
1652 if (cmd == IPC_STAT || cmd == IPC_SET)
1653 infosize = sizeof(struct msqid_ds);
1658 if (cmd == IPC_STAT || cmd == IPC_SET)
1659 infosize = sizeof(struct shmid_ds);
1665 if (cmd == IPC_STAT || cmd == IPC_SET)
1666 infosize = sizeof(struct semid_ds);
1667 else if (cmd == GETALL || cmd == SETALL)
1669 struct semid_ds semds;
1673 getinfo = (cmd == GETALL);
1674 if (Semctl(id, 0, IPC_STAT, semun) == -1)
1676 infosize = semds.sem_nsems * sizeof(short);
1677 /* "short" is technically wrong but much more portable
1678 than guessing about u_?short(_t)? */
1681 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
1685 #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
1687 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
1696 SvPV_force(astr, len);
1697 a = SvGROW(astr, infosize+1);
1701 a = SvPV(astr, len);
1702 if (len != infosize)
1703 Perl_croak(aTHX_ "Bad arg length for %s, is %lu, should be %ld",
1712 a = INT2PTR(char *,i); /* ouch */
1719 ret = msgctl(id, cmd, (struct msqid_ds *)a);
1725 union semun unsemds;
1727 unsemds.buf = (struct semid_ds *)a;
1728 ret = Semctl(id, n, cmd, unsemds);
1730 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
1737 ret = shmctl(id, cmd, (struct shmid_ds *)a);
1741 if (getinfo && ret >= 0) {
1742 SvCUR_set(astr, infosize);
1743 *SvEND(astr) = '\0';
1750 Perl_do_msgsnd(pTHX_ SV **mark, SV **sp)
1756 I32 id, msize, flags;
1759 id = SvIVx(*++mark);
1761 flags = SvIVx(*++mark);
1762 mbuf = SvPV(mstr, len);
1763 if ((msize = len - sizeof(long)) < 0)
1764 Perl_croak(aTHX_ "Arg too short for msgsnd");
1766 return msgsnd(id, (struct msgbuf *)mbuf, msize, flags);
1768 Perl_croak(aTHX_ "msgsnd not implemented");
1773 Perl_do_msgrcv(pTHX_ SV **mark, SV **sp)
1780 I32 id, msize, flags, ret;
1783 id = SvIVx(*++mark);
1785 msize = SvIVx(*++mark);
1786 mtype = (long)SvIVx(*++mark);
1787 flags = SvIVx(*++mark);
1788 SvPV_force(mstr, len);
1789 mbuf = SvGROW(mstr, sizeof(long)+msize+1);
1792 ret = msgrcv(id, (struct msgbuf *)mbuf, msize, mtype, flags);
1794 SvCUR_set(mstr, sizeof(long)+ret);
1795 *SvEND(mstr) = '\0';
1799 Perl_croak(aTHX_ "msgrcv not implemented");
1804 Perl_do_semop(pTHX_ SV **mark, SV **sp)
1813 id = SvIVx(*++mark);
1815 opbuf = SvPV(opstr, opsize);
1816 if (opsize < sizeof(struct sembuf)
1817 || (opsize % sizeof(struct sembuf)) != 0) {
1818 SETERRNO(EINVAL,LIB$_INVARG);
1822 return semop(id, (struct sembuf *)opbuf, opsize/sizeof(struct sembuf));
1824 Perl_croak(aTHX_ "semop not implemented");
1829 Perl_do_shmio(pTHX_ I32 optype, SV **mark, SV **sp)
1835 I32 id, mpos, msize;
1837 struct shmid_ds shmds;
1839 id = SvIVx(*++mark);
1841 mpos = SvIVx(*++mark);
1842 msize = SvIVx(*++mark);
1844 if (shmctl(id, IPC_STAT, &shmds) == -1)
1846 if (mpos < 0 || msize < 0 || mpos + msize > shmds.shm_segsz) {
1847 SETERRNO(EFAULT,SS$_ACCVIO); /* can't do as caller requested */
1850 shm = (char *)shmat(id, (char*)NULL, (optype == OP_SHMREAD) ? SHM_RDONLY : 0);
1851 if (shm == (char *)-1) /* I hate System V IPC, I really do */
1853 if (optype == OP_SHMREAD) {
1854 SvPV_force(mstr, len);
1855 mbuf = SvGROW(mstr, msize+1);
1857 Copy(shm + mpos, mbuf, msize, char);
1858 SvCUR_set(mstr, msize);
1859 *SvEND(mstr) = '\0';
1865 mbuf = SvPV(mstr, len);
1866 if ((n = len) > msize)
1868 Copy(mbuf, shm + mpos, n, char);
1870 memzero(shm + mpos + n, msize - n);
1874 Perl_croak(aTHX_ "shm I/O not implemented");
1878 #endif /* SYSV IPC */