3 * Copyright (c) 1991-2001, 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)
55 Perl_do_open(pTHX_ GV *gv, register char *name, I32 len, int as_raw,
56 int rawmode, int rawperm, PerlIO *supplied_fp)
58 return do_openn(gv, name, len, as_raw, rawmode, rawperm,
59 supplied_fp, (SV **) NULL, 0);
63 Perl_do_open9(pTHX_ GV *gv, register char *name, I32 len, int as_raw,
64 int rawmode, int rawperm, PerlIO *supplied_fp, SV *svs,
67 return do_openn(gv, name, len, as_raw, rawmode, rawperm,
68 supplied_fp, &svs, 1);
72 Perl_do_openn(pTHX_ GV *gv, register char *name, I32 len, int as_raw,
73 int rawmode, int rawperm, PerlIO *supplied_fp, SV **svp,
76 register IO *io = GvIOn(gv);
77 PerlIO *saveifp = Nullfp;
78 PerlIO *saveofp = Nullfp;
80 char savetype = IoTYPE_CLOSED;
85 bool was_fdopen = FALSE;
86 bool in_raw = 0, in_crlf = 0, out_raw = 0, out_crlf = 0;
88 char mode[8]; /* stdio file mode ("r\0", "rb\0", "r+b\0" etc.) */
91 Zero(mode,sizeof(mode),char);
92 PL_forkprocess = 1; /* assume true if no fork */
94 /* Collect default raw/crlf info from the op */
95 if (PL_op && PL_op->op_type == OP_OPEN) {
96 /* set up disciplines */
97 U8 flags = PL_op->op_private;
98 in_raw = (flags & OPpOPEN_IN_RAW);
99 in_crlf = (flags & OPpOPEN_IN_CRLF);
100 out_raw = (flags & OPpOPEN_OUT_RAW);
101 out_crlf = (flags & OPpOPEN_OUT_CRLF);
104 /* If currently open - close before we re-open */
106 fd = PerlIO_fileno(IoIFP(io));
107 if (IoTYPE(io) == IoTYPE_STD) {
108 /* This is a clone of one of STD* handles */
111 else if (fd >= 0 && fd <= PL_maxsysfd) {
112 /* This is one of the original STD* handles */
115 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 /* Why is this not Perl_warn*() call ? */
133 PerlIO_printf(Perl_error_log,
134 "Warning: unable to close filehandle %s properly.\n",
137 IoOFP(io) = IoIFP(io) = Nullfp;
141 /* sysopen style args, i.e. integer mode and permissions */
144 Perl_croak(aTHX_ "panic: sysopen with multiple args");
146 if (rawmode & (O_WRONLY|O_RDWR|O_CREAT
147 #ifdef O_APPEND /* Not fully portable. */
150 #ifdef O_TRUNC /* Not fully portable. */
154 TAINT_PROPER("sysopen");
155 mode[ix++] = '#'; /* Marker to openn to use numeric "sysopen" */
157 #if defined(USE_64_BIT_RAWIO) && defined(O_LARGEFILE)
158 rawmode |= O_LARGEFILE; /* Transparently largefiley. */
162 #define O_ACCMODE 3 /* Assume traditional implementation */
165 switch (result = rawmode & O_ACCMODE) {
167 IoTYPE(io) = IoTYPE_RDONLY;
170 IoTYPE(io) = IoTYPE_WRONLY;
174 IoTYPE(io) = IoTYPE_RDWR;
177 writing = (result != O_RDONLY);
179 if (result == O_RDONLY) {
183 else if (rawmode & O_APPEND) {
185 if (result != O_WRONLY)
190 if (result == O_WRONLY)
197 if (rawmode & O_BINARY)
201 namesv = sv_2mortal(newSVpvn(name,strlen(name)));
205 fp = PerlIO_openn(aTHX_ type, mode, -1, rawmode, rawperm, NULL, num_svs, svp);
208 /* Regular (non-sys) open */
214 type = savepvn(name, len);
217 /* Loose trailing white space */
218 while (tend > type && isSPACE(tend[-1]))
221 /* New style explict name, type is just mode and discipline/layer info */
223 name = SvOK(*svp) ? SvPV(*svp, l) : "";
225 name = savepvn(name, len);
228 for (; isSPACE(*type); type++) ;
235 if ((*type == IoTYPE_RDWR) && /* scary */
236 (*(type+1) == IoTYPE_RDONLY || *(type+1) == IoTYPE_WRONLY) &&
237 ((!num_svs || (tend > type+1 && tend[-1] != IoTYPE_PIPE)))) {
238 TAINT_PROPER("open");
243 if (*type == IoTYPE_PIPE) {
245 if (type[1] != IoTYPE_STD) {
247 Perl_croak(aTHX_ "Unknown open() mode '%.*s'", (int)olen, oname);
252 for (type++; isSPACE(*type); type++) ;
258 /* command is missing 19990114 */
259 if (ckWARN(WARN_PIPE))
260 Perl_warner(aTHX_ WARN_PIPE, "Missing command in piped open");
264 if (strNE(name,"-") || num_svs)
266 TAINT_PROPER("piped open");
267 if (!num_svs && name[len-1] == '|') {
269 if (ckWARN(WARN_PIPE))
270 Perl_warner(aTHX_ WARN_PIPE, "Can't open bidirectional pipe");
279 fp = PerlProc_popen_list(mode, num_svs, svp);
282 fp = PerlProc_popen(name,mode);
285 else if (*type == IoTYPE_WRONLY) {
286 TAINT_PROPER("open");
288 if (*type == IoTYPE_WRONLY) {
289 /* Two IoTYPE_WRONLYs in a row make for an IoTYPE_APPEND. */
290 mode[0] = IoTYPE(io) = IoTYPE_APPEND;
311 if (!num_svs && !*type && supplied_fp) {
312 /* "<+&" etc. is used by typemaps */
317 Perl_croak(aTHX_ "More than one argument to '%c&' open",IoTYPE(io));
319 if (num_svs && SvIOK(*svp)) {
322 else if (isDIGIT(*type)) {
324 for (; isSPACE(*type); type++) ;
330 thatio = sv_2io(*svp);
335 for (; isSPACE(*type); type++) ;
336 thatgv = gv_fetchpv(type,FALSE,SVt_PVIO);
337 thatio = GvIO(thatgv);
341 SETERRNO(EINVAL,SS$_IVCHAN);
346 PerlIO *fp = IoIFP(thatio);
347 /* Flush stdio buffer before dup. --mjd
348 * Unfortunately SEEK_CURing 0 seems to
349 * be optimized away on most platforms;
350 * only Solaris and Linux seem to flush
353 /* sfio fails to clear error on next
354 sfwrite, contrary to documentation.
356 if (PerlIO_seek(fp, 0, SEEK_CUR) == -1)
359 /* On the other hand, do all platforms
360 * take gracefully to flushing a read-only
361 * filehandle? Perhaps we should do
362 * fsetpos(src)+fgetpos(dst)? --nik */
364 fd = PerlIO_fileno(fp);
365 /* When dup()ing STDIN, STDOUT or STDERR
366 * explicitly set appropriate access mode */
367 if (IoIFP(thatio) == PerlIO_stdout()
368 || IoIFP(thatio) == PerlIO_stderr())
369 IoTYPE(io) = IoTYPE_WRONLY;
370 else if (IoIFP(thatio) == PerlIO_stdin())
371 IoTYPE(io) = IoTYPE_RDONLY;
372 /* When dup()ing a socket, say result is
374 else if (IoTYPE(thatio) == IoTYPE_SOCKET)
375 IoTYPE(io) = IoTYPE_SOCKET;
381 fd = PerlLIO_dup(fd);
386 if (!(fp = PerlIO_openn(aTHX_ type,mode,fd,0,0,NULL,num_svs,svp))) {
394 Perl_croak(aTHX_ "More than one argument to '>' open");
397 for (; isSPACE(*type); type++) ;
398 if (*type == IoTYPE_STD && (!type[1] || isSPACE(type[1]) || type[1] == ':')) {
401 fp = PerlIO_stdout();
402 IoTYPE(io) = IoTYPE_STD;
406 namesv = sv_2mortal(newSVpvn(type,strlen(type)));
411 fp = PerlIO_openn(aTHX_ type,mode,-1,0,0,NULL,num_svs,svp);
415 else if (*type == IoTYPE_RDONLY) {
417 Perl_croak(aTHX_ "More than one argument to '<' open");
420 for (type++; isSPACE(*type); type++) ;
430 if (*type == IoTYPE_STD && (!type[1] || isSPACE(type[1]) || type[1] == ':')) {
434 IoTYPE(io) = IoTYPE_STD;
438 namesv = sv_2mortal(newSVpvn(type,strlen(type)));
443 fp = PerlIO_openn(aTHX_ type,mode,-1,0,0,NULL,num_svs,svp);
446 else if ((num_svs && type[0] == IoTYPE_STD && type[1] == IoTYPE_PIPE) ||
447 (!num_svs && tend > type+1 && tend[-1] == IoTYPE_PIPE)) {
449 type += 2; /* skip over '-|' */
453 while (tend > type && isSPACE(tend[-1]))
456 for (; isSPACE(*type); type++) ;
461 /* command is missing 19990114 */
462 if (ckWARN(WARN_PIPE))
463 Perl_warner(aTHX_ WARN_PIPE, "Missing command in piped open");
467 if (strNE(name,"-") || num_svs)
469 TAINT_PROPER("piped open");
476 fp = PerlProc_popen_list(mode,num_svs,svp);
479 fp = PerlProc_popen(name,mode);
481 IoTYPE(io) = IoTYPE_PIPE;
487 IoTYPE(io) = IoTYPE_RDONLY;
489 for (; isSPACE(*name); name++) ;
495 if (strEQ(name,"-")) {
497 IoTYPE(io) = IoTYPE_STD;
501 namesv = sv_2mortal(newSVpvn(type,strlen(type)));
506 fp = PerlIO_openn(aTHX_ type,mode,-1,0,0,NULL,num_svs,svp);
511 if (ckWARN(WARN_NEWLINE) && IoTYPE(io) == IoTYPE_RDONLY && strchr(name, '\n'))
512 Perl_warner(aTHX_ WARN_NEWLINE, PL_warn_nl, "open");
516 if (ckWARN(WARN_IO)) {
517 if ((IoTYPE(io) == IoTYPE_RDONLY) &&
518 (fp == PerlIO_stdout() || fp == PerlIO_stderr())) {
519 Perl_warner(aTHX_ WARN_IO,
520 "Filehandle STD%s opened only for input",
521 (fp == PerlIO_stdout()) ? "OUT" : "ERR");
523 else if ((IoTYPE(io) == IoTYPE_WRONLY) && fp == PerlIO_stdin()) {
524 Perl_warner(aTHX_ WARN_IO,
525 "Filehandle STDIN opened only for output");
529 if (IoTYPE(io) && IoTYPE(io) != IoTYPE_PIPE && IoTYPE(io) != IoTYPE_STD &&
530 /* FIXME: This next term is a hack to avoid fileno on PerlIO::Scalar */
531 !(num_svs && SvROK(*svp))) {
532 if (PerlLIO_fstat(PerlIO_fileno(fp),&PL_statbuf) < 0) {
533 (void)PerlIO_close(fp);
536 if (S_ISSOCK(PL_statbuf.st_mode))
537 IoTYPE(io) = IoTYPE_SOCKET; /* in case a socket was passed in to us */
541 !(PL_statbuf.st_mode & S_IFMT)
545 && IoTYPE(io) != IoTYPE_WRONLY /* Dups of STD* filehandles already have */
546 && IoTYPE(io) != IoTYPE_RDONLY /* type so they aren't marked as sockets */
547 ) { /* on OS's that return 0 on fstat()ed pipe */
549 Sock_size_t buflen = sizeof tmpbuf;
550 if (PerlSock_getsockname(PerlIO_fileno(fp), (struct sockaddr *)tmpbuf,
552 || errno != ENOTSOCK)
553 IoTYPE(io) = IoTYPE_SOCKET; /* some OS's return 0 on fstat()ed socket */
554 /* but some return 0 for streams too, sigh */
558 if (saveifp) { /* must use old fp? */
559 /* If fd is less that PL_maxsysfd i.e. STDIN..STDERR
560 then dup the new fileno down
562 fd = PerlIO_fileno(fp);
564 PerlIO_flush(saveofp); /* emulate PerlIO_close() */
565 if (saveofp != saveifp) { /* was a socket? */
566 PerlIO_close(saveofp);
572 if (PerlLIO_dup2(fd, savefd) < 0) {
573 (void)PerlIO_close(fp);
577 if (savefd != PerlIO_fileno(PerlIO_stdin())) {
578 char newname[FILENAME_MAX+1];
579 if (PerlIO_getname(fp, newname)) {
580 if (fd == PerlIO_fileno(PerlIO_stdout())) Perl_vmssetuserlnm(aTHX_ "SYS$OUTPUT", newname);
581 if (fd == PerlIO_fileno(PerlIO_stderr())) Perl_vmssetuserlnm(aTHX_ "SYS$ERROR", newname);
586 sv = *av_fetch(PL_fdpid,fd,TRUE);
587 (void)SvUPGRADE(sv, SVt_IV);
590 sv = *av_fetch(PL_fdpid,savefd,TRUE);
592 (void)SvUPGRADE(sv, SVt_IV);
600 #if defined(HAS_FCNTL) && defined(F_SETFD)
602 int save_errno = errno;
603 fd = PerlIO_fileno(fp);
604 fcntl(fd,F_SETFD,fd > PL_maxsysfd); /* can change errno */
610 IoFLAGS(io) &= ~IOf_NOLINE;
612 if (IoTYPE(io) == IoTYPE_SOCKET
613 || (IoTYPE(io) == IoTYPE_WRONLY && S_ISCHR(PL_statbuf.st_mode)) ) {
615 if (!(IoOFP(io) = PerlIO_openn(aTHX_ type,mode,PerlIO_fileno(fp),0,0,NULL,num_svs,svp))) {
629 IoTYPE(io) = savetype;
634 Perl_nextargv(pTHX_ register GV *gv)
637 #ifndef FLEXFILENAMES
646 PL_argvoutgv = gv_fetchpv("ARGVOUT",TRUE,SVt_PVIO);
647 if (io && (IoFLAGS(io) & IOf_ARGV) && (IoFLAGS(io) & IOf_START)) {
648 IoFLAGS(io) &= ~IOf_START;
650 if (!PL_argvout_stack)
651 PL_argvout_stack = newAV();
652 av_push(PL_argvout_stack, SvREFCNT_inc(PL_defoutgv));
655 if (PL_filemode & (S_ISUID|S_ISGID)) {
656 PerlIO_flush(IoIFP(GvIOn(PL_argvoutgv))); /* chmod must follow last write */
658 (void)fchmod(PL_lastfd,PL_filemode);
660 (void)PerlLIO_chmod(PL_oldname,PL_filemode);
664 while (av_len(GvAV(gv)) >= 0) {
666 sv = av_shift(GvAV(gv));
668 sv_setsv(GvSV(gv),sv);
669 SvSETMAGIC(GvSV(gv));
670 PL_oldname = SvPVx(GvSV(gv), oldlen);
671 if (do_open(gv,PL_oldname,oldlen,PL_inplace!=0,O_RDONLY,0,Nullfp)) {
673 TAINT_PROPER("inplace open");
674 if (oldlen == 1 && *PL_oldname == '-') {
675 setdefout(gv_fetchpv("STDOUT",TRUE,SVt_PVIO));
676 return IoIFP(GvIOp(gv));
678 #ifndef FLEXFILENAMES
679 filedev = PL_statbuf.st_dev;
680 fileino = PL_statbuf.st_ino;
682 PL_filemode = PL_statbuf.st_mode;
683 fileuid = PL_statbuf.st_uid;
684 filegid = PL_statbuf.st_gid;
685 if (!S_ISREG(PL_filemode)) {
686 if (ckWARN_d(WARN_INPLACE))
687 Perl_warner(aTHX_ WARN_INPLACE,
688 "Can't do inplace edit: %s is not a regular file",
694 char *star = strchr(PL_inplace, '*');
696 char *begin = PL_inplace;
697 sv_setpvn(sv, "", 0);
699 sv_catpvn(sv, begin, star - begin);
700 sv_catpvn(sv, PL_oldname, oldlen);
702 } while ((star = strchr(begin, '*')));
707 sv_catpv(sv,PL_inplace);
709 #ifndef FLEXFILENAMES
710 if (PerlLIO_stat(SvPVX(sv),&PL_statbuf) >= 0
711 && PL_statbuf.st_dev == filedev
712 && PL_statbuf.st_ino == fileino
714 || (_djstat_fail_bits & _STFAIL_TRUENAME)!=0
718 if (ckWARN_d(WARN_INPLACE))
719 Perl_warner(aTHX_ WARN_INPLACE,
720 "Can't do inplace edit: %s would not be unique",
727 #if !defined(DOSISH) && !defined(__CYGWIN__)
728 if (PerlLIO_rename(PL_oldname,SvPVX(sv)) < 0) {
729 if (ckWARN_d(WARN_INPLACE))
730 Perl_warner(aTHX_ WARN_INPLACE,
731 "Can't rename %s to %s: %s, skipping file",
732 PL_oldname, SvPVX(sv), Strerror(errno) );
738 (void)PerlLIO_unlink(SvPVX(sv));
739 (void)PerlLIO_rename(PL_oldname,SvPVX(sv));
740 do_open(gv,SvPVX(sv),SvCUR(sv),PL_inplace!=0,O_RDONLY,0,Nullfp);
743 (void)UNLINK(SvPVX(sv));
744 if (link(PL_oldname,SvPVX(sv)) < 0) {
745 if (ckWARN_d(WARN_INPLACE))
746 Perl_warner(aTHX_ WARN_INPLACE,
747 "Can't rename %s to %s: %s, skipping file",
748 PL_oldname, SvPVX(sv), Strerror(errno) );
752 (void)UNLINK(PL_oldname);
756 #if !defined(DOSISH) && !defined(AMIGAOS)
757 # ifndef VMS /* Don't delete; use automatic file versioning */
758 if (UNLINK(PL_oldname) < 0) {
759 if (ckWARN_d(WARN_INPLACE))
760 Perl_warner(aTHX_ WARN_INPLACE,
761 "Can't remove %s: %s, skipping file",
762 PL_oldname, Strerror(errno) );
768 Perl_croak(aTHX_ "Can't do inplace edit without backup");
772 sv_setpvn(sv,">",!PL_inplace);
773 sv_catpvn(sv,PL_oldname,oldlen);
774 SETERRNO(0,0); /* in case sprintf set errno */
776 if (!do_open(PL_argvoutgv,SvPVX(sv),SvCUR(sv),PL_inplace!=0,
777 O_WRONLY|O_CREAT|O_TRUNC,0,Nullfp))
779 if (!do_open(PL_argvoutgv,SvPVX(sv),SvCUR(sv),PL_inplace!=0,
780 O_WRONLY|O_CREAT|OPEN_EXCL,0666,Nullfp))
783 if (ckWARN_d(WARN_INPLACE))
784 Perl_warner(aTHX_ WARN_INPLACE, "Can't do inplace edit on %s: %s",
785 PL_oldname, Strerror(errno) );
789 setdefout(PL_argvoutgv);
790 PL_lastfd = PerlIO_fileno(IoIFP(GvIOp(PL_argvoutgv)));
791 (void)PerlLIO_fstat(PL_lastfd,&PL_statbuf);
793 (void)fchmod(PL_lastfd,PL_filemode);
795 # if !(defined(WIN32) && defined(__BORLANDC__))
796 /* Borland runtime creates a readonly file! */
797 (void)PerlLIO_chmod(PL_oldname,PL_filemode);
800 if (fileuid != PL_statbuf.st_uid || filegid != PL_statbuf.st_gid) {
802 (void)fchown(PL_lastfd,fileuid,filegid);
805 (void)PerlLIO_chown(PL_oldname,fileuid,filegid);
810 return IoIFP(GvIOp(gv));
813 if (ckWARN_d(WARN_INPLACE)) {
815 if (PerlLIO_stat(PL_oldname, &PL_statbuf) >= 0
816 && !S_ISREG(PL_statbuf.st_mode))
818 Perl_warner(aTHX_ WARN_INPLACE,
819 "Can't do inplace edit: %s is not a regular file",
823 Perl_warner(aTHX_ WARN_INPLACE, "Can't open %s: %s",
824 PL_oldname, Strerror(eno));
828 if (io && (IoFLAGS(io) & IOf_ARGV))
829 IoFLAGS(io) |= IOf_START;
831 (void)do_close(PL_argvoutgv,FALSE);
832 if (io && (IoFLAGS(io) & IOf_ARGV)
833 && PL_argvout_stack && AvFILLp(PL_argvout_stack) >= 0)
835 GV *oldout = (GV*)av_pop(PL_argvout_stack);
837 SvREFCNT_dec(oldout);
840 setdefout(gv_fetchpv("STDOUT",TRUE,SVt_PVIO));
847 Perl_do_pipe(pTHX_ SV *sv, GV *rgv, GV *wgv)
866 if (PerlProc_pipe(fd) < 0)
868 IoIFP(rstio) = PerlIO_fdopen(fd[0], "r");
869 IoOFP(wstio) = PerlIO_fdopen(fd[1], "w");
870 IoIFP(wstio) = IoOFP(wstio);
871 IoTYPE(rstio) = IoTYPE_RDONLY;
872 IoTYPE(wstio) = IoTYPE_WRONLY;
873 if (!IoIFP(rstio) || !IoOFP(wstio)) {
874 if (IoIFP(rstio)) PerlIO_close(IoIFP(rstio));
875 else PerlLIO_close(fd[0]);
876 if (IoOFP(wstio)) PerlIO_close(IoOFP(wstio));
877 else PerlLIO_close(fd[1]);
881 sv_setsv(sv,&PL_sv_yes);
885 sv_setsv(sv,&PL_sv_undef);
890 /* explicit renamed to avoid C++ conflict -- kja */
892 Perl_do_close(pTHX_ GV *gv, bool not_implicit)
899 if (!gv || SvTYPE(gv) != SVt_PVGV) {
901 SETERRNO(EBADF,SS$_IVCHAN);
905 if (!io) { /* never opened */
907 if (ckWARN(WARN_UNOPENED)) /* no check for closed here */
908 report_evil_fh(gv, io, PL_op->op_type);
909 SETERRNO(EBADF,SS$_IVCHAN);
913 retval = io_close(io, not_implicit);
917 IoLINES_LEFT(io) = IoPAGE_LEN(io);
919 IoTYPE(io) = IoTYPE_CLOSED;
924 Perl_io_close(pTHX_ IO *io, bool not_implicit)
930 if (IoTYPE(io) == IoTYPE_PIPE) {
931 status = PerlProc_pclose(IoIFP(io));
933 STATUS_NATIVE_SET(status);
934 retval = (STATUS_POSIX == 0);
937 retval = (status != -1);
940 else if (IoTYPE(io) == IoTYPE_STD)
943 if (IoOFP(io) && IoOFP(io) != IoIFP(io)) { /* a socket */
944 retval = (PerlIO_close(IoOFP(io)) != EOF);
945 PerlIO_close(IoIFP(io)); /* clear stdio, fd already closed */
948 retval = (PerlIO_close(IoIFP(io)) != EOF);
950 IoOFP(io) = IoIFP(io) = Nullfp;
952 else if (not_implicit) {
953 SETERRNO(EBADF,SS$_IVCHAN);
960 Perl_do_eof(pTHX_ GV *gv)
969 else if (ckWARN(WARN_IO) && (IoTYPE(io) == IoTYPE_WRONLY))
971 /* integrate to report_evil_fh()? */
974 SV* sv = sv_newmortal();
975 gv_efullname4(sv, gv, Nullch, FALSE);
976 name = SvPV_nolen(sv);
979 Perl_warner(aTHX_ WARN_IO,
980 "Filehandle %s opened only for output", name);
982 Perl_warner(aTHX_ WARN_IO,
983 "Filehandle opened only for output");
988 if (PerlIO_has_cntptr(IoIFP(io))) { /* (the code works without this) */
989 if (PerlIO_get_cnt(IoIFP(io)) > 0) /* cheat a little, since */
990 return FALSE; /* this is the most usual case */
993 ch = PerlIO_getc(IoIFP(io));
995 (void)PerlIO_ungetc(IoIFP(io),ch);
999 if (PerlIO_has_cntptr(IoIFP(io)) && PerlIO_canset_cnt(IoIFP(io))) {
1000 if (PerlIO_get_cnt(IoIFP(io)) < -1)
1001 PerlIO_set_cnt(IoIFP(io),-1);
1003 if (PL_op->op_flags & OPf_SPECIAL) { /* not necessarily a real EOF yet? */
1004 if (!nextargv(PL_argvgv)) /* get another fp handy */
1008 return TRUE; /* normal fp, definitely end of file */
1014 Perl_do_tell(pTHX_ GV *gv)
1016 register IO *io = 0;
1017 register PerlIO *fp;
1019 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io))) {
1020 #ifdef ULTRIX_STDIO_BOTCH
1022 (void)PerlIO_seek(fp, 0L, 2); /* ultrix 1.2 workaround */
1024 return PerlIO_tell(fp);
1026 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1027 report_evil_fh(gv, io, PL_op->op_type);
1028 SETERRNO(EBADF,RMS$_IFI);
1033 Perl_do_seek(pTHX_ GV *gv, Off_t pos, int whence)
1035 register IO *io = 0;
1036 register PerlIO *fp;
1038 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io))) {
1039 #ifdef ULTRIX_STDIO_BOTCH
1041 (void)PerlIO_seek(fp, 0L, 2); /* ultrix 1.2 workaround */
1043 return PerlIO_seek(fp, pos, whence) >= 0;
1045 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1046 report_evil_fh(gv, io, PL_op->op_type);
1047 SETERRNO(EBADF,RMS$_IFI);
1052 Perl_do_sysseek(pTHX_ GV *gv, Off_t pos, int whence)
1054 register IO *io = 0;
1055 register PerlIO *fp;
1057 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io)))
1058 return PerlLIO_lseek(PerlIO_fileno(fp), pos, whence);
1059 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1060 report_evil_fh(gv, io, PL_op->op_type);
1061 SETERRNO(EBADF,RMS$_IFI);
1066 Perl_mode_from_discipline(pTHX_ SV *discp)
1068 int mode = O_BINARY;
1071 char *s = SvPV(discp,len);
1076 if (len > 3 && strnEQ(s+1, "raw", 3)
1077 && (!s[4] || s[4] == ':' || isSPACE(s[4])))
1086 if (len > 4 && strnEQ(s+1, "crlf", 4)
1087 && (!s[5] || s[5] == ':' || isSPACE(s[5])))
1096 goto fail_discipline;
1099 else if (isSPACE(*s)) {
1106 end = strchr(s+1, ':');
1109 #ifndef PERLIO_LAYERS
1110 Perl_croak(aTHX_ "Unknown discipline '%.*s'", end-s, s);
1121 Perl_do_binmode(pTHX_ PerlIO *fp, int iotype, int mode)
1123 /* The old body of this is now in non-LAYER part of perlio.c
1124 * This is a stub for any XS code which might have been calling it.
1126 char *name = (O_BINARY != O_TEXT && !(mode & O_BINARY)) ? ":crlf" : ":raw";
1127 return PerlIO_binmode(aTHX_ fp, iotype, mode, name);
1130 #if !defined(HAS_TRUNCATE) && !defined(HAS_CHSIZE) && defined(F_FREESP)
1131 /* code courtesy of William Kucharski */
1134 I32 my_chsize(fd, length)
1135 I32 fd; /* file descriptor */
1136 Off_t length; /* length to set file to */
1139 struct stat filebuf;
1141 if (PerlLIO_fstat(fd, &filebuf) < 0)
1144 if (filebuf.st_size < length) {
1146 /* extend file length */
1148 if ((PerlLIO_lseek(fd, (length - 1), 0)) < 0)
1151 /* write a "0" byte */
1153 if ((PerlLIO_write(fd, "", 1)) != 1)
1157 /* truncate length */
1161 fl.l_start = length;
1162 fl.l_type = F_WRLCK; /* write lock on file space */
1165 * This relies on the UNDOCUMENTED F_FREESP argument to
1166 * fcntl(2), which truncates the file so that it ends at the
1167 * position indicated by fl.l_start.
1169 * Will minor miracles never cease?
1172 if (fcntl(fd, F_FREESP, &fl) < 0)
1179 #endif /* F_FREESP */
1182 Perl_do_print(pTHX_ register SV *sv, PerlIO *fp)
1184 register char *tmps;
1187 /* assuming fp is checked earlier */
1193 if (SvIOK(sv) && SvIVX(sv) != 0) {
1194 PerlIO_printf(fp, PL_ofmt, (NV)SvIVX(sv));
1195 return !PerlIO_error(fp);
1197 if ( (SvNOK(sv) && SvNVX(sv) != 0.0)
1198 || (looks_like_number(sv) && sv_2nv(sv) != 0.0) ) {
1199 PerlIO_printf(fp, PL_ofmt, SvNVX(sv));
1200 return !PerlIO_error(fp);
1203 switch (SvTYPE(sv)) {
1205 if (ckWARN(WARN_UNINITIALIZED))
1213 PerlIO_printf(fp, "%"UVuf, (UV)SvUVX(sv));
1215 PerlIO_printf(fp, "%"IVdf, (IV)SvIVX(sv));
1216 return !PerlIO_error(fp);
1220 if (PerlIO_isutf8(fp)) {
1222 sv_utf8_upgrade(sv = sv_mortalcopy(sv));
1224 else if (DO_UTF8(sv)) {
1225 if (!sv_utf8_downgrade((sv = sv_mortalcopy(sv)), TRUE)
1226 && ckWARN(WARN_UTF8))
1228 Perl_warner(aTHX_ WARN_UTF8, "Wide character in print");
1231 tmps = SvPV(sv, len);
1234 /* To detect whether the process is about to overstep its
1235 * filesize limit we would need getrlimit(). We could then
1236 * also transparently raise the limit with setrlimit() --
1237 * but only until the system hard limit/the filesystem limit,
1238 * at which we would get EPERM. Note that when using buffered
1239 * io the write failure can be delayed until the flush/close. --jhi */
1240 if (len && (PerlIO_write(fp,tmps,len) == 0))
1242 return !PerlIO_error(fp);
1252 if (PL_op->op_flags & OPf_REF) {
1257 if (io && IoIFP(io)) {
1259 sv_setpv(PL_statname,"");
1260 PL_laststype = OP_STAT;
1261 return (PL_laststatval = PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache));
1265 return PL_laststatval;
1266 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1267 report_evil_fh(gv, io, PL_op->op_type);
1269 sv_setpv(PL_statname,"");
1270 return (PL_laststatval = -1);
1278 if (SvTYPE(sv) == SVt_PVGV) {
1282 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
1289 sv_setpv(PL_statname, s);
1290 PL_laststype = OP_STAT;
1291 PL_laststatval = PerlLIO_stat(s, &PL_statcache);
1292 if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(s, '\n'))
1293 Perl_warner(aTHX_ WARN_NEWLINE, PL_warn_nl, "stat");
1294 return PL_laststatval;
1304 if (PL_op->op_flags & OPf_REF) {
1306 if (cGVOP_gv == PL_defgv) {
1307 if (PL_laststype != OP_LSTAT)
1308 Perl_croak(aTHX_ "The stat preceding -l _ wasn't an lstat");
1309 return PL_laststatval;
1311 Perl_croak(aTHX_ "You can't use -l on a filehandle");
1314 PL_laststype = OP_LSTAT;
1318 sv_setpv(PL_statname,SvPV(sv, n_a));
1319 PL_laststatval = PerlLIO_lstat(SvPV(sv, n_a),&PL_statcache);
1320 if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(SvPV(sv, n_a), '\n'))
1321 Perl_warner(aTHX_ WARN_NEWLINE, PL_warn_nl, "lstat");
1322 return PL_laststatval;
1326 Perl_do_aexec(pTHX_ SV *really, register SV **mark, register SV **sp)
1328 return do_aexec5(really, mark, sp, 0, 0);
1332 Perl_do_aexec5(pTHX_ SV *really, register SV **mark, register SV **sp,
1333 int fd, int do_report)
1335 #ifdef MACOS_TRADITIONAL
1336 Perl_croak(aTHX_ "exec? I'm not *that* kind of operating system");
1339 char *tmps = Nullch;
1343 New(401,PL_Argv, sp - mark + 1, char*);
1345 while (++mark <= sp) {
1347 *a++ = SvPVx(*mark, n_a);
1353 tmps = SvPV(really, n_a);
1354 if ((!really && *PL_Argv[0] != '/') ||
1355 (really && *tmps != '/')) /* will execvp use PATH? */
1356 TAINT_ENV(); /* testing IFS here is overkill, probably */
1357 if (really && *tmps)
1358 PerlProc_execvp(tmps,EXEC_ARGV_CAST(PL_Argv));
1360 PerlProc_execvp(PL_Argv[0],EXEC_ARGV_CAST(PL_Argv));
1361 if (ckWARN(WARN_EXEC))
1362 Perl_warner(aTHX_ WARN_EXEC, "Can't exec \"%s\": %s",
1363 (really ? tmps : PL_Argv[0]), Strerror(errno));
1367 PerlLIO_write(fd, (void*)&e, sizeof(int));
1377 Perl_do_execfree(pTHX)
1381 PL_Argv = Null(char **);
1389 #if !defined(OS2) && !defined(WIN32) && !defined(DJGPP) && !defined(EPOC) && !defined(MACOS_TRADITIONAL)
1392 Perl_do_exec(pTHX_ char *cmd)
1394 return do_exec3(cmd,0,0);
1398 Perl_do_exec3(pTHX_ char *cmd, int fd, int do_report)
1403 while (*cmd && isSPACE(*cmd))
1406 /* save an extra exec if possible */
1411 if (strnEQ(cmd,PL_cshname,PL_cshlen) &&
1412 strnEQ(cmd+PL_cshlen," -c",3)) {
1414 s = cmd+PL_cshlen+3;
1428 if (s[-1] == '\'') {
1430 PerlProc_execl(PL_cshname,"csh", flags, ncmd, (char*)0);
1439 /* see if there are shell metacharacters in it */
1441 if (*cmd == '.' && isSPACE(cmd[1]))
1444 if (strnEQ(cmd,"exec",4) && isSPACE(cmd[4]))
1447 for (s = cmd; *s && isALNUM(*s); s++) ; /* catch VAR=val gizmo */
1451 for (s = cmd; *s; s++) {
1452 if (*s != ' ' && !isALPHA(*s) &&
1453 strchr("$&*(){}[]'\";\\|?<>~`\n",*s)) {
1454 if (*s == '\n' && !s[1]) {
1458 /* handle the 2>&1 construct at the end */
1459 if (*s == '>' && s[1] == '&' && s[2] == '1'
1460 && s > cmd + 1 && s[-1] == '2' && isSPACE(s[-2])
1461 && (!s[3] || isSPACE(s[3])))
1465 while (*t && isSPACE(*t))
1467 if (!*t && (dup2(1,2) != -1)) {
1473 PerlProc_execl(PL_sh_path, "sh", "-c", cmd, (char*)0);
1478 New(402,PL_Argv, (s - cmd) / 2 + 2, char*);
1479 PL_Cmd = savepvn(cmd, s-cmd);
1481 for (s = PL_Cmd; *s;) {
1482 while (*s && isSPACE(*s)) s++;
1485 while (*s && !isSPACE(*s)) s++;
1491 PerlProc_execvp(PL_Argv[0],PL_Argv);
1492 if (errno == ENOEXEC) { /* for system V NIH syndrome */
1499 if (ckWARN(WARN_EXEC))
1500 Perl_warner(aTHX_ WARN_EXEC, "Can't exec \"%s\": %s",
1501 PL_Argv[0], Strerror(errno));
1503 PerlLIO_write(fd, (void*)&e, sizeof(int));
1512 #endif /* OS2 || WIN32 */
1515 Perl_apply(pTHX_ I32 type, register SV **mark, register SV **sp)
1519 register I32 tot = 0;
1522 SV **oldmark = mark;
1525 #define APPLY_TAINT_PROPER() \
1527 if (PL_tainted) { TAINT_PROPER(what); } \
1530 /* This is a first heuristic; it doesn't catch tainting magic. */
1532 while (++mark <= sp) {
1533 if (SvTAINTED(*mark)) {
1543 APPLY_TAINT_PROPER();
1546 APPLY_TAINT_PROPER();
1548 while (++mark <= sp) {
1549 char *name = SvPVx(*mark, n_a);
1550 APPLY_TAINT_PROPER();
1551 if (PerlLIO_chmod(name, val))
1559 APPLY_TAINT_PROPER();
1560 if (sp - mark > 2) {
1561 val = SvIVx(*++mark);
1562 val2 = SvIVx(*++mark);
1563 APPLY_TAINT_PROPER();
1565 while (++mark <= sp) {
1566 char *name = SvPVx(*mark, n_a);
1567 APPLY_TAINT_PROPER();
1568 if (PerlLIO_chown(name, val, val2))
1575 XXX Should we make lchown() directly available from perl?
1576 For now, we'll let Configure test for HAS_LCHOWN, but do
1577 nothing in the core.
1583 APPLY_TAINT_PROPER();
1586 s = SvPVx(*++mark, n_a);
1588 if (*s == 'S' && s[1] == 'I' && s[2] == 'G')
1590 if (!(val = whichsig(s)))
1591 Perl_croak(aTHX_ "Unrecognized signal name \"%s\"",s);
1595 APPLY_TAINT_PROPER();
1598 /* kill() doesn't do process groups (job trees?) under VMS */
1599 if (val < 0) val = -val;
1600 if (val == SIGKILL) {
1601 # include <starlet.h>
1602 /* Use native sys$delprc() to insure that target process is
1603 * deleted; supervisor-mode images don't pay attention to
1604 * CRTL's emulation of Unix-style signals and kill()
1606 while (++mark <= sp) {
1607 I32 proc = SvIVx(*mark);
1608 register unsigned long int __vmssts;
1609 APPLY_TAINT_PROPER();
1610 if (!((__vmssts = sys$delprc(&proc,0)) & 1)) {
1614 case SS$_NOSUCHNODE:
1615 SETERRNO(ESRCH,__vmssts);
1618 SETERRNO(EPERM,__vmssts);
1621 SETERRNO(EVMSERR,__vmssts);
1630 while (++mark <= sp) {
1631 I32 proc = SvIVx(*mark);
1632 APPLY_TAINT_PROPER();
1634 if (PerlProc_killpg(proc,val)) /* BSD */
1636 if (PerlProc_kill(-proc,val)) /* SYSV */
1642 while (++mark <= sp) {
1643 I32 proc = SvIVx(*mark);
1644 APPLY_TAINT_PROPER();
1645 if (PerlProc_kill(proc, val))
1653 APPLY_TAINT_PROPER();
1655 while (++mark <= sp) {
1656 s = SvPVx(*mark, n_a);
1657 APPLY_TAINT_PROPER();
1658 if (PL_euid || PL_unsafe) {
1662 else { /* don't let root wipe out directories without -U */
1663 if (PerlLIO_lstat(s,&PL_statbuf) < 0 || S_ISDIR(PL_statbuf.st_mode))
1675 APPLY_TAINT_PROPER();
1676 if (sp - mark > 2) {
1677 #if defined(I_UTIME) || defined(VMS)
1678 struct utimbuf utbuf;
1686 SV* accessed = *++mark;
1687 SV* modified = *++mark;
1688 void * utbufp = &utbuf;
1690 /* be like C, and if both times are undefined, let the C
1691 library figure out what to do. This usually means
1694 if ( accessed == &PL_sv_undef && modified == &PL_sv_undef )
1697 Zero(&utbuf, sizeof utbuf, char);
1699 utbuf.actime = (Time_t)SvNVx(accessed); /* time accessed */
1700 utbuf.modtime = (Time_t)SvNVx(modified); /* time modified */
1702 utbuf.actime = (Time_t)SvIVx(accessed); /* time accessed */
1703 utbuf.modtime = (Time_t)SvIVx(modified); /* time modified */
1705 APPLY_TAINT_PROPER();
1707 while (++mark <= sp) {
1708 char *name = SvPVx(*mark, n_a);
1709 APPLY_TAINT_PROPER();
1710 if (PerlLIO_utime(name, utbufp))
1721 #undef APPLY_TAINT_PROPER
1724 /* Do the permissions allow some operation? Assumes statcache already set. */
1725 #ifndef VMS /* VMS' cando is in vms.c */
1727 Perl_cando(pTHX_ Mode_t mode, Uid_t effective, register Stat_t *statbufp)
1728 /* Note: we use `effective' both for uids and gids.
1729 * Here we are betting on Uid_t being equal or wider than Gid_t. */
1732 /* [Comments and code from Len Reed]
1733 * MS-DOS "user" is similar to UNIX's "superuser," but can't write
1734 * to write-protected files. The execute permission bit is set
1735 * by the Miscrosoft C library stat() function for the following:
1740 * All files and directories are readable.
1741 * Directories and special files, e.g. "CON", cannot be
1743 * [Comment by Tom Dinger -- a directory can have the write-protect
1744 * bit set in the file system, but DOS permits changes to
1745 * the directory anyway. In addition, all bets are off
1746 * here for networked software, such as Novell and
1750 /* Atari stat() does pretty much the same thing. we set x_bit_set_in_stat
1751 * too so it will actually look into the files for magic numbers
1753 return (mode & statbufp->st_mode) ? TRUE : FALSE;
1755 #else /* ! DOSISH */
1756 if ((effective ? PL_euid : PL_uid) == 0) { /* root is special */
1757 if (mode == S_IXUSR) {
1758 if (statbufp->st_mode & 0111 || S_ISDIR(statbufp->st_mode))
1762 return TRUE; /* root reads and writes anything */
1765 if (statbufp->st_uid == (effective ? PL_euid : PL_uid) ) {
1766 if (statbufp->st_mode & mode)
1767 return TRUE; /* ok as "user" */
1769 else if (ingroup(statbufp->st_gid,effective)) {
1770 if (statbufp->st_mode & mode >> 3)
1771 return TRUE; /* ok as "group" */
1773 else if (statbufp->st_mode & mode >> 6)
1774 return TRUE; /* ok as "other" */
1776 #endif /* ! DOSISH */
1781 Perl_ingroup(pTHX_ Gid_t testgid, Uid_t effective)
1783 #ifdef MACOS_TRADITIONAL
1784 /* This is simply not correct for AppleShare, but fix it yerself. */
1787 if (testgid == (effective ? PL_egid : PL_gid))
1789 #ifdef HAS_GETGROUPS
1794 Groups_t gary[NGROUPS];
1797 anum = getgroups(NGROUPS,gary);
1799 if (gary[anum] == testgid)
1807 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
1810 Perl_do_ipcget(pTHX_ I32 optype, SV **mark, SV **sp)
1815 key = (key_t)SvNVx(*++mark);
1816 n = (optype == OP_MSGGET) ? 0 : SvIVx(*++mark);
1817 flags = SvIVx(*++mark);
1823 return msgget(key, flags);
1827 return semget(key, n, flags);
1831 return shmget(key, n, flags);
1833 #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
1835 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
1838 return -1; /* should never happen */
1842 Perl_do_ipcctl(pTHX_ I32 optype, SV **mark, SV **sp)
1846 I32 id, n, cmd, infosize, getinfo;
1849 id = SvIVx(*++mark);
1850 n = (optype == OP_SEMCTL) ? SvIVx(*++mark) : 0;
1851 cmd = SvIVx(*++mark);
1854 getinfo = (cmd == IPC_STAT);
1860 if (cmd == IPC_STAT || cmd == IPC_SET)
1861 infosize = sizeof(struct msqid_ds);
1866 if (cmd == IPC_STAT || cmd == IPC_SET)
1867 infosize = sizeof(struct shmid_ds);
1873 if (cmd == IPC_STAT || cmd == IPC_SET)
1874 infosize = sizeof(struct semid_ds);
1875 else if (cmd == GETALL || cmd == SETALL)
1877 struct semid_ds semds;
1879 #ifdef EXTRA_F_IN_SEMUN_BUF
1880 semun.buff = &semds;
1884 getinfo = (cmd == GETALL);
1885 if (Semctl(id, 0, IPC_STAT, semun) == -1)
1887 infosize = semds.sem_nsems * sizeof(short);
1888 /* "short" is technically wrong but much more portable
1889 than guessing about u_?short(_t)? */
1892 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
1896 #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
1898 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
1907 SvPV_force(astr, len);
1908 a = SvGROW(astr, infosize+1);
1912 a = SvPV(astr, len);
1913 if (len != infosize)
1914 Perl_croak(aTHX_ "Bad arg length for %s, is %lu, should be %ld",
1923 a = INT2PTR(char *,i); /* ouch */
1930 ret = msgctl(id, cmd, (struct msqid_ds *)a);
1936 union semun unsemds;
1938 #ifdef EXTRA_F_IN_SEMUN_BUF
1939 unsemds.buff = (struct semid_ds *)a;
1941 unsemds.buf = (struct semid_ds *)a;
1943 ret = Semctl(id, n, cmd, unsemds);
1945 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
1952 ret = shmctl(id, cmd, (struct shmid_ds *)a);
1956 if (getinfo && ret >= 0) {
1957 SvCUR_set(astr, infosize);
1958 *SvEND(astr) = '\0';
1965 Perl_do_msgsnd(pTHX_ SV **mark, SV **sp)
1970 I32 id, msize, flags;
1973 id = SvIVx(*++mark);
1975 flags = SvIVx(*++mark);
1976 mbuf = SvPV(mstr, len);
1977 if ((msize = len - sizeof(long)) < 0)
1978 Perl_croak(aTHX_ "Arg too short for msgsnd");
1980 return msgsnd(id, (struct msgbuf *)mbuf, msize, flags);
1982 Perl_croak(aTHX_ "msgsnd not implemented");
1987 Perl_do_msgrcv(pTHX_ SV **mark, SV **sp)
1993 I32 id, msize, flags, ret;
1996 id = SvIVx(*++mark);
1998 /* suppress warning when reading into undef var --jhi */
2000 sv_setpvn(mstr, "", 0);
2001 msize = SvIVx(*++mark);
2002 mtype = (long)SvIVx(*++mark);
2003 flags = SvIVx(*++mark);
2004 SvPV_force(mstr, len);
2005 mbuf = SvGROW(mstr, sizeof(long)+msize+1);
2008 ret = msgrcv(id, (struct msgbuf *)mbuf, msize, mtype, flags);
2010 SvCUR_set(mstr, sizeof(long)+ret);
2011 *SvEND(mstr) = '\0';
2012 #ifndef INCOMPLETE_TAINTS
2013 /* who knows who has been playing with this message? */
2019 Perl_croak(aTHX_ "msgrcv not implemented");
2024 Perl_do_semop(pTHX_ SV **mark, SV **sp)
2032 id = SvIVx(*++mark);
2034 opbuf = SvPV(opstr, opsize);
2035 if (opsize < 3 * SHORTSIZE
2036 || (opsize % (3 * SHORTSIZE))) {
2037 SETERRNO(EINVAL,LIB$_INVARG);
2041 /* We can't assume that sizeof(struct sembuf) == 3 * sizeof(short). */
2043 int nsops = opsize / (3 * sizeof (short));
2045 short *ops = (short *) opbuf;
2047 struct sembuf *temps, *t;
2050 New (0, temps, nsops, struct sembuf);
2058 result = semop(id, temps, nsops);
2072 Perl_croak(aTHX_ "semop not implemented");
2077 Perl_do_shmio(pTHX_ I32 optype, SV **mark, SV **sp)
2082 I32 id, mpos, msize;
2084 struct shmid_ds shmds;
2086 id = SvIVx(*++mark);
2088 mpos = SvIVx(*++mark);
2089 msize = SvIVx(*++mark);
2091 if (shmctl(id, IPC_STAT, &shmds) == -1)
2093 if (mpos < 0 || msize < 0 || mpos + msize > shmds.shm_segsz) {
2094 SETERRNO(EFAULT,SS$_ACCVIO); /* can't do as caller requested */
2097 shm = (char *)shmat(id, (char*)NULL, (optype == OP_SHMREAD) ? SHM_RDONLY : 0);
2098 if (shm == (char *)-1) /* I hate System V IPC, I really do */
2100 if (optype == OP_SHMREAD) {
2101 /* suppress warning when reading into undef var (tchrist 3/Mar/00) */
2103 sv_setpvn(mstr, "", 0);
2104 SvPV_force(mstr, len);
2105 mbuf = SvGROW(mstr, msize+1);
2107 Copy(shm + mpos, mbuf, msize, char);
2108 SvCUR_set(mstr, msize);
2109 *SvEND(mstr) = '\0';
2111 #ifndef INCOMPLETE_TAINTS
2112 /* who knows who has been playing with this shared memory? */
2119 mbuf = SvPV(mstr, len);
2120 if ((n = len) > msize)
2122 Copy(mbuf, shm + mpos, n, char);
2124 memzero(shm + mpos + n, msize - n);
2128 Perl_croak(aTHX_ "shm I/O not implemented");
2132 #endif /* SYSV IPC */
2135 =for apidoc start_glob
2137 Function called by C<do_readline> to spawn a glob (or do the glob inside
2138 perl on VMS). This code used to be inline, but now perl uses C<File::Glob>
2139 this glob starter is only used by miniperl during the build proccess.
2140 Moving it away shrinks pp_hot.c; shrinking pp_hot.c helps speed perl up.
2146 Perl_start_glob (pTHX_ SV *tmpglob, IO *io)
2148 SV *tmpcmd = NEWSV(55, 0);
2152 #ifdef VMS /* expand the wildcards right here, rather than opening a pipe, */
2153 /* since spawning off a process is a real performance hit */
2155 #include <descrip.h>
2156 #include <lib$routines.h>
2159 char rslt[NAM$C_MAXRSS+1+sizeof(unsigned short int)] = {'\0','\0'};
2160 char vmsspec[NAM$C_MAXRSS+1];
2161 char *rstr = rslt + sizeof(unsigned short int), *begin, *end, *cp;
2162 $DESCRIPTOR(dfltdsc,"SYS$DISK:[]*.*;");
2165 struct dsc$descriptor_s wilddsc
2166 = {0, DSC$K_DTYPE_T, DSC$K_CLASS_S, 0};
2167 struct dsc$descriptor_vs rsdsc
2168 = {sizeof rslt, DSC$K_DTYPE_VT, DSC$K_CLASS_VS, rslt};
2169 unsigned long int cxt = 0, sts = 0, ok = 1, hasdir = 0, hasver = 0, isunix = 0;
2171 /* We could find out if there's an explicit dev/dir or version
2172 by peeking into lib$find_file's internal context at
2173 ((struct NAM *)((struct FAB *)cxt)->fab$l_nam)->nam$l_fnb
2174 but that's unsupported, so I don't want to do it now and
2175 have it bite someone in the future. */
2176 cp = SvPV(tmpglob,i);
2178 if (cp[i] == ';') hasver = 1;
2180 if (sts) hasver = 1;
2184 hasdir = isunix = 1;
2187 if (cp[i] == ']' || cp[i] == '>' || cp[i] == ':') {
2192 if ((tmpfp = PerlIO_tmpfile()) != NULL) {
2194 if (!PerlLIO_stat(SvPVX(tmpglob),&st) && S_ISDIR(st.st_mode))
2195 ok = ((wilddsc.dsc$a_pointer = tovmspath(SvPVX(tmpglob),vmsspec)) != NULL);
2196 else ok = ((wilddsc.dsc$a_pointer = tovmsspec(SvPVX(tmpglob),vmsspec)) != NULL);
2197 if (ok) wilddsc.dsc$w_length = (unsigned short int) strlen(wilddsc.dsc$a_pointer);
2198 for (cp=wilddsc.dsc$a_pointer; ok && cp && *cp; cp++)
2199 if (*cp == '?') *cp = '%'; /* VMS style single-char wildcard */
2200 while (ok && ((sts = lib$find_file(&wilddsc,&rsdsc,&cxt,
2201 &dfltdsc,NULL,NULL,NULL))&1)) {
2202 end = rstr + (unsigned long int) *rslt;
2203 if (!hasver) while (*end != ';') end--;
2204 *(end++) = '\n'; *end = '\0';
2205 for (cp = rstr; *cp; cp++) *cp = _tolower(*cp);
2207 if (isunix) trim_unixpath(rstr,SvPVX(tmpglob),1);
2212 while (*(--begin) != ']' && *begin != '>') ;
2215 ok = (PerlIO_puts(tmpfp,begin) != EOF);
2217 if (cxt) (void)lib$find_file_end(&cxt);
2218 if (ok && sts != RMS$_NMF &&
2219 sts != RMS$_DNF && sts != RMS$_FNF) ok = 0;
2222 SETERRNO((sts == RMS$_SYN ? EINVAL : EVMSERR),sts);
2224 PerlIO_close(tmpfp);
2228 PerlIO_rewind(tmpfp);
2229 IoTYPE(io) = IoTYPE_RDONLY;
2230 IoIFP(io) = fp = tmpfp;
2231 IoFLAGS(io) &= ~IOf_UNTAINT; /* maybe redundant */
2236 #ifdef MACOS_TRADITIONAL
2237 sv_setpv(tmpcmd, "glob ");
2238 sv_catsv(tmpcmd, tmpglob);
2239 sv_catpv(tmpcmd, " |");
2243 sv_setpv(tmpcmd, "for a in ");
2244 sv_catsv(tmpcmd, tmpglob);
2245 sv_catpv(tmpcmd, "; do echo \"$a\\0\\c\"; done |");
2248 sv_setpv(tmpcmd, "/dev/dosglob/"); /* File System Extension */
2249 sv_catsv(tmpcmd, tmpglob);
2251 sv_setpv(tmpcmd, "perlglob ");
2252 sv_catsv(tmpcmd, tmpglob);
2253 sv_catpv(tmpcmd, " |");
2258 sv_setpvn(tmpcmd, PL_cshname, PL_cshlen);
2259 sv_catpv(tmpcmd, " -cf 'set nonomatch; glob ");
2260 sv_catsv(tmpcmd, tmpglob);
2261 sv_catpv(tmpcmd, "' 2>/dev/null |");
2263 sv_setpv(tmpcmd, "echo ");
2264 sv_catsv(tmpcmd, tmpglob);
2266 sv_catpv(tmpcmd, "|tr -s ' \t\f\r' '\\012\\012\\012\\012'|");
2268 sv_catpv(tmpcmd, "|tr -s ' \t\f\r' '\\n\\n\\n\\n'|");
2271 #endif /* !DOSISH */
2272 #endif /* MACOS_TRADITIONAL */
2273 (void)do_open(PL_last_in_gv, SvPVX(tmpcmd), SvCUR(tmpcmd),
2274 FALSE, O_RDONLY, 0, Nullfp);