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(PerlIO_stderr(),
138 "Warning: unable to close filehandle %s properly.\n",
140 IoOFP(io) = IoIFP(io) = Nullfp;
144 #if defined(USE_64_BIT_OFFSETS) && 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);
1018 PerlIO_printf(fp, "%" PERL_PRIu64, (UV)SvUVX(sv));
1020 PerlIO_printf(fp, "%" PERL_PRId64, (IV)SvIVX(sv));
1023 PerlIO_printf(fp, "%lu", (unsigned long)SvUVX(sv));
1025 PerlIO_printf(fp, "%ld", (long)SvIVX(sv));
1027 return !PerlIO_error(fp);
1031 tmps = SvPV(sv, len);
1034 /* To detect whether the process is about to overstep its
1035 * filesize limit we would need getrlimit(). We could then
1036 * also transparently raise the limit with setrlimit() --
1037 * but only until the system hard limit/the filesystem limit,
1038 * at which we would get EPERM. Note that when using buffered
1039 * io the write failure can be delayed until the flush/close. --jhi */
1040 if (len && (PerlIO_write(fp,tmps,len) == 0 || PerlIO_error(fp)))
1042 return !PerlIO_error(fp);
1052 if (PL_op->op_flags & OPf_REF) {
1054 tmpgv = cGVOP->op_gv;
1057 if (io && IoIFP(io)) {
1059 sv_setpv(PL_statname,"");
1060 PL_laststype = OP_STAT;
1061 return (PL_laststatval = PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache));
1064 if (tmpgv == PL_defgv)
1065 return PL_laststatval;
1066 if (ckWARN(WARN_UNOPENED))
1067 Perl_warner(aTHX_ WARN_UNOPENED, "Stat on unopened file <%s>",
1070 sv_setpv(PL_statname,"");
1071 return (PL_laststatval = -1);
1079 if (SvTYPE(sv) == SVt_PVGV) {
1083 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
1084 tmpgv = (GV*)SvRV(sv);
1090 sv_setpv(PL_statname, s);
1091 PL_laststype = OP_STAT;
1092 PL_laststatval = PerlLIO_stat(s, &PL_statcache);
1093 if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(s, '\n'))
1094 Perl_warner(aTHX_ WARN_NEWLINE, PL_warn_nl, "stat");
1095 return PL_laststatval;
1105 if (PL_op->op_flags & OPf_REF) {
1107 if (cGVOP->op_gv == PL_defgv) {
1108 if (PL_laststype != OP_LSTAT)
1109 Perl_croak(aTHX_ "The stat preceding -l _ wasn't an lstat");
1110 return PL_laststatval;
1112 Perl_croak(aTHX_ "You can't use -l on a filehandle");
1115 PL_laststype = OP_LSTAT;
1119 sv_setpv(PL_statname,SvPV(sv, n_a));
1120 PL_laststatval = PerlLIO_lstat(SvPV(sv, n_a),&PL_statcache);
1121 if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(SvPV(sv, n_a), '\n'))
1122 Perl_warner(aTHX_ WARN_NEWLINE, PL_warn_nl, "lstat");
1123 return PL_laststatval;
1127 Perl_do_aexec(pTHX_ SV *really, register SV **mark, register SV **sp)
1129 return do_aexec5(really, mark, sp, 0, 0);
1133 Perl_do_aexec5(pTHX_ SV *really, register SV **mark, register SV **sp,
1134 int fd, int do_report)
1142 New(401,PL_Argv, sp - mark + 1, char*);
1144 while (++mark <= sp) {
1146 *a++ = SvPVx(*mark, n_a);
1151 if (*PL_Argv[0] != '/') /* will execvp use PATH? */
1152 TAINT_ENV(); /* testing IFS here is overkill, probably */
1153 if (really && *(tmps = SvPV(really, n_a)))
1154 PerlProc_execvp(tmps,PL_Argv);
1156 PerlProc_execvp(PL_Argv[0],PL_Argv);
1157 if (ckWARN(WARN_EXEC))
1158 Perl_warner(aTHX_ WARN_EXEC, "Can't exec \"%s\": %s",
1159 PL_Argv[0], Strerror(errno));
1163 PerlLIO_write(fd, (void*)&e, sizeof(int));
1172 Perl_do_execfree(pTHX)
1176 PL_Argv = Null(char **);
1184 #if !defined(OS2) && !defined(WIN32) && !defined(DJGPP) && !defined(EPOC)
1187 Perl_do_exec(pTHX_ char *cmd)
1189 return do_exec3(cmd,0,0);
1193 Perl_do_exec3(pTHX_ char *cmd, int fd, int do_report)
1199 while (*cmd && isSPACE(*cmd))
1202 /* save an extra exec if possible */
1205 if (strnEQ(cmd,PL_cshname,PL_cshlen) && strnEQ(cmd+PL_cshlen," -c",3)) {
1207 s = cmd+PL_cshlen+3;
1221 if (s[-1] == '\'') {
1223 PerlProc_execl(PL_cshname,"csh", flags,ncmd,(char*)0);
1231 /* see if there are shell metacharacters in it */
1233 if (*cmd == '.' && isSPACE(cmd[1]))
1236 if (strnEQ(cmd,"exec",4) && isSPACE(cmd[4]))
1239 for (s = cmd; *s && isALPHA(*s); s++) ; /* catch VAR=val gizmo */
1243 for (s = cmd; *s; s++) {
1244 if (*s != ' ' && !isALPHA(*s) && strchr("$&*(){}[]'\";\\|?<>~`\n",*s)) {
1245 if (*s == '\n' && !s[1]) {
1249 /* handle the 2>&1 construct at the end */
1250 if (*s == '>' && s[1] == '&' && s[2] == '1'
1251 && s > cmd + 1 && s[-1] == '2' && isSPACE(s[-2])
1252 && (!s[3] || isSPACE(s[3])))
1256 while (*t && isSPACE(*t))
1258 if (!*t && (dup2(1,2) != -1)) {
1264 PerlProc_execl(PL_sh_path, "sh", "-c", cmd, (char*)0);
1269 New(402,PL_Argv, (s - cmd) / 2 + 2, char*);
1270 PL_Cmd = savepvn(cmd, s-cmd);
1272 for (s = PL_Cmd; *s;) {
1273 while (*s && isSPACE(*s)) s++;
1276 while (*s && !isSPACE(*s)) s++;
1282 PerlProc_execvp(PL_Argv[0],PL_Argv);
1283 if (errno == ENOEXEC) { /* for system V NIH syndrome */
1291 if (ckWARN(WARN_EXEC))
1292 Perl_warner(aTHX_ WARN_EXEC, "Can't exec \"%s\": %s",
1293 PL_Argv[0], Strerror(errno));
1295 PerlLIO_write(fd, (void*)&e, sizeof(int));
1304 #endif /* OS2 || WIN32 */
1307 Perl_apply(pTHX_ I32 type, register SV **mark, register SV **sp)
1312 register I32 tot = 0;
1315 SV **oldmark = mark;
1318 #define APPLY_TAINT_PROPER() \
1320 if (PL_tainted) { TAINT_PROPER(what); } \
1323 /* This is a first heuristic; it doesn't catch tainting magic. */
1325 while (++mark <= sp) {
1326 if (SvTAINTED(*mark)) {
1336 APPLY_TAINT_PROPER();
1339 APPLY_TAINT_PROPER();
1341 while (++mark <= sp) {
1342 char *name = SvPVx(*mark, n_a);
1343 APPLY_TAINT_PROPER();
1344 if (PerlLIO_chmod(name, val))
1352 APPLY_TAINT_PROPER();
1353 if (sp - mark > 2) {
1354 val = SvIVx(*++mark);
1355 val2 = SvIVx(*++mark);
1356 APPLY_TAINT_PROPER();
1358 while (++mark <= sp) {
1359 char *name = SvPVx(*mark, n_a);
1360 APPLY_TAINT_PROPER();
1361 if (PerlLIO_chown(name, val, val2))
1368 XXX Should we make lchown() directly available from perl?
1369 For now, we'll let Configure test for HAS_LCHOWN, but do
1370 nothing in the core.
1376 APPLY_TAINT_PROPER();
1379 s = SvPVx(*++mark, n_a);
1381 if (*s == 'S' && s[1] == 'I' && s[2] == 'G')
1383 if (!(val = whichsig(s)))
1384 Perl_croak(aTHX_ "Unrecognized signal name \"%s\"",s);
1388 APPLY_TAINT_PROPER();
1391 /* kill() doesn't do process groups (job trees?) under VMS */
1392 if (val < 0) val = -val;
1393 if (val == SIGKILL) {
1394 # include <starlet.h>
1395 /* Use native sys$delprc() to insure that target process is
1396 * deleted; supervisor-mode images don't pay attention to
1397 * CRTL's emulation of Unix-style signals and kill()
1399 while (++mark <= sp) {
1400 I32 proc = SvIVx(*mark);
1401 register unsigned long int __vmssts;
1402 APPLY_TAINT_PROPER();
1403 if (!((__vmssts = sys$delprc(&proc,0)) & 1)) {
1407 case SS$_NOSUCHNODE:
1408 SETERRNO(ESRCH,__vmssts);
1411 SETERRNO(EPERM,__vmssts);
1414 SETERRNO(EVMSERR,__vmssts);
1423 while (++mark <= sp) {
1424 I32 proc = SvIVx(*mark);
1425 APPLY_TAINT_PROPER();
1427 if (PerlProc_killpg(proc,val)) /* BSD */
1429 if (PerlProc_kill(-proc,val)) /* SYSV */
1435 while (++mark <= sp) {
1436 I32 proc = SvIVx(*mark);
1437 APPLY_TAINT_PROPER();
1438 if (PerlProc_kill(proc, val))
1446 APPLY_TAINT_PROPER();
1448 while (++mark <= sp) {
1449 s = SvPVx(*mark, n_a);
1450 APPLY_TAINT_PROPER();
1451 if (PL_euid || PL_unsafe) {
1455 else { /* don't let root wipe out directories without -U */
1456 if (PerlLIO_lstat(s,&PL_statbuf) < 0 || S_ISDIR(PL_statbuf.st_mode))
1468 APPLY_TAINT_PROPER();
1469 if (sp - mark > 2) {
1470 #if defined(I_UTIME) || defined(VMS)
1471 struct utimbuf utbuf;
1479 Zero(&utbuf, sizeof utbuf, char);
1481 utbuf.actime = (Time_t)SvNVx(*++mark); /* time accessed */
1482 utbuf.modtime = (Time_t)SvNVx(*++mark); /* time modified */
1484 utbuf.actime = (Time_t)SvIVx(*++mark); /* time accessed */
1485 utbuf.modtime = (Time_t)SvIVx(*++mark); /* time modified */
1487 APPLY_TAINT_PROPER();
1489 while (++mark <= sp) {
1490 char *name = SvPVx(*mark, n_a);
1491 APPLY_TAINT_PROPER();
1492 if (PerlLIO_utime(name, &utbuf))
1503 #undef APPLY_TAINT_PROPER
1506 /* Do the permissions allow some operation? Assumes statcache already set. */
1507 #ifndef VMS /* VMS' cando is in vms.c */
1509 Perl_cando(pTHX_ Mode_t mode, Uid_t effective, register Stat_t *statbufp)
1510 /* Note: we use `effective' both for uids and gids.
1511 * Here we are betting on Uid_t being equal or wider than Gid_t. */
1514 /* [Comments and code from Len Reed]
1515 * MS-DOS "user" is similar to UNIX's "superuser," but can't write
1516 * to write-protected files. The execute permission bit is set
1517 * by the Miscrosoft C library stat() function for the following:
1522 * All files and directories are readable.
1523 * Directories and special files, e.g. "CON", cannot be
1525 * [Comment by Tom Dinger -- a directory can have the write-protect
1526 * bit set in the file system, but DOS permits changes to
1527 * the directory anyway. In addition, all bets are off
1528 * here for networked software, such as Novell and
1532 /* Atari stat() does pretty much the same thing. we set x_bit_set_in_stat
1533 * too so it will actually look into the files for magic numbers
1535 return (mode & statbufp->st_mode) ? TRUE : FALSE;
1537 #else /* ! DOSISH */
1538 if ((effective ? PL_euid : PL_uid) == 0) { /* root is special */
1539 if (mode == S_IXUSR) {
1540 if (statbufp->st_mode & 0111 || S_ISDIR(statbufp->st_mode))
1544 return TRUE; /* root reads and writes anything */
1547 if (statbufp->st_uid == (effective ? PL_euid : PL_uid) ) {
1548 if (statbufp->st_mode & mode)
1549 return TRUE; /* ok as "user" */
1551 else if (ingroup(statbufp->st_gid,effective)) {
1552 if (statbufp->st_mode & mode >> 3)
1553 return TRUE; /* ok as "group" */
1555 else if (statbufp->st_mode & mode >> 6)
1556 return TRUE; /* ok as "other" */
1558 #endif /* ! DOSISH */
1563 Perl_ingroup(pTHX_ Gid_t testgid, Uid_t effective)
1565 if (testgid == (effective ? PL_egid : PL_gid))
1567 #ifdef HAS_GETGROUPS
1572 Groups_t gary[NGROUPS];
1575 anum = getgroups(NGROUPS,gary);
1577 if (gary[anum] == testgid)
1584 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
1587 Perl_do_ipcget(pTHX_ I32 optype, SV **mark, SV **sp)
1593 key = (key_t)SvNVx(*++mark);
1594 n = (optype == OP_MSGGET) ? 0 : SvIVx(*++mark);
1595 flags = SvIVx(*++mark);
1601 return msgget(key, flags);
1605 return semget(key, n, flags);
1609 return shmget(key, n, flags);
1611 #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
1613 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
1616 return -1; /* should never happen */
1620 Perl_do_ipcctl(pTHX_ I32 optype, SV **mark, SV **sp)
1625 I32 id, n, cmd, infosize, getinfo;
1628 id = SvIVx(*++mark);
1629 n = (optype == OP_SEMCTL) ? SvIVx(*++mark) : 0;
1630 cmd = SvIVx(*++mark);
1633 getinfo = (cmd == IPC_STAT);
1639 if (cmd == IPC_STAT || cmd == IPC_SET)
1640 infosize = sizeof(struct msqid_ds);
1645 if (cmd == IPC_STAT || cmd == IPC_SET)
1646 infosize = sizeof(struct shmid_ds);
1652 if (cmd == IPC_STAT || cmd == IPC_SET)
1653 infosize = sizeof(struct semid_ds);
1654 else if (cmd == GETALL || cmd == SETALL)
1656 struct semid_ds semds;
1660 getinfo = (cmd == GETALL);
1661 if (Semctl(id, 0, IPC_STAT, semun) == -1)
1663 infosize = semds.sem_nsems * sizeof(short);
1664 /* "short" is technically wrong but much more portable
1665 than guessing about u_?short(_t)? */
1668 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
1672 #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
1674 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
1683 SvPV_force(astr, len);
1684 a = SvGROW(astr, infosize+1);
1688 a = SvPV(astr, len);
1689 if (len != infosize)
1690 Perl_croak(aTHX_ "Bad arg length for %s, is %lu, should be %ld",
1699 a = INT2PTR(char *,i); /* ouch */
1706 ret = msgctl(id, cmd, (struct msqid_ds *)a);
1712 union semun unsemds;
1714 unsemds.buf = (struct semid_ds *)a;
1715 ret = Semctl(id, n, cmd, unsemds);
1717 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
1724 ret = shmctl(id, cmd, (struct shmid_ds *)a);
1728 if (getinfo && ret >= 0) {
1729 SvCUR_set(astr, infosize);
1730 *SvEND(astr) = '\0';
1737 Perl_do_msgsnd(pTHX_ SV **mark, SV **sp)
1743 I32 id, msize, flags;
1746 id = SvIVx(*++mark);
1748 flags = SvIVx(*++mark);
1749 mbuf = SvPV(mstr, len);
1750 if ((msize = len - sizeof(long)) < 0)
1751 Perl_croak(aTHX_ "Arg too short for msgsnd");
1753 return msgsnd(id, (struct msgbuf *)mbuf, msize, flags);
1755 Perl_croak(aTHX_ "msgsnd not implemented");
1760 Perl_do_msgrcv(pTHX_ SV **mark, SV **sp)
1767 I32 id, msize, flags, ret;
1770 id = SvIVx(*++mark);
1772 msize = SvIVx(*++mark);
1773 mtype = (long)SvIVx(*++mark);
1774 flags = SvIVx(*++mark);
1775 SvPV_force(mstr, len);
1776 mbuf = SvGROW(mstr, sizeof(long)+msize+1);
1779 ret = msgrcv(id, (struct msgbuf *)mbuf, msize, mtype, flags);
1781 SvCUR_set(mstr, sizeof(long)+ret);
1782 *SvEND(mstr) = '\0';
1786 Perl_croak(aTHX_ "msgrcv not implemented");
1791 Perl_do_semop(pTHX_ SV **mark, SV **sp)
1800 id = SvIVx(*++mark);
1802 opbuf = SvPV(opstr, opsize);
1803 if (opsize < sizeof(struct sembuf)
1804 || (opsize % sizeof(struct sembuf)) != 0) {
1805 SETERRNO(EINVAL,LIB$_INVARG);
1809 return semop(id, (struct sembuf *)opbuf, opsize/sizeof(struct sembuf));
1811 Perl_croak(aTHX_ "semop not implemented");
1816 Perl_do_shmio(pTHX_ I32 optype, SV **mark, SV **sp)
1822 I32 id, mpos, msize;
1824 struct shmid_ds shmds;
1826 id = SvIVx(*++mark);
1828 mpos = SvIVx(*++mark);
1829 msize = SvIVx(*++mark);
1831 if (shmctl(id, IPC_STAT, &shmds) == -1)
1833 if (mpos < 0 || msize < 0 || mpos + msize > shmds.shm_segsz) {
1834 SETERRNO(EFAULT,SS$_ACCVIO); /* can't do as caller requested */
1837 shm = (char *)shmat(id, (char*)NULL, (optype == OP_SHMREAD) ? SHM_RDONLY : 0);
1838 if (shm == (char *)-1) /* I hate System V IPC, I really do */
1840 if (optype == OP_SHMREAD) {
1841 SvPV_force(mstr, len);
1842 mbuf = SvGROW(mstr, msize+1);
1844 Copy(shm + mpos, mbuf, msize, char);
1845 SvCUR_set(mstr, msize);
1846 *SvEND(mstr) = '\0';
1852 mbuf = SvPV(mstr, len);
1853 if ((n = len) > msize)
1855 Copy(mbuf, shm + mpos, n, char);
1857 memzero(shm + mpos + n, msize - n);
1861 Perl_croak(aTHX_ "shm I/O not implemented");
1865 #endif /* SYSV IPC */