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 > 0);
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)))) {
242 if (*type == IoTYPE_PIPE) {
244 if (type[1] != IoTYPE_STD) {
246 Perl_croak(aTHX_ "Unknown open() mode '%.*s'", (int)olen, oname);
251 for (type++; isSPACE(*type); type++) ;
257 /* command is missing 19990114 */
258 if (ckWARN(WARN_PIPE))
259 Perl_warner(aTHX_ WARN_PIPE, "Missing command in piped open");
263 if (strNE(name,"-") || num_svs)
265 TAINT_PROPER("piped open");
266 if (!num_svs && name[len-1] == '|') {
268 if (ckWARN(WARN_PIPE))
269 Perl_warner(aTHX_ WARN_PIPE, "Can't open bidirectional pipe");
278 fp = PerlProc_popen_list(mode, num_svs, svp);
281 fp = PerlProc_popen(name,mode);
284 else if (*type == IoTYPE_WRONLY) {
285 TAINT_PROPER("open");
287 if (*type == IoTYPE_WRONLY) {
288 /* Two IoTYPE_WRONLYs in a row make for an IoTYPE_APPEND. */
289 mode[0] = IoTYPE(io) = IoTYPE_APPEND;
310 if (!num_svs && !*type && supplied_fp) {
311 /* "<+&" etc. is used by typemaps */
316 Perl_croak(aTHX_ "More than one argument to '%c&' open",IoTYPE(io));
318 if (num_svs && SvIOK(*svp)) {
321 else if (isDIGIT(*type)) {
323 for (; isSPACE(*type); type++) ;
329 thatio = sv_2io(*svp);
334 for (; isSPACE(*type); type++) ;
335 thatgv = gv_fetchpv(type,FALSE,SVt_PVIO);
336 thatio = GvIO(thatgv);
340 SETERRNO(EINVAL,SS$_IVCHAN);
345 PerlIO *fp = IoIFP(thatio);
346 /* Flush stdio buffer before dup. --mjd
347 * Unfortunately SEEK_CURing 0 seems to
348 * be optimized away on most platforms;
349 * only Solaris and Linux seem to flush
352 /* sfio fails to clear error on next
353 sfwrite, contrary to documentation.
355 if (PerlIO_seek(fp, 0, SEEK_CUR) == -1)
358 /* On the other hand, do all platforms
359 * take gracefully to flushing a read-only
360 * filehandle? Perhaps we should do
361 * fsetpos(src)+fgetpos(dst)? --nik */
363 fd = PerlIO_fileno(fp);
364 /* When dup()ing STDIN, STDOUT or STDERR
365 * explicitly set appropriate access mode */
366 if (IoIFP(thatio) == PerlIO_stdout()
367 || IoIFP(thatio) == PerlIO_stderr())
368 IoTYPE(io) = IoTYPE_WRONLY;
369 else if (IoIFP(thatio) == PerlIO_stdin())
370 IoTYPE(io) = IoTYPE_RDONLY;
371 /* When dup()ing a socket, say result is
373 else if (IoTYPE(thatio) == IoTYPE_SOCKET)
374 IoTYPE(io) = IoTYPE_SOCKET;
380 fd = PerlLIO_dup(fd);
385 if (!(fp = PerlIO_openn(aTHX_ type,mode,fd,0,0,NULL,num_svs,svp))) {
393 Perl_croak(aTHX_ "More than one argument to '>' open");
396 for (; isSPACE(*type); type++) ;
397 if (*type == IoTYPE_STD && (!type[1] || isSPACE(type[1]) || type[1] == ':')) {
400 fp = PerlIO_stdout();
401 IoTYPE(io) = IoTYPE_STD;
405 namesv = sv_2mortal(newSVpvn(type,strlen(type)));
410 fp = PerlIO_openn(aTHX_ type,mode,-1,0,0,NULL,num_svs,svp);
414 else if (*type == IoTYPE_RDONLY) {
416 Perl_croak(aTHX_ "More than one argument to '<' open");
419 for (type++; isSPACE(*type); type++) ;
429 if (*type == IoTYPE_STD && (!type[1] || isSPACE(type[1]) || type[1] == ':')) {
433 IoTYPE(io) = IoTYPE_STD;
437 namesv = sv_2mortal(newSVpvn(type,strlen(type)));
442 fp = PerlIO_openn(aTHX_ type,mode,-1,0,0,NULL,num_svs,svp);
445 else if ((num_svs && type[0] == IoTYPE_STD && type[1] == IoTYPE_PIPE) ||
446 (!num_svs && tend > type+1 && tend[-1] == IoTYPE_PIPE)) {
448 type += 2; /* skip over '-|' */
452 while (tend > type && isSPACE(tend[-1]))
455 for (; isSPACE(*type); type++) ;
460 /* command is missing 19990114 */
461 if (ckWARN(WARN_PIPE))
462 Perl_warner(aTHX_ WARN_PIPE, "Missing command in piped open");
466 if (strNE(name,"-") || num_svs)
468 TAINT_PROPER("piped open");
475 fp = PerlProc_popen_list(mode,num_svs,svp);
478 fp = PerlProc_popen(name,mode);
480 IoTYPE(io) = IoTYPE_PIPE;
486 IoTYPE(io) = IoTYPE_RDONLY;
488 for (; isSPACE(*name); name++) ;
494 if (strEQ(name,"-")) {
496 IoTYPE(io) = IoTYPE_STD;
500 namesv = sv_2mortal(newSVpvn(type,strlen(type)));
505 fp = PerlIO_openn(aTHX_ type,mode,-1,0,0,NULL,num_svs,svp);
510 if (ckWARN(WARN_NEWLINE) && IoTYPE(io) == IoTYPE_RDONLY && strchr(name, '\n'))
511 Perl_warner(aTHX_ WARN_NEWLINE, PL_warn_nl, "open");
515 if (ckWARN(WARN_IO)) {
516 if ((IoTYPE(io) == IoTYPE_RDONLY) &&
517 (fp == PerlIO_stdout() || fp == PerlIO_stderr())) {
518 Perl_warner(aTHX_ WARN_IO,
519 "Filehandle STD%s opened only for input",
520 (fp == PerlIO_stdout()) ? "OUT" : "ERR");
522 else if ((IoTYPE(io) == IoTYPE_WRONLY) && fp == PerlIO_stdin()) {
523 Perl_warner(aTHX_ WARN_IO,
524 "Filehandle STDIN opened only for output");
528 if (IoTYPE(io) && IoTYPE(io) != IoTYPE_PIPE && IoTYPE(io) != IoTYPE_STD &&
529 /* FIXME: This next term is a hack to avoid fileno on PerlIO::Scalar */
530 !(num_svs && SvROK(*svp))) {
531 if (PerlLIO_fstat(PerlIO_fileno(fp),&PL_statbuf) < 0) {
532 (void)PerlIO_close(fp);
535 if (S_ISSOCK(PL_statbuf.st_mode))
536 IoTYPE(io) = IoTYPE_SOCKET; /* in case a socket was passed in to us */
540 !(PL_statbuf.st_mode & S_IFMT)
544 && IoTYPE(io) != IoTYPE_WRONLY /* Dups of STD* filehandles already have */
545 && IoTYPE(io) != IoTYPE_RDONLY /* type so they aren't marked as sockets */
546 ) { /* on OS's that return 0 on fstat()ed pipe */
548 Sock_size_t buflen = sizeof tmpbuf;
549 if (PerlSock_getsockname(PerlIO_fileno(fp), (struct sockaddr *)tmpbuf,
551 || errno != ENOTSOCK)
552 IoTYPE(io) = IoTYPE_SOCKET; /* some OS's return 0 on fstat()ed socket */
553 /* but some return 0 for streams too, sigh */
557 if (saveifp) { /* must use old fp? */
558 /* If fd is less that PL_maxsysfd i.e. STDIN..STDERR
559 then dup the new fileno down
561 fd = PerlIO_fileno(fp);
563 PerlIO_flush(saveofp); /* emulate PerlIO_close() */
564 if (saveofp != saveifp) { /* was a socket? */
565 PerlIO_close(saveofp);
571 if (PerlLIO_dup2(fd, savefd) < 0) {
572 (void)PerlIO_close(fp);
576 if (savefd != PerlIO_fileno(PerlIO_stdin())) {
577 char newname[FILENAME_MAX+1];
578 if (PerlIO_getname(fp, newname)) {
579 if (fd == PerlIO_fileno(PerlIO_stdout())) Perl_vmssetuserlnm(aTHX_ "SYS$OUTPUT", newname);
580 if (fd == PerlIO_fileno(PerlIO_stderr())) Perl_vmssetuserlnm(aTHX_ "SYS$ERROR", newname);
585 sv = *av_fetch(PL_fdpid,fd,TRUE);
586 (void)SvUPGRADE(sv, SVt_IV);
589 sv = *av_fetch(PL_fdpid,savefd,TRUE);
591 (void)SvUPGRADE(sv, SVt_IV);
599 #if defined(HAS_FCNTL) && defined(F_SETFD)
601 int save_errno = errno;
602 fd = PerlIO_fileno(fp);
603 fcntl(fd,F_SETFD,fd > PL_maxsysfd); /* can change errno */
609 IoFLAGS(io) &= ~IOf_NOLINE;
611 if (IoTYPE(io) == IoTYPE_SOCKET
612 || (IoTYPE(io) == IoTYPE_WRONLY && S_ISCHR(PL_statbuf.st_mode)) ) {
614 if (!(IoOFP(io) = PerlIO_openn(aTHX_ type,mode,PerlIO_fileno(fp),0,0,NULL,num_svs,svp))) {
628 IoTYPE(io) = savetype;
633 Perl_nextargv(pTHX_ register GV *gv)
636 #ifndef FLEXFILENAMES
645 PL_argvoutgv = gv_fetchpv("ARGVOUT",TRUE,SVt_PVIO);
646 if (io && (IoFLAGS(io) & IOf_ARGV) && (IoFLAGS(io) & IOf_START)) {
647 IoFLAGS(io) &= ~IOf_START;
649 if (!PL_argvout_stack)
650 PL_argvout_stack = newAV();
651 av_push(PL_argvout_stack, SvREFCNT_inc(PL_defoutgv));
654 if (PL_filemode & (S_ISUID|S_ISGID)) {
655 PerlIO_flush(IoIFP(GvIOn(PL_argvoutgv))); /* chmod must follow last write */
657 (void)fchmod(PL_lastfd,PL_filemode);
659 (void)PerlLIO_chmod(PL_oldname,PL_filemode);
663 while (av_len(GvAV(gv)) >= 0) {
665 sv = av_shift(GvAV(gv));
667 sv_setsv(GvSV(gv),sv);
668 SvSETMAGIC(GvSV(gv));
669 PL_oldname = SvPVx(GvSV(gv), oldlen);
670 if (do_open(gv,PL_oldname,oldlen,PL_inplace!=0,O_RDONLY,0,Nullfp)) {
672 TAINT_PROPER("inplace open");
673 if (oldlen == 1 && *PL_oldname == '-') {
674 setdefout(gv_fetchpv("STDOUT",TRUE,SVt_PVIO));
675 return IoIFP(GvIOp(gv));
677 #ifndef FLEXFILENAMES
678 filedev = PL_statbuf.st_dev;
679 fileino = PL_statbuf.st_ino;
681 PL_filemode = PL_statbuf.st_mode;
682 fileuid = PL_statbuf.st_uid;
683 filegid = PL_statbuf.st_gid;
684 if (!S_ISREG(PL_filemode)) {
685 if (ckWARN_d(WARN_INPLACE))
686 Perl_warner(aTHX_ WARN_INPLACE,
687 "Can't do inplace edit: %s is not a regular file",
693 char *star = strchr(PL_inplace, '*');
695 char *begin = PL_inplace;
696 sv_setpvn(sv, "", 0);
698 sv_catpvn(sv, begin, star - begin);
699 sv_catpvn(sv, PL_oldname, oldlen);
701 } while ((star = strchr(begin, '*')));
706 sv_catpv(sv,PL_inplace);
708 #ifndef FLEXFILENAMES
709 if (PerlLIO_stat(SvPVX(sv),&PL_statbuf) >= 0
710 && PL_statbuf.st_dev == filedev
711 && PL_statbuf.st_ino == fileino
713 || (_djstat_fail_bits & _STFAIL_TRUENAME)!=0
717 if (ckWARN_d(WARN_INPLACE))
718 Perl_warner(aTHX_ WARN_INPLACE,
719 "Can't do inplace edit: %s would not be unique",
726 #if !defined(DOSISH) && !defined(__CYGWIN__)
727 if (PerlLIO_rename(PL_oldname,SvPVX(sv)) < 0) {
728 if (ckWARN_d(WARN_INPLACE))
729 Perl_warner(aTHX_ WARN_INPLACE,
730 "Can't rename %s to %s: %s, skipping file",
731 PL_oldname, SvPVX(sv), Strerror(errno) );
737 (void)PerlLIO_unlink(SvPVX(sv));
738 (void)PerlLIO_rename(PL_oldname,SvPVX(sv));
739 do_open(gv,SvPVX(sv),SvCUR(sv),PL_inplace!=0,O_RDONLY,0,Nullfp);
742 (void)UNLINK(SvPVX(sv));
743 if (link(PL_oldname,SvPVX(sv)) < 0) {
744 if (ckWARN_d(WARN_INPLACE))
745 Perl_warner(aTHX_ WARN_INPLACE,
746 "Can't rename %s to %s: %s, skipping file",
747 PL_oldname, SvPVX(sv), Strerror(errno) );
751 (void)UNLINK(PL_oldname);
755 #if !defined(DOSISH) && !defined(AMIGAOS)
756 # ifndef VMS /* Don't delete; use automatic file versioning */
757 if (UNLINK(PL_oldname) < 0) {
758 if (ckWARN_d(WARN_INPLACE))
759 Perl_warner(aTHX_ WARN_INPLACE,
760 "Can't remove %s: %s, skipping file",
761 PL_oldname, Strerror(errno) );
767 Perl_croak(aTHX_ "Can't do inplace edit without backup");
771 sv_setpvn(sv,">",!PL_inplace);
772 sv_catpvn(sv,PL_oldname,oldlen);
773 SETERRNO(0,0); /* in case sprintf set errno */
775 if (!do_open(PL_argvoutgv,SvPVX(sv),SvCUR(sv),PL_inplace!=0,
776 O_WRONLY|O_CREAT|O_TRUNC,0,Nullfp))
778 if (!do_open(PL_argvoutgv,SvPVX(sv),SvCUR(sv),PL_inplace!=0,
779 O_WRONLY|O_CREAT|OPEN_EXCL,0666,Nullfp))
782 if (ckWARN_d(WARN_INPLACE))
783 Perl_warner(aTHX_ WARN_INPLACE, "Can't do inplace edit on %s: %s",
784 PL_oldname, Strerror(errno) );
788 setdefout(PL_argvoutgv);
789 PL_lastfd = PerlIO_fileno(IoIFP(GvIOp(PL_argvoutgv)));
790 (void)PerlLIO_fstat(PL_lastfd,&PL_statbuf);
792 (void)fchmod(PL_lastfd,PL_filemode);
794 # if !(defined(WIN32) && defined(__BORLANDC__))
795 /* Borland runtime creates a readonly file! */
796 (void)PerlLIO_chmod(PL_oldname,PL_filemode);
799 if (fileuid != PL_statbuf.st_uid || filegid != PL_statbuf.st_gid) {
801 (void)fchown(PL_lastfd,fileuid,filegid);
804 (void)PerlLIO_chown(PL_oldname,fileuid,filegid);
809 return IoIFP(GvIOp(gv));
812 if (ckWARN_d(WARN_INPLACE)) {
814 if (PerlLIO_stat(PL_oldname, &PL_statbuf) >= 0
815 && !S_ISREG(PL_statbuf.st_mode))
817 Perl_warner(aTHX_ WARN_INPLACE,
818 "Can't do inplace edit: %s is not a regular file",
822 Perl_warner(aTHX_ WARN_INPLACE, "Can't open %s: %s",
823 PL_oldname, Strerror(eno));
827 if (io && (IoFLAGS(io) & IOf_ARGV))
828 IoFLAGS(io) |= IOf_START;
830 (void)do_close(PL_argvoutgv,FALSE);
831 if (io && (IoFLAGS(io) & IOf_ARGV)
832 && PL_argvout_stack && AvFILLp(PL_argvout_stack) >= 0)
834 GV *oldout = (GV*)av_pop(PL_argvout_stack);
836 SvREFCNT_dec(oldout);
839 setdefout(gv_fetchpv("STDOUT",TRUE,SVt_PVIO));
846 Perl_do_pipe(pTHX_ SV *sv, GV *rgv, GV *wgv)
865 if (PerlProc_pipe(fd) < 0)
867 IoIFP(rstio) = PerlIO_fdopen(fd[0], "r");
868 IoOFP(wstio) = PerlIO_fdopen(fd[1], "w");
869 IoIFP(wstio) = IoOFP(wstio);
870 IoTYPE(rstio) = IoTYPE_RDONLY;
871 IoTYPE(wstio) = IoTYPE_WRONLY;
872 if (!IoIFP(rstio) || !IoOFP(wstio)) {
873 if (IoIFP(rstio)) PerlIO_close(IoIFP(rstio));
874 else PerlLIO_close(fd[0]);
875 if (IoOFP(wstio)) PerlIO_close(IoOFP(wstio));
876 else PerlLIO_close(fd[1]);
880 sv_setsv(sv,&PL_sv_yes);
884 sv_setsv(sv,&PL_sv_undef);
889 /* explicit renamed to avoid C++ conflict -- kja */
891 Perl_do_close(pTHX_ GV *gv, bool not_implicit)
898 if (!gv || SvTYPE(gv) != SVt_PVGV) {
900 SETERRNO(EBADF,SS$_IVCHAN);
904 if (!io) { /* never opened */
906 if (ckWARN(WARN_UNOPENED)) /* no check for closed here */
907 report_evil_fh(gv, io, PL_op->op_type);
908 SETERRNO(EBADF,SS$_IVCHAN);
912 retval = io_close(io, not_implicit);
916 IoLINES_LEFT(io) = IoPAGE_LEN(io);
918 IoTYPE(io) = IoTYPE_CLOSED;
923 Perl_io_close(pTHX_ IO *io, bool not_implicit)
929 if (IoTYPE(io) == IoTYPE_PIPE) {
930 status = PerlProc_pclose(IoIFP(io));
932 STATUS_NATIVE_SET(status);
933 retval = (STATUS_POSIX == 0);
936 retval = (status != -1);
939 else if (IoTYPE(io) == IoTYPE_STD)
942 if (IoOFP(io) && IoOFP(io) != IoIFP(io)) { /* a socket */
943 retval = (PerlIO_close(IoOFP(io)) != EOF);
944 PerlIO_close(IoIFP(io)); /* clear stdio, fd already closed */
947 retval = (PerlIO_close(IoIFP(io)) != EOF);
949 IoOFP(io) = IoIFP(io) = Nullfp;
951 else if (not_implicit) {
952 SETERRNO(EBADF,SS$_IVCHAN);
959 Perl_do_eof(pTHX_ GV *gv)
968 else if (ckWARN(WARN_IO) && (IoTYPE(io) == IoTYPE_WRONLY))
970 /* integrate to report_evil_fh()? */
973 SV* sv = sv_newmortal();
974 gv_efullname4(sv, gv, Nullch, FALSE);
975 name = SvPV_nolen(sv);
978 Perl_warner(aTHX_ WARN_IO,
979 "Filehandle %s opened only for output", name);
981 Perl_warner(aTHX_ WARN_IO,
982 "Filehandle opened only for output");
987 if (PerlIO_has_cntptr(IoIFP(io))) { /* (the code works without this) */
988 if (PerlIO_get_cnt(IoIFP(io)) > 0) /* cheat a little, since */
989 return FALSE; /* this is the most usual case */
992 ch = PerlIO_getc(IoIFP(io));
994 (void)PerlIO_ungetc(IoIFP(io),ch);
998 if (PerlIO_has_cntptr(IoIFP(io)) && PerlIO_canset_cnt(IoIFP(io))) {
999 if (PerlIO_get_cnt(IoIFP(io)) < -1)
1000 PerlIO_set_cnt(IoIFP(io),-1);
1002 if (PL_op->op_flags & OPf_SPECIAL) { /* not necessarily a real EOF yet? */
1003 if (!nextargv(PL_argvgv)) /* get another fp handy */
1007 return TRUE; /* normal fp, definitely end of file */
1013 Perl_do_tell(pTHX_ GV *gv)
1015 register IO *io = 0;
1016 register PerlIO *fp;
1018 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io))) {
1019 #ifdef ULTRIX_STDIO_BOTCH
1021 (void)PerlIO_seek(fp, 0L, 2); /* ultrix 1.2 workaround */
1023 return PerlIO_tell(fp);
1025 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1026 report_evil_fh(gv, io, PL_op->op_type);
1027 SETERRNO(EBADF,RMS$_IFI);
1032 Perl_do_seek(pTHX_ GV *gv, Off_t pos, int whence)
1034 register IO *io = 0;
1035 register PerlIO *fp;
1037 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io))) {
1038 #ifdef ULTRIX_STDIO_BOTCH
1040 (void)PerlIO_seek(fp, 0L, 2); /* ultrix 1.2 workaround */
1042 return PerlIO_seek(fp, pos, whence) >= 0;
1044 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1045 report_evil_fh(gv, io, PL_op->op_type);
1046 SETERRNO(EBADF,RMS$_IFI);
1051 Perl_do_sysseek(pTHX_ GV *gv, Off_t pos, int whence)
1053 register IO *io = 0;
1054 register PerlIO *fp;
1056 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io)))
1057 return PerlLIO_lseek(PerlIO_fileno(fp), pos, whence);
1058 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1059 report_evil_fh(gv, io, PL_op->op_type);
1060 SETERRNO(EBADF,RMS$_IFI);
1065 Perl_mode_from_discipline(pTHX_ SV *discp)
1067 int mode = O_BINARY;
1070 char *s = SvPV(discp,len);
1075 if (len > 3 && strnEQ(s+1, "raw", 3)
1076 && (!s[4] || s[4] == ':' || isSPACE(s[4])))
1085 if (len > 4 && strnEQ(s+1, "crlf", 4)
1086 && (!s[5] || s[5] == ':' || isSPACE(s[5])))
1095 goto fail_discipline;
1098 else if (isSPACE(*s)) {
1105 end = strchr(s+1, ':');
1108 #ifndef PERLIO_LAYERS
1109 Perl_croak(aTHX_ "Unknown discipline '%.*s'", end-s, s);
1120 Perl_do_binmode(pTHX_ PerlIO *fp, int iotype, int mode)
1122 /* The old body of this is now in non-LAYER part of perlio.c
1123 * This is a stub for any XS code which might have been calling it.
1125 char *name = (O_BINARY != O_TEXT && !(mode & O_BINARY)) ? ":crlf" : ":raw";
1126 return PerlIO_binmode(aTHX_ fp, iotype, mode, name);
1129 #if !defined(HAS_TRUNCATE) && !defined(HAS_CHSIZE) && defined(F_FREESP)
1130 /* code courtesy of William Kucharski */
1133 I32 my_chsize(fd, length)
1134 I32 fd; /* file descriptor */
1135 Off_t length; /* length to set file to */
1138 struct stat filebuf;
1140 if (PerlLIO_fstat(fd, &filebuf) < 0)
1143 if (filebuf.st_size < length) {
1145 /* extend file length */
1147 if ((PerlLIO_lseek(fd, (length - 1), 0)) < 0)
1150 /* write a "0" byte */
1152 if ((PerlLIO_write(fd, "", 1)) != 1)
1156 /* truncate length */
1160 fl.l_start = length;
1161 fl.l_type = F_WRLCK; /* write lock on file space */
1164 * This relies on the UNDOCUMENTED F_FREESP argument to
1165 * fcntl(2), which truncates the file so that it ends at the
1166 * position indicated by fl.l_start.
1168 * Will minor miracles never cease?
1171 if (fcntl(fd, F_FREESP, &fl) < 0)
1178 #endif /* F_FREESP */
1181 Perl_do_print(pTHX_ register SV *sv, PerlIO *fp)
1183 register char *tmps;
1186 /* assuming fp is checked earlier */
1192 if (SvIOK(sv) && SvIVX(sv) != 0) {
1193 PerlIO_printf(fp, PL_ofmt, (NV)SvIVX(sv));
1194 return !PerlIO_error(fp);
1196 if ( (SvNOK(sv) && SvNVX(sv) != 0.0)
1197 || (looks_like_number(sv) && sv_2nv(sv) != 0.0) ) {
1198 PerlIO_printf(fp, PL_ofmt, SvNVX(sv));
1199 return !PerlIO_error(fp);
1202 switch (SvTYPE(sv)) {
1204 if (ckWARN(WARN_UNINITIALIZED))
1212 PerlIO_printf(fp, "%"UVuf, (UV)SvUVX(sv));
1214 PerlIO_printf(fp, "%"IVdf, (IV)SvIVX(sv));
1215 return !PerlIO_error(fp);
1219 if (PerlIO_isutf8(fp)) {
1221 sv_utf8_upgrade(sv = sv_mortalcopy(sv));
1223 else if (DO_UTF8(sv)) {
1224 if (!sv_utf8_downgrade((sv = sv_mortalcopy(sv)), TRUE)) {
1225 Perl_warner(aTHX_ WARN_UTF8, "Wide character in print");
1228 tmps = SvPV(sv, len);
1231 /* To detect whether the process is about to overstep its
1232 * filesize limit we would need getrlimit(). We could then
1233 * also transparently raise the limit with setrlimit() --
1234 * but only until the system hard limit/the filesystem limit,
1235 * at which we would get EPERM. Note that when using buffered
1236 * io the write failure can be delayed until the flush/close. --jhi */
1237 if (len && (PerlIO_write(fp,tmps,len) == 0))
1239 return !PerlIO_error(fp);
1249 if (PL_op->op_flags & OPf_REF) {
1254 if (io && IoIFP(io)) {
1256 sv_setpv(PL_statname,"");
1257 PL_laststype = OP_STAT;
1258 return (PL_laststatval = PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache));
1262 return PL_laststatval;
1263 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1264 report_evil_fh(gv, io, PL_op->op_type);
1266 sv_setpv(PL_statname,"");
1267 return (PL_laststatval = -1);
1275 if (SvTYPE(sv) == SVt_PVGV) {
1279 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
1286 sv_setpv(PL_statname, s);
1287 PL_laststype = OP_STAT;
1288 PL_laststatval = PerlLIO_stat(s, &PL_statcache);
1289 if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(s, '\n'))
1290 Perl_warner(aTHX_ WARN_NEWLINE, PL_warn_nl, "stat");
1291 return PL_laststatval;
1301 if (PL_op->op_flags & OPf_REF) {
1303 if (cGVOP_gv == PL_defgv) {
1304 if (PL_laststype != OP_LSTAT)
1305 Perl_croak(aTHX_ "The stat preceding -l _ wasn't an lstat");
1306 return PL_laststatval;
1308 Perl_croak(aTHX_ "You can't use -l on a filehandle");
1311 PL_laststype = OP_LSTAT;
1315 sv_setpv(PL_statname,SvPV(sv, n_a));
1316 PL_laststatval = PerlLIO_lstat(SvPV(sv, n_a),&PL_statcache);
1317 if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(SvPV(sv, n_a), '\n'))
1318 Perl_warner(aTHX_ WARN_NEWLINE, PL_warn_nl, "lstat");
1319 return PL_laststatval;
1323 Perl_do_aexec(pTHX_ SV *really, register SV **mark, register SV **sp)
1325 return do_aexec5(really, mark, sp, 0, 0);
1329 Perl_do_aexec5(pTHX_ SV *really, register SV **mark, register SV **sp,
1330 int fd, int do_report)
1332 #ifdef MACOS_TRADITIONAL
1333 Perl_croak(aTHX_ "exec? I'm not *that* kind of operating system");
1336 char *tmps = Nullch;
1340 New(401,PL_Argv, sp - mark + 1, char*);
1342 while (++mark <= sp) {
1344 *a++ = SvPVx(*mark, n_a);
1350 tmps = SvPV(really, n_a);
1351 if ((!really && *PL_Argv[0] != '/') ||
1352 (really && *tmps != '/')) /* will execvp use PATH? */
1353 TAINT_ENV(); /* testing IFS here is overkill, probably */
1354 if (really && *tmps)
1355 PerlProc_execvp(tmps,EXEC_ARGV_CAST(PL_Argv));
1357 PerlProc_execvp(PL_Argv[0],EXEC_ARGV_CAST(PL_Argv));
1358 if (ckWARN(WARN_EXEC))
1359 Perl_warner(aTHX_ WARN_EXEC, "Can't exec \"%s\": %s",
1360 (really ? tmps : PL_Argv[0]), Strerror(errno));
1364 PerlLIO_write(fd, (void*)&e, sizeof(int));
1374 Perl_do_execfree(pTHX)
1378 PL_Argv = Null(char **);
1386 #if !defined(OS2) && !defined(WIN32) && !defined(DJGPP) && !defined(EPOC) && !defined(MACOS_TRADITIONAL)
1389 Perl_do_exec(pTHX_ char *cmd)
1391 return do_exec3(cmd,0,0);
1395 Perl_do_exec3(pTHX_ char *cmd, int fd, int do_report)
1400 while (*cmd && isSPACE(*cmd))
1403 /* save an extra exec if possible */
1408 if (strnEQ(cmd,PL_cshname,PL_cshlen) &&
1409 strnEQ(cmd+PL_cshlen," -c",3)) {
1411 s = cmd+PL_cshlen+3;
1425 if (s[-1] == '\'') {
1427 PerlProc_execl(PL_cshname,"csh", flags, ncmd, (char*)0);
1436 /* see if there are shell metacharacters in it */
1438 if (*cmd == '.' && isSPACE(cmd[1]))
1441 if (strnEQ(cmd,"exec",4) && isSPACE(cmd[4]))
1444 for (s = cmd; *s && isALNUM(*s); s++) ; /* catch VAR=val gizmo */
1448 for (s = cmd; *s; s++) {
1449 if (*s != ' ' && !isALPHA(*s) &&
1450 strchr("$&*(){}[]'\";\\|?<>~`\n",*s)) {
1451 if (*s == '\n' && !s[1]) {
1455 /* handle the 2>&1 construct at the end */
1456 if (*s == '>' && s[1] == '&' && s[2] == '1'
1457 && s > cmd + 1 && s[-1] == '2' && isSPACE(s[-2])
1458 && (!s[3] || isSPACE(s[3])))
1462 while (*t && isSPACE(*t))
1464 if (!*t && (dup2(1,2) != -1)) {
1470 PerlProc_execl(PL_sh_path, "sh", "-c", cmd, (char*)0);
1475 New(402,PL_Argv, (s - cmd) / 2 + 2, char*);
1476 PL_Cmd = savepvn(cmd, s-cmd);
1478 for (s = PL_Cmd; *s;) {
1479 while (*s && isSPACE(*s)) s++;
1482 while (*s && !isSPACE(*s)) s++;
1488 PerlProc_execvp(PL_Argv[0],PL_Argv);
1489 if (errno == ENOEXEC) { /* for system V NIH syndrome */
1496 if (ckWARN(WARN_EXEC))
1497 Perl_warner(aTHX_ WARN_EXEC, "Can't exec \"%s\": %s",
1498 PL_Argv[0], Strerror(errno));
1500 PerlLIO_write(fd, (void*)&e, sizeof(int));
1509 #endif /* OS2 || WIN32 */
1512 Perl_apply(pTHX_ I32 type, register SV **mark, register SV **sp)
1516 register I32 tot = 0;
1519 SV **oldmark = mark;
1522 #define APPLY_TAINT_PROPER() \
1524 if (PL_tainted) { TAINT_PROPER(what); } \
1527 /* This is a first heuristic; it doesn't catch tainting magic. */
1529 while (++mark <= sp) {
1530 if (SvTAINTED(*mark)) {
1540 APPLY_TAINT_PROPER();
1543 APPLY_TAINT_PROPER();
1545 while (++mark <= sp) {
1546 char *name = SvPVx(*mark, n_a);
1547 APPLY_TAINT_PROPER();
1548 if (PerlLIO_chmod(name, val))
1556 APPLY_TAINT_PROPER();
1557 if (sp - mark > 2) {
1558 val = SvIVx(*++mark);
1559 val2 = SvIVx(*++mark);
1560 APPLY_TAINT_PROPER();
1562 while (++mark <= sp) {
1563 char *name = SvPVx(*mark, n_a);
1564 APPLY_TAINT_PROPER();
1565 if (PerlLIO_chown(name, val, val2))
1572 XXX Should we make lchown() directly available from perl?
1573 For now, we'll let Configure test for HAS_LCHOWN, but do
1574 nothing in the core.
1580 APPLY_TAINT_PROPER();
1583 s = SvPVx(*++mark, n_a);
1585 if (*s == 'S' && s[1] == 'I' && s[2] == 'G')
1587 if (!(val = whichsig(s)))
1588 Perl_croak(aTHX_ "Unrecognized signal name \"%s\"",s);
1592 APPLY_TAINT_PROPER();
1595 /* kill() doesn't do process groups (job trees?) under VMS */
1596 if (val < 0) val = -val;
1597 if (val == SIGKILL) {
1598 # include <starlet.h>
1599 /* Use native sys$delprc() to insure that target process is
1600 * deleted; supervisor-mode images don't pay attention to
1601 * CRTL's emulation of Unix-style signals and kill()
1603 while (++mark <= sp) {
1604 I32 proc = SvIVx(*mark);
1605 register unsigned long int __vmssts;
1606 APPLY_TAINT_PROPER();
1607 if (!((__vmssts = sys$delprc(&proc,0)) & 1)) {
1611 case SS$_NOSUCHNODE:
1612 SETERRNO(ESRCH,__vmssts);
1615 SETERRNO(EPERM,__vmssts);
1618 SETERRNO(EVMSERR,__vmssts);
1627 while (++mark <= sp) {
1628 I32 proc = SvIVx(*mark);
1629 APPLY_TAINT_PROPER();
1631 if (PerlProc_killpg(proc,val)) /* BSD */
1633 if (PerlProc_kill(-proc,val)) /* SYSV */
1639 while (++mark <= sp) {
1640 I32 proc = SvIVx(*mark);
1641 APPLY_TAINT_PROPER();
1642 if (PerlProc_kill(proc, val))
1650 APPLY_TAINT_PROPER();
1652 while (++mark <= sp) {
1653 s = SvPVx(*mark, n_a);
1654 APPLY_TAINT_PROPER();
1655 if (PL_euid || PL_unsafe) {
1659 else { /* don't let root wipe out directories without -U */
1660 if (PerlLIO_lstat(s,&PL_statbuf) < 0 || S_ISDIR(PL_statbuf.st_mode))
1672 APPLY_TAINT_PROPER();
1673 if (sp - mark > 2) {
1674 #if defined(I_UTIME) || defined(VMS)
1675 struct utimbuf utbuf;
1683 SV* accessed = *++mark;
1684 SV* modified = *++mark;
1685 void * utbufp = &utbuf;
1687 /* be like C, and if both times are undefined, let the C
1688 library figure out what to do. This usually means
1691 if ( accessed == &PL_sv_undef && modified == &PL_sv_undef )
1694 Zero(&utbuf, sizeof utbuf, char);
1696 utbuf.actime = (Time_t)SvNVx(accessed); /* time accessed */
1697 utbuf.modtime = (Time_t)SvNVx(modified); /* time modified */
1699 utbuf.actime = (Time_t)SvIVx(accessed); /* time accessed */
1700 utbuf.modtime = (Time_t)SvIVx(modified); /* time modified */
1702 APPLY_TAINT_PROPER();
1704 while (++mark <= sp) {
1705 char *name = SvPVx(*mark, n_a);
1706 APPLY_TAINT_PROPER();
1707 if (PerlLIO_utime(name, utbufp))
1718 #undef APPLY_TAINT_PROPER
1721 /* Do the permissions allow some operation? Assumes statcache already set. */
1722 #ifndef VMS /* VMS' cando is in vms.c */
1724 Perl_cando(pTHX_ Mode_t mode, Uid_t effective, register Stat_t *statbufp)
1725 /* Note: we use `effective' both for uids and gids.
1726 * Here we are betting on Uid_t being equal or wider than Gid_t. */
1729 /* [Comments and code from Len Reed]
1730 * MS-DOS "user" is similar to UNIX's "superuser," but can't write
1731 * to write-protected files. The execute permission bit is set
1732 * by the Miscrosoft C library stat() function for the following:
1737 * All files and directories are readable.
1738 * Directories and special files, e.g. "CON", cannot be
1740 * [Comment by Tom Dinger -- a directory can have the write-protect
1741 * bit set in the file system, but DOS permits changes to
1742 * the directory anyway. In addition, all bets are off
1743 * here for networked software, such as Novell and
1747 /* Atari stat() does pretty much the same thing. we set x_bit_set_in_stat
1748 * too so it will actually look into the files for magic numbers
1750 return (mode & statbufp->st_mode) ? TRUE : FALSE;
1752 #else /* ! DOSISH */
1753 if ((effective ? PL_euid : PL_uid) == 0) { /* root is special */
1754 if (mode == S_IXUSR) {
1755 if (statbufp->st_mode & 0111 || S_ISDIR(statbufp->st_mode))
1759 return TRUE; /* root reads and writes anything */
1762 if (statbufp->st_uid == (effective ? PL_euid : PL_uid) ) {
1763 if (statbufp->st_mode & mode)
1764 return TRUE; /* ok as "user" */
1766 else if (ingroup(statbufp->st_gid,effective)) {
1767 if (statbufp->st_mode & mode >> 3)
1768 return TRUE; /* ok as "group" */
1770 else if (statbufp->st_mode & mode >> 6)
1771 return TRUE; /* ok as "other" */
1773 #endif /* ! DOSISH */
1778 Perl_ingroup(pTHX_ Gid_t testgid, Uid_t effective)
1780 #ifdef MACOS_TRADITIONAL
1781 /* This is simply not correct for AppleShare, but fix it yerself. */
1784 if (testgid == (effective ? PL_egid : PL_gid))
1786 #ifdef HAS_GETGROUPS
1791 Groups_t gary[NGROUPS];
1794 anum = getgroups(NGROUPS,gary);
1796 if (gary[anum] == testgid)
1804 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
1807 Perl_do_ipcget(pTHX_ I32 optype, SV **mark, SV **sp)
1812 key = (key_t)SvNVx(*++mark);
1813 n = (optype == OP_MSGGET) ? 0 : SvIVx(*++mark);
1814 flags = SvIVx(*++mark);
1820 return msgget(key, flags);
1824 return semget(key, n, flags);
1828 return shmget(key, n, flags);
1830 #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
1832 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
1835 return -1; /* should never happen */
1839 Perl_do_ipcctl(pTHX_ I32 optype, SV **mark, SV **sp)
1843 I32 id, n, cmd, infosize, getinfo;
1846 id = SvIVx(*++mark);
1847 n = (optype == OP_SEMCTL) ? SvIVx(*++mark) : 0;
1848 cmd = SvIVx(*++mark);
1851 getinfo = (cmd == IPC_STAT);
1857 if (cmd == IPC_STAT || cmd == IPC_SET)
1858 infosize = sizeof(struct msqid_ds);
1863 if (cmd == IPC_STAT || cmd == IPC_SET)
1864 infosize = sizeof(struct shmid_ds);
1870 if (cmd == IPC_STAT || cmd == IPC_SET)
1871 infosize = sizeof(struct semid_ds);
1872 else if (cmd == GETALL || cmd == SETALL)
1874 struct semid_ds semds;
1876 #ifdef EXTRA_F_IN_SEMUN_BUF
1877 semun.buff = &semds;
1881 getinfo = (cmd == GETALL);
1882 if (Semctl(id, 0, IPC_STAT, semun) == -1)
1884 infosize = semds.sem_nsems * sizeof(short);
1885 /* "short" is technically wrong but much more portable
1886 than guessing about u_?short(_t)? */
1889 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
1893 #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
1895 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
1904 SvPV_force(astr, len);
1905 a = SvGROW(astr, infosize+1);
1909 a = SvPV(astr, len);
1910 if (len != infosize)
1911 Perl_croak(aTHX_ "Bad arg length for %s, is %lu, should be %ld",
1920 a = INT2PTR(char *,i); /* ouch */
1927 ret = msgctl(id, cmd, (struct msqid_ds *)a);
1933 union semun unsemds;
1935 #ifdef EXTRA_F_IN_SEMUN_BUF
1936 unsemds.buff = (struct semid_ds *)a;
1938 unsemds.buf = (struct semid_ds *)a;
1940 ret = Semctl(id, n, cmd, unsemds);
1942 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
1949 ret = shmctl(id, cmd, (struct shmid_ds *)a);
1953 if (getinfo && ret >= 0) {
1954 SvCUR_set(astr, infosize);
1955 *SvEND(astr) = '\0';
1962 Perl_do_msgsnd(pTHX_ SV **mark, SV **sp)
1967 I32 id, msize, flags;
1970 id = SvIVx(*++mark);
1972 flags = SvIVx(*++mark);
1973 mbuf = SvPV(mstr, len);
1974 if ((msize = len - sizeof(long)) < 0)
1975 Perl_croak(aTHX_ "Arg too short for msgsnd");
1977 return msgsnd(id, (struct msgbuf *)mbuf, msize, flags);
1979 Perl_croak(aTHX_ "msgsnd not implemented");
1984 Perl_do_msgrcv(pTHX_ SV **mark, SV **sp)
1990 I32 id, msize, flags, ret;
1993 id = SvIVx(*++mark);
1995 /* suppress warning when reading into undef var --jhi */
1997 sv_setpvn(mstr, "", 0);
1998 msize = SvIVx(*++mark);
1999 mtype = (long)SvIVx(*++mark);
2000 flags = SvIVx(*++mark);
2001 SvPV_force(mstr, len);
2002 mbuf = SvGROW(mstr, sizeof(long)+msize+1);
2005 ret = msgrcv(id, (struct msgbuf *)mbuf, msize, mtype, flags);
2007 SvCUR_set(mstr, sizeof(long)+ret);
2008 *SvEND(mstr) = '\0';
2009 #ifndef INCOMPLETE_TAINTS
2010 /* who knows who has been playing with this message? */
2016 Perl_croak(aTHX_ "msgrcv not implemented");
2021 Perl_do_semop(pTHX_ SV **mark, SV **sp)
2029 id = SvIVx(*++mark);
2031 opbuf = SvPV(opstr, opsize);
2032 if (opsize < 3 * SHORTSIZE
2033 || (opsize % (3 * SHORTSIZE))) {
2034 SETERRNO(EINVAL,LIB$_INVARG);
2038 /* We can't assume that sizeof(struct sembuf) == 3 * sizeof(short). */
2040 int nsops = opsize / (3 * sizeof (short));
2042 short *ops = (short *) opbuf;
2044 struct sembuf *temps, *t;
2047 New (0, temps, nsops, struct sembuf);
2055 result = semop(id, temps, nsops);
2069 Perl_croak(aTHX_ "semop not implemented");
2074 Perl_do_shmio(pTHX_ I32 optype, SV **mark, SV **sp)
2079 I32 id, mpos, msize;
2081 struct shmid_ds shmds;
2083 id = SvIVx(*++mark);
2085 mpos = SvIVx(*++mark);
2086 msize = SvIVx(*++mark);
2088 if (shmctl(id, IPC_STAT, &shmds) == -1)
2090 if (mpos < 0 || msize < 0 || mpos + msize > shmds.shm_segsz) {
2091 SETERRNO(EFAULT,SS$_ACCVIO); /* can't do as caller requested */
2094 shm = (char *)shmat(id, (char*)NULL, (optype == OP_SHMREAD) ? SHM_RDONLY : 0);
2095 if (shm == (char *)-1) /* I hate System V IPC, I really do */
2097 if (optype == OP_SHMREAD) {
2098 /* suppress warning when reading into undef var (tchrist 3/Mar/00) */
2100 sv_setpvn(mstr, "", 0);
2101 SvPV_force(mstr, len);
2102 mbuf = SvGROW(mstr, msize+1);
2104 Copy(shm + mpos, mbuf, msize, char);
2105 SvCUR_set(mstr, msize);
2106 *SvEND(mstr) = '\0';
2108 #ifndef INCOMPLETE_TAINTS
2109 /* who knows who has been playing with this shared memory? */
2116 mbuf = SvPV(mstr, len);
2117 if ((n = len) > msize)
2119 Copy(mbuf, shm + mpos, n, char);
2121 memzero(shm + mpos + n, msize - n);
2125 Perl_croak(aTHX_ "shm I/O not implemented");
2129 #endif /* SYSV IPC */
2132 =for apidoc start_glob
2134 Function called by C<do_readline> to spawn a glob (or do the glob inside
2135 perl on VMS). This code used to be inline, but now perl uses C<File::Glob>
2136 this glob starter is only used by miniperl during the build proccess.
2137 Moving it away shrinks pp_hot.c; shrinking pp_hot.c helps speed perl up.
2143 Perl_start_glob (pTHX_ SV *tmpglob, IO *io)
2145 SV *tmpcmd = NEWSV(55, 0);
2149 #ifdef VMS /* expand the wildcards right here, rather than opening a pipe, */
2150 /* since spawning off a process is a real performance hit */
2152 #include <descrip.h>
2153 #include <lib$routines.h>
2156 char rslt[NAM$C_MAXRSS+1+sizeof(unsigned short int)] = {'\0','\0'};
2157 char vmsspec[NAM$C_MAXRSS+1];
2158 char *rstr = rslt + sizeof(unsigned short int), *begin, *end, *cp;
2159 $DESCRIPTOR(dfltdsc,"SYS$DISK:[]*.*;");
2162 struct dsc$descriptor_s wilddsc
2163 = {0, DSC$K_DTYPE_T, DSC$K_CLASS_S, 0};
2164 struct dsc$descriptor_vs rsdsc
2165 = {sizeof rslt, DSC$K_DTYPE_VT, DSC$K_CLASS_VS, rslt};
2166 unsigned long int cxt = 0, sts = 0, ok = 1, hasdir = 0, hasver = 0, isunix = 0;
2168 /* We could find out if there's an explicit dev/dir or version
2169 by peeking into lib$find_file's internal context at
2170 ((struct NAM *)((struct FAB *)cxt)->fab$l_nam)->nam$l_fnb
2171 but that's unsupported, so I don't want to do it now and
2172 have it bite someone in the future. */
2173 cp = SvPV(tmpglob,i);
2175 if (cp[i] == ';') hasver = 1;
2177 if (sts) hasver = 1;
2181 hasdir = isunix = 1;
2184 if (cp[i] == ']' || cp[i] == '>' || cp[i] == ':') {
2189 if ((tmpfp = PerlIO_tmpfile()) != NULL) {
2191 if (!PerlLIO_stat(SvPVX(tmpglob),&st) && S_ISDIR(st.st_mode))
2192 ok = ((wilddsc.dsc$a_pointer = tovmspath(SvPVX(tmpglob),vmsspec)) != NULL);
2193 else ok = ((wilddsc.dsc$a_pointer = tovmsspec(SvPVX(tmpglob),vmsspec)) != NULL);
2194 if (ok) wilddsc.dsc$w_length = (unsigned short int) strlen(wilddsc.dsc$a_pointer);
2195 while (ok && ((sts = lib$find_file(&wilddsc,&rsdsc,&cxt,
2196 &dfltdsc,NULL,NULL,NULL))&1)) {
2197 end = rstr + (unsigned long int) *rslt;
2198 if (!hasver) while (*end != ';') end--;
2199 *(end++) = '\n'; *end = '\0';
2200 for (cp = rstr; *cp; cp++) *cp = _tolower(*cp);
2202 if (isunix) trim_unixpath(rstr,SvPVX(tmpglob),1);
2207 while (*(--begin) != ']' && *begin != '>') ;
2210 ok = (PerlIO_puts(tmpfp,begin) != EOF);
2212 if (cxt) (void)lib$find_file_end(&cxt);
2213 if (ok && sts != RMS$_NMF &&
2214 sts != RMS$_DNF && sts != RMS$_FNF) ok = 0;
2217 SETERRNO((sts == RMS$_SYN ? EINVAL : EVMSERR),sts);
2219 PerlIO_close(tmpfp);
2223 PerlIO_rewind(tmpfp);
2224 IoTYPE(io) = IoTYPE_RDONLY;
2225 IoIFP(io) = fp = tmpfp;
2226 IoFLAGS(io) &= ~IOf_UNTAINT; /* maybe redundant */
2231 #ifdef MACOS_TRADITIONAL
2232 sv_setpv(tmpcmd, "glob ");
2233 sv_catsv(tmpcmd, tmpglob);
2234 sv_catpv(tmpcmd, " |");
2238 sv_setpv(tmpcmd, "for a in ");
2239 sv_catsv(tmpcmd, tmpglob);
2240 sv_catpv(tmpcmd, "; do echo \"$a\\0\\c\"; done |");
2243 sv_setpv(tmpcmd, "/dev/dosglob/"); /* File System Extension */
2244 sv_catsv(tmpcmd, tmpglob);
2246 sv_setpv(tmpcmd, "perlglob ");
2247 sv_catsv(tmpcmd, tmpglob);
2248 sv_catpv(tmpcmd, " |");
2253 sv_setpvn(tmpcmd, PL_cshname, PL_cshlen);
2254 sv_catpv(tmpcmd, " -cf 'set nonomatch; glob ");
2255 sv_catsv(tmpcmd, tmpglob);
2256 sv_catpv(tmpcmd, "' 2>/dev/null |");
2258 sv_setpv(tmpcmd, "echo ");
2259 sv_catsv(tmpcmd, tmpglob);
2261 sv_catpv(tmpcmd, "|tr -s ' \t\f\r' '\\012\\012\\012\\012'|");
2263 sv_catpv(tmpcmd, "|tr -s ' \t\f\r' '\\n\\n\\n\\n'|");
2266 #endif /* !DOSISH */
2267 #endif /* MACOS_TRADITIONAL */
2268 (void)do_open(PL_last_in_gv, SvPVX(tmpcmd), SvCUR(tmpcmd),
2269 FALSE, O_RDONLY, 0, Nullfp);