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. */
161 IoTYPE(io) = PerlIO_intmode2str(rawmode, &mode[ix], &writing);
163 namesv = sv_2mortal(newSVpvn(name,strlen(name)));
167 fp = PerlIO_openn(aTHX_ type, mode, -1, rawmode, rawperm, NULL, num_svs, svp);
170 /* Regular (non-sys) open */
175 PerlIO *that_fp = NULL;
177 type = savepvn(name, len);
181 /* Lose leading and trailing white space */
183 for (; isSPACE(*type); type++) ;
184 while (tend > type && isSPACE(tend[-1]))
188 /* New style explict name, type is just mode and discipline/layer info */
190 name = SvOK(*svp) ? SvPV(*svp, l) : "";
192 name = savepvn(name, len);
200 if ((*type == IoTYPE_RDWR) && /* scary */
201 (*(type+1) == IoTYPE_RDONLY || *(type+1) == IoTYPE_WRONLY) &&
202 ((!num_svs || (tend > type+1 && tend[-1] != IoTYPE_PIPE)))) {
203 TAINT_PROPER("open");
208 if (*type == IoTYPE_PIPE) {
210 if (type[1] != IoTYPE_STD) {
212 Perl_croak(aTHX_ "Unknown open() mode '%.*s'", (int)olen, oname);
217 for (type++; isSPACE(*type); type++) ;
223 /* command is missing 19990114 */
224 if (ckWARN(WARN_PIPE))
225 Perl_warner(aTHX_ WARN_PIPE, "Missing command in piped open");
229 if (strNE(name,"-") || num_svs)
231 TAINT_PROPER("piped open");
232 if (!num_svs && name[len-1] == '|') {
234 if (ckWARN(WARN_PIPE))
235 Perl_warner(aTHX_ WARN_PIPE, "Can't open bidirectional pipe");
244 fp = PerlProc_popen_list(mode, num_svs, svp);
247 fp = PerlProc_popen(name,mode);
250 else if (*type == IoTYPE_WRONLY) {
251 TAINT_PROPER("open");
253 if (*type == IoTYPE_WRONLY) {
254 /* Two IoTYPE_WRONLYs in a row make for an IoTYPE_APPEND. */
255 mode[0] = IoTYPE(io) = IoTYPE_APPEND;
270 dodup = PERLIO_DUP_FD;
276 if (!num_svs && !*type && supplied_fp) {
277 /* "<+&" etc. is used by typemaps */
282 Perl_croak(aTHX_ "More than one argument to '%c&' open",IoTYPE(io));
284 if (num_svs && SvIOK(*svp)) {
287 else if (isDIGIT(*type)) {
289 for (; isSPACE(*type); type++) ;
295 thatio = sv_2io(*svp);
300 for (; isSPACE(*type); type++) ;
301 thatgv = gv_fetchpv(type,FALSE,SVt_PVIO);
302 thatio = GvIO(thatgv);
306 SETERRNO(EINVAL,SS$_IVCHAN);
311 that_fp = IoIFP(thatio);
312 /* Flush stdio buffer before dup. --mjd
313 * Unfortunately SEEK_CURing 0 seems to
314 * be optimized away on most platforms;
315 * only Solaris and Linux seem to flush
318 /* sfio fails to clear error on next
319 sfwrite, contrary to documentation.
321 if (PerlIO_seek(that_fp, 0, SEEK_CUR) == -1)
322 PerlIO_clearerr(that_fp);
324 /* On the other hand, do all platforms
325 * take gracefully to flushing a read-only
326 * filehandle? Perhaps we should do
327 * fsetpos(src)+fgetpos(dst)? --nik */
328 PerlIO_flush(that_fp);
329 fd = PerlIO_fileno(that_fp);
330 /* When dup()ing STDIN, STDOUT or STDERR
331 * explicitly set appropriate access mode */
332 if (IoIFP(thatio) == PerlIO_stdout()
333 || IoIFP(thatio) == PerlIO_stderr())
334 IoTYPE(io) = IoTYPE_WRONLY;
335 else if (IoIFP(thatio) == PerlIO_stdin())
336 IoTYPE(io) = IoTYPE_RDONLY;
337 /* When dup()ing a socket, say result is
339 else if (IoTYPE(thatio) == IoTYPE_SOCKET)
340 IoTYPE(io) = IoTYPE_SOCKET;
348 fp = PerlIO_fdupopen(aTHX_ that_fp, NULL, dodup);
352 fd = PerlLIO_dup(fd);
355 if (!(fp = PerlIO_openn(aTHX_ type,mode,fd,0,0,NULL,num_svs,svp))) {
364 Perl_croak(aTHX_ "More than one argument to '>' open");
367 for (; isSPACE(*type); type++) ;
368 if (*type == IoTYPE_STD && (!type[1] || isSPACE(type[1]) || type[1] == ':')) {
371 fp = PerlIO_stdout();
372 IoTYPE(io) = IoTYPE_STD;
376 namesv = sv_2mortal(newSVpvn(type,strlen(type)));
381 fp = PerlIO_openn(aTHX_ type,mode,-1,0,0,NULL,num_svs,svp);
385 else if (*type == IoTYPE_RDONLY) {
387 Perl_croak(aTHX_ "More than one argument to '<' open");
390 for (type++; isSPACE(*type); type++) ;
400 if (*type == IoTYPE_STD && (!type[1] || isSPACE(type[1]) || type[1] == ':')) {
404 IoTYPE(io) = IoTYPE_STD;
408 namesv = sv_2mortal(newSVpvn(type,strlen(type)));
413 fp = PerlIO_openn(aTHX_ type,mode,-1,0,0,NULL,num_svs,svp);
416 else if ((num_svs && type[0] == IoTYPE_STD && type[1] == IoTYPE_PIPE) ||
417 (!num_svs && tend > type+1 && tend[-1] == IoTYPE_PIPE)) {
419 type += 2; /* skip over '-|' */
423 while (tend > type && isSPACE(tend[-1]))
426 for (; isSPACE(*type); type++) ;
431 /* command is missing 19990114 */
432 if (ckWARN(WARN_PIPE))
433 Perl_warner(aTHX_ WARN_PIPE, "Missing command in piped open");
437 if (strNE(name,"-") || num_svs)
439 TAINT_PROPER("piped open");
446 fp = PerlProc_popen_list(mode,num_svs,svp);
449 fp = PerlProc_popen(name,mode);
451 IoTYPE(io) = IoTYPE_PIPE;
457 IoTYPE(io) = IoTYPE_RDONLY;
459 for (; isSPACE(*name); name++) ;
465 if (strEQ(name,"-")) {
467 IoTYPE(io) = IoTYPE_STD;
471 namesv = sv_2mortal(newSVpvn(type,strlen(type)));
476 fp = PerlIO_openn(aTHX_ type,mode,-1,0,0,NULL,num_svs,svp);
481 if (ckWARN(WARN_NEWLINE) && IoTYPE(io) == IoTYPE_RDONLY && strchr(name, '\n'))
482 Perl_warner(aTHX_ WARN_NEWLINE, PL_warn_nl, "open");
486 if (ckWARN(WARN_IO)) {
487 if ((IoTYPE(io) == IoTYPE_RDONLY) &&
488 (fp == PerlIO_stdout() || fp == PerlIO_stderr())) {
489 Perl_warner(aTHX_ WARN_IO,
490 "Filehandle STD%s opened only for input",
491 (fp == PerlIO_stdout()) ? "OUT" : "ERR");
493 else if ((IoTYPE(io) == IoTYPE_WRONLY) && fp == PerlIO_stdin()) {
494 Perl_warner(aTHX_ WARN_IO,
495 "Filehandle STDIN opened only for output");
499 if (IoTYPE(io) && IoTYPE(io) != IoTYPE_PIPE && IoTYPE(io) != IoTYPE_STD &&
500 /* FIXME: This next term is a hack to avoid fileno on PerlIO::Scalar */
501 !(num_svs && SvROK(*svp))) {
502 if (PerlLIO_fstat(PerlIO_fileno(fp),&PL_statbuf) < 0) {
503 (void)PerlIO_close(fp);
507 if (S_ISSOCK(PL_statbuf.st_mode))
508 IoTYPE(io) = IoTYPE_SOCKET; /* in case a socket was passed in to us */
512 !(PL_statbuf.st_mode & S_IFMT)
516 && IoTYPE(io) != IoTYPE_WRONLY /* Dups of STD* filehandles already have */
517 && IoTYPE(io) != IoTYPE_RDONLY /* type so they aren't marked as sockets */
518 ) { /* on OS's that return 0 on fstat()ed pipe */
520 Sock_size_t buflen = sizeof tmpbuf;
521 if (PerlSock_getsockname(PerlIO_fileno(fp), (struct sockaddr *)tmpbuf,
523 || errno != ENOTSOCK)
524 IoTYPE(io) = IoTYPE_SOCKET; /* some OS's return 0 on fstat()ed socket */
525 /* but some return 0 for streams too, sigh */
527 #endif /* !PERL_MICRO */
530 if (saveifp) { /* must use old fp? */
531 /* If fd is less that PL_maxsysfd i.e. STDIN..STDERR
532 then dup the new fileno down
534 fd = PerlIO_fileno(fp);
536 PerlIO_flush(saveofp); /* emulate PerlIO_close() */
537 if (saveofp != saveifp) { /* was a socket? */
538 PerlIO_close(saveofp);
544 /* Still a small can-of-worms here if (say) PerlIO::Scalar
545 is assigned to (say) STDOUT - for now let dup2() fail
546 and provide the error
548 if (PerlLIO_dup2(fd, savefd) < 0) {
549 (void)PerlIO_close(fp);
553 if (savefd != PerlIO_fileno(PerlIO_stdin())) {
554 char newname[FILENAME_MAX+1];
555 if (PerlIO_getname(fp, newname)) {
556 if (fd == PerlIO_fileno(PerlIO_stdout())) Perl_vmssetuserlnm(aTHX_ "SYS$OUTPUT", newname);
557 if (fd == PerlIO_fileno(PerlIO_stderr())) Perl_vmssetuserlnm(aTHX_ "SYS$ERROR", newname);
562 sv = *av_fetch(PL_fdpid,fd,TRUE);
563 (void)SvUPGRADE(sv, SVt_IV);
566 sv = *av_fetch(PL_fdpid,savefd,TRUE);
568 (void)SvUPGRADE(sv, SVt_IV);
577 #if defined(HAS_FCNTL) && defined(F_SETFD)
579 int save_errno = errno;
580 fd = PerlIO_fileno(fp);
581 fcntl(fd,F_SETFD,fd > PL_maxsysfd); /* can change errno */
587 IoFLAGS(io) &= ~IOf_NOLINE;
589 if (IoTYPE(io) == IoTYPE_SOCKET
590 || (IoTYPE(io) == IoTYPE_WRONLY && S_ISCHR(PL_statbuf.st_mode)) ) {
592 if (!(IoOFP(io) = PerlIO_openn(aTHX_ type,mode,PerlIO_fileno(fp),0,0,NULL,num_svs,svp))) {
606 IoTYPE(io) = savetype;
611 Perl_nextargv(pTHX_ register GV *gv)
614 #ifndef FLEXFILENAMES
623 PL_argvoutgv = gv_fetchpv("ARGVOUT",TRUE,SVt_PVIO);
624 if (io && (IoFLAGS(io) & IOf_ARGV) && (IoFLAGS(io) & IOf_START)) {
625 IoFLAGS(io) &= ~IOf_START;
627 if (!PL_argvout_stack)
628 PL_argvout_stack = newAV();
629 av_push(PL_argvout_stack, SvREFCNT_inc(PL_defoutgv));
632 if (PL_filemode & (S_ISUID|S_ISGID)) {
633 PerlIO_flush(IoIFP(GvIOn(PL_argvoutgv))); /* chmod must follow last write */
635 (void)fchmod(PL_lastfd,PL_filemode);
637 (void)PerlLIO_chmod(PL_oldname,PL_filemode);
641 while (av_len(GvAV(gv)) >= 0) {
643 sv = av_shift(GvAV(gv));
645 sv_setsv(GvSV(gv),sv);
646 SvSETMAGIC(GvSV(gv));
647 PL_oldname = SvPVx(GvSV(gv), oldlen);
648 if (do_open(gv,PL_oldname,oldlen,PL_inplace!=0,O_RDONLY,0,Nullfp)) {
650 TAINT_PROPER("inplace open");
651 if (oldlen == 1 && *PL_oldname == '-') {
652 setdefout(gv_fetchpv("STDOUT",TRUE,SVt_PVIO));
653 return IoIFP(GvIOp(gv));
655 #ifndef FLEXFILENAMES
656 filedev = PL_statbuf.st_dev;
657 fileino = PL_statbuf.st_ino;
659 PL_filemode = PL_statbuf.st_mode;
660 fileuid = PL_statbuf.st_uid;
661 filegid = PL_statbuf.st_gid;
662 if (!S_ISREG(PL_filemode)) {
663 if (ckWARN_d(WARN_INPLACE))
664 Perl_warner(aTHX_ WARN_INPLACE,
665 "Can't do inplace edit: %s is not a regular file",
671 char *star = strchr(PL_inplace, '*');
673 char *begin = PL_inplace;
674 sv_setpvn(sv, "", 0);
676 sv_catpvn(sv, begin, star - begin);
677 sv_catpvn(sv, PL_oldname, oldlen);
679 } while ((star = strchr(begin, '*')));
684 sv_catpv(sv,PL_inplace);
686 #ifndef FLEXFILENAMES
687 if (PerlLIO_stat(SvPVX(sv),&PL_statbuf) >= 0
688 && PL_statbuf.st_dev == filedev
689 && PL_statbuf.st_ino == fileino
691 || (_djstat_fail_bits & _STFAIL_TRUENAME)!=0
695 if (ckWARN_d(WARN_INPLACE))
696 Perl_warner(aTHX_ WARN_INPLACE,
697 "Can't do inplace edit: %s would not be unique",
704 #if !defined(DOSISH) && !defined(__CYGWIN__)
705 if (PerlLIO_rename(PL_oldname,SvPVX(sv)) < 0) {
706 if (ckWARN_d(WARN_INPLACE))
707 Perl_warner(aTHX_ WARN_INPLACE,
708 "Can't rename %s to %s: %s, skipping file",
709 PL_oldname, SvPVX(sv), Strerror(errno) );
715 (void)PerlLIO_unlink(SvPVX(sv));
716 (void)PerlLIO_rename(PL_oldname,SvPVX(sv));
717 do_open(gv,SvPVX(sv),SvCUR(sv),PL_inplace!=0,O_RDONLY,0,Nullfp);
720 (void)UNLINK(SvPVX(sv));
721 if (link(PL_oldname,SvPVX(sv)) < 0) {
722 if (ckWARN_d(WARN_INPLACE))
723 Perl_warner(aTHX_ WARN_INPLACE,
724 "Can't rename %s to %s: %s, skipping file",
725 PL_oldname, SvPVX(sv), Strerror(errno) );
729 (void)UNLINK(PL_oldname);
733 #if !defined(DOSISH) && !defined(AMIGAOS)
734 # ifndef VMS /* Don't delete; use automatic file versioning */
735 if (UNLINK(PL_oldname) < 0) {
736 if (ckWARN_d(WARN_INPLACE))
737 Perl_warner(aTHX_ WARN_INPLACE,
738 "Can't remove %s: %s, skipping file",
739 PL_oldname, Strerror(errno) );
745 Perl_croak(aTHX_ "Can't do inplace edit without backup");
749 sv_setpvn(sv,">",!PL_inplace);
750 sv_catpvn(sv,PL_oldname,oldlen);
751 SETERRNO(0,0); /* in case sprintf set errno */
753 if (!do_open(PL_argvoutgv,SvPVX(sv),SvCUR(sv),PL_inplace!=0,
754 O_WRONLY|O_CREAT|O_TRUNC,0,Nullfp))
756 if (!do_open(PL_argvoutgv,SvPVX(sv),SvCUR(sv),PL_inplace!=0,
757 O_WRONLY|O_CREAT|OPEN_EXCL,0666,Nullfp))
760 if (ckWARN_d(WARN_INPLACE))
761 Perl_warner(aTHX_ WARN_INPLACE, "Can't do inplace edit on %s: %s",
762 PL_oldname, Strerror(errno) );
766 setdefout(PL_argvoutgv);
767 PL_lastfd = PerlIO_fileno(IoIFP(GvIOp(PL_argvoutgv)));
768 (void)PerlLIO_fstat(PL_lastfd,&PL_statbuf);
770 (void)fchmod(PL_lastfd,PL_filemode);
772 # if !(defined(WIN32) && defined(__BORLANDC__))
773 /* Borland runtime creates a readonly file! */
774 (void)PerlLIO_chmod(PL_oldname,PL_filemode);
777 if (fileuid != PL_statbuf.st_uid || filegid != PL_statbuf.st_gid) {
779 (void)fchown(PL_lastfd,fileuid,filegid);
782 (void)PerlLIO_chown(PL_oldname,fileuid,filegid);
787 return IoIFP(GvIOp(gv));
790 if (ckWARN_d(WARN_INPLACE)) {
792 if (PerlLIO_stat(PL_oldname, &PL_statbuf) >= 0
793 && !S_ISREG(PL_statbuf.st_mode))
795 Perl_warner(aTHX_ WARN_INPLACE,
796 "Can't do inplace edit: %s is not a regular file",
800 Perl_warner(aTHX_ WARN_INPLACE, "Can't open %s: %s",
801 PL_oldname, Strerror(eno));
805 if (io && (IoFLAGS(io) & IOf_ARGV))
806 IoFLAGS(io) |= IOf_START;
808 (void)do_close(PL_argvoutgv,FALSE);
809 if (io && (IoFLAGS(io) & IOf_ARGV)
810 && PL_argvout_stack && AvFILLp(PL_argvout_stack) >= 0)
812 GV *oldout = (GV*)av_pop(PL_argvout_stack);
814 SvREFCNT_dec(oldout);
817 setdefout(gv_fetchpv("STDOUT",TRUE,SVt_PVIO));
824 Perl_do_pipe(pTHX_ SV *sv, GV *rgv, GV *wgv)
843 if (PerlProc_pipe(fd) < 0)
845 IoIFP(rstio) = PerlIO_fdopen(fd[0], "r");
846 IoOFP(wstio) = PerlIO_fdopen(fd[1], "w");
847 IoIFP(wstio) = IoOFP(wstio);
848 IoTYPE(rstio) = IoTYPE_RDONLY;
849 IoTYPE(wstio) = IoTYPE_WRONLY;
850 if (!IoIFP(rstio) || !IoOFP(wstio)) {
851 if (IoIFP(rstio)) PerlIO_close(IoIFP(rstio));
852 else PerlLIO_close(fd[0]);
853 if (IoOFP(wstio)) PerlIO_close(IoOFP(wstio));
854 else PerlLIO_close(fd[1]);
858 sv_setsv(sv,&PL_sv_yes);
862 sv_setsv(sv,&PL_sv_undef);
867 /* explicit renamed to avoid C++ conflict -- kja */
869 Perl_do_close(pTHX_ GV *gv, bool not_implicit)
876 if (!gv || SvTYPE(gv) != SVt_PVGV) {
878 SETERRNO(EBADF,SS$_IVCHAN);
882 if (!io) { /* never opened */
884 if (ckWARN(WARN_UNOPENED)) /* no check for closed here */
885 report_evil_fh(gv, io, PL_op->op_type);
886 SETERRNO(EBADF,SS$_IVCHAN);
890 retval = io_close(io, not_implicit);
894 IoLINES_LEFT(io) = IoPAGE_LEN(io);
896 IoTYPE(io) = IoTYPE_CLOSED;
901 Perl_io_close(pTHX_ IO *io, bool not_implicit)
907 if (IoTYPE(io) == IoTYPE_PIPE) {
908 status = PerlProc_pclose(IoIFP(io));
910 STATUS_NATIVE_SET(status);
911 retval = (STATUS_POSIX == 0);
914 retval = (status != -1);
917 else if (IoTYPE(io) == IoTYPE_STD)
920 if (IoOFP(io) && IoOFP(io) != IoIFP(io)) { /* a socket */
921 retval = (PerlIO_close(IoOFP(io)) != EOF);
922 PerlIO_close(IoIFP(io)); /* clear stdio, fd already closed */
925 retval = (PerlIO_close(IoIFP(io)) != EOF);
927 IoOFP(io) = IoIFP(io) = Nullfp;
929 else if (not_implicit) {
930 SETERRNO(EBADF,SS$_IVCHAN);
937 Perl_do_eof(pTHX_ GV *gv)
946 else if (ckWARN(WARN_IO) && (IoTYPE(io) == IoTYPE_WRONLY))
948 /* integrate to report_evil_fh()? */
951 SV* sv = sv_newmortal();
952 gv_efullname4(sv, gv, Nullch, FALSE);
953 name = SvPV_nolen(sv);
956 Perl_warner(aTHX_ WARN_IO,
957 "Filehandle %s opened only for output", name);
959 Perl_warner(aTHX_ WARN_IO,
960 "Filehandle opened only for output");
965 if (PerlIO_has_cntptr(IoIFP(io))) { /* (the code works without this) */
966 if (PerlIO_get_cnt(IoIFP(io)) > 0) /* cheat a little, since */
967 return FALSE; /* this is the most usual case */
970 ch = PerlIO_getc(IoIFP(io));
972 (void)PerlIO_ungetc(IoIFP(io),ch);
976 if (PerlIO_has_cntptr(IoIFP(io)) && PerlIO_canset_cnt(IoIFP(io))) {
977 if (PerlIO_get_cnt(IoIFP(io)) < -1)
978 PerlIO_set_cnt(IoIFP(io),-1);
980 if (PL_op->op_flags & OPf_SPECIAL) { /* not necessarily a real EOF yet? */
981 if (!nextargv(PL_argvgv)) /* get another fp handy */
985 return TRUE; /* normal fp, definitely end of file */
991 Perl_do_tell(pTHX_ GV *gv)
996 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io))) {
997 #ifdef ULTRIX_STDIO_BOTCH
999 (void)PerlIO_seek(fp, 0L, 2); /* ultrix 1.2 workaround */
1001 return PerlIO_tell(fp);
1003 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1004 report_evil_fh(gv, io, PL_op->op_type);
1005 SETERRNO(EBADF,RMS$_IFI);
1010 Perl_do_seek(pTHX_ GV *gv, Off_t pos, int whence)
1012 register IO *io = 0;
1013 register PerlIO *fp;
1015 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io))) {
1016 #ifdef ULTRIX_STDIO_BOTCH
1018 (void)PerlIO_seek(fp, 0L, 2); /* ultrix 1.2 workaround */
1020 return PerlIO_seek(fp, pos, whence) >= 0;
1022 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1023 report_evil_fh(gv, io, PL_op->op_type);
1024 SETERRNO(EBADF,RMS$_IFI);
1029 Perl_do_sysseek(pTHX_ GV *gv, Off_t pos, int whence)
1031 register IO *io = 0;
1032 register PerlIO *fp;
1034 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io)))
1035 return PerlLIO_lseek(PerlIO_fileno(fp), pos, whence);
1036 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1037 report_evil_fh(gv, io, PL_op->op_type);
1038 SETERRNO(EBADF,RMS$_IFI);
1043 Perl_mode_from_discipline(pTHX_ SV *discp)
1045 int mode = O_BINARY;
1048 char *s = SvPV(discp,len);
1053 if (len > 3 && strnEQ(s+1, "raw", 3)
1054 && (!s[4] || s[4] == ':' || isSPACE(s[4])))
1063 if (len > 4 && strnEQ(s+1, "crlf", 4)
1064 && (!s[5] || s[5] == ':' || isSPACE(s[5])))
1073 goto fail_discipline;
1076 else if (isSPACE(*s)) {
1083 end = strchr(s+1, ':');
1086 #ifndef PERLIO_LAYERS
1087 Perl_croak(aTHX_ "Unknown discipline '%.*s'", end-s, s);
1098 Perl_do_binmode(pTHX_ PerlIO *fp, int iotype, int mode)
1100 /* The old body of this is now in non-LAYER part of perlio.c
1101 * This is a stub for any XS code which might have been calling it.
1103 char *name = (O_BINARY != O_TEXT && !(mode & O_BINARY)) ? ":crlf" : ":raw";
1104 return PerlIO_binmode(aTHX_ fp, iotype, mode, name);
1107 #if !defined(HAS_TRUNCATE) && !defined(HAS_CHSIZE) && defined(F_FREESP)
1108 /* code courtesy of William Kucharski */
1111 I32 my_chsize(fd, length)
1112 I32 fd; /* file descriptor */
1113 Off_t length; /* length to set file to */
1116 struct stat filebuf;
1118 if (PerlLIO_fstat(fd, &filebuf) < 0)
1121 if (filebuf.st_size < length) {
1123 /* extend file length */
1125 if ((PerlLIO_lseek(fd, (length - 1), 0)) < 0)
1128 /* write a "0" byte */
1130 if ((PerlLIO_write(fd, "", 1)) != 1)
1134 /* truncate length */
1138 fl.l_start = length;
1139 fl.l_type = F_WRLCK; /* write lock on file space */
1142 * This relies on the UNDOCUMENTED F_FREESP argument to
1143 * fcntl(2), which truncates the file so that it ends at the
1144 * position indicated by fl.l_start.
1146 * Will minor miracles never cease?
1149 if (fcntl(fd, F_FREESP, &fl) < 0)
1156 #endif /* F_FREESP */
1159 Perl_do_print(pTHX_ register SV *sv, PerlIO *fp)
1161 register char *tmps;
1164 /* assuming fp is checked earlier */
1170 if (SvIOK(sv) && SvIVX(sv) != 0) {
1171 PerlIO_printf(fp, PL_ofmt, (NV)SvIVX(sv));
1172 return !PerlIO_error(fp);
1174 if ( (SvNOK(sv) && SvNVX(sv) != 0.0)
1175 || (looks_like_number(sv) && sv_2nv(sv) != 0.0) ) {
1176 PerlIO_printf(fp, PL_ofmt, SvNVX(sv));
1177 return !PerlIO_error(fp);
1180 switch (SvTYPE(sv)) {
1182 if (ckWARN(WARN_UNINITIALIZED))
1190 PerlIO_printf(fp, "%"UVuf, (UV)SvUVX(sv));
1192 PerlIO_printf(fp, "%"IVdf, (IV)SvIVX(sv));
1193 return !PerlIO_error(fp);
1197 if (PerlIO_isutf8(fp)) {
1199 sv_utf8_upgrade(sv = sv_mortalcopy(sv));
1201 else if (DO_UTF8(sv)) {
1202 if (!sv_utf8_downgrade((sv = sv_mortalcopy(sv)), TRUE)
1203 && ckWARN(WARN_UTF8))
1205 Perl_warner(aTHX_ WARN_UTF8, "Wide character in print");
1208 tmps = SvPV(sv, len);
1211 /* To detect whether the process is about to overstep its
1212 * filesize limit we would need getrlimit(). We could then
1213 * also transparently raise the limit with setrlimit() --
1214 * but only until the system hard limit/the filesystem limit,
1215 * at which we would get EPERM. Note that when using buffered
1216 * io the write failure can be delayed until the flush/close. --jhi */
1217 if (len && (PerlIO_write(fp,tmps,len) == 0))
1219 return !PerlIO_error(fp);
1229 if (PL_op->op_flags & OPf_REF) {
1234 if (io && IoIFP(io)) {
1236 sv_setpv(PL_statname,"");
1237 PL_laststype = OP_STAT;
1238 return (PL_laststatval = PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache));
1242 return PL_laststatval;
1243 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1244 report_evil_fh(gv, io, PL_op->op_type);
1246 sv_setpv(PL_statname,"");
1247 return (PL_laststatval = -1);
1255 if (SvTYPE(sv) == SVt_PVGV) {
1259 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
1266 sv_setpv(PL_statname, s);
1267 PL_laststype = OP_STAT;
1268 PL_laststatval = PerlLIO_stat(s, &PL_statcache);
1269 if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(s, '\n'))
1270 Perl_warner(aTHX_ WARN_NEWLINE, PL_warn_nl, "stat");
1271 return PL_laststatval;
1281 if (PL_op->op_flags & OPf_REF) {
1283 if (cGVOP_gv == PL_defgv) {
1284 if (PL_laststype != OP_LSTAT)
1285 Perl_croak(aTHX_ "The stat preceding -l _ wasn't an lstat");
1286 return PL_laststatval;
1288 Perl_croak(aTHX_ "You can't use -l on a filehandle");
1291 PL_laststype = OP_LSTAT;
1295 sv_setpv(PL_statname,SvPV(sv, n_a));
1296 PL_laststatval = PerlLIO_lstat(SvPV(sv, n_a),&PL_statcache);
1297 if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(SvPV(sv, n_a), '\n'))
1298 Perl_warner(aTHX_ WARN_NEWLINE, PL_warn_nl, "lstat");
1299 return PL_laststatval;
1303 Perl_do_aexec(pTHX_ SV *really, register SV **mark, register SV **sp)
1305 return do_aexec5(really, mark, sp, 0, 0);
1309 Perl_do_aexec5(pTHX_ SV *really, register SV **mark, register SV **sp,
1310 int fd, int do_report)
1312 #ifdef MACOS_TRADITIONAL
1313 Perl_croak(aTHX_ "exec? I'm not *that* kind of operating system");
1316 char *tmps = Nullch;
1320 New(401,PL_Argv, sp - mark + 1, char*);
1322 while (++mark <= sp) {
1324 *a++ = SvPVx(*mark, n_a);
1330 tmps = SvPV(really, n_a);
1331 if ((!really && *PL_Argv[0] != '/') ||
1332 (really && *tmps != '/')) /* will execvp use PATH? */
1333 TAINT_ENV(); /* testing IFS here is overkill, probably */
1334 if (really && *tmps)
1335 PerlProc_execvp(tmps,EXEC_ARGV_CAST(PL_Argv));
1337 PerlProc_execvp(PL_Argv[0],EXEC_ARGV_CAST(PL_Argv));
1338 if (ckWARN(WARN_EXEC))
1339 Perl_warner(aTHX_ WARN_EXEC, "Can't exec \"%s\": %s",
1340 (really ? tmps : PL_Argv[0]), Strerror(errno));
1344 PerlLIO_write(fd, (void*)&e, sizeof(int));
1354 Perl_do_execfree(pTHX)
1358 PL_Argv = Null(char **);
1366 #if !defined(OS2) && !defined(WIN32) && !defined(DJGPP) && !defined(EPOC) && !defined(MACOS_TRADITIONAL)
1369 Perl_do_exec(pTHX_ char *cmd)
1371 return do_exec3(cmd,0,0);
1375 Perl_do_exec3(pTHX_ char *cmd, int fd, int do_report)
1380 while (*cmd && isSPACE(*cmd))
1383 /* save an extra exec if possible */
1388 if (strnEQ(cmd,PL_cshname,PL_cshlen) &&
1389 strnEQ(cmd+PL_cshlen," -c",3)) {
1391 s = cmd+PL_cshlen+3;
1405 if (s[-1] == '\'') {
1407 PerlProc_execl(PL_cshname,"csh", flags, ncmd, (char*)0);
1416 /* see if there are shell metacharacters in it */
1418 if (*cmd == '.' && isSPACE(cmd[1]))
1421 if (strnEQ(cmd,"exec",4) && isSPACE(cmd[4]))
1424 for (s = cmd; *s && isALNUM(*s); s++) ; /* catch VAR=val gizmo */
1428 for (s = cmd; *s; s++) {
1429 if (*s != ' ' && !isALPHA(*s) &&
1430 strchr("$&*(){}[]'\";\\|?<>~`\n",*s)) {
1431 if (*s == '\n' && !s[1]) {
1435 /* handle the 2>&1 construct at the end */
1436 if (*s == '>' && s[1] == '&' && s[2] == '1'
1437 && s > cmd + 1 && s[-1] == '2' && isSPACE(s[-2])
1438 && (!s[3] || isSPACE(s[3])))
1442 while (*t && isSPACE(*t))
1444 if (!*t && (dup2(1,2) != -1)) {
1450 PerlProc_execl(PL_sh_path, "sh", "-c", cmd, (char*)0);
1455 New(402,PL_Argv, (s - cmd) / 2 + 2, char*);
1456 PL_Cmd = savepvn(cmd, s-cmd);
1458 for (s = PL_Cmd; *s;) {
1459 while (*s && isSPACE(*s)) s++;
1462 while (*s && !isSPACE(*s)) s++;
1468 PerlProc_execvp(PL_Argv[0],PL_Argv);
1469 if (errno == ENOEXEC) { /* for system V NIH syndrome */
1476 if (ckWARN(WARN_EXEC))
1477 Perl_warner(aTHX_ WARN_EXEC, "Can't exec \"%s\": %s",
1478 PL_Argv[0], Strerror(errno));
1480 PerlLIO_write(fd, (void*)&e, sizeof(int));
1489 #endif /* OS2 || WIN32 */
1492 Perl_apply(pTHX_ I32 type, register SV **mark, register SV **sp)
1496 register I32 tot = 0;
1499 SV **oldmark = mark;
1502 #define APPLY_TAINT_PROPER() \
1504 if (PL_tainted) { TAINT_PROPER(what); } \
1507 /* This is a first heuristic; it doesn't catch tainting magic. */
1509 while (++mark <= sp) {
1510 if (SvTAINTED(*mark)) {
1520 APPLY_TAINT_PROPER();
1523 APPLY_TAINT_PROPER();
1525 while (++mark <= sp) {
1526 char *name = SvPVx(*mark, n_a);
1527 APPLY_TAINT_PROPER();
1528 if (PerlLIO_chmod(name, val))
1536 APPLY_TAINT_PROPER();
1537 if (sp - mark > 2) {
1538 val = SvIVx(*++mark);
1539 val2 = SvIVx(*++mark);
1540 APPLY_TAINT_PROPER();
1542 while (++mark <= sp) {
1543 char *name = SvPVx(*mark, n_a);
1544 APPLY_TAINT_PROPER();
1545 if (PerlLIO_chown(name, val, val2))
1552 XXX Should we make lchown() directly available from perl?
1553 For now, we'll let Configure test for HAS_LCHOWN, but do
1554 nothing in the core.
1560 APPLY_TAINT_PROPER();
1563 s = SvPVx(*++mark, n_a);
1565 if (*s == 'S' && s[1] == 'I' && s[2] == 'G')
1567 if (!(val = whichsig(s)))
1568 Perl_croak(aTHX_ "Unrecognized signal name \"%s\"",s);
1572 APPLY_TAINT_PROPER();
1575 /* kill() doesn't do process groups (job trees?) under VMS */
1576 if (val < 0) val = -val;
1577 if (val == SIGKILL) {
1578 # include <starlet.h>
1579 /* Use native sys$delprc() to insure that target process is
1580 * deleted; supervisor-mode images don't pay attention to
1581 * CRTL's emulation of Unix-style signals and kill()
1583 while (++mark <= sp) {
1584 I32 proc = SvIVx(*mark);
1585 register unsigned long int __vmssts;
1586 APPLY_TAINT_PROPER();
1587 if (!((__vmssts = sys$delprc(&proc,0)) & 1)) {
1591 case SS$_NOSUCHNODE:
1592 SETERRNO(ESRCH,__vmssts);
1595 SETERRNO(EPERM,__vmssts);
1598 SETERRNO(EVMSERR,__vmssts);
1607 while (++mark <= sp) {
1608 I32 proc = SvIVx(*mark);
1609 APPLY_TAINT_PROPER();
1611 if (PerlProc_killpg(proc,val)) /* BSD */
1613 if (PerlProc_kill(-proc,val)) /* SYSV */
1619 while (++mark <= sp) {
1620 I32 proc = SvIVx(*mark);
1621 APPLY_TAINT_PROPER();
1622 if (PerlProc_kill(proc, val))
1630 APPLY_TAINT_PROPER();
1632 while (++mark <= sp) {
1633 s = SvPVx(*mark, n_a);
1634 APPLY_TAINT_PROPER();
1635 if (PL_euid || PL_unsafe) {
1639 else { /* don't let root wipe out directories without -U */
1640 if (PerlLIO_lstat(s,&PL_statbuf) < 0 || S_ISDIR(PL_statbuf.st_mode))
1652 APPLY_TAINT_PROPER();
1653 if (sp - mark > 2) {
1654 #if defined(I_UTIME) || defined(VMS)
1655 struct utimbuf utbuf;
1663 SV* accessed = *++mark;
1664 SV* modified = *++mark;
1665 void * utbufp = &utbuf;
1667 /* be like C, and if both times are undefined, let the C
1668 library figure out what to do. This usually means
1671 if ( accessed == &PL_sv_undef && modified == &PL_sv_undef )
1674 Zero(&utbuf, sizeof utbuf, char);
1676 utbuf.actime = (Time_t)SvNVx(accessed); /* time accessed */
1677 utbuf.modtime = (Time_t)SvNVx(modified); /* time modified */
1679 utbuf.actime = (Time_t)SvIVx(accessed); /* time accessed */
1680 utbuf.modtime = (Time_t)SvIVx(modified); /* time modified */
1682 APPLY_TAINT_PROPER();
1684 while (++mark <= sp) {
1685 char *name = SvPVx(*mark, n_a);
1686 APPLY_TAINT_PROPER();
1687 if (PerlLIO_utime(name, utbufp))
1698 #undef APPLY_TAINT_PROPER
1701 /* Do the permissions allow some operation? Assumes statcache already set. */
1702 #ifndef VMS /* VMS' cando is in vms.c */
1704 Perl_cando(pTHX_ Mode_t mode, Uid_t effective, register Stat_t *statbufp)
1705 /* Note: we use `effective' both for uids and gids.
1706 * Here we are betting on Uid_t being equal or wider than Gid_t. */
1709 /* [Comments and code from Len Reed]
1710 * MS-DOS "user" is similar to UNIX's "superuser," but can't write
1711 * to write-protected files. The execute permission bit is set
1712 * by the Miscrosoft C library stat() function for the following:
1717 * All files and directories are readable.
1718 * Directories and special files, e.g. "CON", cannot be
1720 * [Comment by Tom Dinger -- a directory can have the write-protect
1721 * bit set in the file system, but DOS permits changes to
1722 * the directory anyway. In addition, all bets are off
1723 * here for networked software, such as Novell and
1727 /* Atari stat() does pretty much the same thing. we set x_bit_set_in_stat
1728 * too so it will actually look into the files for magic numbers
1730 return (mode & statbufp->st_mode) ? TRUE : FALSE;
1732 #else /* ! DOSISH */
1733 if ((effective ? PL_euid : PL_uid) == 0) { /* root is special */
1734 if (mode == S_IXUSR) {
1735 if (statbufp->st_mode & 0111 || S_ISDIR(statbufp->st_mode))
1739 return TRUE; /* root reads and writes anything */
1742 if (statbufp->st_uid == (effective ? PL_euid : PL_uid) ) {
1743 if (statbufp->st_mode & mode)
1744 return TRUE; /* ok as "user" */
1746 else if (ingroup(statbufp->st_gid,effective)) {
1747 if (statbufp->st_mode & mode >> 3)
1748 return TRUE; /* ok as "group" */
1750 else if (statbufp->st_mode & mode >> 6)
1751 return TRUE; /* ok as "other" */
1753 #endif /* ! DOSISH */
1758 Perl_ingroup(pTHX_ Gid_t testgid, Uid_t effective)
1760 #ifdef MACOS_TRADITIONAL
1761 /* This is simply not correct for AppleShare, but fix it yerself. */
1764 if (testgid == (effective ? PL_egid : PL_gid))
1766 #ifdef HAS_GETGROUPS
1771 Groups_t gary[NGROUPS];
1774 anum = getgroups(NGROUPS,gary);
1776 if (gary[anum] == testgid)
1784 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
1787 Perl_do_ipcget(pTHX_ I32 optype, SV **mark, SV **sp)
1792 key = (key_t)SvNVx(*++mark);
1793 n = (optype == OP_MSGGET) ? 0 : SvIVx(*++mark);
1794 flags = SvIVx(*++mark);
1800 return msgget(key, flags);
1804 return semget(key, n, flags);
1808 return shmget(key, n, flags);
1810 #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
1812 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
1815 return -1; /* should never happen */
1819 Perl_do_ipcctl(pTHX_ I32 optype, SV **mark, SV **sp)
1823 I32 id, n, cmd, infosize, getinfo;
1826 id = SvIVx(*++mark);
1827 n = (optype == OP_SEMCTL) ? SvIVx(*++mark) : 0;
1828 cmd = SvIVx(*++mark);
1831 getinfo = (cmd == IPC_STAT);
1837 if (cmd == IPC_STAT || cmd == IPC_SET)
1838 infosize = sizeof(struct msqid_ds);
1843 if (cmd == IPC_STAT || cmd == IPC_SET)
1844 infosize = sizeof(struct shmid_ds);
1850 if (cmd == IPC_STAT || cmd == IPC_SET)
1851 infosize = sizeof(struct semid_ds);
1852 else if (cmd == GETALL || cmd == SETALL)
1854 struct semid_ds semds;
1856 #ifdef EXTRA_F_IN_SEMUN_BUF
1857 semun.buff = &semds;
1861 getinfo = (cmd == GETALL);
1862 if (Semctl(id, 0, IPC_STAT, semun) == -1)
1864 infosize = semds.sem_nsems * sizeof(short);
1865 /* "short" is technically wrong but much more portable
1866 than guessing about u_?short(_t)? */
1869 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
1873 #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
1875 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
1884 SvPV_force(astr, len);
1885 a = SvGROW(astr, infosize+1);
1889 a = SvPV(astr, len);
1890 if (len != infosize)
1891 Perl_croak(aTHX_ "Bad arg length for %s, is %lu, should be %ld",
1900 a = INT2PTR(char *,i); /* ouch */
1907 ret = msgctl(id, cmd, (struct msqid_ds *)a);
1913 union semun unsemds;
1915 #ifdef EXTRA_F_IN_SEMUN_BUF
1916 unsemds.buff = (struct semid_ds *)a;
1918 unsemds.buf = (struct semid_ds *)a;
1920 ret = Semctl(id, n, cmd, unsemds);
1922 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
1929 ret = shmctl(id, cmd, (struct shmid_ds *)a);
1933 if (getinfo && ret >= 0) {
1934 SvCUR_set(astr, infosize);
1935 *SvEND(astr) = '\0';
1942 Perl_do_msgsnd(pTHX_ SV **mark, SV **sp)
1947 I32 id, msize, flags;
1950 id = SvIVx(*++mark);
1952 flags = SvIVx(*++mark);
1953 mbuf = SvPV(mstr, len);
1954 if ((msize = len - sizeof(long)) < 0)
1955 Perl_croak(aTHX_ "Arg too short for msgsnd");
1957 return msgsnd(id, (struct msgbuf *)mbuf, msize, flags);
1959 Perl_croak(aTHX_ "msgsnd not implemented");
1964 Perl_do_msgrcv(pTHX_ SV **mark, SV **sp)
1970 I32 id, msize, flags, ret;
1973 id = SvIVx(*++mark);
1975 /* suppress warning when reading into undef var --jhi */
1977 sv_setpvn(mstr, "", 0);
1978 msize = SvIVx(*++mark);
1979 mtype = (long)SvIVx(*++mark);
1980 flags = SvIVx(*++mark);
1981 SvPV_force(mstr, len);
1982 mbuf = SvGROW(mstr, sizeof(long)+msize+1);
1985 ret = msgrcv(id, (struct msgbuf *)mbuf, msize, mtype, flags);
1987 SvCUR_set(mstr, sizeof(long)+ret);
1988 *SvEND(mstr) = '\0';
1989 #ifndef INCOMPLETE_TAINTS
1990 /* who knows who has been playing with this message? */
1996 Perl_croak(aTHX_ "msgrcv not implemented");
2001 Perl_do_semop(pTHX_ SV **mark, SV **sp)
2009 id = SvIVx(*++mark);
2011 opbuf = SvPV(opstr, opsize);
2012 if (opsize < 3 * SHORTSIZE
2013 || (opsize % (3 * SHORTSIZE))) {
2014 SETERRNO(EINVAL,LIB$_INVARG);
2018 /* We can't assume that sizeof(struct sembuf) == 3 * sizeof(short). */
2020 int nsops = opsize / (3 * sizeof (short));
2022 short *ops = (short *) opbuf;
2024 struct sembuf *temps, *t;
2027 New (0, temps, nsops, struct sembuf);
2035 result = semop(id, temps, nsops);
2049 Perl_croak(aTHX_ "semop not implemented");
2054 Perl_do_shmio(pTHX_ I32 optype, SV **mark, SV **sp)
2059 I32 id, mpos, msize;
2061 struct shmid_ds shmds;
2063 id = SvIVx(*++mark);
2065 mpos = SvIVx(*++mark);
2066 msize = SvIVx(*++mark);
2068 if (shmctl(id, IPC_STAT, &shmds) == -1)
2070 if (mpos < 0 || msize < 0 || mpos + msize > shmds.shm_segsz) {
2071 SETERRNO(EFAULT,SS$_ACCVIO); /* can't do as caller requested */
2074 shm = (char *)shmat(id, (char*)NULL, (optype == OP_SHMREAD) ? SHM_RDONLY : 0);
2075 if (shm == (char *)-1) /* I hate System V IPC, I really do */
2077 if (optype == OP_SHMREAD) {
2078 /* suppress warning when reading into undef var (tchrist 3/Mar/00) */
2080 sv_setpvn(mstr, "", 0);
2081 SvPV_force(mstr, len);
2082 mbuf = SvGROW(mstr, msize+1);
2084 Copy(shm + mpos, mbuf, msize, char);
2085 SvCUR_set(mstr, msize);
2086 *SvEND(mstr) = '\0';
2088 #ifndef INCOMPLETE_TAINTS
2089 /* who knows who has been playing with this shared memory? */
2096 mbuf = SvPV(mstr, len);
2097 if ((n = len) > msize)
2099 Copy(mbuf, shm + mpos, n, char);
2101 memzero(shm + mpos + n, msize - n);
2105 Perl_croak(aTHX_ "shm I/O not implemented");
2109 #endif /* SYSV IPC */
2112 =for apidoc start_glob
2114 Function called by C<do_readline> to spawn a glob (or do the glob inside
2115 perl on VMS). This code used to be inline, but now perl uses C<File::Glob>
2116 this glob starter is only used by miniperl during the build process.
2117 Moving it away shrinks pp_hot.c; shrinking pp_hot.c helps speed perl up.
2123 Perl_start_glob (pTHX_ SV *tmpglob, IO *io)
2125 SV *tmpcmd = NEWSV(55, 0);
2129 #ifdef VMS /* expand the wildcards right here, rather than opening a pipe, */
2130 /* since spawning off a process is a real performance hit */
2132 #include <descrip.h>
2133 #include <lib$routines.h>
2136 char rslt[NAM$C_MAXRSS+1+sizeof(unsigned short int)] = {'\0','\0'};
2137 char vmsspec[NAM$C_MAXRSS+1];
2138 char *rstr = rslt + sizeof(unsigned short int), *begin, *end, *cp;
2139 $DESCRIPTOR(dfltdsc,"SYS$DISK:[]*.*;");
2142 struct dsc$descriptor_s wilddsc
2143 = {0, DSC$K_DTYPE_T, DSC$K_CLASS_S, 0};
2144 struct dsc$descriptor_vs rsdsc
2145 = {sizeof rslt, DSC$K_DTYPE_VT, DSC$K_CLASS_VS, rslt};
2146 unsigned long int cxt = 0, sts = 0, ok = 1, hasdir = 0, hasver = 0, isunix = 0;
2148 /* We could find out if there's an explicit dev/dir or version
2149 by peeking into lib$find_file's internal context at
2150 ((struct NAM *)((struct FAB *)cxt)->fab$l_nam)->nam$l_fnb
2151 but that's unsupported, so I don't want to do it now and
2152 have it bite someone in the future. */
2153 cp = SvPV(tmpglob,i);
2155 if (cp[i] == ';') hasver = 1;
2157 if (sts) hasver = 1;
2161 hasdir = isunix = 1;
2164 if (cp[i] == ']' || cp[i] == '>' || cp[i] == ':') {
2169 if ((tmpfp = PerlIO_tmpfile()) != NULL) {
2171 if (!PerlLIO_stat(SvPVX(tmpglob),&st) && S_ISDIR(st.st_mode))
2172 ok = ((wilddsc.dsc$a_pointer = tovmspath(SvPVX(tmpglob),vmsspec)) != NULL);
2173 else ok = ((wilddsc.dsc$a_pointer = tovmsspec(SvPVX(tmpglob),vmsspec)) != NULL);
2174 if (ok) wilddsc.dsc$w_length = (unsigned short int) strlen(wilddsc.dsc$a_pointer);
2175 for (cp=wilddsc.dsc$a_pointer; ok && cp && *cp; cp++)
2176 if (*cp == '?') *cp = '%'; /* VMS style single-char wildcard */
2177 while (ok && ((sts = lib$find_file(&wilddsc,&rsdsc,&cxt,
2178 &dfltdsc,NULL,NULL,NULL))&1)) {
2179 end = rstr + (unsigned long int) *rslt;
2180 if (!hasver) while (*end != ';') end--;
2181 *(end++) = '\n'; *end = '\0';
2182 for (cp = rstr; *cp; cp++) *cp = _tolower(*cp);
2184 if (isunix) trim_unixpath(rstr,SvPVX(tmpglob),1);
2189 while (*(--begin) != ']' && *begin != '>') ;
2192 ok = (PerlIO_puts(tmpfp,begin) != EOF);
2194 if (cxt) (void)lib$find_file_end(&cxt);
2195 if (ok && sts != RMS$_NMF &&
2196 sts != RMS$_DNF && sts != RMS$_FNF) ok = 0;
2199 SETERRNO((sts == RMS$_SYN ? EINVAL : EVMSERR),sts);
2201 PerlIO_close(tmpfp);
2205 PerlIO_rewind(tmpfp);
2206 IoTYPE(io) = IoTYPE_RDONLY;
2207 IoIFP(io) = fp = tmpfp;
2208 IoFLAGS(io) &= ~IOf_UNTAINT; /* maybe redundant */
2213 #ifdef MACOS_TRADITIONAL
2214 sv_setpv(tmpcmd, "glob ");
2215 sv_catsv(tmpcmd, tmpglob);
2216 sv_catpv(tmpcmd, " |");
2220 sv_setpv(tmpcmd, "for a in ");
2221 sv_catsv(tmpcmd, tmpglob);
2222 sv_catpv(tmpcmd, "; do echo \"$a\\0\\c\"; done |");
2225 sv_setpv(tmpcmd, "/dev/dosglob/"); /* File System Extension */
2226 sv_catsv(tmpcmd, tmpglob);
2228 sv_setpv(tmpcmd, "perlglob ");
2229 sv_catsv(tmpcmd, tmpglob);
2230 sv_catpv(tmpcmd, " |");
2235 sv_setpvn(tmpcmd, PL_cshname, PL_cshlen);
2236 sv_catpv(tmpcmd, " -cf 'set nonomatch; glob ");
2237 sv_catsv(tmpcmd, tmpglob);
2238 sv_catpv(tmpcmd, "' 2>/dev/null |");
2240 sv_setpv(tmpcmd, "echo ");
2241 sv_catsv(tmpcmd, tmpglob);
2243 sv_catpv(tmpcmd, "|tr -s ' \t\f\r' '\\012\\012\\012\\012'|");
2245 sv_catpv(tmpcmd, "|tr -s ' \t\f\r' '\\n\\n\\n\\n'|");
2248 #endif /* !DOSISH */
2249 #endif /* MACOS_TRADITIONAL */
2250 (void)do_open(PL_last_in_gv, SvPVX(tmpcmd), SvCUR(tmpcmd),
2251 FALSE, O_RDONLY, 0, Nullfp);