3 * Copyright (c) 1991-2000, 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>
45 # define OPEN_EXCL O_EXCL
50 #if !defined(NSIG) || defined(M_UNIX) || defined(M_XENIX)
54 /* XXX If this causes problems, set i_unistd=undef in the hint file. */
59 #if defined(HAS_SOCKET) && !defined(VMS) /* VMS handles sockets via vmsish.h */
60 # include <sys/socket.h>
61 # if defined(USE_SOCKS) && defined(I_SOCKS)
69 # include <net/errno.h>
75 Perl_do_open(pTHX_ GV *gv, register char *name, I32 len, int as_raw,
76 int rawmode, int rawperm, PerlIO *supplied_fp)
78 return do_open9(gv, name, len, as_raw, rawmode, rawperm,
79 supplied_fp, Nullsv, 0);
83 Perl_do_open9(pTHX_ GV *gv, register char *name, I32 len, int as_raw,
84 int rawmode, int rawperm, PerlIO *supplied_fp, SV *svs,
87 register IO *io = GvIOn(gv);
88 PerlIO *saveifp = Nullfp;
89 PerlIO *saveofp = Nullfp;
95 bool was_fdopen = FALSE;
97 PL_forkprocess = 1; /* assume true if no fork */
100 fd = PerlIO_fileno(IoIFP(io));
101 if (IoTYPE(io) == '-')
103 else if (fd <= PL_maxsysfd) {
106 savetype = IoTYPE(io);
109 else if (IoTYPE(io) == '|')
110 result = PerlProc_pclose(IoIFP(io));
111 else if (IoIFP(io) != IoOFP(io)) {
113 result = PerlIO_close(IoOFP(io));
114 PerlIO_close(IoIFP(io)); /* clear stdio, fd already closed */
117 result = PerlIO_close(IoIFP(io));
120 result = PerlIO_close(IoIFP(io));
121 if (result == EOF && fd > PL_maxsysfd)
122 PerlIO_printf(Perl_error_log,
123 "Warning: unable to close filehandle %s properly.\n",
125 IoOFP(io) = IoIFP(io) = Nullfp;
129 #if defined(USE_64_BIT_RAWIO) && defined(O_LARGEFILE)
130 rawmode |= O_LARGEFILE;
134 #define O_ACCMODE 3 /* Assume traditional implementation */
137 switch (result = rawmode & O_ACCMODE) {
150 writing = (result > 0);
151 fd = PerlLIO_open3(name, rawmode, rawperm);
157 if (result == O_RDONLY)
160 else if (rawmode & O_APPEND)
161 fpmode = (result == O_WRONLY) ? "a" : "a+";
164 fpmode = (result == O_WRONLY) ? "w" : "r+";
165 fp = PerlIO_fdopen(fd, fpmode);
175 char mode[3]; /* stdio file mode ("r\0" or "r+\0") */
178 type = savepvn(name, len);
183 name = SvPV(svs, l) ;
185 name = savepvn(name, len);
189 while (tlen && isSPACE(type[tlen-1]))
194 mode[0] = mode[1] = mode[2] = '\0';
196 if (*type == '+' && tlen > 1 && type[tlen-1] != '|') { /* scary */
203 if (num_svs && (tlen != 2 || type[1] != '-')) {
205 Perl_croak(aTHX_ "Unknown open() mode '%.*s'", (int)olen, oname);
208 for (type++, tlen--; isSPACE(*type); type++, tlen--) ;
213 if (*name == '\0') { /* command is missing 19990114 */
215 if (ckWARN(WARN_PIPE))
216 Perl_warner(aTHX_ WARN_PIPE, "Missing command in piped open");
220 if (strNE(name,"-") || num_svs)
222 TAINT_PROPER("piped open");
223 if (name[len-1] == '|') {
226 if (ckWARN(WARN_PIPE))
227 Perl_warner(aTHX_ WARN_PIPE, "Can't open bidirectional pipe");
229 fp = PerlProc_popen(name,"w");
232 else if (*type == '>') {
233 TAINT_PROPER("open");
236 mode[0] = IoTYPE(io) = 'a';
244 if (num_svs && tlen != 1)
255 if (!*name && supplied_fp)
259 for (; isSPACE(*name); name++) ;
264 gv = gv_fetchpv(name,FALSE,SVt_PVIO);
268 SETERRNO(EINVAL,SS$_IVCHAN);
273 PerlIO *fp = IoIFP(thatio);
274 /* Flush stdio buffer before dup. --mjd
275 * Unfortunately SEEK_CURing 0 seems to
276 * be optimized away on most platforms;
277 * only Solaris and Linux seem to flush
279 PerlIO_seek(fp, 0, SEEK_CUR);
280 /* On the other hand, do all platforms
281 * take gracefully to flushing a read-only
282 * filehandle? Perhaps we should do
283 * fsetpos(src)+fgetpos(dst)? --nik */
285 fd = PerlIO_fileno(fp);
286 if (IoTYPE(thatio) == 's')
293 fd = PerlLIO_dup(fd);
296 if (!(fp = PerlIO_fdopen(fd,mode))) {
304 for (; isSPACE(*type); type++) ;
305 if (strEQ(type,"-")) {
306 fp = PerlIO_stdout();
310 fp = PerlIO_open((num_svs ? name : type), mode);
314 else if (*type == '<') {
315 if (num_svs && tlen != 1)
318 for (type++; isSPACE(*type); type++) ;
324 if (strEQ(type,"-")) {
329 fp = PerlIO_open((num_svs ? name : type), mode);
331 else if (tlen > 1 && type[tlen-1] == '|') {
333 if (tlen != 2 || type[0] != '-')
338 while (tlen && isSPACE(type[tlen-1]))
341 for (; isSPACE(*type); type++) ;
344 if (*name == '\0') { /* command is missing 19990114 */
346 if (ckWARN(WARN_PIPE))
347 Perl_warner(aTHX_ WARN_PIPE, "Missing command in piped open");
351 if (strNE(name,"-") || num_svs)
353 TAINT_PROPER("piped open");
354 fp = PerlProc_popen(name,"r");
363 for (; isSPACE(*name); name++) ;
364 if (strEQ(name,"-")) {
369 fp = PerlIO_open(name,"r");
374 if (ckWARN(WARN_NEWLINE) && IoTYPE(io) == '<' && strchr(name, '\n'))
375 Perl_warner(aTHX_ WARN_NEWLINE, PL_warn_nl, "open");
379 IoTYPE(io) != '|' && IoTYPE(io) != '-') {
381 if (PerlLIO_fstat(PerlIO_fileno(fp),&PL_statbuf) < 0) {
382 (void)PerlIO_close(fp);
385 if (S_ISSOCK(PL_statbuf.st_mode))
386 IoTYPE(io) = 's'; /* in case a socket was passed in to us */
390 !(PL_statbuf.st_mode & S_IFMT)
396 Sock_size_t buflen = sizeof tmpbuf;
397 if (PerlSock_getsockname(PerlIO_fileno(fp), (struct sockaddr *)tmpbuf,
399 || errno != ENOTSOCK)
400 IoTYPE(io) = 's'; /* some OS's return 0 on fstat()ed socket */
401 /* but some return 0 for streams too, sigh */
405 if (saveifp) { /* must use old fp? */
406 fd = PerlIO_fileno(saveifp);
408 PerlIO_flush(saveofp); /* emulate PerlIO_close() */
409 if (saveofp != saveifp) { /* was a socket? */
410 PerlIO_close(saveofp);
415 if (fd != PerlIO_fileno(fp)) {
419 PerlLIO_dup2(PerlIO_fileno(fp), fd);
420 sv = *av_fetch(PL_fdpid,PerlIO_fileno(fp),TRUE);
421 (void)SvUPGRADE(sv, SVt_IV);
424 sv = *av_fetch(PL_fdpid,fd,TRUE);
425 (void)SvUPGRADE(sv, SVt_IV);
434 #if defined(HAS_FCNTL) && defined(F_SETFD)
436 int save_errno = errno;
437 fd = PerlIO_fileno(fp);
438 fcntl(fd,F_SETFD,fd > PL_maxsysfd); /* can change errno */
443 IoFLAGS(io) &= ~IOf_NOLINE;
446 if (IoTYPE(io) == 's'
447 || (IoTYPE(io) == '>' && S_ISCHR(PL_statbuf.st_mode)) ) {
448 if (!(IoOFP(io) = PerlIO_fdopen(PerlIO_fileno(fp),"w"))) {
462 IoTYPE(io) = savetype;
467 Perl_nextargv(pTHX_ register GV *gv)
470 #ifndef FLEXFILENAMES
479 PL_argvoutgv = gv_fetchpv("ARGVOUT",TRUE,SVt_PVIO);
480 if (io && (IoFLAGS(io) & IOf_ARGV) && (IoFLAGS(io) & IOf_START)) {
481 IoFLAGS(io) &= ~IOf_START;
483 if (!PL_argvout_stack)
484 PL_argvout_stack = newAV();
485 av_push(PL_argvout_stack, SvREFCNT_inc(PL_defoutgv));
488 if (PL_filemode & (S_ISUID|S_ISGID)) {
489 PerlIO_flush(IoIFP(GvIOn(PL_argvoutgv))); /* chmod must follow last write */
491 (void)fchmod(PL_lastfd,PL_filemode);
493 (void)PerlLIO_chmod(PL_oldname,PL_filemode);
497 while (av_len(GvAV(gv)) >= 0) {
500 sv = av_shift(GvAV(gv));
502 sv_setsv(GvSV(gv),sv);
503 SvSETMAGIC(GvSV(gv));
504 PL_oldname = SvPVx(GvSV(gv), oldlen);
505 if (do_open(gv,PL_oldname,oldlen,PL_inplace!=0,O_RDONLY,0,Nullfp)) {
507 TAINT_PROPER("inplace open");
508 if (oldlen == 1 && *PL_oldname == '-') {
509 setdefout(gv_fetchpv("STDOUT",TRUE,SVt_PVIO));
510 return IoIFP(GvIOp(gv));
512 #ifndef FLEXFILENAMES
513 filedev = PL_statbuf.st_dev;
514 fileino = PL_statbuf.st_ino;
516 PL_filemode = PL_statbuf.st_mode;
517 fileuid = PL_statbuf.st_uid;
518 filegid = PL_statbuf.st_gid;
519 if (!S_ISREG(PL_filemode)) {
520 if (ckWARN_d(WARN_INPLACE))
521 Perl_warner(aTHX_ WARN_INPLACE,
522 "Can't do inplace edit: %s is not a regular file",
528 char *star = strchr(PL_inplace, '*');
530 char *begin = PL_inplace;
531 sv_setpvn(sv, "", 0);
533 sv_catpvn(sv, begin, star - begin);
534 sv_catpvn(sv, PL_oldname, oldlen);
536 } while ((star = strchr(begin, '*')));
541 sv_catpv(sv,PL_inplace);
543 #ifndef FLEXFILENAMES
544 if (PerlLIO_stat(SvPVX(sv),&PL_statbuf) >= 0
545 && PL_statbuf.st_dev == filedev
546 && PL_statbuf.st_ino == fileino
548 || (_djstat_fail_bits & _STFAIL_TRUENAME)!=0
552 if (ckWARN_d(WARN_INPLACE))
553 Perl_warner(aTHX_ WARN_INPLACE,
554 "Can't do inplace edit: %s would not be unique",
561 #if !defined(DOSISH) && !defined(__CYGWIN__)
562 if (PerlLIO_rename(PL_oldname,SvPVX(sv)) < 0) {
563 if (ckWARN_d(WARN_INPLACE))
564 Perl_warner(aTHX_ WARN_INPLACE,
565 "Can't rename %s to %s: %s, skipping file",
566 PL_oldname, SvPVX(sv), Strerror(errno) );
572 (void)PerlLIO_unlink(SvPVX(sv));
573 (void)PerlLIO_rename(PL_oldname,SvPVX(sv));
574 do_open(gv,SvPVX(sv),SvCUR(sv),PL_inplace!=0,O_RDONLY,0,Nullfp);
577 (void)UNLINK(SvPVX(sv));
578 if (link(PL_oldname,SvPVX(sv)) < 0) {
579 if (ckWARN_d(WARN_INPLACE))
580 Perl_warner(aTHX_ WARN_INPLACE,
581 "Can't rename %s to %s: %s, skipping file",
582 PL_oldname, SvPVX(sv), Strerror(errno) );
586 (void)UNLINK(PL_oldname);
590 #if !defined(DOSISH) && !defined(AMIGAOS)
591 # ifndef VMS /* Don't delete; use automatic file versioning */
592 if (UNLINK(PL_oldname) < 0) {
593 if (ckWARN_d(WARN_INPLACE))
594 Perl_warner(aTHX_ WARN_INPLACE,
595 "Can't remove %s: %s, skipping file",
596 PL_oldname, Strerror(errno) );
602 Perl_croak(aTHX_ "Can't do inplace edit without backup");
606 sv_setpvn(sv,">",!PL_inplace);
607 sv_catpvn(sv,PL_oldname,oldlen);
608 SETERRNO(0,0); /* in case sprintf set errno */
610 if (!do_open(PL_argvoutgv,SvPVX(sv),SvCUR(sv),PL_inplace!=0,
611 O_WRONLY|O_CREAT|O_TRUNC,0,Nullfp))
613 if (!do_open(PL_argvoutgv,SvPVX(sv),SvCUR(sv),PL_inplace!=0,
614 O_WRONLY|O_CREAT|OPEN_EXCL,0666,Nullfp))
617 if (ckWARN_d(WARN_INPLACE))
618 Perl_warner(aTHX_ WARN_INPLACE, "Can't do inplace edit on %s: %s",
619 PL_oldname, Strerror(errno) );
623 setdefout(PL_argvoutgv);
624 PL_lastfd = PerlIO_fileno(IoIFP(GvIOp(PL_argvoutgv)));
625 (void)PerlLIO_fstat(PL_lastfd,&PL_statbuf);
627 (void)fchmod(PL_lastfd,PL_filemode);
629 # if !(defined(WIN32) && defined(__BORLANDC__))
630 /* Borland runtime creates a readonly file! */
631 (void)PerlLIO_chmod(PL_oldname,PL_filemode);
634 if (fileuid != PL_statbuf.st_uid || filegid != PL_statbuf.st_gid) {
636 (void)fchown(PL_lastfd,fileuid,filegid);
639 (void)PerlLIO_chown(PL_oldname,fileuid,filegid);
644 return IoIFP(GvIOp(gv));
648 if (ckWARN_d(WARN_INPLACE)) {
649 if (!S_ISREG(PL_statbuf.st_mode))
650 Perl_warner(aTHX_ WARN_INPLACE,
651 "Can't do inplace edit: %s is not a regular file",
654 Perl_warner(aTHX_ WARN_INPLACE, "Can't open %s: %s",
655 PL_oldname, Strerror(errno));
659 if (io && (IoFLAGS(io) & IOf_ARGV))
660 IoFLAGS(io) |= IOf_START;
662 (void)do_close(PL_argvoutgv,FALSE);
663 if (io && (IoFLAGS(io) & IOf_ARGV)
664 && PL_argvout_stack && AvFILLp(PL_argvout_stack) >= 0)
666 GV *oldout = (GV*)av_pop(PL_argvout_stack);
668 SvREFCNT_dec(oldout);
671 setdefout(gv_fetchpv("STDOUT",TRUE,SVt_PVIO));
678 Perl_do_pipe(pTHX_ SV *sv, GV *rgv, GV *wgv)
697 if (PerlProc_pipe(fd) < 0)
699 IoIFP(rstio) = PerlIO_fdopen(fd[0], "r");
700 IoOFP(wstio) = PerlIO_fdopen(fd[1], "w");
701 IoIFP(wstio) = IoOFP(wstio);
704 if (!IoIFP(rstio) || !IoOFP(wstio)) {
705 if (IoIFP(rstio)) PerlIO_close(IoIFP(rstio));
706 else PerlLIO_close(fd[0]);
707 if (IoOFP(wstio)) PerlIO_close(IoOFP(wstio));
708 else PerlLIO_close(fd[1]);
712 sv_setsv(sv,&PL_sv_yes);
716 sv_setsv(sv,&PL_sv_undef);
721 /* explicit renamed to avoid C++ conflict -- kja */
723 Perl_do_close(pTHX_ GV *gv, bool not_implicit)
730 if (!gv || SvTYPE(gv) != SVt_PVGV) {
732 SETERRNO(EBADF,SS$_IVCHAN);
736 if (!io) { /* never opened */
739 if (ckWARN(WARN_UNOPENED))
740 Perl_warner(aTHX_ WARN_UNOPENED,
741 "Close on unopened file <%s>",GvENAME(gv));
742 SETERRNO(EBADF,SS$_IVCHAN);
746 retval = io_close(io, not_implicit);
750 IoLINES_LEFT(io) = IoPAGE_LEN(io);
757 Perl_io_close(pTHX_ IO *io, bool not_implicit)
763 if (IoTYPE(io) == '|') {
764 status = PerlProc_pclose(IoIFP(io));
766 STATUS_NATIVE_SET(status);
767 retval = (STATUS_POSIX == 0);
770 retval = (status != -1);
773 else if (IoTYPE(io) == '-')
776 if (IoOFP(io) && IoOFP(io) != IoIFP(io)) { /* a socket */
777 retval = (PerlIO_close(IoOFP(io)) != EOF);
778 PerlIO_close(IoIFP(io)); /* clear stdio, fd already closed */
781 retval = (PerlIO_close(IoIFP(io)) != EOF);
783 IoOFP(io) = IoIFP(io) = Nullfp;
785 else if (not_implicit) {
786 SETERRNO(EBADF,SS$_IVCHAN);
793 Perl_do_eof(pTHX_ GV *gv)
803 else if (ckWARN(WARN_IO)
804 && (IoTYPE(io) == '>' || IoIFP(io) == PerlIO_stdout()
805 || IoIFP(io) == PerlIO_stderr()))
807 SV* sv = sv_newmortal();
808 gv_efullname3(sv, gv, Nullch);
809 Perl_warner(aTHX_ WARN_IO, "Filehandle %s opened only for output",
815 if (PerlIO_has_cntptr(IoIFP(io))) { /* (the code works without this) */
816 if (PerlIO_get_cnt(IoIFP(io)) > 0) /* cheat a little, since */
817 return FALSE; /* this is the most usual case */
820 ch = PerlIO_getc(IoIFP(io));
822 (void)PerlIO_ungetc(IoIFP(io),ch);
825 if (PerlIO_has_cntptr(IoIFP(io)) && PerlIO_canset_cnt(IoIFP(io))) {
826 if (PerlIO_get_cnt(IoIFP(io)) < -1)
827 PerlIO_set_cnt(IoIFP(io),-1);
829 if (PL_op->op_flags & OPf_SPECIAL) { /* not necessarily a real EOF yet? */
830 if (!nextargv(PL_argvgv)) /* get another fp handy */
834 return TRUE; /* normal fp, definitely end of file */
840 Perl_do_tell(pTHX_ GV *gv)
845 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io))) {
846 #ifdef ULTRIX_STDIO_BOTCH
848 (void)PerlIO_seek(fp, 0L, 2); /* ultrix 1.2 workaround */
850 return PerlIO_tell(fp);
854 if (ckWARN(WARN_UNOPENED))
855 Perl_warner(aTHX_ WARN_UNOPENED, "tell() on unopened file");
857 SETERRNO(EBADF,RMS$_IFI);
862 Perl_do_seek(pTHX_ GV *gv, Off_t pos, int whence)
867 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io))) {
868 #ifdef ULTRIX_STDIO_BOTCH
870 (void)PerlIO_seek(fp, 0L, 2); /* ultrix 1.2 workaround */
872 return PerlIO_seek(fp, pos, whence) >= 0;
876 if (ckWARN(WARN_UNOPENED))
877 Perl_warner(aTHX_ WARN_UNOPENED, "seek() on unopened file");
879 SETERRNO(EBADF,RMS$_IFI);
884 Perl_do_sysseek(pTHX_ GV *gv, Off_t pos, int whence)
889 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io)))
890 return PerlLIO_lseek(PerlIO_fileno(fp), pos, whence);
893 if (ckWARN(WARN_UNOPENED))
894 Perl_warner(aTHX_ WARN_UNOPENED, "sysseek() on unopened file");
896 SETERRNO(EBADF,RMS$_IFI);
901 Perl_do_binmode(pTHX_ PerlIO *fp, int iotype, int flag)
904 Perl_croak(aTHX_ "panic: unsetting binmode"); /* Not implemented yet */
906 #if defined(atarist) || defined(__MINT__)
907 if (!PerlIO_flush(fp) && (fp->_flag |= _IOBIN))
912 if (PerlLIO_setmode(PerlIO_fileno(fp), OP_BINARY) != -1) {
913 #if defined(WIN32) && defined(__BORLANDC__)
914 /* The translation mode of the stream is maintained independent
915 * of the translation mode of the fd in the Borland RTL (heavy
916 * digging through their runtime sources reveal). User has to
917 * set the mode explicitly for the stream (though they don't
918 * document this anywhere). GSAR 97-5-24
920 PerlIO_seek(fp,0L,0);
921 ((FILE*)fp)->flags |= _F_BIN;
929 #if defined(USEMYBINMODE)
930 if (my_binmode(fp,iotype) != FALSE)
940 #if !defined(HAS_TRUNCATE) && !defined(HAS_CHSIZE) && defined(F_FREESP)
941 /* code courtesy of William Kucharski */
944 I32 my_chsize(fd, length)
945 I32 fd; /* file descriptor */
946 Off_t length; /* length to set file to */
951 if (PerlLIO_fstat(fd, &filebuf) < 0)
954 if (filebuf.st_size < length) {
956 /* extend file length */
958 if ((PerlLIO_lseek(fd, (length - 1), 0)) < 0)
961 /* write a "0" byte */
963 if ((PerlLIO_write(fd, "", 1)) != 1)
967 /* truncate length */
972 fl.l_type = F_WRLCK; /* write lock on file space */
975 * This relies on the UNDOCUMENTED F_FREESP argument to
976 * fcntl(2), which truncates the file so that it ends at the
977 * position indicated by fl.l_start.
979 * Will minor miracles never cease?
982 if (fcntl(fd, F_FREESP, &fl) < 0)
989 #endif /* F_FREESP */
992 Perl_do_print(pTHX_ register SV *sv, PerlIO *fp)
997 /* assuming fp is checked earlier */
1003 if (SvIOK(sv) && SvIVX(sv) != 0) {
1004 PerlIO_printf(fp, PL_ofmt, (NV)SvIVX(sv));
1005 return !PerlIO_error(fp);
1007 if ( (SvNOK(sv) && SvNVX(sv) != 0.0)
1008 || (looks_like_number(sv) && sv_2nv(sv) != 0.0) ) {
1009 PerlIO_printf(fp, PL_ofmt, SvNVX(sv));
1010 return !PerlIO_error(fp);
1013 switch (SvTYPE(sv)) {
1017 if (ckWARN(WARN_UNINITIALIZED))
1026 PerlIO_printf(fp, "%"UVuf, (UV)SvUVX(sv));
1028 PerlIO_printf(fp, "%"IVdf, (IV)SvIVX(sv));
1029 return !PerlIO_error(fp);
1033 tmps = SvPV(sv, len);
1036 /* To detect whether the process is about to overstep its
1037 * filesize limit we would need getrlimit(). We could then
1038 * also transparently raise the limit with setrlimit() --
1039 * but only until the system hard limit/the filesystem limit,
1040 * at which we would get EPERM. Note that when using buffered
1041 * io the write failure can be delayed until the flush/close. --jhi */
1042 if (len && (PerlIO_write(fp,tmps,len) == 0 || PerlIO_error(fp)))
1044 return !PerlIO_error(fp);
1054 if (PL_op->op_flags & OPf_REF) {
1059 if (io && IoIFP(io)) {
1061 sv_setpv(PL_statname,"");
1062 PL_laststype = OP_STAT;
1063 return (PL_laststatval = PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache));
1066 if (tmpgv == PL_defgv)
1067 return PL_laststatval;
1068 if (ckWARN(WARN_UNOPENED))
1069 Perl_warner(aTHX_ WARN_UNOPENED, "Stat on unopened file <%s>",
1072 sv_setpv(PL_statname,"");
1073 return (PL_laststatval = -1);
1081 if (SvTYPE(sv) == SVt_PVGV) {
1085 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
1086 tmpgv = (GV*)SvRV(sv);
1092 sv_setpv(PL_statname, s);
1093 PL_laststype = OP_STAT;
1094 PL_laststatval = PerlLIO_stat(s, &PL_statcache);
1095 if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(s, '\n'))
1096 Perl_warner(aTHX_ WARN_NEWLINE, PL_warn_nl, "stat");
1097 return PL_laststatval;
1107 if (PL_op->op_flags & OPf_REF) {
1109 if (cGVOP_gv == PL_defgv) {
1110 if (PL_laststype != OP_LSTAT)
1111 Perl_croak(aTHX_ "The stat preceding -l _ wasn't an lstat");
1112 return PL_laststatval;
1114 Perl_croak(aTHX_ "You can't use -l on a filehandle");
1117 PL_laststype = OP_LSTAT;
1121 sv_setpv(PL_statname,SvPV(sv, n_a));
1122 PL_laststatval = PerlLIO_lstat(SvPV(sv, n_a),&PL_statcache);
1123 if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(SvPV(sv, n_a), '\n'))
1124 Perl_warner(aTHX_ WARN_NEWLINE, PL_warn_nl, "lstat");
1125 return PL_laststatval;
1129 Perl_do_aexec(pTHX_ SV *really, register SV **mark, register SV **sp)
1131 return do_aexec5(really, mark, sp, 0, 0);
1135 Perl_do_aexec5(pTHX_ SV *really, register SV **mark, register SV **sp,
1136 int fd, int do_report)
1138 #ifdef MACOS_TRADITIONAL
1139 Perl_croak(aTHX_ "exec? I'm not *that* kind of operating system");
1147 New(401,PL_Argv, sp - mark + 1, char*);
1149 while (++mark <= sp) {
1151 *a++ = SvPVx(*mark, n_a);
1156 if (*PL_Argv[0] != '/') /* will execvp use PATH? */
1157 TAINT_ENV(); /* testing IFS here is overkill, probably */
1158 if (really && *(tmps = SvPV(really, n_a)))
1159 PerlProc_execvp(tmps,PL_Argv);
1161 PerlProc_execvp(PL_Argv[0],PL_Argv);
1162 if (ckWARN(WARN_EXEC))
1163 Perl_warner(aTHX_ WARN_EXEC, "Can't exec \"%s\": %s",
1164 PL_Argv[0], Strerror(errno));
1168 PerlLIO_write(fd, (void*)&e, sizeof(int));
1178 Perl_do_execfree(pTHX)
1182 PL_Argv = Null(char **);
1190 #if !defined(OS2) && !defined(WIN32) && !defined(DJGPP) && !defined(EPOC) && !defined(MACOS_TRADITIONAL)
1193 Perl_do_exec(pTHX_ char *cmd)
1195 return do_exec3(cmd,0,0);
1199 Perl_do_exec3(pTHX_ char *cmd, int fd, int do_report)
1205 while (*cmd && isSPACE(*cmd))
1208 /* save an extra exec if possible */
1211 if (strnEQ(cmd,PL_cshname,PL_cshlen) && strnEQ(cmd+PL_cshlen," -c",3)) {
1213 s = cmd+PL_cshlen+3;
1227 if (s[-1] == '\'') {
1229 PerlProc_execl(PL_cshname,"csh", flags,ncmd,(char*)0);
1237 /* see if there are shell metacharacters in it */
1239 if (*cmd == '.' && isSPACE(cmd[1]))
1242 if (strnEQ(cmd,"exec",4) && isSPACE(cmd[4]))
1245 for (s = cmd; *s && isALNUM(*s); s++) ; /* catch VAR=val gizmo */
1249 for (s = cmd; *s; s++) {
1250 if (*s != ' ' && !isALPHA(*s) && strchr("$&*(){}[]'\";\\|?<>~`\n",*s)) {
1251 if (*s == '\n' && !s[1]) {
1255 /* handle the 2>&1 construct at the end */
1256 if (*s == '>' && s[1] == '&' && s[2] == '1'
1257 && s > cmd + 1 && s[-1] == '2' && isSPACE(s[-2])
1258 && (!s[3] || isSPACE(s[3])))
1262 while (*t && isSPACE(*t))
1264 if (!*t && (dup2(1,2) != -1)) {
1270 PerlProc_execl(PL_sh_path, "sh", "-c", cmd, (char*)0);
1275 New(402,PL_Argv, (s - cmd) / 2 + 2, char*);
1276 PL_Cmd = savepvn(cmd, s-cmd);
1278 for (s = PL_Cmd; *s;) {
1279 while (*s && isSPACE(*s)) s++;
1282 while (*s && !isSPACE(*s)) s++;
1288 PerlProc_execvp(PL_Argv[0],PL_Argv);
1289 if (errno == ENOEXEC) { /* for system V NIH syndrome */
1297 if (ckWARN(WARN_EXEC))
1298 Perl_warner(aTHX_ WARN_EXEC, "Can't exec \"%s\": %s",
1299 PL_Argv[0], Strerror(errno));
1301 PerlLIO_write(fd, (void*)&e, sizeof(int));
1310 #endif /* OS2 || WIN32 */
1313 Perl_apply(pTHX_ I32 type, register SV **mark, register SV **sp)
1318 register I32 tot = 0;
1321 SV **oldmark = mark;
1324 #define APPLY_TAINT_PROPER() \
1326 if (PL_tainted) { TAINT_PROPER(what); } \
1329 /* This is a first heuristic; it doesn't catch tainting magic. */
1331 while (++mark <= sp) {
1332 if (SvTAINTED(*mark)) {
1342 APPLY_TAINT_PROPER();
1345 APPLY_TAINT_PROPER();
1347 while (++mark <= sp) {
1348 char *name = SvPVx(*mark, n_a);
1349 APPLY_TAINT_PROPER();
1350 if (PerlLIO_chmod(name, val))
1358 APPLY_TAINT_PROPER();
1359 if (sp - mark > 2) {
1360 val = SvIVx(*++mark);
1361 val2 = SvIVx(*++mark);
1362 APPLY_TAINT_PROPER();
1364 while (++mark <= sp) {
1365 char *name = SvPVx(*mark, n_a);
1366 APPLY_TAINT_PROPER();
1367 if (PerlLIO_chown(name, val, val2))
1374 XXX Should we make lchown() directly available from perl?
1375 For now, we'll let Configure test for HAS_LCHOWN, but do
1376 nothing in the core.
1382 APPLY_TAINT_PROPER();
1385 s = SvPVx(*++mark, n_a);
1387 if (*s == 'S' && s[1] == 'I' && s[2] == 'G')
1389 if (!(val = whichsig(s)))
1390 Perl_croak(aTHX_ "Unrecognized signal name \"%s\"",s);
1394 APPLY_TAINT_PROPER();
1397 /* kill() doesn't do process groups (job trees?) under VMS */
1398 if (val < 0) val = -val;
1399 if (val == SIGKILL) {
1400 # include <starlet.h>
1401 /* Use native sys$delprc() to insure that target process is
1402 * deleted; supervisor-mode images don't pay attention to
1403 * CRTL's emulation of Unix-style signals and kill()
1405 while (++mark <= sp) {
1406 I32 proc = SvIVx(*mark);
1407 register unsigned long int __vmssts;
1408 APPLY_TAINT_PROPER();
1409 if (!((__vmssts = sys$delprc(&proc,0)) & 1)) {
1413 case SS$_NOSUCHNODE:
1414 SETERRNO(ESRCH,__vmssts);
1417 SETERRNO(EPERM,__vmssts);
1420 SETERRNO(EVMSERR,__vmssts);
1429 while (++mark <= sp) {
1430 I32 proc = SvIVx(*mark);
1431 APPLY_TAINT_PROPER();
1433 if (PerlProc_killpg(proc,val)) /* BSD */
1435 if (PerlProc_kill(-proc,val)) /* SYSV */
1441 while (++mark <= sp) {
1442 I32 proc = SvIVx(*mark);
1443 APPLY_TAINT_PROPER();
1444 if (PerlProc_kill(proc, val))
1452 APPLY_TAINT_PROPER();
1454 while (++mark <= sp) {
1455 s = SvPVx(*mark, n_a);
1456 APPLY_TAINT_PROPER();
1457 if (PL_euid || PL_unsafe) {
1461 else { /* don't let root wipe out directories without -U */
1462 if (PerlLIO_lstat(s,&PL_statbuf) < 0 || S_ISDIR(PL_statbuf.st_mode))
1474 APPLY_TAINT_PROPER();
1475 if (sp - mark > 2) {
1476 #if defined(I_UTIME) || defined(VMS)
1477 struct utimbuf utbuf;
1485 Zero(&utbuf, sizeof utbuf, char);
1487 utbuf.actime = (Time_t)SvNVx(*++mark); /* time accessed */
1488 utbuf.modtime = (Time_t)SvNVx(*++mark); /* time modified */
1490 utbuf.actime = (Time_t)SvIVx(*++mark); /* time accessed */
1491 utbuf.modtime = (Time_t)SvIVx(*++mark); /* time modified */
1493 APPLY_TAINT_PROPER();
1495 while (++mark <= sp) {
1496 char *name = SvPVx(*mark, n_a);
1497 APPLY_TAINT_PROPER();
1498 if (PerlLIO_utime(name, &utbuf))
1509 #undef APPLY_TAINT_PROPER
1512 /* Do the permissions allow some operation? Assumes statcache already set. */
1513 #ifndef VMS /* VMS' cando is in vms.c */
1515 Perl_cando(pTHX_ Mode_t mode, Uid_t effective, register Stat_t *statbufp)
1516 /* Note: we use `effective' both for uids and gids.
1517 * Here we are betting on Uid_t being equal or wider than Gid_t. */
1520 /* [Comments and code from Len Reed]
1521 * MS-DOS "user" is similar to UNIX's "superuser," but can't write
1522 * to write-protected files. The execute permission bit is set
1523 * by the Miscrosoft C library stat() function for the following:
1528 * All files and directories are readable.
1529 * Directories and special files, e.g. "CON", cannot be
1531 * [Comment by Tom Dinger -- a directory can have the write-protect
1532 * bit set in the file system, but DOS permits changes to
1533 * the directory anyway. In addition, all bets are off
1534 * here for networked software, such as Novell and
1538 /* Atari stat() does pretty much the same thing. we set x_bit_set_in_stat
1539 * too so it will actually look into the files for magic numbers
1541 return (mode & statbufp->st_mode) ? TRUE : FALSE;
1543 #else /* ! DOSISH */
1544 if ((effective ? PL_euid : PL_uid) == 0) { /* root is special */
1545 if (mode == S_IXUSR) {
1546 if (statbufp->st_mode & 0111 || S_ISDIR(statbufp->st_mode))
1550 return TRUE; /* root reads and writes anything */
1553 if (statbufp->st_uid == (effective ? PL_euid : PL_uid) ) {
1554 if (statbufp->st_mode & mode)
1555 return TRUE; /* ok as "user" */
1557 else if (ingroup(statbufp->st_gid,effective)) {
1558 if (statbufp->st_mode & mode >> 3)
1559 return TRUE; /* ok as "group" */
1561 else if (statbufp->st_mode & mode >> 6)
1562 return TRUE; /* ok as "other" */
1564 #endif /* ! DOSISH */
1569 Perl_ingroup(pTHX_ Gid_t testgid, Uid_t effective)
1571 #ifdef MACOS_TRADITIONAL
1572 /* This is simply not correct for AppleShare, but fix it yerself. */
1575 if (testgid == (effective ? PL_egid : PL_gid))
1577 #ifdef HAS_GETGROUPS
1582 Groups_t gary[NGROUPS];
1585 anum = getgroups(NGROUPS,gary);
1587 if (gary[anum] == testgid)
1595 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
1598 Perl_do_ipcget(pTHX_ I32 optype, SV **mark, SV **sp)
1604 key = (key_t)SvNVx(*++mark);
1605 n = (optype == OP_MSGGET) ? 0 : SvIVx(*++mark);
1606 flags = SvIVx(*++mark);
1612 return msgget(key, flags);
1616 return semget(key, n, flags);
1620 return shmget(key, n, flags);
1622 #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
1624 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
1627 return -1; /* should never happen */
1631 Perl_do_ipcctl(pTHX_ I32 optype, SV **mark, SV **sp)
1636 I32 id, n, cmd, infosize, getinfo;
1639 id = SvIVx(*++mark);
1640 n = (optype == OP_SEMCTL) ? SvIVx(*++mark) : 0;
1641 cmd = SvIVx(*++mark);
1644 getinfo = (cmd == IPC_STAT);
1650 if (cmd == IPC_STAT || cmd == IPC_SET)
1651 infosize = sizeof(struct msqid_ds);
1656 if (cmd == IPC_STAT || cmd == IPC_SET)
1657 infosize = sizeof(struct shmid_ds);
1663 if (cmd == IPC_STAT || cmd == IPC_SET)
1664 infosize = sizeof(struct semid_ds);
1665 else if (cmd == GETALL || cmd == SETALL)
1667 struct semid_ds semds;
1671 getinfo = (cmd == GETALL);
1672 if (Semctl(id, 0, IPC_STAT, semun) == -1)
1674 infosize = semds.sem_nsems * sizeof(short);
1675 /* "short" is technically wrong but much more portable
1676 than guessing about u_?short(_t)? */
1679 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
1683 #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
1685 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
1694 SvPV_force(astr, len);
1695 a = SvGROW(astr, infosize+1);
1699 a = SvPV(astr, len);
1700 if (len != infosize)
1701 Perl_croak(aTHX_ "Bad arg length for %s, is %lu, should be %ld",
1710 a = INT2PTR(char *,i); /* ouch */
1717 ret = msgctl(id, cmd, (struct msqid_ds *)a);
1723 union semun unsemds;
1725 unsemds.buf = (struct semid_ds *)a;
1726 ret = Semctl(id, n, cmd, unsemds);
1728 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
1735 ret = shmctl(id, cmd, (struct shmid_ds *)a);
1739 if (getinfo && ret >= 0) {
1740 SvCUR_set(astr, infosize);
1741 *SvEND(astr) = '\0';
1748 Perl_do_msgsnd(pTHX_ SV **mark, SV **sp)
1754 I32 id, msize, flags;
1757 id = SvIVx(*++mark);
1759 flags = SvIVx(*++mark);
1760 mbuf = SvPV(mstr, len);
1761 if ((msize = len - sizeof(long)) < 0)
1762 Perl_croak(aTHX_ "Arg too short for msgsnd");
1764 return msgsnd(id, (struct msgbuf *)mbuf, msize, flags);
1766 Perl_croak(aTHX_ "msgsnd not implemented");
1771 Perl_do_msgrcv(pTHX_ SV **mark, SV **sp)
1778 I32 id, msize, flags, ret;
1781 id = SvIVx(*++mark);
1783 msize = SvIVx(*++mark);
1784 mtype = (long)SvIVx(*++mark);
1785 flags = SvIVx(*++mark);
1786 SvPV_force(mstr, len);
1787 mbuf = SvGROW(mstr, sizeof(long)+msize+1);
1790 ret = msgrcv(id, (struct msgbuf *)mbuf, msize, mtype, flags);
1792 SvCUR_set(mstr, sizeof(long)+ret);
1793 *SvEND(mstr) = '\0';
1797 Perl_croak(aTHX_ "msgrcv not implemented");
1802 Perl_do_semop(pTHX_ SV **mark, SV **sp)
1811 id = SvIVx(*++mark);
1813 opbuf = SvPV(opstr, opsize);
1814 if (opsize < sizeof(struct sembuf)
1815 || (opsize % sizeof(struct sembuf)) != 0) {
1816 SETERRNO(EINVAL,LIB$_INVARG);
1820 return semop(id, (struct sembuf *)opbuf, opsize/sizeof(struct sembuf));
1822 Perl_croak(aTHX_ "semop not implemented");
1827 Perl_do_shmio(pTHX_ I32 optype, SV **mark, SV **sp)
1833 I32 id, mpos, msize;
1835 struct shmid_ds shmds;
1837 id = SvIVx(*++mark);
1839 mpos = SvIVx(*++mark);
1840 msize = SvIVx(*++mark);
1842 if (shmctl(id, IPC_STAT, &shmds) == -1)
1844 if (mpos < 0 || msize < 0 || mpos + msize > shmds.shm_segsz) {
1845 SETERRNO(EFAULT,SS$_ACCVIO); /* can't do as caller requested */
1848 shm = (char *)shmat(id, (char*)NULL, (optype == OP_SHMREAD) ? SHM_RDONLY : 0);
1849 if (shm == (char *)-1) /* I hate System V IPC, I really do */
1851 if (optype == OP_SHMREAD) {
1852 SvPV_force(mstr, len);
1853 mbuf = SvGROW(mstr, msize+1);
1855 Copy(shm + mpos, mbuf, msize, char);
1856 SvCUR_set(mstr, msize);
1857 *SvEND(mstr) = '\0';
1863 mbuf = SvPV(mstr, len);
1864 if ((n = len) > msize)
1866 Copy(mbuf, shm + mpos, n, char);
1868 memzero(shm + mpos + n, msize - n);
1872 Perl_croak(aTHX_ "shm I/O not implemented");
1876 #endif /* SYSV IPC */