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_gv == 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)
1149 #ifdef MACOS_TRADITIONAL
1150 Perl_croak(aTHX_ "exec? I'm not *that* kind of operating system");
1158 New(401,PL_Argv, sp - mark + 1, char*);
1160 while (++mark <= sp) {
1162 *a++ = SvPVx(*mark, n_a);
1167 if (*PL_Argv[0] != '/') /* will execvp use PATH? */
1168 TAINT_ENV(); /* testing IFS here is overkill, probably */
1169 if (really && *(tmps = SvPV(really, n_a)))
1170 PerlProc_execvp(tmps,PL_Argv);
1172 PerlProc_execvp(PL_Argv[0],PL_Argv);
1173 if (ckWARN(WARN_EXEC))
1174 Perl_warner(aTHX_ WARN_EXEC, "Can't exec \"%s\": %s",
1175 PL_Argv[0], Strerror(errno));
1179 PerlLIO_write(fd, (void*)&e, sizeof(int));
1189 Perl_do_execfree(pTHX)
1193 PL_Argv = Null(char **);
1201 #if !defined(OS2) && !defined(WIN32) && !defined(DJGPP) && !defined(EPOC) && !defined(MACOS_TRADITIONAL)
1204 Perl_do_exec(pTHX_ char *cmd)
1206 return do_exec3(cmd,0,0);
1210 Perl_do_exec3(pTHX_ char *cmd, int fd, int do_report)
1216 while (*cmd && isSPACE(*cmd))
1219 /* save an extra exec if possible */
1222 if (strnEQ(cmd,PL_cshname,PL_cshlen) && strnEQ(cmd+PL_cshlen," -c",3)) {
1224 s = cmd+PL_cshlen+3;
1238 if (s[-1] == '\'') {
1240 PerlProc_execl(PL_cshname,"csh", flags,ncmd,(char*)0);
1248 /* see if there are shell metacharacters in it */
1250 if (*cmd == '.' && isSPACE(cmd[1]))
1253 if (strnEQ(cmd,"exec",4) && isSPACE(cmd[4]))
1256 for (s = cmd; *s && isALPHA(*s); s++) ; /* catch VAR=val gizmo */
1260 for (s = cmd; *s; s++) {
1261 if (*s != ' ' && !isALPHA(*s) && strchr("$&*(){}[]'\";\\|?<>~`\n",*s)) {
1262 if (*s == '\n' && !s[1]) {
1266 /* handle the 2>&1 construct at the end */
1267 if (*s == '>' && s[1] == '&' && s[2] == '1'
1268 && s > cmd + 1 && s[-1] == '2' && isSPACE(s[-2])
1269 && (!s[3] || isSPACE(s[3])))
1273 while (*t && isSPACE(*t))
1275 if (!*t && (dup2(1,2) != -1)) {
1281 PerlProc_execl(PL_sh_path, "sh", "-c", cmd, (char*)0);
1286 New(402,PL_Argv, (s - cmd) / 2 + 2, char*);
1287 PL_Cmd = savepvn(cmd, s-cmd);
1289 for (s = PL_Cmd; *s;) {
1290 while (*s && isSPACE(*s)) s++;
1293 while (*s && !isSPACE(*s)) s++;
1299 PerlProc_execvp(PL_Argv[0],PL_Argv);
1300 if (errno == ENOEXEC) { /* for system V NIH syndrome */
1308 if (ckWARN(WARN_EXEC))
1309 Perl_warner(aTHX_ WARN_EXEC, "Can't exec \"%s\": %s",
1310 PL_Argv[0], Strerror(errno));
1312 PerlLIO_write(fd, (void*)&e, sizeof(int));
1321 #endif /* OS2 || WIN32 */
1324 Perl_apply(pTHX_ I32 type, register SV **mark, register SV **sp)
1329 register I32 tot = 0;
1332 SV **oldmark = mark;
1335 #define APPLY_TAINT_PROPER() \
1337 if (PL_tainted) { TAINT_PROPER(what); } \
1340 /* This is a first heuristic; it doesn't catch tainting magic. */
1342 while (++mark <= sp) {
1343 if (SvTAINTED(*mark)) {
1353 APPLY_TAINT_PROPER();
1356 APPLY_TAINT_PROPER();
1358 while (++mark <= sp) {
1359 char *name = SvPVx(*mark, n_a);
1360 APPLY_TAINT_PROPER();
1361 if (PerlLIO_chmod(name, val))
1369 APPLY_TAINT_PROPER();
1370 if (sp - mark > 2) {
1371 val = SvIVx(*++mark);
1372 val2 = SvIVx(*++mark);
1373 APPLY_TAINT_PROPER();
1375 while (++mark <= sp) {
1376 char *name = SvPVx(*mark, n_a);
1377 APPLY_TAINT_PROPER();
1378 if (PerlLIO_chown(name, val, val2))
1385 XXX Should we make lchown() directly available from perl?
1386 For now, we'll let Configure test for HAS_LCHOWN, but do
1387 nothing in the core.
1393 APPLY_TAINT_PROPER();
1396 s = SvPVx(*++mark, n_a);
1398 if (*s == 'S' && s[1] == 'I' && s[2] == 'G')
1400 if (!(val = whichsig(s)))
1401 Perl_croak(aTHX_ "Unrecognized signal name \"%s\"",s);
1405 APPLY_TAINT_PROPER();
1408 /* kill() doesn't do process groups (job trees?) under VMS */
1409 if (val < 0) val = -val;
1410 if (val == SIGKILL) {
1411 # include <starlet.h>
1412 /* Use native sys$delprc() to insure that target process is
1413 * deleted; supervisor-mode images don't pay attention to
1414 * CRTL's emulation of Unix-style signals and kill()
1416 while (++mark <= sp) {
1417 I32 proc = SvIVx(*mark);
1418 register unsigned long int __vmssts;
1419 APPLY_TAINT_PROPER();
1420 if (!((__vmssts = sys$delprc(&proc,0)) & 1)) {
1424 case SS$_NOSUCHNODE:
1425 SETERRNO(ESRCH,__vmssts);
1428 SETERRNO(EPERM,__vmssts);
1431 SETERRNO(EVMSERR,__vmssts);
1440 while (++mark <= sp) {
1441 I32 proc = SvIVx(*mark);
1442 APPLY_TAINT_PROPER();
1444 if (PerlProc_killpg(proc,val)) /* BSD */
1446 if (PerlProc_kill(-proc,val)) /* SYSV */
1452 while (++mark <= sp) {
1453 I32 proc = SvIVx(*mark);
1454 APPLY_TAINT_PROPER();
1455 if (PerlProc_kill(proc, val))
1463 APPLY_TAINT_PROPER();
1465 while (++mark <= sp) {
1466 s = SvPVx(*mark, n_a);
1467 APPLY_TAINT_PROPER();
1468 if (PL_euid || PL_unsafe) {
1472 else { /* don't let root wipe out directories without -U */
1473 if (PerlLIO_lstat(s,&PL_statbuf) < 0 || S_ISDIR(PL_statbuf.st_mode))
1485 APPLY_TAINT_PROPER();
1486 if (sp - mark > 2) {
1487 #if defined(I_UTIME) || defined(VMS)
1488 struct utimbuf utbuf;
1496 Zero(&utbuf, sizeof utbuf, char);
1498 utbuf.actime = (Time_t)SvNVx(*++mark); /* time accessed */
1499 utbuf.modtime = (Time_t)SvNVx(*++mark); /* time modified */
1501 utbuf.actime = (Time_t)SvIVx(*++mark); /* time accessed */
1502 utbuf.modtime = (Time_t)SvIVx(*++mark); /* time modified */
1504 APPLY_TAINT_PROPER();
1506 while (++mark <= sp) {
1507 char *name = SvPVx(*mark, n_a);
1508 APPLY_TAINT_PROPER();
1509 if (PerlLIO_utime(name, &utbuf))
1520 #undef APPLY_TAINT_PROPER
1523 /* Do the permissions allow some operation? Assumes statcache already set. */
1524 #ifndef VMS /* VMS' cando is in vms.c */
1526 Perl_cando(pTHX_ Mode_t mode, Uid_t effective, register Stat_t *statbufp)
1527 /* Note: we use `effective' both for uids and gids.
1528 * Here we are betting on Uid_t being equal or wider than Gid_t. */
1531 /* [Comments and code from Len Reed]
1532 * MS-DOS "user" is similar to UNIX's "superuser," but can't write
1533 * to write-protected files. The execute permission bit is set
1534 * by the Miscrosoft C library stat() function for the following:
1539 * All files and directories are readable.
1540 * Directories and special files, e.g. "CON", cannot be
1542 * [Comment by Tom Dinger -- a directory can have the write-protect
1543 * bit set in the file system, but DOS permits changes to
1544 * the directory anyway. In addition, all bets are off
1545 * here for networked software, such as Novell and
1549 /* Atari stat() does pretty much the same thing. we set x_bit_set_in_stat
1550 * too so it will actually look into the files for magic numbers
1552 return (mode & statbufp->st_mode) ? TRUE : FALSE;
1554 #else /* ! DOSISH */
1555 if ((effective ? PL_euid : PL_uid) == 0) { /* root is special */
1556 if (mode == S_IXUSR) {
1557 if (statbufp->st_mode & 0111 || S_ISDIR(statbufp->st_mode))
1561 return TRUE; /* root reads and writes anything */
1564 if (statbufp->st_uid == (effective ? PL_euid : PL_uid) ) {
1565 if (statbufp->st_mode & mode)
1566 return TRUE; /* ok as "user" */
1568 else if (ingroup(statbufp->st_gid,effective)) {
1569 if (statbufp->st_mode & mode >> 3)
1570 return TRUE; /* ok as "group" */
1572 else if (statbufp->st_mode & mode >> 6)
1573 return TRUE; /* ok as "other" */
1575 #endif /* ! DOSISH */
1580 Perl_ingroup(pTHX_ Gid_t testgid, Uid_t effective)
1582 #ifdef MACOS_TRADITIONAL
1583 /* This is simply not correct for AppleShare, but fix it yerself. */
1586 if (testgid == (effective ? PL_egid : PL_gid))
1588 #ifdef HAS_GETGROUPS
1593 Groups_t gary[NGROUPS];
1596 anum = getgroups(NGROUPS,gary);
1598 if (gary[anum] == testgid)
1606 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
1609 Perl_do_ipcget(pTHX_ I32 optype, SV **mark, SV **sp)
1615 key = (key_t)SvNVx(*++mark);
1616 n = (optype == OP_MSGGET) ? 0 : SvIVx(*++mark);
1617 flags = SvIVx(*++mark);
1623 return msgget(key, flags);
1627 return semget(key, n, flags);
1631 return shmget(key, n, flags);
1633 #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
1635 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
1638 return -1; /* should never happen */
1642 Perl_do_ipcctl(pTHX_ I32 optype, SV **mark, SV **sp)
1647 I32 id, n, cmd, infosize, getinfo;
1650 id = SvIVx(*++mark);
1651 n = (optype == OP_SEMCTL) ? SvIVx(*++mark) : 0;
1652 cmd = SvIVx(*++mark);
1655 getinfo = (cmd == IPC_STAT);
1661 if (cmd == IPC_STAT || cmd == IPC_SET)
1662 infosize = sizeof(struct msqid_ds);
1667 if (cmd == IPC_STAT || cmd == IPC_SET)
1668 infosize = sizeof(struct shmid_ds);
1674 if (cmd == IPC_STAT || cmd == IPC_SET)
1675 infosize = sizeof(struct semid_ds);
1676 else if (cmd == GETALL || cmd == SETALL)
1678 struct semid_ds semds;
1682 getinfo = (cmd == GETALL);
1683 if (Semctl(id, 0, IPC_STAT, semun) == -1)
1685 infosize = semds.sem_nsems * sizeof(short);
1686 /* "short" is technically wrong but much more portable
1687 than guessing about u_?short(_t)? */
1690 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
1694 #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
1696 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
1705 SvPV_force(astr, len);
1706 a = SvGROW(astr, infosize+1);
1710 a = SvPV(astr, len);
1711 if (len != infosize)
1712 Perl_croak(aTHX_ "Bad arg length for %s, is %lu, should be %ld",
1721 a = INT2PTR(char *,i); /* ouch */
1728 ret = msgctl(id, cmd, (struct msqid_ds *)a);
1734 union semun unsemds;
1736 unsemds.buf = (struct semid_ds *)a;
1737 ret = Semctl(id, n, cmd, unsemds);
1739 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
1746 ret = shmctl(id, cmd, (struct shmid_ds *)a);
1750 if (getinfo && ret >= 0) {
1751 SvCUR_set(astr, infosize);
1752 *SvEND(astr) = '\0';
1759 Perl_do_msgsnd(pTHX_ SV **mark, SV **sp)
1765 I32 id, msize, flags;
1768 id = SvIVx(*++mark);
1770 flags = SvIVx(*++mark);
1771 mbuf = SvPV(mstr, len);
1772 if ((msize = len - sizeof(long)) < 0)
1773 Perl_croak(aTHX_ "Arg too short for msgsnd");
1775 return msgsnd(id, (struct msgbuf *)mbuf, msize, flags);
1777 Perl_croak(aTHX_ "msgsnd not implemented");
1782 Perl_do_msgrcv(pTHX_ SV **mark, SV **sp)
1789 I32 id, msize, flags, ret;
1792 id = SvIVx(*++mark);
1794 msize = SvIVx(*++mark);
1795 mtype = (long)SvIVx(*++mark);
1796 flags = SvIVx(*++mark);
1797 SvPV_force(mstr, len);
1798 mbuf = SvGROW(mstr, sizeof(long)+msize+1);
1801 ret = msgrcv(id, (struct msgbuf *)mbuf, msize, mtype, flags);
1803 SvCUR_set(mstr, sizeof(long)+ret);
1804 *SvEND(mstr) = '\0';
1808 Perl_croak(aTHX_ "msgrcv not implemented");
1813 Perl_do_semop(pTHX_ SV **mark, SV **sp)
1822 id = SvIVx(*++mark);
1824 opbuf = SvPV(opstr, opsize);
1825 if (opsize < sizeof(struct sembuf)
1826 || (opsize % sizeof(struct sembuf)) != 0) {
1827 SETERRNO(EINVAL,LIB$_INVARG);
1831 return semop(id, (struct sembuf *)opbuf, opsize/sizeof(struct sembuf));
1833 Perl_croak(aTHX_ "semop not implemented");
1838 Perl_do_shmio(pTHX_ I32 optype, SV **mark, SV **sp)
1844 I32 id, mpos, msize;
1846 struct shmid_ds shmds;
1848 id = SvIVx(*++mark);
1850 mpos = SvIVx(*++mark);
1851 msize = SvIVx(*++mark);
1853 if (shmctl(id, IPC_STAT, &shmds) == -1)
1855 if (mpos < 0 || msize < 0 || mpos + msize > shmds.shm_segsz) {
1856 SETERRNO(EFAULT,SS$_ACCVIO); /* can't do as caller requested */
1859 shm = (char *)shmat(id, (char*)NULL, (optype == OP_SHMREAD) ? SHM_RDONLY : 0);
1860 if (shm == (char *)-1) /* I hate System V IPC, I really do */
1862 if (optype == OP_SHMREAD) {
1863 SvPV_force(mstr, len);
1864 mbuf = SvGROW(mstr, msize+1);
1866 Copy(shm + mpos, mbuf, msize, char);
1867 SvCUR_set(mstr, msize);
1868 *SvEND(mstr) = '\0';
1874 mbuf = SvPV(mstr, len);
1875 if ((n = len) > msize)
1877 Copy(mbuf, shm + mpos, n, char);
1879 memzero(shm + mpos + n, msize - n);
1883 Perl_croak(aTHX_ "shm I/O not implemented");
1887 #endif /* SYSV IPC */