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;
96 bool in_raw = 0, in_crlf = 0, out_raw = 0, out_crlf = 0;
98 PL_forkprocess = 1; /* assume true if no fork */
100 if (PL_op && PL_op->op_type == OP_OPEN) {
101 /* set up disciplines */
102 U8 flags = PL_op->op_private;
103 in_raw = (flags & OPpOPEN_IN_RAW);
104 in_crlf = (flags & OPpOPEN_IN_CRLF);
105 out_raw = (flags & OPpOPEN_OUT_RAW);
106 out_crlf = (flags & OPpOPEN_OUT_CRLF);
110 fd = PerlIO_fileno(IoIFP(io));
111 if (IoTYPE(io) == IoTYPE_STD)
113 else if (fd <= PL_maxsysfd) {
116 savetype = IoTYPE(io);
119 else if (IoTYPE(io) == IoTYPE_PIPE)
120 result = PerlProc_pclose(IoIFP(io));
121 else if (IoIFP(io) != IoOFP(io)) {
123 result = PerlIO_close(IoOFP(io));
124 PerlIO_close(IoIFP(io)); /* clear stdio, fd already closed */
127 result = PerlIO_close(IoIFP(io));
130 result = PerlIO_close(IoIFP(io));
131 if (result == EOF && fd > PL_maxsysfd)
132 PerlIO_printf(Perl_error_log,
133 "Warning: unable to close filehandle %s properly.\n",
135 IoOFP(io) = IoIFP(io) = Nullfp;
139 #if defined(USE_64_BIT_RAWIO) && defined(O_LARGEFILE)
140 rawmode |= O_LARGEFILE;
144 #define O_ACCMODE 3 /* Assume traditional implementation */
147 switch (result = rawmode & O_ACCMODE) {
149 IoTYPE(io) = IoTYPE_RDONLY;
152 IoTYPE(io) = IoTYPE_WRONLY;
156 IoTYPE(io) = IoTYPE_RDWR;
160 writing = (result > 0);
161 fd = PerlLIO_open3(name, rawmode, rawperm);
168 if (result == O_RDONLY)
171 else if (rawmode & O_APPEND) {
173 if (result != O_WRONLY)
178 if (result == O_WRONLY)
185 if (rawmode & O_BINARY)
188 fp = PerlIO_fdopen(fd, fpmode);
198 char mode[4]; /* stdio file mode ("r\0", "rb\0", "r+b\0" etc.) */
201 type = savepvn(name, len);
206 name = SvPV(svs, l) ;
208 name = savepvn(name, len);
212 while (tlen && isSPACE(type[tlen-1]))
217 mode[0] = mode[1] = mode[2] = mode[3] = '\0';
219 if (*type == '+' && tlen > 1 && type[tlen-1] != '|') { /* scary */
226 if (num_svs && (tlen != 2 || type[1] != '-')) {
228 Perl_croak(aTHX_ "Unknown open() mode '%.*s'", (int)olen, oname);
231 for (type++, tlen--; isSPACE(*type); type++, tlen--) ;
236 if (*name == '\0') { /* command is missing 19990114 */
238 if (ckWARN(WARN_PIPE))
239 Perl_warner(aTHX_ WARN_PIPE, "Missing command in piped open");
243 if (strNE(name,"-") || num_svs)
245 TAINT_PROPER("piped open");
246 if (name[len-1] == '|') {
249 if (ckWARN(WARN_PIPE))
250 Perl_warner(aTHX_ WARN_PIPE, "Can't open bidirectional pipe");
260 fp = PerlProc_popen(name,mode);
264 else if (*type == '>') {
265 TAINT_PROPER("open");
268 mode[0] = IoTYPE(io) = IoTYPE_APPEND;
281 if (num_svs && tlen != 1)
292 if (!*name && supplied_fp)
296 for (; isSPACE(*name); name++) ;
301 gv = gv_fetchpv(name,FALSE,SVt_PVIO);
305 SETERRNO(EINVAL,SS$_IVCHAN);
310 PerlIO *fp = IoIFP(thatio);
311 /* Flush stdio buffer before dup. --mjd
312 * Unfortunately SEEK_CURing 0 seems to
313 * be optimized away on most platforms;
314 * only Solaris and Linux seem to flush
316 PerlIO_seek(fp, 0, SEEK_CUR);
317 /* On the other hand, do all platforms
318 * take gracefully to flushing a read-only
319 * filehandle? Perhaps we should do
320 * fsetpos(src)+fgetpos(dst)? --nik */
322 fd = PerlIO_fileno(fp);
323 if (IoTYPE(thatio) == IoTYPE_SOCKET)
324 IoTYPE(io) = IoTYPE_SOCKET;
330 fd = PerlLIO_dup(fd);
333 if (!(fp = PerlIO_fdopen(fd,mode))) {
341 for (; isSPACE(*type); type++) ;
342 if (strEQ(type,"-")) {
343 fp = PerlIO_stdout();
344 IoTYPE(io) = IoTYPE_STD;
347 fp = PerlIO_open((num_svs ? name : type), mode);
351 else if (*type == '<') {
352 if (num_svs && tlen != 1)
355 for (type++; isSPACE(*type); type++) ;
366 if (strEQ(type,"-")) {
368 IoTYPE(io) = IoTYPE_STD;
371 fp = PerlIO_open((num_svs ? name : type), mode);
373 else if (tlen > 1 && type[tlen-1] == '|') {
375 if (tlen != 2 || type[0] != '-')
380 while (tlen && isSPACE(type[tlen-1]))
383 for (; isSPACE(*type); type++) ;
386 if (*name == '\0') { /* command is missing 19990114 */
388 if (ckWARN(WARN_PIPE))
389 Perl_warner(aTHX_ WARN_PIPE, "Missing command in piped open");
393 if (strNE(name,"-") || num_svs)
395 TAINT_PROPER("piped open");
404 fp = PerlProc_popen(name,mode);
406 IoTYPE(io) = IoTYPE_PIPE;
412 IoTYPE(io) = IoTYPE_RDONLY;
414 for (; isSPACE(*name); name++) ;
415 if (strEQ(name,"-")) {
417 IoTYPE(io) = IoTYPE_STD;
427 fp = PerlIO_open(name,mode);
433 if (ckWARN(WARN_NEWLINE) && IoTYPE(io) == IoTYPE_RDONLY && strchr(name, '\n'))
434 Perl_warner(aTHX_ WARN_NEWLINE, PL_warn_nl, "open");
438 IoTYPE(io) != IoTYPE_PIPE && IoTYPE(io) != IoTYPE_STD) {
440 if (PerlLIO_fstat(PerlIO_fileno(fp),&PL_statbuf) < 0) {
441 (void)PerlIO_close(fp);
444 if (S_ISSOCK(PL_statbuf.st_mode))
445 IoTYPE(io) = IoTYPE_SOCKET; /* in case a socket was passed in to us */
449 !(PL_statbuf.st_mode & S_IFMT)
455 Sock_size_t buflen = sizeof tmpbuf;
456 if (PerlSock_getsockname(PerlIO_fileno(fp), (struct sockaddr *)tmpbuf,
458 || errno != ENOTSOCK)
459 IoTYPE(io) = IoTYPE_SOCKET; /* some OS's return 0 on fstat()ed socket */
460 /* but some return 0 for streams too, sigh */
464 if (saveifp) { /* must use old fp? */
465 fd = PerlIO_fileno(saveifp);
467 PerlIO_flush(saveofp); /* emulate PerlIO_close() */
468 if (saveofp != saveifp) { /* was a socket? */
469 PerlIO_close(saveofp);
474 if (fd != PerlIO_fileno(fp)) {
478 PerlLIO_dup2(PerlIO_fileno(fp), fd);
480 sv = *av_fetch(PL_fdpid,PerlIO_fileno(fp),TRUE);
481 (void)SvUPGRADE(sv, SVt_IV);
484 sv = *av_fetch(PL_fdpid,fd,TRUE);
486 (void)SvUPGRADE(sv, SVt_IV);
495 #if defined(HAS_FCNTL) && defined(F_SETFD)
497 int save_errno = errno;
498 fd = PerlIO_fileno(fp);
499 fcntl(fd,F_SETFD,fd > PL_maxsysfd); /* can change errno */
504 IoFLAGS(io) &= ~IOf_NOLINE;
507 if (IoTYPE(io) == IoTYPE_SOCKET
508 || (IoTYPE(io) == IoTYPE_WRONLY && S_ISCHR(PL_statbuf.st_mode)) )
518 if (!(IoOFP(io) = PerlIO_fdopen(PerlIO_fileno(fp),mode))) {
532 IoTYPE(io) = savetype;
537 Perl_nextargv(pTHX_ register GV *gv)
540 #ifndef FLEXFILENAMES
549 PL_argvoutgv = gv_fetchpv("ARGVOUT",TRUE,SVt_PVIO);
550 if (io && (IoFLAGS(io) & IOf_ARGV) && (IoFLAGS(io) & IOf_START)) {
551 IoFLAGS(io) &= ~IOf_START;
553 if (!PL_argvout_stack)
554 PL_argvout_stack = newAV();
555 av_push(PL_argvout_stack, SvREFCNT_inc(PL_defoutgv));
558 if (PL_filemode & (S_ISUID|S_ISGID)) {
559 PerlIO_flush(IoIFP(GvIOn(PL_argvoutgv))); /* chmod must follow last write */
561 (void)fchmod(PL_lastfd,PL_filemode);
563 (void)PerlLIO_chmod(PL_oldname,PL_filemode);
567 while (av_len(GvAV(gv)) >= 0) {
570 sv = av_shift(GvAV(gv));
572 sv_setsv(GvSV(gv),sv);
573 SvSETMAGIC(GvSV(gv));
574 PL_oldname = SvPVx(GvSV(gv), oldlen);
575 if (do_open(gv,PL_oldname,oldlen,PL_inplace!=0,O_RDONLY,0,Nullfp)) {
577 TAINT_PROPER("inplace open");
578 if (oldlen == 1 && *PL_oldname == '-') {
579 setdefout(gv_fetchpv("STDOUT",TRUE,SVt_PVIO));
580 return IoIFP(GvIOp(gv));
582 #ifndef FLEXFILENAMES
583 filedev = PL_statbuf.st_dev;
584 fileino = PL_statbuf.st_ino;
586 PL_filemode = PL_statbuf.st_mode;
587 fileuid = PL_statbuf.st_uid;
588 filegid = PL_statbuf.st_gid;
589 if (!S_ISREG(PL_filemode)) {
590 if (ckWARN_d(WARN_INPLACE))
591 Perl_warner(aTHX_ WARN_INPLACE,
592 "Can't do inplace edit: %s is not a regular file",
598 char *star = strchr(PL_inplace, '*');
600 char *begin = PL_inplace;
601 sv_setpvn(sv, "", 0);
603 sv_catpvn(sv, begin, star - begin);
604 sv_catpvn(sv, PL_oldname, oldlen);
606 } while ((star = strchr(begin, '*')));
611 sv_catpv(sv,PL_inplace);
613 #ifndef FLEXFILENAMES
614 if (PerlLIO_stat(SvPVX(sv),&PL_statbuf) >= 0
615 && PL_statbuf.st_dev == filedev
616 && PL_statbuf.st_ino == fileino
618 || (_djstat_fail_bits & _STFAIL_TRUENAME)!=0
622 if (ckWARN_d(WARN_INPLACE))
623 Perl_warner(aTHX_ WARN_INPLACE,
624 "Can't do inplace edit: %s would not be unique",
631 #if !defined(DOSISH) && !defined(__CYGWIN__)
632 if (PerlLIO_rename(PL_oldname,SvPVX(sv)) < 0) {
633 if (ckWARN_d(WARN_INPLACE))
634 Perl_warner(aTHX_ WARN_INPLACE,
635 "Can't rename %s to %s: %s, skipping file",
636 PL_oldname, SvPVX(sv), Strerror(errno) );
642 (void)PerlLIO_unlink(SvPVX(sv));
643 (void)PerlLIO_rename(PL_oldname,SvPVX(sv));
644 do_open(gv,SvPVX(sv),SvCUR(sv),PL_inplace!=0,O_RDONLY,0,Nullfp);
647 (void)UNLINK(SvPVX(sv));
648 if (link(PL_oldname,SvPVX(sv)) < 0) {
649 if (ckWARN_d(WARN_INPLACE))
650 Perl_warner(aTHX_ WARN_INPLACE,
651 "Can't rename %s to %s: %s, skipping file",
652 PL_oldname, SvPVX(sv), Strerror(errno) );
656 (void)UNLINK(PL_oldname);
660 #if !defined(DOSISH) && !defined(AMIGAOS)
661 # ifndef VMS /* Don't delete; use automatic file versioning */
662 if (UNLINK(PL_oldname) < 0) {
663 if (ckWARN_d(WARN_INPLACE))
664 Perl_warner(aTHX_ WARN_INPLACE,
665 "Can't remove %s: %s, skipping file",
666 PL_oldname, Strerror(errno) );
672 Perl_croak(aTHX_ "Can't do inplace edit without backup");
676 sv_setpvn(sv,">",!PL_inplace);
677 sv_catpvn(sv,PL_oldname,oldlen);
678 SETERRNO(0,0); /* in case sprintf set errno */
680 if (!do_open(PL_argvoutgv,SvPVX(sv),SvCUR(sv),PL_inplace!=0,
681 O_WRONLY|O_CREAT|O_TRUNC,0,Nullfp))
683 if (!do_open(PL_argvoutgv,SvPVX(sv),SvCUR(sv),PL_inplace!=0,
684 O_WRONLY|O_CREAT|OPEN_EXCL,0666,Nullfp))
687 if (ckWARN_d(WARN_INPLACE))
688 Perl_warner(aTHX_ WARN_INPLACE, "Can't do inplace edit on %s: %s",
689 PL_oldname, Strerror(errno) );
693 setdefout(PL_argvoutgv);
694 PL_lastfd = PerlIO_fileno(IoIFP(GvIOp(PL_argvoutgv)));
695 (void)PerlLIO_fstat(PL_lastfd,&PL_statbuf);
697 (void)fchmod(PL_lastfd,PL_filemode);
699 # if !(defined(WIN32) && defined(__BORLANDC__))
700 /* Borland runtime creates a readonly file! */
701 (void)PerlLIO_chmod(PL_oldname,PL_filemode);
704 if (fileuid != PL_statbuf.st_uid || filegid != PL_statbuf.st_gid) {
706 (void)fchown(PL_lastfd,fileuid,filegid);
709 (void)PerlLIO_chown(PL_oldname,fileuid,filegid);
714 return IoIFP(GvIOp(gv));
718 if (ckWARN_d(WARN_INPLACE)) {
720 if (PerlLIO_stat(PL_oldname, &PL_statbuf) >= 0
721 && !S_ISREG(PL_statbuf.st_mode))
723 Perl_warner(aTHX_ WARN_INPLACE,
724 "Can't do inplace edit: %s is not a regular file",
728 Perl_warner(aTHX_ WARN_INPLACE, "Can't open %s: %s",
729 PL_oldname, Strerror(eno));
733 if (io && (IoFLAGS(io) & IOf_ARGV))
734 IoFLAGS(io) |= IOf_START;
736 (void)do_close(PL_argvoutgv,FALSE);
737 if (io && (IoFLAGS(io) & IOf_ARGV)
738 && PL_argvout_stack && AvFILLp(PL_argvout_stack) >= 0)
740 GV *oldout = (GV*)av_pop(PL_argvout_stack);
742 SvREFCNT_dec(oldout);
745 setdefout(gv_fetchpv("STDOUT",TRUE,SVt_PVIO));
752 Perl_do_pipe(pTHX_ SV *sv, GV *rgv, GV *wgv)
771 if (PerlProc_pipe(fd) < 0)
773 IoIFP(rstio) = PerlIO_fdopen(fd[0], "r");
774 IoOFP(wstio) = PerlIO_fdopen(fd[1], "w");
775 IoIFP(wstio) = IoOFP(wstio);
776 IoTYPE(rstio) = IoTYPE_RDONLY;
777 IoTYPE(wstio) = IoTYPE_WRONLY;
778 if (!IoIFP(rstio) || !IoOFP(wstio)) {
779 if (IoIFP(rstio)) PerlIO_close(IoIFP(rstio));
780 else PerlLIO_close(fd[0]);
781 if (IoOFP(wstio)) PerlIO_close(IoOFP(wstio));
782 else PerlLIO_close(fd[1]);
786 sv_setsv(sv,&PL_sv_yes);
790 sv_setsv(sv,&PL_sv_undef);
795 /* explicit renamed to avoid C++ conflict -- kja */
797 Perl_do_close(pTHX_ GV *gv, bool not_implicit)
804 if (!gv || SvTYPE(gv) != SVt_PVGV) {
806 SETERRNO(EBADF,SS$_IVCHAN);
810 if (!io) { /* never opened */
813 if (ckWARN(WARN_UNOPENED)) /* no check for closed here */
814 report_evil_fh(gv, io, PL_op->op_type);
815 SETERRNO(EBADF,SS$_IVCHAN);
819 retval = io_close(io, not_implicit);
823 IoLINES_LEFT(io) = IoPAGE_LEN(io);
825 IoTYPE(io) = IoTYPE_CLOSED;
830 Perl_io_close(pTHX_ IO *io, bool not_implicit)
836 if (IoTYPE(io) == IoTYPE_PIPE) {
837 status = PerlProc_pclose(IoIFP(io));
839 STATUS_NATIVE_SET(status);
840 retval = (STATUS_POSIX == 0);
843 retval = (status != -1);
846 else if (IoTYPE(io) == IoTYPE_STD)
849 if (IoOFP(io) && IoOFP(io) != IoIFP(io)) { /* a socket */
850 retval = (PerlIO_close(IoOFP(io)) != EOF);
851 PerlIO_close(IoIFP(io)); /* clear stdio, fd already closed */
854 retval = (PerlIO_close(IoIFP(io)) != EOF);
856 IoOFP(io) = IoIFP(io) = Nullfp;
858 else if (not_implicit) {
859 SETERRNO(EBADF,SS$_IVCHAN);
866 Perl_do_eof(pTHX_ GV *gv)
876 else if (ckWARN(WARN_IO)
877 && (IoTYPE(io) == IoTYPE_WRONLY || IoIFP(io) == PerlIO_stdout()
878 || IoIFP(io) == PerlIO_stderr()))
880 /* integrate to report_evil_fh()? */
883 SV* sv = sv_newmortal();
884 gv_efullname4(sv, gv, Nullch, FALSE);
885 name = SvPV_nolen(sv);
888 Perl_warner(aTHX_ WARN_IO,
889 "Filehandle %s opened only for output", name);
891 Perl_warner(aTHX_ WARN_IO,
892 "Filehandle opened only for output");
897 if (PerlIO_has_cntptr(IoIFP(io))) { /* (the code works without this) */
898 if (PerlIO_get_cnt(IoIFP(io)) > 0) /* cheat a little, since */
899 return FALSE; /* this is the most usual case */
902 ch = PerlIO_getc(IoIFP(io));
904 (void)PerlIO_ungetc(IoIFP(io),ch);
907 if (PerlIO_has_cntptr(IoIFP(io)) && PerlIO_canset_cnt(IoIFP(io))) {
908 if (PerlIO_get_cnt(IoIFP(io)) < -1)
909 PerlIO_set_cnt(IoIFP(io),-1);
911 if (PL_op->op_flags & OPf_SPECIAL) { /* not necessarily a real EOF yet? */
912 if (!nextargv(PL_argvgv)) /* get another fp handy */
916 return TRUE; /* normal fp, definitely end of file */
922 Perl_do_tell(pTHX_ GV *gv)
927 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io))) {
928 #ifdef ULTRIX_STDIO_BOTCH
930 (void)PerlIO_seek(fp, 0L, 2); /* ultrix 1.2 workaround */
932 return PerlIO_tell(fp);
936 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
937 report_evil_fh(gv, io, PL_op->op_type);
939 SETERRNO(EBADF,RMS$_IFI);
944 Perl_do_seek(pTHX_ GV *gv, Off_t pos, int whence)
949 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io))) {
950 #ifdef ULTRIX_STDIO_BOTCH
952 (void)PerlIO_seek(fp, 0L, 2); /* ultrix 1.2 workaround */
954 return PerlIO_seek(fp, pos, whence) >= 0;
958 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
959 report_evil_fh(gv, io, PL_op->op_type);
961 SETERRNO(EBADF,RMS$_IFI);
966 Perl_do_sysseek(pTHX_ GV *gv, Off_t pos, int whence)
971 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io)))
972 return PerlLIO_lseek(PerlIO_fileno(fp), pos, whence);
975 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
976 report_evil_fh(gv, io, PL_op->op_type);
978 SETERRNO(EBADF,RMS$_IFI);
983 Perl_mode_from_discipline(pTHX_ SV *discp)
988 char *s = SvPV(discp,len);
993 if (len > 3 && strnEQ(s+1, "raw", 3)
994 && (!s[4] || s[4] == ':' || isSPACE(s[4])))
1003 if (len > 4 && strnEQ(s+1, "crlf", 4)
1004 && (!s[5] || s[5] == ':' || isSPACE(s[5])))
1013 goto fail_discipline;
1016 else if (isSPACE(*s)) {
1023 end = strchr(s+1, ':');
1026 Perl_croak(aTHX_ "Unknown discipline '%.*s'", end-s, s);
1034 Perl_do_binmode(pTHX_ PerlIO *fp, int iotype, int mode)
1037 # if defined(atarist) || defined(__MINT__)
1038 if (!PerlIO_flush(fp)) {
1039 if (mode & O_BINARY)
1040 ((FILE*)fp)->_flag |= _IOBIN;
1042 ((FILE*)fp)->_flag &= ~ _IOBIN;
1047 if (PerlLIO_setmode(PerlIO_fileno(fp), mode) != -1) {
1048 # if defined(WIN32) && defined(__BORLANDC__)
1049 /* The translation mode of the stream is maintained independent
1050 * of the translation mode of the fd in the Borland RTL (heavy
1051 * digging through their runtime sources reveal). User has to
1052 * set the mode explicitly for the stream (though they don't
1053 * document this anywhere). GSAR 97-5-24
1055 PerlIO_seek(fp,0L,0);
1056 if (mode & O_BINARY)
1057 ((FILE*)fp)->flags |= _F_BIN;
1059 ((FILE*)fp)->flags &= ~ _F_BIN;
1067 # if defined(USEMYBINMODE)
1068 if (my_binmode(fp, iotype, mode) != FALSE)
1078 #if !defined(HAS_TRUNCATE) && !defined(HAS_CHSIZE) && defined(F_FREESP)
1079 /* code courtesy of William Kucharski */
1082 I32 my_chsize(fd, length)
1083 I32 fd; /* file descriptor */
1084 Off_t length; /* length to set file to */
1087 struct stat filebuf;
1089 if (PerlLIO_fstat(fd, &filebuf) < 0)
1092 if (filebuf.st_size < length) {
1094 /* extend file length */
1096 if ((PerlLIO_lseek(fd, (length - 1), 0)) < 0)
1099 /* write a "0" byte */
1101 if ((PerlLIO_write(fd, "", 1)) != 1)
1105 /* truncate length */
1109 fl.l_start = length;
1110 fl.l_type = F_WRLCK; /* write lock on file space */
1113 * This relies on the UNDOCUMENTED F_FREESP argument to
1114 * fcntl(2), which truncates the file so that it ends at the
1115 * position indicated by fl.l_start.
1117 * Will minor miracles never cease?
1120 if (fcntl(fd, F_FREESP, &fl) < 0)
1127 #endif /* F_FREESP */
1130 Perl_do_print(pTHX_ register SV *sv, PerlIO *fp)
1132 register char *tmps;
1135 /* assuming fp is checked earlier */
1141 if (SvIOK(sv) && SvIVX(sv) != 0) {
1142 PerlIO_printf(fp, PL_ofmt, (NV)SvIVX(sv));
1143 return !PerlIO_error(fp);
1145 if ( (SvNOK(sv) && SvNVX(sv) != 0.0)
1146 || (looks_like_number(sv) && sv_2nv(sv) != 0.0) ) {
1147 PerlIO_printf(fp, PL_ofmt, SvNVX(sv));
1148 return !PerlIO_error(fp);
1151 switch (SvTYPE(sv)) {
1155 if (ckWARN(WARN_UNINITIALIZED))
1164 PerlIO_printf(fp, "%"UVuf, (UV)SvUVX(sv));
1166 PerlIO_printf(fp, "%"IVdf, (IV)SvIVX(sv));
1167 return !PerlIO_error(fp);
1171 tmps = SvPV(sv, len);
1174 /* To detect whether the process is about to overstep its
1175 * filesize limit we would need getrlimit(). We could then
1176 * also transparently raise the limit with setrlimit() --
1177 * but only until the system hard limit/the filesystem limit,
1178 * at which we would get EPERM. Note that when using buffered
1179 * io the write failure can be delayed until the flush/close. --jhi */
1180 if (len && (PerlIO_write(fp,tmps,len) == 0 || PerlIO_error(fp)))
1182 return !PerlIO_error(fp);
1192 if (PL_op->op_flags & OPf_REF) {
1197 if (io && IoIFP(io)) {
1199 sv_setpv(PL_statname,"");
1200 PL_laststype = OP_STAT;
1201 return (PL_laststatval = PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache));
1205 return PL_laststatval;
1206 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1207 report_evil_fh(gv, io, PL_op->op_type);
1209 sv_setpv(PL_statname,"");
1210 return (PL_laststatval = -1);
1218 if (SvTYPE(sv) == SVt_PVGV) {
1222 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
1229 sv_setpv(PL_statname, s);
1230 PL_laststype = OP_STAT;
1231 PL_laststatval = PerlLIO_stat(s, &PL_statcache);
1232 if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(s, '\n'))
1233 Perl_warner(aTHX_ WARN_NEWLINE, PL_warn_nl, "stat");
1234 return PL_laststatval;
1244 if (PL_op->op_flags & OPf_REF) {
1246 if (cGVOP_gv == PL_defgv) {
1247 if (PL_laststype != OP_LSTAT)
1248 Perl_croak(aTHX_ "The stat preceding -l _ wasn't an lstat");
1249 return PL_laststatval;
1251 Perl_croak(aTHX_ "You can't use -l on a filehandle");
1254 PL_laststype = OP_LSTAT;
1258 sv_setpv(PL_statname,SvPV(sv, n_a));
1259 PL_laststatval = PerlLIO_lstat(SvPV(sv, n_a),&PL_statcache);
1260 if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(SvPV(sv, n_a), '\n'))
1261 Perl_warner(aTHX_ WARN_NEWLINE, PL_warn_nl, "lstat");
1262 return PL_laststatval;
1266 Perl_do_aexec(pTHX_ SV *really, register SV **mark, register SV **sp)
1268 return do_aexec5(really, mark, sp, 0, 0);
1272 Perl_do_aexec5(pTHX_ SV *really, register SV **mark, register SV **sp,
1273 int fd, int do_report)
1275 #ifdef MACOS_TRADITIONAL
1276 Perl_croak(aTHX_ "exec? I'm not *that* kind of operating system");
1284 New(401,PL_Argv, sp - mark + 1, char*);
1286 while (++mark <= sp) {
1288 *a++ = SvPVx(*mark, n_a);
1293 if (*PL_Argv[0] != '/') /* will execvp use PATH? */
1294 TAINT_ENV(); /* testing IFS here is overkill, probably */
1295 if (really && *(tmps = SvPV(really, n_a)))
1296 PerlProc_execvp(tmps,PL_Argv);
1298 PerlProc_execvp(PL_Argv[0],PL_Argv);
1299 if (ckWARN(WARN_EXEC))
1300 Perl_warner(aTHX_ WARN_EXEC, "Can't exec \"%s\": %s",
1301 PL_Argv[0], Strerror(errno));
1305 PerlLIO_write(fd, (void*)&e, sizeof(int));
1315 Perl_do_execfree(pTHX)
1319 PL_Argv = Null(char **);
1327 #if !defined(OS2) && !defined(WIN32) && !defined(DJGPP) && !defined(EPOC) && !defined(MACOS_TRADITIONAL)
1330 Perl_do_exec(pTHX_ char *cmd)
1332 return do_exec3(cmd,0,0);
1336 Perl_do_exec3(pTHX_ char *cmd, int fd, int do_report)
1342 while (*cmd && isSPACE(*cmd))
1345 /* save an extra exec if possible */
1348 if (strnEQ(cmd,PL_cshname,PL_cshlen) && strnEQ(cmd+PL_cshlen," -c",3)) {
1350 s = cmd+PL_cshlen+3;
1364 if (s[-1] == '\'') {
1366 PerlProc_execl(PL_cshname,"csh", flags,ncmd,(char*)0);
1374 /* see if there are shell metacharacters in it */
1376 if (*cmd == '.' && isSPACE(cmd[1]))
1379 if (strnEQ(cmd,"exec",4) && isSPACE(cmd[4]))
1382 for (s = cmd; *s && isALNUM(*s); s++) ; /* catch VAR=val gizmo */
1386 for (s = cmd; *s; s++) {
1387 if (*s != ' ' && !isALPHA(*s) && strchr("$&*(){}[]'\";\\|?<>~`\n",*s)) {
1388 if (*s == '\n' && !s[1]) {
1392 /* handle the 2>&1 construct at the end */
1393 if (*s == '>' && s[1] == '&' && s[2] == '1'
1394 && s > cmd + 1 && s[-1] == '2' && isSPACE(s[-2])
1395 && (!s[3] || isSPACE(s[3])))
1399 while (*t && isSPACE(*t))
1401 if (!*t && (dup2(1,2) != -1)) {
1407 PerlProc_execl(PL_sh_path, "sh", "-c", cmd, (char*)0);
1412 New(402,PL_Argv, (s - cmd) / 2 + 2, char*);
1413 PL_Cmd = savepvn(cmd, s-cmd);
1415 for (s = PL_Cmd; *s;) {
1416 while (*s && isSPACE(*s)) s++;
1419 while (*s && !isSPACE(*s)) s++;
1425 PerlProc_execvp(PL_Argv[0],PL_Argv);
1426 if (errno == ENOEXEC) { /* for system V NIH syndrome */
1434 if (ckWARN(WARN_EXEC))
1435 Perl_warner(aTHX_ WARN_EXEC, "Can't exec \"%s\": %s",
1436 PL_Argv[0], Strerror(errno));
1438 PerlLIO_write(fd, (void*)&e, sizeof(int));
1447 #endif /* OS2 || WIN32 */
1450 Perl_apply(pTHX_ I32 type, register SV **mark, register SV **sp)
1455 register I32 tot = 0;
1458 SV **oldmark = mark;
1461 #define APPLY_TAINT_PROPER() \
1463 if (PL_tainted) { TAINT_PROPER(what); } \
1466 /* This is a first heuristic; it doesn't catch tainting magic. */
1468 while (++mark <= sp) {
1469 if (SvTAINTED(*mark)) {
1479 APPLY_TAINT_PROPER();
1482 APPLY_TAINT_PROPER();
1484 while (++mark <= sp) {
1485 char *name = SvPVx(*mark, n_a);
1486 APPLY_TAINT_PROPER();
1487 if (PerlLIO_chmod(name, val))
1495 APPLY_TAINT_PROPER();
1496 if (sp - mark > 2) {
1497 val = SvIVx(*++mark);
1498 val2 = SvIVx(*++mark);
1499 APPLY_TAINT_PROPER();
1501 while (++mark <= sp) {
1502 char *name = SvPVx(*mark, n_a);
1503 APPLY_TAINT_PROPER();
1504 if (PerlLIO_chown(name, val, val2))
1511 XXX Should we make lchown() directly available from perl?
1512 For now, we'll let Configure test for HAS_LCHOWN, but do
1513 nothing in the core.
1519 APPLY_TAINT_PROPER();
1522 s = SvPVx(*++mark, n_a);
1524 if (*s == 'S' && s[1] == 'I' && s[2] == 'G')
1526 if (!(val = whichsig(s)))
1527 Perl_croak(aTHX_ "Unrecognized signal name \"%s\"",s);
1531 APPLY_TAINT_PROPER();
1534 /* kill() doesn't do process groups (job trees?) under VMS */
1535 if (val < 0) val = -val;
1536 if (val == SIGKILL) {
1537 # include <starlet.h>
1538 /* Use native sys$delprc() to insure that target process is
1539 * deleted; supervisor-mode images don't pay attention to
1540 * CRTL's emulation of Unix-style signals and kill()
1542 while (++mark <= sp) {
1543 I32 proc = SvIVx(*mark);
1544 register unsigned long int __vmssts;
1545 APPLY_TAINT_PROPER();
1546 if (!((__vmssts = sys$delprc(&proc,0)) & 1)) {
1550 case SS$_NOSUCHNODE:
1551 SETERRNO(ESRCH,__vmssts);
1554 SETERRNO(EPERM,__vmssts);
1557 SETERRNO(EVMSERR,__vmssts);
1566 while (++mark <= sp) {
1567 I32 proc = SvIVx(*mark);
1568 APPLY_TAINT_PROPER();
1570 if (PerlProc_killpg(proc,val)) /* BSD */
1572 if (PerlProc_kill(-proc,val)) /* SYSV */
1578 while (++mark <= sp) {
1579 I32 proc = SvIVx(*mark);
1580 APPLY_TAINT_PROPER();
1581 if (PerlProc_kill(proc, val))
1589 APPLY_TAINT_PROPER();
1591 while (++mark <= sp) {
1592 s = SvPVx(*mark, n_a);
1593 APPLY_TAINT_PROPER();
1594 if (PL_euid || PL_unsafe) {
1598 else { /* don't let root wipe out directories without -U */
1599 if (PerlLIO_lstat(s,&PL_statbuf) < 0 || S_ISDIR(PL_statbuf.st_mode))
1611 APPLY_TAINT_PROPER();
1612 if (sp - mark > 2) {
1613 #if defined(I_UTIME) || defined(VMS)
1614 struct utimbuf utbuf;
1622 Zero(&utbuf, sizeof utbuf, char);
1624 utbuf.actime = (Time_t)SvNVx(*++mark); /* time accessed */
1625 utbuf.modtime = (Time_t)SvNVx(*++mark); /* time modified */
1627 utbuf.actime = (Time_t)SvIVx(*++mark); /* time accessed */
1628 utbuf.modtime = (Time_t)SvIVx(*++mark); /* time modified */
1630 APPLY_TAINT_PROPER();
1632 while (++mark <= sp) {
1633 char *name = SvPVx(*mark, n_a);
1634 APPLY_TAINT_PROPER();
1635 if (PerlLIO_utime(name, &utbuf))
1646 #undef APPLY_TAINT_PROPER
1649 /* Do the permissions allow some operation? Assumes statcache already set. */
1650 #ifndef VMS /* VMS' cando is in vms.c */
1652 Perl_cando(pTHX_ Mode_t mode, Uid_t effective, register Stat_t *statbufp)
1653 /* Note: we use `effective' both for uids and gids.
1654 * Here we are betting on Uid_t being equal or wider than Gid_t. */
1657 /* [Comments and code from Len Reed]
1658 * MS-DOS "user" is similar to UNIX's "superuser," but can't write
1659 * to write-protected files. The execute permission bit is set
1660 * by the Miscrosoft C library stat() function for the following:
1665 * All files and directories are readable.
1666 * Directories and special files, e.g. "CON", cannot be
1668 * [Comment by Tom Dinger -- a directory can have the write-protect
1669 * bit set in the file system, but DOS permits changes to
1670 * the directory anyway. In addition, all bets are off
1671 * here for networked software, such as Novell and
1675 /* Atari stat() does pretty much the same thing. we set x_bit_set_in_stat
1676 * too so it will actually look into the files for magic numbers
1678 return (mode & statbufp->st_mode) ? TRUE : FALSE;
1680 #else /* ! DOSISH */
1681 if ((effective ? PL_euid : PL_uid) == 0) { /* root is special */
1682 if (mode == S_IXUSR) {
1683 if (statbufp->st_mode & 0111 || S_ISDIR(statbufp->st_mode))
1687 return TRUE; /* root reads and writes anything */
1690 if (statbufp->st_uid == (effective ? PL_euid : PL_uid) ) {
1691 if (statbufp->st_mode & mode)
1692 return TRUE; /* ok as "user" */
1694 else if (ingroup(statbufp->st_gid,effective)) {
1695 if (statbufp->st_mode & mode >> 3)
1696 return TRUE; /* ok as "group" */
1698 else if (statbufp->st_mode & mode >> 6)
1699 return TRUE; /* ok as "other" */
1701 #endif /* ! DOSISH */
1706 Perl_ingroup(pTHX_ Gid_t testgid, Uid_t effective)
1708 #ifdef MACOS_TRADITIONAL
1709 /* This is simply not correct for AppleShare, but fix it yerself. */
1712 if (testgid == (effective ? PL_egid : PL_gid))
1714 #ifdef HAS_GETGROUPS
1719 Groups_t gary[NGROUPS];
1722 anum = getgroups(NGROUPS,gary);
1724 if (gary[anum] == testgid)
1732 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
1735 Perl_do_ipcget(pTHX_ I32 optype, SV **mark, SV **sp)
1741 key = (key_t)SvNVx(*++mark);
1742 n = (optype == OP_MSGGET) ? 0 : SvIVx(*++mark);
1743 flags = SvIVx(*++mark);
1749 return msgget(key, flags);
1753 return semget(key, n, flags);
1757 return shmget(key, n, flags);
1759 #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
1761 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
1764 return -1; /* should never happen */
1768 Perl_do_ipcctl(pTHX_ I32 optype, SV **mark, SV **sp)
1773 I32 id, n, cmd, infosize, getinfo;
1776 id = SvIVx(*++mark);
1777 n = (optype == OP_SEMCTL) ? SvIVx(*++mark) : 0;
1778 cmd = SvIVx(*++mark);
1781 getinfo = (cmd == IPC_STAT);
1787 if (cmd == IPC_STAT || cmd == IPC_SET)
1788 infosize = sizeof(struct msqid_ds);
1793 if (cmd == IPC_STAT || cmd == IPC_SET)
1794 infosize = sizeof(struct shmid_ds);
1800 if (cmd == IPC_STAT || cmd == IPC_SET)
1801 infosize = sizeof(struct semid_ds);
1802 else if (cmd == GETALL || cmd == SETALL)
1804 struct semid_ds semds;
1806 #ifdef EXTRA_F_IN_SEMUN_BUF
1807 semun.buff = &semds;
1811 getinfo = (cmd == GETALL);
1812 if (Semctl(id, 0, IPC_STAT, semun) == -1)
1814 infosize = semds.sem_nsems * sizeof(short);
1815 /* "short" is technically wrong but much more portable
1816 than guessing about u_?short(_t)? */
1819 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
1823 #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
1825 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
1834 SvPV_force(astr, len);
1835 a = SvGROW(astr, infosize+1);
1839 a = SvPV(astr, len);
1840 if (len != infosize)
1841 Perl_croak(aTHX_ "Bad arg length for %s, is %lu, should be %ld",
1850 a = INT2PTR(char *,i); /* ouch */
1857 ret = msgctl(id, cmd, (struct msqid_ds *)a);
1863 union semun unsemds;
1865 #ifdef EXTRA_F_IN_SEMUN_BUF
1866 unsemds.buff = (struct semid_ds *)a;
1868 unsemds.buf = (struct semid_ds *)a;
1870 ret = Semctl(id, n, cmd, unsemds);
1872 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
1879 ret = shmctl(id, cmd, (struct shmid_ds *)a);
1883 if (getinfo && ret >= 0) {
1884 SvCUR_set(astr, infosize);
1885 *SvEND(astr) = '\0';
1892 Perl_do_msgsnd(pTHX_ SV **mark, SV **sp)
1898 I32 id, msize, flags;
1901 id = SvIVx(*++mark);
1903 flags = SvIVx(*++mark);
1904 mbuf = SvPV(mstr, len);
1905 if ((msize = len - sizeof(long)) < 0)
1906 Perl_croak(aTHX_ "Arg too short for msgsnd");
1908 return msgsnd(id, (struct msgbuf *)mbuf, msize, flags);
1910 Perl_croak(aTHX_ "msgsnd not implemented");
1915 Perl_do_msgrcv(pTHX_ SV **mark, SV **sp)
1922 I32 id, msize, flags, ret;
1925 id = SvIVx(*++mark);
1927 /* suppress warning when reading into undef var --jhi */
1929 sv_setpvn(mstr, "", 0);
1930 msize = SvIVx(*++mark);
1931 mtype = (long)SvIVx(*++mark);
1932 flags = SvIVx(*++mark);
1933 SvPV_force(mstr, len);
1934 mbuf = SvGROW(mstr, sizeof(long)+msize+1);
1937 ret = msgrcv(id, (struct msgbuf *)mbuf, msize, mtype, flags);
1939 SvCUR_set(mstr, sizeof(long)+ret);
1940 *SvEND(mstr) = '\0';
1941 #ifndef INCOMPLETE_TAINTS
1942 /* who knows who has been playing with this message? */
1948 Perl_croak(aTHX_ "msgrcv not implemented");
1953 Perl_do_semop(pTHX_ SV **mark, SV **sp)
1962 id = SvIVx(*++mark);
1964 opbuf = SvPV(opstr, opsize);
1965 if (opsize < sizeof(struct sembuf)
1966 || (opsize % sizeof(struct sembuf)) != 0) {
1967 SETERRNO(EINVAL,LIB$_INVARG);
1971 return semop(id, (struct sembuf *)opbuf, opsize/sizeof(struct sembuf));
1973 Perl_croak(aTHX_ "semop not implemented");
1978 Perl_do_shmio(pTHX_ I32 optype, SV **mark, SV **sp)
1984 I32 id, mpos, msize;
1986 struct shmid_ds shmds;
1988 id = SvIVx(*++mark);
1990 mpos = SvIVx(*++mark);
1991 msize = SvIVx(*++mark);
1993 if (shmctl(id, IPC_STAT, &shmds) == -1)
1995 if (mpos < 0 || msize < 0 || mpos + msize > shmds.shm_segsz) {
1996 SETERRNO(EFAULT,SS$_ACCVIO); /* can't do as caller requested */
1999 shm = (char *)shmat(id, (char*)NULL, (optype == OP_SHMREAD) ? SHM_RDONLY : 0);
2000 if (shm == (char *)-1) /* I hate System V IPC, I really do */
2002 if (optype == OP_SHMREAD) {
2003 /* suppress warning when reading into undef var (tchrist 3/Mar/00) */
2005 sv_setpvn(mstr, "", 0);
2006 SvPV_force(mstr, len);
2007 mbuf = SvGROW(mstr, msize+1);
2009 Copy(shm + mpos, mbuf, msize, char);
2010 SvCUR_set(mstr, msize);
2011 *SvEND(mstr) = '\0';
2013 #ifndef INCOMPLETE_TAINTS
2014 /* who knows who has been playing with this shared memory? */
2021 mbuf = SvPV(mstr, len);
2022 if ((n = len) > msize)
2024 Copy(mbuf, shm + mpos, n, char);
2026 memzero(shm + mpos + n, msize - n);
2030 Perl_croak(aTHX_ "shm I/O not implemented");
2034 #endif /* SYSV IPC */