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
489 PL_argvoutgv = gv_fetchpv("ARGVOUT",TRUE,SVt_PVIO);
490 if (PL_filemode & (S_ISUID|S_ISGID)) {
491 PerlIO_flush(IoIFP(GvIOn(PL_argvoutgv))); /* chmod must follow last write */
493 (void)fchmod(PL_lastfd,PL_filemode);
495 (void)PerlLIO_chmod(PL_oldname,PL_filemode);
499 while (av_len(GvAV(gv)) >= 0) {
502 sv = av_shift(GvAV(gv));
504 sv_setsv(GvSV(gv),sv);
505 SvSETMAGIC(GvSV(gv));
506 PL_oldname = SvPVx(GvSV(gv), oldlen);
507 if (do_open(gv,PL_oldname,oldlen,PL_inplace!=0,O_RDONLY,0,Nullfp)) {
509 TAINT_PROPER("inplace open");
510 if (oldlen == 1 && *PL_oldname == '-') {
511 setdefout(gv_fetchpv("STDOUT",TRUE,SVt_PVIO));
512 return IoIFP(GvIOp(gv));
514 #ifndef FLEXFILENAMES
515 filedev = PL_statbuf.st_dev;
516 fileino = PL_statbuf.st_ino;
518 PL_filemode = PL_statbuf.st_mode;
519 fileuid = PL_statbuf.st_uid;
520 filegid = PL_statbuf.st_gid;
521 if (!S_ISREG(PL_filemode)) {
522 if (ckWARN_d(WARN_INPLACE))
523 Perl_warner(aTHX_ WARN_INPLACE,
524 "Can't do inplace edit: %s is not a regular file",
530 char *star = strchr(PL_inplace, '*');
532 char *begin = PL_inplace;
533 sv_setpvn(sv, "", 0);
535 sv_catpvn(sv, begin, star - begin);
536 sv_catpvn(sv, PL_oldname, oldlen);
538 } while ((star = strchr(begin, '*')));
543 sv_catpv(sv,PL_inplace);
545 #ifndef FLEXFILENAMES
546 if (PerlLIO_stat(SvPVX(sv),&PL_statbuf) >= 0
547 && PL_statbuf.st_dev == filedev
548 && PL_statbuf.st_ino == fileino
550 || (_djstat_fail_bits & _STFAIL_TRUENAME)!=0
554 if (ckWARN_d(WARN_INPLACE))
555 Perl_warner(aTHX_ WARN_INPLACE,
556 "Can't do inplace edit: %s would not be unique",
563 #if !defined(DOSISH) && !defined(CYGWIN)
564 if (PerlLIO_rename(PL_oldname,SvPVX(sv)) < 0) {
565 if (ckWARN_d(WARN_INPLACE))
566 Perl_warner(aTHX_ WARN_INPLACE,
567 "Can't rename %s to %s: %s, skipping file",
568 PL_oldname, SvPVX(sv), Strerror(errno) );
574 (void)PerlLIO_unlink(SvPVX(sv));
575 (void)PerlLIO_rename(PL_oldname,SvPVX(sv));
576 do_open(gv,SvPVX(sv),SvCUR(sv),PL_inplace!=0,O_RDONLY,0,Nullfp);
579 (void)UNLINK(SvPVX(sv));
580 if (link(PL_oldname,SvPVX(sv)) < 0) {
581 if (ckWARN_d(WARN_INPLACE))
582 Perl_warner(aTHX_ WARN_INPLACE,
583 "Can't rename %s to %s: %s, skipping file",
584 PL_oldname, SvPVX(sv), Strerror(errno) );
588 (void)UNLINK(PL_oldname);
592 #if !defined(DOSISH) && !defined(AMIGAOS)
593 # ifndef VMS /* Don't delete; use automatic file versioning */
594 if (UNLINK(PL_oldname) < 0) {
595 if (ckWARN_d(WARN_INPLACE))
596 Perl_warner(aTHX_ WARN_INPLACE,
597 "Can't remove %s: %s, skipping file",
598 PL_oldname, Strerror(errno) );
604 Perl_croak(aTHX_ "Can't do inplace edit without backup");
608 sv_setpvn(sv,">",!PL_inplace);
609 sv_catpvn(sv,PL_oldname,oldlen);
610 SETERRNO(0,0); /* in case sprintf set errno */
612 if (!do_open(PL_argvoutgv,SvPVX(sv),SvCUR(sv),PL_inplace!=0,
613 O_WRONLY|O_CREAT|O_TRUNC,0,Nullfp)) {
615 if (!do_open(PL_argvoutgv,SvPVX(sv),SvCUR(sv),PL_inplace!=0,
616 O_WRONLY|O_CREAT|OPEN_EXCL,0666,Nullfp)) {
618 if (ckWARN_d(WARN_INPLACE))
619 Perl_warner(aTHX_ WARN_INPLACE, "Can't do inplace edit on %s: %s",
620 PL_oldname, Strerror(errno) );
624 setdefout(PL_argvoutgv);
625 PL_lastfd = PerlIO_fileno(IoIFP(GvIOp(PL_argvoutgv)));
626 (void)PerlLIO_fstat(PL_lastfd,&PL_statbuf);
628 (void)fchmod(PL_lastfd,PL_filemode);
630 # if !(defined(WIN32) && defined(__BORLANDC__))
631 /* Borland runtime creates a readonly file! */
632 (void)PerlLIO_chmod(PL_oldname,PL_filemode);
635 if (fileuid != PL_statbuf.st_uid || filegid != PL_statbuf.st_gid) {
637 (void)fchown(PL_lastfd,fileuid,filegid);
640 (void)PerlLIO_chown(PL_oldname,fileuid,filegid);
645 return IoIFP(GvIOp(gv));
649 if (ckWARN_d(WARN_INPLACE)) {
650 if (!S_ISREG(PL_statbuf.st_mode))
651 Perl_warner(aTHX_ WARN_INPLACE,
652 "Can't do inplace edit: %s is not a regular file",
655 Perl_warner(aTHX_ WARN_INPLACE, "Can't open %s: %s\n",
656 PL_oldname, Strerror(errno));
661 (void)do_close(PL_argvoutgv,FALSE);
662 setdefout(gv_fetchpv("STDOUT",TRUE,SVt_PVIO));
669 Perl_do_pipe(pTHX_ SV *sv, GV *rgv, GV *wgv)
688 if (PerlProc_pipe(fd) < 0)
690 IoIFP(rstio) = PerlIO_fdopen(fd[0], "r");
691 IoOFP(wstio) = PerlIO_fdopen(fd[1], "w");
692 IoIFP(wstio) = IoOFP(wstio);
695 if (!IoIFP(rstio) || !IoOFP(wstio)) {
696 if (IoIFP(rstio)) PerlIO_close(IoIFP(rstio));
697 else PerlLIO_close(fd[0]);
698 if (IoOFP(wstio)) PerlIO_close(IoOFP(wstio));
699 else PerlLIO_close(fd[1]);
703 sv_setsv(sv,&PL_sv_yes);
707 sv_setsv(sv,&PL_sv_undef);
712 /* explicit renamed to avoid C++ conflict -- kja */
714 Perl_do_close(pTHX_ GV *gv, bool not_implicit)
721 if (!gv || SvTYPE(gv) != SVt_PVGV) {
723 SETERRNO(EBADF,SS$_IVCHAN);
727 if (!io) { /* never opened */
730 if (ckWARN(WARN_UNOPENED))
731 Perl_warner(aTHX_ WARN_UNOPENED,
732 "Close on unopened file <%s>",GvENAME(gv));
733 SETERRNO(EBADF,SS$_IVCHAN);
737 retval = io_close(io, not_implicit);
741 IoLINES_LEFT(io) = IoPAGE_LEN(io);
748 Perl_io_close(pTHX_ IO *io, bool not_implicit)
754 if (IoTYPE(io) == '|') {
755 status = PerlProc_pclose(IoIFP(io));
757 STATUS_NATIVE_SET(status);
758 retval = (STATUS_POSIX == 0);
761 retval = (status != -1);
764 else if (IoTYPE(io) == '-')
767 if (IoOFP(io) && IoOFP(io) != IoIFP(io)) { /* a socket */
768 retval = (PerlIO_close(IoOFP(io)) != EOF);
769 PerlIO_close(IoIFP(io)); /* clear stdio, fd already closed */
772 retval = (PerlIO_close(IoIFP(io)) != EOF);
774 IoOFP(io) = IoIFP(io) = Nullfp;
776 else if (not_implicit) {
777 SETERRNO(EBADF,SS$_IVCHAN);
784 Perl_do_eof(pTHX_ GV *gv)
794 else if (ckWARN(WARN_IO)
795 && (IoTYPE(io) == '>' || IoIFP(io) == PerlIO_stdout()
796 || IoIFP(io) == PerlIO_stderr()))
798 SV* sv = sv_newmortal();
799 gv_efullname3(sv, gv, Nullch);
800 Perl_warner(aTHX_ WARN_IO, "Filehandle %s opened only for output",
806 if (PerlIO_has_cntptr(IoIFP(io))) { /* (the code works without this) */
807 if (PerlIO_get_cnt(IoIFP(io)) > 0) /* cheat a little, since */
808 return FALSE; /* this is the most usual case */
811 ch = PerlIO_getc(IoIFP(io));
813 (void)PerlIO_ungetc(IoIFP(io),ch);
816 if (PerlIO_has_cntptr(IoIFP(io)) && PerlIO_canset_cnt(IoIFP(io))) {
817 if (PerlIO_get_cnt(IoIFP(io)) < -1)
818 PerlIO_set_cnt(IoIFP(io),-1);
820 if (PL_op->op_flags & OPf_SPECIAL) { /* not necessarily a real EOF yet? */
821 if (!nextargv(PL_argvgv)) /* get another fp handy */
825 return TRUE; /* normal fp, definitely end of file */
831 Perl_do_tell(pTHX_ GV *gv)
836 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io))) {
837 #ifdef ULTRIX_STDIO_BOTCH
839 (void)PerlIO_seek(fp, 0L, 2); /* ultrix 1.2 workaround */
841 return PerlIO_tell(fp);
845 if (ckWARN(WARN_UNOPENED))
846 Perl_warner(aTHX_ WARN_UNOPENED, "tell() on unopened file");
848 SETERRNO(EBADF,RMS$_IFI);
853 Perl_do_seek(pTHX_ GV *gv, Off_t pos, int whence)
858 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io))) {
859 #ifdef ULTRIX_STDIO_BOTCH
861 (void)PerlIO_seek(fp, 0L, 2); /* ultrix 1.2 workaround */
863 return PerlIO_seek(fp, pos, whence) >= 0;
867 if (ckWARN(WARN_UNOPENED))
868 Perl_warner(aTHX_ WARN_UNOPENED, "seek() on unopened file");
870 SETERRNO(EBADF,RMS$_IFI);
875 Perl_do_sysseek(pTHX_ GV *gv, Off_t pos, int whence)
880 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io)))
881 return PerlLIO_lseek(PerlIO_fileno(fp), pos, whence);
884 if (ckWARN(WARN_UNOPENED))
885 Perl_warner(aTHX_ WARN_UNOPENED, "sysseek() on unopened file");
887 SETERRNO(EBADF,RMS$_IFI);
892 Perl_do_binmode(pTHX_ PerlIO *fp, int iotype, int flag)
895 Perl_croak(aTHX_ "panic: unsetting binmode"); /* Not implemented yet */
897 #if defined(atarist) || defined(__MINT__)
898 if (!PerlIO_flush(fp) && (fp->_flag |= _IOBIN))
903 if (PerlLIO_setmode(PerlIO_fileno(fp), OP_BINARY) != -1) {
904 #if defined(WIN32) && defined(__BORLANDC__)
905 /* The translation mode of the stream is maintained independent
906 * of the translation mode of the fd in the Borland RTL (heavy
907 * digging through their runtime sources reveal). User has to
908 * set the mode explicitly for the stream (though they don't
909 * document this anywhere). GSAR 97-5-24
911 PerlIO_seek(fp,0L,0);
912 ((FILE*)fp)->flags |= _F_BIN;
920 #if defined(USEMYBINMODE)
921 if (my_binmode(fp,iotype) != FALSE)
931 #if !defined(HAS_TRUNCATE) && !defined(HAS_CHSIZE) && defined(F_FREESP)
932 /* code courtesy of William Kucharski */
935 I32 my_chsize(fd, length)
936 I32 fd; /* file descriptor */
937 Off_t length; /* length to set file to */
942 if (PerlLIO_fstat(fd, &filebuf) < 0)
945 if (filebuf.st_size < length) {
947 /* extend file length */
949 if ((PerlLIO_lseek(fd, (length - 1), 0)) < 0)
952 /* write a "0" byte */
954 if ((PerlLIO_write(fd, "", 1)) != 1)
958 /* truncate length */
963 fl.l_type = F_WRLCK; /* write lock on file space */
966 * This relies on the UNDOCUMENTED F_FREESP argument to
967 * fcntl(2), which truncates the file so that it ends at the
968 * position indicated by fl.l_start.
970 * Will minor miracles never cease?
973 if (fcntl(fd, F_FREESP, &fl) < 0)
980 #endif /* F_FREESP */
983 Perl_do_print(pTHX_ register SV *sv, PerlIO *fp)
988 /* assuming fp is checked earlier */
994 if (SvIOK(sv) && SvIVX(sv) != 0) {
995 PerlIO_printf(fp, PL_ofmt, (NV)SvIVX(sv));
996 return !PerlIO_error(fp);
998 if ( (SvNOK(sv) && SvNVX(sv) != 0.0)
999 || (looks_like_number(sv) && sv_2nv(sv) != 0.0) ) {
1000 PerlIO_printf(fp, PL_ofmt, SvNVX(sv));
1001 return !PerlIO_error(fp);
1004 switch (SvTYPE(sv)) {
1008 if (ckWARN(WARN_UNINITIALIZED))
1009 Perl_warner(aTHX_ WARN_UNINITIALIZED, PL_warn_uninit);
1017 PerlIO_printf(fp, "%"UVuf, (UV)SvUVX(sv));
1019 PerlIO_printf(fp, "%"IVdf, (IV)SvIVX(sv));
1020 return !PerlIO_error(fp);
1024 tmps = SvPV(sv, len);
1027 /* To detect whether the process is about to overstep its
1028 * filesize limit we would need getrlimit(). We could then
1029 * also transparently raise the limit with setrlimit() --
1030 * but only until the system hard limit/the filesystem limit,
1031 * at which we would get EPERM. Note that when using buffered
1032 * io the write failure can be delayed until the flush/close. --jhi */
1033 if (len && (PerlIO_write(fp,tmps,len) == 0 || PerlIO_error(fp)))
1035 return !PerlIO_error(fp);
1045 if (PL_op->op_flags & OPf_REF) {
1047 tmpgv = (GV*)cSVOP->op_sv;
1050 if (io && IoIFP(io)) {
1052 sv_setpv(PL_statname,"");
1053 PL_laststype = OP_STAT;
1054 return (PL_laststatval = PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache));
1057 if (tmpgv == PL_defgv)
1058 return PL_laststatval;
1059 if (ckWARN(WARN_UNOPENED))
1060 Perl_warner(aTHX_ WARN_UNOPENED, "Stat on unopened file <%s>",
1063 sv_setpv(PL_statname,"");
1064 return (PL_laststatval = -1);
1072 if (SvTYPE(sv) == SVt_PVGV) {
1076 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
1077 tmpgv = (GV*)SvRV(sv);
1083 sv_setpv(PL_statname, s);
1084 PL_laststype = OP_STAT;
1085 PL_laststatval = PerlLIO_stat(s, &PL_statcache);
1086 if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(s, '\n'))
1087 Perl_warner(aTHX_ WARN_NEWLINE, PL_warn_nl, "stat");
1088 return PL_laststatval;
1098 if (PL_op->op_flags & OPf_REF) {
1100 if ((GV*)cSVOP->op_sv == PL_defgv) {
1101 if (PL_laststype != OP_LSTAT)
1102 Perl_croak(aTHX_ "The stat preceding -l _ wasn't an lstat");
1103 return PL_laststatval;
1105 Perl_croak(aTHX_ "You can't use -l on a filehandle");
1108 PL_laststype = OP_LSTAT;
1112 sv_setpv(PL_statname,SvPV(sv, n_a));
1113 PL_laststatval = PerlLIO_lstat(SvPV(sv, n_a),&PL_statcache);
1114 if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(SvPV(sv, n_a), '\n'))
1115 Perl_warner(aTHX_ WARN_NEWLINE, PL_warn_nl, "lstat");
1116 return PL_laststatval;
1120 Perl_do_aexec(pTHX_ SV *really, register SV **mark, register SV **sp)
1122 return do_aexec5(really, mark, sp, 0, 0);
1126 Perl_do_aexec5(pTHX_ SV *really, register SV **mark, register SV **sp,
1127 int fd, int do_report)
1129 #ifdef MACOS_TRADITIONAL
1130 Perl_croak(aTHX_ "exec? I'm not *that* kind of operating system");
1138 New(401,PL_Argv, sp - mark + 1, char*);
1140 while (++mark <= sp) {
1142 *a++ = SvPVx(*mark, n_a);
1147 if (*PL_Argv[0] != '/') /* will execvp use PATH? */
1148 TAINT_ENV(); /* testing IFS here is overkill, probably */
1149 if (really && *(tmps = SvPV(really, n_a)))
1150 PerlProc_execvp(tmps,PL_Argv);
1152 PerlProc_execvp(PL_Argv[0],PL_Argv);
1153 if (ckWARN(WARN_EXEC))
1154 Perl_warner(aTHX_ WARN_EXEC, "Can't exec \"%s\": %s",
1155 PL_Argv[0], Strerror(errno));
1159 PerlLIO_write(fd, (void*)&e, sizeof(int));
1169 Perl_do_execfree(pTHX)
1173 PL_Argv = Null(char **);
1181 #if !defined(OS2) && !defined(WIN32) && !defined(DJGPP) && !defined(EPOC) && !defined(MACOS_TRADITIONAL)
1184 Perl_do_exec(pTHX_ char *cmd)
1186 return do_exec3(cmd,0,0);
1190 Perl_do_exec3(pTHX_ char *cmd, int fd, int do_report)
1196 while (*cmd && isSPACE(*cmd))
1199 /* save an extra exec if possible */
1202 if (strnEQ(cmd,PL_cshname,PL_cshlen) && strnEQ(cmd+PL_cshlen," -c",3)) {
1204 s = cmd+PL_cshlen+3;
1218 if (s[-1] == '\'') {
1220 PerlProc_execl(PL_cshname,"csh", flags,ncmd,(char*)0);
1228 /* see if there are shell metacharacters in it */
1230 if (*cmd == '.' && isSPACE(cmd[1]))
1233 if (strnEQ(cmd,"exec",4) && isSPACE(cmd[4]))
1236 for (s = cmd; *s && isALPHA(*s); s++) ; /* catch VAR=val gizmo */
1240 for (s = cmd; *s; s++) {
1241 if (*s != ' ' && !isALPHA(*s) && strchr("$&*(){}[]'\";\\|?<>~`\n",*s)) {
1242 if (*s == '\n' && !s[1]) {
1246 /* handle the 2>&1 construct at the end */
1247 if (*s == '>' && s[1] == '&' && s[2] == '1'
1248 && s > cmd + 1 && s[-1] == '2' && isSPACE(s[-2])
1249 && (!s[3] || isSPACE(s[3])))
1253 while (*t && isSPACE(*t))
1255 if (!*t && (dup2(1,2) != -1)) {
1261 PerlProc_execl(PL_sh_path, "sh", "-c", cmd, (char*)0);
1266 New(402,PL_Argv, (s - cmd) / 2 + 2, char*);
1267 PL_Cmd = savepvn(cmd, s-cmd);
1269 for (s = PL_Cmd; *s;) {
1270 while (*s && isSPACE(*s)) s++;
1273 while (*s && !isSPACE(*s)) s++;
1279 PerlProc_execvp(PL_Argv[0],PL_Argv);
1280 if (errno == ENOEXEC) { /* for system V NIH syndrome */
1288 if (ckWARN(WARN_EXEC))
1289 Perl_warner(aTHX_ WARN_EXEC, "Can't exec \"%s\": %s",
1290 PL_Argv[0], Strerror(errno));
1292 PerlLIO_write(fd, (void*)&e, sizeof(int));
1301 #endif /* OS2 || WIN32 */
1304 Perl_apply(pTHX_ I32 type, register SV **mark, register SV **sp)
1309 register I32 tot = 0;
1312 SV **oldmark = mark;
1315 #define APPLY_TAINT_PROPER() \
1317 if (PL_tainted) { TAINT_PROPER(what); } \
1320 /* This is a first heuristic; it doesn't catch tainting magic. */
1322 while (++mark <= sp) {
1323 if (SvTAINTED(*mark)) {
1333 APPLY_TAINT_PROPER();
1336 APPLY_TAINT_PROPER();
1338 while (++mark <= sp) {
1339 char *name = SvPVx(*mark, n_a);
1340 APPLY_TAINT_PROPER();
1341 if (PerlLIO_chmod(name, val))
1349 APPLY_TAINT_PROPER();
1350 if (sp - mark > 2) {
1351 val = SvIVx(*++mark);
1352 val2 = SvIVx(*++mark);
1353 APPLY_TAINT_PROPER();
1355 while (++mark <= sp) {
1356 char *name = SvPVx(*mark, n_a);
1357 APPLY_TAINT_PROPER();
1358 if (PerlLIO_chown(name, val, val2))
1365 XXX Should we make lchown() directly available from perl?
1366 For now, we'll let Configure test for HAS_LCHOWN, but do
1367 nothing in the core.
1373 APPLY_TAINT_PROPER();
1376 s = SvPVx(*++mark, n_a);
1378 if (*s == 'S' && s[1] == 'I' && s[2] == 'G')
1380 if (!(val = whichsig(s)))
1381 Perl_croak(aTHX_ "Unrecognized signal name \"%s\"",s);
1385 APPLY_TAINT_PROPER();
1388 /* kill() doesn't do process groups (job trees?) under VMS */
1389 if (val < 0) val = -val;
1390 if (val == SIGKILL) {
1391 # include <starlet.h>
1392 /* Use native sys$delprc() to insure that target process is
1393 * deleted; supervisor-mode images don't pay attention to
1394 * CRTL's emulation of Unix-style signals and kill()
1396 while (++mark <= sp) {
1397 I32 proc = SvIVx(*mark);
1398 register unsigned long int __vmssts;
1399 APPLY_TAINT_PROPER();
1400 if (!((__vmssts = sys$delprc(&proc,0)) & 1)) {
1404 case SS$_NOSUCHNODE:
1405 SETERRNO(ESRCH,__vmssts);
1408 SETERRNO(EPERM,__vmssts);
1411 SETERRNO(EVMSERR,__vmssts);
1420 while (++mark <= sp) {
1421 I32 proc = SvIVx(*mark);
1422 APPLY_TAINT_PROPER();
1424 if (PerlProc_killpg(proc,val)) /* BSD */
1426 if (PerlProc_kill(-proc,val)) /* SYSV */
1432 while (++mark <= sp) {
1433 I32 proc = SvIVx(*mark);
1434 APPLY_TAINT_PROPER();
1435 if (PerlProc_kill(proc, val))
1443 APPLY_TAINT_PROPER();
1445 while (++mark <= sp) {
1446 s = SvPVx(*mark, n_a);
1447 APPLY_TAINT_PROPER();
1448 if (PL_euid || PL_unsafe) {
1452 else { /* don't let root wipe out directories without -U */
1453 if (PerlLIO_lstat(s,&PL_statbuf) < 0 || S_ISDIR(PL_statbuf.st_mode))
1465 APPLY_TAINT_PROPER();
1466 if (sp - mark > 2) {
1467 #if defined(I_UTIME) || defined(VMS)
1468 struct utimbuf utbuf;
1476 Zero(&utbuf, sizeof utbuf, char);
1478 utbuf.actime = (Time_t)SvNVx(*++mark); /* time accessed */
1479 utbuf.modtime = (Time_t)SvNVx(*++mark); /* time modified */
1481 utbuf.actime = (Time_t)SvIVx(*++mark); /* time accessed */
1482 utbuf.modtime = (Time_t)SvIVx(*++mark); /* time modified */
1484 APPLY_TAINT_PROPER();
1486 while (++mark <= sp) {
1487 char *name = SvPVx(*mark, n_a);
1488 APPLY_TAINT_PROPER();
1489 if (PerlLIO_utime(name, &utbuf))
1500 #undef APPLY_TAINT_PROPER
1503 /* Do the permissions allow some operation? Assumes statcache already set. */
1504 #ifndef VMS /* VMS' cando is in vms.c */
1506 Perl_cando(pTHX_ Mode_t mode, Uid_t effective, register Stat_t *statbufp)
1507 /* Note: we use `effective' both for uids and gids.
1508 * Here we are betting on Uid_t being equal or wider than Gid_t. */
1511 /* [Comments and code from Len Reed]
1512 * MS-DOS "user" is similar to UNIX's "superuser," but can't write
1513 * to write-protected files. The execute permission bit is set
1514 * by the Miscrosoft C library stat() function for the following:
1519 * All files and directories are readable.
1520 * Directories and special files, e.g. "CON", cannot be
1522 * [Comment by Tom Dinger -- a directory can have the write-protect
1523 * bit set in the file system, but DOS permits changes to
1524 * the directory anyway. In addition, all bets are off
1525 * here for networked software, such as Novell and
1529 /* Atari stat() does pretty much the same thing. we set x_bit_set_in_stat
1530 * too so it will actually look into the files for magic numbers
1532 return (mode & statbufp->st_mode) ? TRUE : FALSE;
1534 #else /* ! DOSISH */
1535 if ((effective ? PL_euid : PL_uid) == 0) { /* root is special */
1536 if (mode == S_IXUSR) {
1537 if (statbufp->st_mode & 0111 || S_ISDIR(statbufp->st_mode))
1541 return TRUE; /* root reads and writes anything */
1544 if (statbufp->st_uid == (effective ? PL_euid : PL_uid) ) {
1545 if (statbufp->st_mode & mode)
1546 return TRUE; /* ok as "user" */
1548 else if (ingroup(statbufp->st_gid,effective)) {
1549 if (statbufp->st_mode & mode >> 3)
1550 return TRUE; /* ok as "group" */
1552 else if (statbufp->st_mode & mode >> 6)
1553 return TRUE; /* ok as "other" */
1555 #endif /* ! DOSISH */
1560 Perl_ingroup(pTHX_ Gid_t testgid, Uid_t effective)
1562 #ifdef MACOS_TRADITIONAL
1563 /* This is simply not correct for AppleShare, but fix it yerself. */
1566 if (testgid == (effective ? PL_egid : PL_gid))
1568 #ifdef HAS_GETGROUPS
1573 Groups_t gary[NGROUPS];
1576 anum = getgroups(NGROUPS,gary);
1578 if (gary[anum] == testgid)
1586 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
1589 Perl_do_ipcget(pTHX_ I32 optype, SV **mark, SV **sp)
1595 key = (key_t)SvNVx(*++mark);
1596 n = (optype == OP_MSGGET) ? 0 : SvIVx(*++mark);
1597 flags = SvIVx(*++mark);
1603 return msgget(key, flags);
1607 return semget(key, n, flags);
1611 return shmget(key, n, flags);
1613 #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
1615 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
1618 return -1; /* should never happen */
1622 Perl_do_ipcctl(pTHX_ I32 optype, SV **mark, SV **sp)
1627 I32 id, n, cmd, infosize, getinfo;
1630 id = SvIVx(*++mark);
1631 n = (optype == OP_SEMCTL) ? SvIVx(*++mark) : 0;
1632 cmd = SvIVx(*++mark);
1635 getinfo = (cmd == IPC_STAT);
1641 if (cmd == IPC_STAT || cmd == IPC_SET)
1642 infosize = sizeof(struct msqid_ds);
1647 if (cmd == IPC_STAT || cmd == IPC_SET)
1648 infosize = sizeof(struct shmid_ds);
1654 if (cmd == IPC_STAT || cmd == IPC_SET)
1655 infosize = sizeof(struct semid_ds);
1656 else if (cmd == GETALL || cmd == SETALL)
1658 struct semid_ds semds;
1662 getinfo = (cmd == GETALL);
1663 if (Semctl(id, 0, IPC_STAT, semun) == -1)
1665 infosize = semds.sem_nsems * sizeof(short);
1666 /* "short" is technically wrong but much more portable
1667 than guessing about u_?short(_t)? */
1670 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
1674 #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
1676 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
1685 SvPV_force(astr, len);
1686 a = SvGROW(astr, infosize+1);
1690 a = SvPV(astr, len);
1691 if (len != infosize)
1692 Perl_croak(aTHX_ "Bad arg length for %s, is %lu, should be %ld",
1701 a = INT2PTR(char *,i); /* ouch */
1708 ret = msgctl(id, cmd, (struct msqid_ds *)a);
1714 union semun unsemds;
1716 unsemds.buf = (struct semid_ds *)a;
1717 ret = Semctl(id, n, cmd, unsemds);
1719 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
1726 ret = shmctl(id, cmd, (struct shmid_ds *)a);
1730 if (getinfo && ret >= 0) {
1731 SvCUR_set(astr, infosize);
1732 *SvEND(astr) = '\0';
1739 Perl_do_msgsnd(pTHX_ SV **mark, SV **sp)
1745 I32 id, msize, flags;
1748 id = SvIVx(*++mark);
1750 flags = SvIVx(*++mark);
1751 mbuf = SvPV(mstr, len);
1752 if ((msize = len - sizeof(long)) < 0)
1753 Perl_croak(aTHX_ "Arg too short for msgsnd");
1755 return msgsnd(id, (struct msgbuf *)mbuf, msize, flags);
1757 Perl_croak(aTHX_ "msgsnd not implemented");
1762 Perl_do_msgrcv(pTHX_ SV **mark, SV **sp)
1769 I32 id, msize, flags, ret;
1772 id = SvIVx(*++mark);
1774 msize = SvIVx(*++mark);
1775 mtype = (long)SvIVx(*++mark);
1776 flags = SvIVx(*++mark);
1777 SvPV_force(mstr, len);
1778 mbuf = SvGROW(mstr, sizeof(long)+msize+1);
1781 ret = msgrcv(id, (struct msgbuf *)mbuf, msize, mtype, flags);
1783 SvCUR_set(mstr, sizeof(long)+ret);
1784 *SvEND(mstr) = '\0';
1788 Perl_croak(aTHX_ "msgrcv not implemented");
1793 Perl_do_semop(pTHX_ SV **mark, SV **sp)
1802 id = SvIVx(*++mark);
1804 opbuf = SvPV(opstr, opsize);
1805 if (opsize < sizeof(struct sembuf)
1806 || (opsize % sizeof(struct sembuf)) != 0) {
1807 SETERRNO(EINVAL,LIB$_INVARG);
1811 return semop(id, (struct sembuf *)opbuf, opsize/sizeof(struct sembuf));
1813 Perl_croak(aTHX_ "semop not implemented");
1818 Perl_do_shmio(pTHX_ I32 optype, SV **mark, SV **sp)
1824 I32 id, mpos, msize;
1826 struct shmid_ds shmds;
1828 id = SvIVx(*++mark);
1830 mpos = SvIVx(*++mark);
1831 msize = SvIVx(*++mark);
1833 if (shmctl(id, IPC_STAT, &shmds) == -1)
1835 if (mpos < 0 || msize < 0 || mpos + msize > shmds.shm_segsz) {
1836 SETERRNO(EFAULT,SS$_ACCVIO); /* can't do as caller requested */
1839 shm = (char *)shmat(id, (char*)NULL, (optype == OP_SHMREAD) ? SHM_RDONLY : 0);
1840 if (shm == (char *)-1) /* I hate System V IPC, I really do */
1842 if (optype == OP_SHMREAD) {
1843 SvPV_force(mstr, len);
1844 mbuf = SvGROW(mstr, msize+1);
1846 Copy(shm + mpos, mbuf, msize, char);
1847 SvCUR_set(mstr, msize);
1848 *SvEND(mstr) = '\0';
1854 mbuf = SvPV(mstr, len);
1855 if ((n = len) > msize)
1857 Copy(mbuf, shm + mpos, n, char);
1859 memzero(shm + mpos + n, msize - n);
1863 Perl_croak(aTHX_ "shm I/O not implemented");
1867 #endif /* SYSV IPC */