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 */
176 type = savepvn(name, len);
180 /* Lose leading and trailing white space */
182 for (; isSPACE(*type); type++) ;
183 while (tend > type && isSPACE(tend[-1]))
187 /* New style explict name, type is just mode and discipline/layer info */
189 name = SvOK(*svp) ? SvPV(*svp, l) : "";
191 name = savepvn(name, len);
199 if ((*type == IoTYPE_RDWR) && /* scary */
200 (*(type+1) == IoTYPE_RDONLY || *(type+1) == IoTYPE_WRONLY) &&
201 ((!num_svs || (tend > type+1 && tend[-1] != IoTYPE_PIPE)))) {
202 TAINT_PROPER("open");
207 if (*type == IoTYPE_PIPE) {
209 if (type[1] != IoTYPE_STD) {
211 Perl_croak(aTHX_ "Unknown open() mode '%.*s'", (int)olen, oname);
216 for (type++; isSPACE(*type); type++) ;
222 /* command is missing 19990114 */
223 if (ckWARN(WARN_PIPE))
224 Perl_warner(aTHX_ WARN_PIPE, "Missing command in piped open");
228 if (strNE(name,"-") || num_svs)
230 TAINT_PROPER("piped open");
231 if (!num_svs && name[len-1] == '|') {
233 if (ckWARN(WARN_PIPE))
234 Perl_warner(aTHX_ WARN_PIPE, "Can't open bidirectional pipe");
243 fp = PerlProc_popen_list(mode, num_svs, svp);
246 fp = PerlProc_popen(name,mode);
249 else if (*type == IoTYPE_WRONLY) {
250 TAINT_PROPER("open");
252 if (*type == IoTYPE_WRONLY) {
253 /* Two IoTYPE_WRONLYs in a row make for an IoTYPE_APPEND. */
254 mode[0] = IoTYPE(io) = IoTYPE_APPEND;
275 if (!num_svs && !*type && supplied_fp) {
276 /* "<+&" etc. is used by typemaps */
281 Perl_croak(aTHX_ "More than one argument to '%c&' open",IoTYPE(io));
283 if (num_svs && SvIOK(*svp)) {
286 else if (isDIGIT(*type)) {
288 for (; isSPACE(*type); type++) ;
294 thatio = sv_2io(*svp);
299 for (; isSPACE(*type); type++) ;
300 thatgv = gv_fetchpv(type,FALSE,SVt_PVIO);
301 thatio = GvIO(thatgv);
305 SETERRNO(EINVAL,SS$_IVCHAN);
310 PerlIO *fp = IoIFP(thatio);
311 /* Flush stdio buffer before dup. --mjd
312 * Unfortunately SEEK_CURing 0 seems to
313 * be optimized away on most platforms;
314 * only Solaris and Linux seem to flush
317 /* sfio fails to clear error on next
318 sfwrite, contrary to documentation.
320 if (PerlIO_seek(fp, 0, SEEK_CUR) == -1)
323 /* On the other hand, do all platforms
324 * take gracefully to flushing a read-only
325 * filehandle? Perhaps we should do
326 * fsetpos(src)+fgetpos(dst)? --nik */
328 fd = PerlIO_fileno(fp);
329 /* When dup()ing STDIN, STDOUT or STDERR
330 * explicitly set appropriate access mode */
331 if (IoIFP(thatio) == PerlIO_stdout()
332 || IoIFP(thatio) == PerlIO_stderr())
333 IoTYPE(io) = IoTYPE_WRONLY;
334 else if (IoIFP(thatio) == PerlIO_stdin())
335 IoTYPE(io) = IoTYPE_RDONLY;
336 /* When dup()ing a socket, say result is
338 else if (IoTYPE(thatio) == IoTYPE_SOCKET)
339 IoTYPE(io) = IoTYPE_SOCKET;
345 fd = PerlLIO_dup(fd);
350 if (!(fp = PerlIO_openn(aTHX_ type,mode,fd,0,0,NULL,num_svs,svp))) {
358 Perl_croak(aTHX_ "More than one argument to '>' open");
361 for (; isSPACE(*type); type++) ;
362 if (*type == IoTYPE_STD && (!type[1] || isSPACE(type[1]) || type[1] == ':')) {
365 fp = PerlIO_stdout();
366 IoTYPE(io) = IoTYPE_STD;
370 namesv = sv_2mortal(newSVpvn(type,strlen(type)));
375 fp = PerlIO_openn(aTHX_ type,mode,-1,0,0,NULL,num_svs,svp);
379 else if (*type == IoTYPE_RDONLY) {
381 Perl_croak(aTHX_ "More than one argument to '<' open");
384 for (type++; isSPACE(*type); type++) ;
394 if (*type == IoTYPE_STD && (!type[1] || isSPACE(type[1]) || type[1] == ':')) {
398 IoTYPE(io) = IoTYPE_STD;
402 namesv = sv_2mortal(newSVpvn(type,strlen(type)));
407 fp = PerlIO_openn(aTHX_ type,mode,-1,0,0,NULL,num_svs,svp);
410 else if ((num_svs && type[0] == IoTYPE_STD && type[1] == IoTYPE_PIPE) ||
411 (!num_svs && tend > type+1 && tend[-1] == IoTYPE_PIPE)) {
413 type += 2; /* skip over '-|' */
417 while (tend > type && isSPACE(tend[-1]))
420 for (; isSPACE(*type); type++) ;
425 /* command is missing 19990114 */
426 if (ckWARN(WARN_PIPE))
427 Perl_warner(aTHX_ WARN_PIPE, "Missing command in piped open");
431 if (strNE(name,"-") || num_svs)
433 TAINT_PROPER("piped open");
440 fp = PerlProc_popen_list(mode,num_svs,svp);
443 fp = PerlProc_popen(name,mode);
445 IoTYPE(io) = IoTYPE_PIPE;
451 IoTYPE(io) = IoTYPE_RDONLY;
453 for (; isSPACE(*name); name++) ;
459 if (strEQ(name,"-")) {
461 IoTYPE(io) = IoTYPE_STD;
465 namesv = sv_2mortal(newSVpvn(type,strlen(type)));
470 fp = PerlIO_openn(aTHX_ type,mode,-1,0,0,NULL,num_svs,svp);
475 if (ckWARN(WARN_NEWLINE) && IoTYPE(io) == IoTYPE_RDONLY && strchr(name, '\n'))
476 Perl_warner(aTHX_ WARN_NEWLINE, PL_warn_nl, "open");
480 if (ckWARN(WARN_IO)) {
481 if ((IoTYPE(io) == IoTYPE_RDONLY) &&
482 (fp == PerlIO_stdout() || fp == PerlIO_stderr())) {
483 Perl_warner(aTHX_ WARN_IO,
484 "Filehandle STD%s opened only for input",
485 (fp == PerlIO_stdout()) ? "OUT" : "ERR");
487 else if ((IoTYPE(io) == IoTYPE_WRONLY) && fp == PerlIO_stdin()) {
488 Perl_warner(aTHX_ WARN_IO,
489 "Filehandle STDIN opened only for output");
493 if (IoTYPE(io) && IoTYPE(io) != IoTYPE_PIPE && IoTYPE(io) != IoTYPE_STD &&
494 /* FIXME: This next term is a hack to avoid fileno on PerlIO::Scalar */
495 !(num_svs && SvROK(*svp))) {
496 if (PerlLIO_fstat(PerlIO_fileno(fp),&PL_statbuf) < 0) {
497 (void)PerlIO_close(fp);
501 if (S_ISSOCK(PL_statbuf.st_mode))
502 IoTYPE(io) = IoTYPE_SOCKET; /* in case a socket was passed in to us */
506 !(PL_statbuf.st_mode & S_IFMT)
510 && IoTYPE(io) != IoTYPE_WRONLY /* Dups of STD* filehandles already have */
511 && IoTYPE(io) != IoTYPE_RDONLY /* type so they aren't marked as sockets */
512 ) { /* on OS's that return 0 on fstat()ed pipe */
514 Sock_size_t buflen = sizeof tmpbuf;
515 if (PerlSock_getsockname(PerlIO_fileno(fp), (struct sockaddr *)tmpbuf,
517 || errno != ENOTSOCK)
518 IoTYPE(io) = IoTYPE_SOCKET; /* some OS's return 0 on fstat()ed socket */
519 /* but some return 0 for streams too, sigh */
521 #endif /* !PERL_MICRO */
524 if (saveifp) { /* must use old fp? */
525 /* If fd is less that PL_maxsysfd i.e. STDIN..STDERR
526 then dup the new fileno down
528 fd = PerlIO_fileno(fp);
530 PerlIO_flush(saveofp); /* emulate PerlIO_close() */
531 if (saveofp != saveifp) { /* was a socket? */
532 PerlIO_close(saveofp);
538 if (PerlLIO_dup2(fd, savefd) < 0) {
539 (void)PerlIO_close(fp);
543 if (savefd != PerlIO_fileno(PerlIO_stdin())) {
544 char newname[FILENAME_MAX+1];
545 if (PerlIO_getname(fp, newname)) {
546 if (fd == PerlIO_fileno(PerlIO_stdout())) Perl_vmssetuserlnm(aTHX_ "SYS$OUTPUT", newname);
547 if (fd == PerlIO_fileno(PerlIO_stderr())) Perl_vmssetuserlnm(aTHX_ "SYS$ERROR", newname);
552 sv = *av_fetch(PL_fdpid,fd,TRUE);
553 (void)SvUPGRADE(sv, SVt_IV);
556 sv = *av_fetch(PL_fdpid,savefd,TRUE);
558 (void)SvUPGRADE(sv, SVt_IV);
566 #if defined(HAS_FCNTL) && defined(F_SETFD)
568 int save_errno = errno;
569 fd = PerlIO_fileno(fp);
570 fcntl(fd,F_SETFD,fd > PL_maxsysfd); /* can change errno */
576 IoFLAGS(io) &= ~IOf_NOLINE;
578 if (IoTYPE(io) == IoTYPE_SOCKET
579 || (IoTYPE(io) == IoTYPE_WRONLY && S_ISCHR(PL_statbuf.st_mode)) ) {
581 if (!(IoOFP(io) = PerlIO_openn(aTHX_ type,mode,PerlIO_fileno(fp),0,0,NULL,num_svs,svp))) {
595 IoTYPE(io) = savetype;
600 Perl_nextargv(pTHX_ register GV *gv)
603 #ifndef FLEXFILENAMES
612 PL_argvoutgv = gv_fetchpv("ARGVOUT",TRUE,SVt_PVIO);
613 if (io && (IoFLAGS(io) & IOf_ARGV) && (IoFLAGS(io) & IOf_START)) {
614 IoFLAGS(io) &= ~IOf_START;
616 if (!PL_argvout_stack)
617 PL_argvout_stack = newAV();
618 av_push(PL_argvout_stack, SvREFCNT_inc(PL_defoutgv));
621 if (PL_filemode & (S_ISUID|S_ISGID)) {
622 PerlIO_flush(IoIFP(GvIOn(PL_argvoutgv))); /* chmod must follow last write */
624 (void)fchmod(PL_lastfd,PL_filemode);
626 (void)PerlLIO_chmod(PL_oldname,PL_filemode);
630 while (av_len(GvAV(gv)) >= 0) {
632 sv = av_shift(GvAV(gv));
634 sv_setsv(GvSV(gv),sv);
635 SvSETMAGIC(GvSV(gv));
636 PL_oldname = SvPVx(GvSV(gv), oldlen);
637 if (do_open(gv,PL_oldname,oldlen,PL_inplace!=0,O_RDONLY,0,Nullfp)) {
639 TAINT_PROPER("inplace open");
640 if (oldlen == 1 && *PL_oldname == '-') {
641 setdefout(gv_fetchpv("STDOUT",TRUE,SVt_PVIO));
642 return IoIFP(GvIOp(gv));
644 #ifndef FLEXFILENAMES
645 filedev = PL_statbuf.st_dev;
646 fileino = PL_statbuf.st_ino;
648 PL_filemode = PL_statbuf.st_mode;
649 fileuid = PL_statbuf.st_uid;
650 filegid = PL_statbuf.st_gid;
651 if (!S_ISREG(PL_filemode)) {
652 if (ckWARN_d(WARN_INPLACE))
653 Perl_warner(aTHX_ WARN_INPLACE,
654 "Can't do inplace edit: %s is not a regular file",
660 char *star = strchr(PL_inplace, '*');
662 char *begin = PL_inplace;
663 sv_setpvn(sv, "", 0);
665 sv_catpvn(sv, begin, star - begin);
666 sv_catpvn(sv, PL_oldname, oldlen);
668 } while ((star = strchr(begin, '*')));
673 sv_catpv(sv,PL_inplace);
675 #ifndef FLEXFILENAMES
676 if (PerlLIO_stat(SvPVX(sv),&PL_statbuf) >= 0
677 && PL_statbuf.st_dev == filedev
678 && PL_statbuf.st_ino == fileino
680 || (_djstat_fail_bits & _STFAIL_TRUENAME)!=0
684 if (ckWARN_d(WARN_INPLACE))
685 Perl_warner(aTHX_ WARN_INPLACE,
686 "Can't do inplace edit: %s would not be unique",
693 #if !defined(DOSISH) && !defined(__CYGWIN__)
694 if (PerlLIO_rename(PL_oldname,SvPVX(sv)) < 0) {
695 if (ckWARN_d(WARN_INPLACE))
696 Perl_warner(aTHX_ WARN_INPLACE,
697 "Can't rename %s to %s: %s, skipping file",
698 PL_oldname, SvPVX(sv), Strerror(errno) );
704 (void)PerlLIO_unlink(SvPVX(sv));
705 (void)PerlLIO_rename(PL_oldname,SvPVX(sv));
706 do_open(gv,SvPVX(sv),SvCUR(sv),PL_inplace!=0,O_RDONLY,0,Nullfp);
709 (void)UNLINK(SvPVX(sv));
710 if (link(PL_oldname,SvPVX(sv)) < 0) {
711 if (ckWARN_d(WARN_INPLACE))
712 Perl_warner(aTHX_ WARN_INPLACE,
713 "Can't rename %s to %s: %s, skipping file",
714 PL_oldname, SvPVX(sv), Strerror(errno) );
718 (void)UNLINK(PL_oldname);
722 #if !defined(DOSISH) && !defined(AMIGAOS)
723 # ifndef VMS /* Don't delete; use automatic file versioning */
724 if (UNLINK(PL_oldname) < 0) {
725 if (ckWARN_d(WARN_INPLACE))
726 Perl_warner(aTHX_ WARN_INPLACE,
727 "Can't remove %s: %s, skipping file",
728 PL_oldname, Strerror(errno) );
734 Perl_croak(aTHX_ "Can't do inplace edit without backup");
738 sv_setpvn(sv,">",!PL_inplace);
739 sv_catpvn(sv,PL_oldname,oldlen);
740 SETERRNO(0,0); /* in case sprintf set errno */
742 if (!do_open(PL_argvoutgv,SvPVX(sv),SvCUR(sv),PL_inplace!=0,
743 O_WRONLY|O_CREAT|O_TRUNC,0,Nullfp))
745 if (!do_open(PL_argvoutgv,SvPVX(sv),SvCUR(sv),PL_inplace!=0,
746 O_WRONLY|O_CREAT|OPEN_EXCL,0666,Nullfp))
749 if (ckWARN_d(WARN_INPLACE))
750 Perl_warner(aTHX_ WARN_INPLACE, "Can't do inplace edit on %s: %s",
751 PL_oldname, Strerror(errno) );
755 setdefout(PL_argvoutgv);
756 PL_lastfd = PerlIO_fileno(IoIFP(GvIOp(PL_argvoutgv)));
757 (void)PerlLIO_fstat(PL_lastfd,&PL_statbuf);
759 (void)fchmod(PL_lastfd,PL_filemode);
761 # if !(defined(WIN32) && defined(__BORLANDC__))
762 /* Borland runtime creates a readonly file! */
763 (void)PerlLIO_chmod(PL_oldname,PL_filemode);
766 if (fileuid != PL_statbuf.st_uid || filegid != PL_statbuf.st_gid) {
768 (void)fchown(PL_lastfd,fileuid,filegid);
771 (void)PerlLIO_chown(PL_oldname,fileuid,filegid);
776 return IoIFP(GvIOp(gv));
779 if (ckWARN_d(WARN_INPLACE)) {
781 if (PerlLIO_stat(PL_oldname, &PL_statbuf) >= 0
782 && !S_ISREG(PL_statbuf.st_mode))
784 Perl_warner(aTHX_ WARN_INPLACE,
785 "Can't do inplace edit: %s is not a regular file",
789 Perl_warner(aTHX_ WARN_INPLACE, "Can't open %s: %s",
790 PL_oldname, Strerror(eno));
794 if (io && (IoFLAGS(io) & IOf_ARGV))
795 IoFLAGS(io) |= IOf_START;
797 (void)do_close(PL_argvoutgv,FALSE);
798 if (io && (IoFLAGS(io) & IOf_ARGV)
799 && PL_argvout_stack && AvFILLp(PL_argvout_stack) >= 0)
801 GV *oldout = (GV*)av_pop(PL_argvout_stack);
803 SvREFCNT_dec(oldout);
806 setdefout(gv_fetchpv("STDOUT",TRUE,SVt_PVIO));
813 Perl_do_pipe(pTHX_ SV *sv, GV *rgv, GV *wgv)
832 if (PerlProc_pipe(fd) < 0)
834 IoIFP(rstio) = PerlIO_fdopen(fd[0], "r");
835 IoOFP(wstio) = PerlIO_fdopen(fd[1], "w");
836 IoIFP(wstio) = IoOFP(wstio);
837 IoTYPE(rstio) = IoTYPE_RDONLY;
838 IoTYPE(wstio) = IoTYPE_WRONLY;
839 if (!IoIFP(rstio) || !IoOFP(wstio)) {
840 if (IoIFP(rstio)) PerlIO_close(IoIFP(rstio));
841 else PerlLIO_close(fd[0]);
842 if (IoOFP(wstio)) PerlIO_close(IoOFP(wstio));
843 else PerlLIO_close(fd[1]);
847 sv_setsv(sv,&PL_sv_yes);
851 sv_setsv(sv,&PL_sv_undef);
856 /* explicit renamed to avoid C++ conflict -- kja */
858 Perl_do_close(pTHX_ GV *gv, bool not_implicit)
865 if (!gv || SvTYPE(gv) != SVt_PVGV) {
867 SETERRNO(EBADF,SS$_IVCHAN);
871 if (!io) { /* never opened */
873 if (ckWARN(WARN_UNOPENED)) /* no check for closed here */
874 report_evil_fh(gv, io, PL_op->op_type);
875 SETERRNO(EBADF,SS$_IVCHAN);
879 retval = io_close(io, not_implicit);
883 IoLINES_LEFT(io) = IoPAGE_LEN(io);
885 IoTYPE(io) = IoTYPE_CLOSED;
890 Perl_io_close(pTHX_ IO *io, bool not_implicit)
896 if (IoTYPE(io) == IoTYPE_PIPE) {
897 status = PerlProc_pclose(IoIFP(io));
899 STATUS_NATIVE_SET(status);
900 retval = (STATUS_POSIX == 0);
903 retval = (status != -1);
906 else if (IoTYPE(io) == IoTYPE_STD)
909 if (IoOFP(io) && IoOFP(io) != IoIFP(io)) { /* a socket */
910 retval = (PerlIO_close(IoOFP(io)) != EOF);
911 PerlIO_close(IoIFP(io)); /* clear stdio, fd already closed */
914 retval = (PerlIO_close(IoIFP(io)) != EOF);
916 IoOFP(io) = IoIFP(io) = Nullfp;
918 else if (not_implicit) {
919 SETERRNO(EBADF,SS$_IVCHAN);
926 Perl_do_eof(pTHX_ GV *gv)
935 else if (ckWARN(WARN_IO) && (IoTYPE(io) == IoTYPE_WRONLY))
937 /* integrate to report_evil_fh()? */
940 SV* sv = sv_newmortal();
941 gv_efullname4(sv, gv, Nullch, FALSE);
942 name = SvPV_nolen(sv);
945 Perl_warner(aTHX_ WARN_IO,
946 "Filehandle %s opened only for output", name);
948 Perl_warner(aTHX_ WARN_IO,
949 "Filehandle opened only for output");
954 if (PerlIO_has_cntptr(IoIFP(io))) { /* (the code works without this) */
955 if (PerlIO_get_cnt(IoIFP(io)) > 0) /* cheat a little, since */
956 return FALSE; /* this is the most usual case */
959 ch = PerlIO_getc(IoIFP(io));
961 (void)PerlIO_ungetc(IoIFP(io),ch);
965 if (PerlIO_has_cntptr(IoIFP(io)) && PerlIO_canset_cnt(IoIFP(io))) {
966 if (PerlIO_get_cnt(IoIFP(io)) < -1)
967 PerlIO_set_cnt(IoIFP(io),-1);
969 if (PL_op->op_flags & OPf_SPECIAL) { /* not necessarily a real EOF yet? */
970 if (!nextargv(PL_argvgv)) /* get another fp handy */
974 return TRUE; /* normal fp, definitely end of file */
980 Perl_do_tell(pTHX_ GV *gv)
985 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io))) {
986 #ifdef ULTRIX_STDIO_BOTCH
988 (void)PerlIO_seek(fp, 0L, 2); /* ultrix 1.2 workaround */
990 return PerlIO_tell(fp);
992 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
993 report_evil_fh(gv, io, PL_op->op_type);
994 SETERRNO(EBADF,RMS$_IFI);
999 Perl_do_seek(pTHX_ GV *gv, Off_t pos, int whence)
1001 register IO *io = 0;
1002 register PerlIO *fp;
1004 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io))) {
1005 #ifdef ULTRIX_STDIO_BOTCH
1007 (void)PerlIO_seek(fp, 0L, 2); /* ultrix 1.2 workaround */
1009 return PerlIO_seek(fp, pos, whence) >= 0;
1011 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1012 report_evil_fh(gv, io, PL_op->op_type);
1013 SETERRNO(EBADF,RMS$_IFI);
1018 Perl_do_sysseek(pTHX_ GV *gv, Off_t pos, int whence)
1020 register IO *io = 0;
1021 register PerlIO *fp;
1023 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io)))
1024 return PerlLIO_lseek(PerlIO_fileno(fp), pos, whence);
1025 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1026 report_evil_fh(gv, io, PL_op->op_type);
1027 SETERRNO(EBADF,RMS$_IFI);
1032 Perl_mode_from_discipline(pTHX_ SV *discp)
1034 int mode = O_BINARY;
1037 char *s = SvPV(discp,len);
1042 if (len > 3 && strnEQ(s+1, "raw", 3)
1043 && (!s[4] || s[4] == ':' || isSPACE(s[4])))
1052 if (len > 4 && strnEQ(s+1, "crlf", 4)
1053 && (!s[5] || s[5] == ':' || isSPACE(s[5])))
1062 goto fail_discipline;
1065 else if (isSPACE(*s)) {
1072 end = strchr(s+1, ':');
1075 #ifndef PERLIO_LAYERS
1076 Perl_croak(aTHX_ "Unknown discipline '%.*s'", end-s, s);
1087 Perl_do_binmode(pTHX_ PerlIO *fp, int iotype, int mode)
1089 /* The old body of this is now in non-LAYER part of perlio.c
1090 * This is a stub for any XS code which might have been calling it.
1092 char *name = (O_BINARY != O_TEXT && !(mode & O_BINARY)) ? ":crlf" : ":raw";
1093 return PerlIO_binmode(aTHX_ fp, iotype, mode, name);
1096 #if !defined(HAS_TRUNCATE) && !defined(HAS_CHSIZE) && defined(F_FREESP)
1097 /* code courtesy of William Kucharski */
1100 I32 my_chsize(fd, length)
1101 I32 fd; /* file descriptor */
1102 Off_t length; /* length to set file to */
1105 struct stat filebuf;
1107 if (PerlLIO_fstat(fd, &filebuf) < 0)
1110 if (filebuf.st_size < length) {
1112 /* extend file length */
1114 if ((PerlLIO_lseek(fd, (length - 1), 0)) < 0)
1117 /* write a "0" byte */
1119 if ((PerlLIO_write(fd, "", 1)) != 1)
1123 /* truncate length */
1127 fl.l_start = length;
1128 fl.l_type = F_WRLCK; /* write lock on file space */
1131 * This relies on the UNDOCUMENTED F_FREESP argument to
1132 * fcntl(2), which truncates the file so that it ends at the
1133 * position indicated by fl.l_start.
1135 * Will minor miracles never cease?
1138 if (fcntl(fd, F_FREESP, &fl) < 0)
1145 #endif /* F_FREESP */
1148 Perl_do_print(pTHX_ register SV *sv, PerlIO *fp)
1150 register char *tmps;
1153 /* assuming fp is checked earlier */
1159 if (SvIOK(sv) && SvIVX(sv) != 0) {
1160 PerlIO_printf(fp, PL_ofmt, (NV)SvIVX(sv));
1161 return !PerlIO_error(fp);
1163 if ( (SvNOK(sv) && SvNVX(sv) != 0.0)
1164 || (looks_like_number(sv) && sv_2nv(sv) != 0.0) ) {
1165 PerlIO_printf(fp, PL_ofmt, SvNVX(sv));
1166 return !PerlIO_error(fp);
1169 switch (SvTYPE(sv)) {
1171 if (ckWARN(WARN_UNINITIALIZED))
1179 PerlIO_printf(fp, "%"UVuf, (UV)SvUVX(sv));
1181 PerlIO_printf(fp, "%"IVdf, (IV)SvIVX(sv));
1182 return !PerlIO_error(fp);
1186 if (PerlIO_isutf8(fp)) {
1188 sv_utf8_upgrade(sv = sv_mortalcopy(sv));
1190 else if (DO_UTF8(sv)) {
1191 if (!sv_utf8_downgrade((sv = sv_mortalcopy(sv)), TRUE)
1192 && ckWARN(WARN_UTF8))
1194 Perl_warner(aTHX_ WARN_UTF8, "Wide character in print");
1197 tmps = SvPV(sv, len);
1200 /* To detect whether the process is about to overstep its
1201 * filesize limit we would need getrlimit(). We could then
1202 * also transparently raise the limit with setrlimit() --
1203 * but only until the system hard limit/the filesystem limit,
1204 * at which we would get EPERM. Note that when using buffered
1205 * io the write failure can be delayed until the flush/close. --jhi */
1206 if (len && (PerlIO_write(fp,tmps,len) == 0))
1208 return !PerlIO_error(fp);
1218 if (PL_op->op_flags & OPf_REF) {
1223 if (io && IoIFP(io)) {
1225 sv_setpv(PL_statname,"");
1226 PL_laststype = OP_STAT;
1227 return (PL_laststatval = PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache));
1231 return PL_laststatval;
1232 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1233 report_evil_fh(gv, io, PL_op->op_type);
1235 sv_setpv(PL_statname,"");
1236 return (PL_laststatval = -1);
1244 if (SvTYPE(sv) == SVt_PVGV) {
1248 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
1255 sv_setpv(PL_statname, s);
1256 PL_laststype = OP_STAT;
1257 PL_laststatval = PerlLIO_stat(s, &PL_statcache);
1258 if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(s, '\n'))
1259 Perl_warner(aTHX_ WARN_NEWLINE, PL_warn_nl, "stat");
1260 return PL_laststatval;
1270 if (PL_op->op_flags & OPf_REF) {
1272 if (cGVOP_gv == PL_defgv) {
1273 if (PL_laststype != OP_LSTAT)
1274 Perl_croak(aTHX_ "The stat preceding -l _ wasn't an lstat");
1275 return PL_laststatval;
1277 Perl_croak(aTHX_ "You can't use -l on a filehandle");
1280 PL_laststype = OP_LSTAT;
1284 sv_setpv(PL_statname,SvPV(sv, n_a));
1285 PL_laststatval = PerlLIO_lstat(SvPV(sv, n_a),&PL_statcache);
1286 if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(SvPV(sv, n_a), '\n'))
1287 Perl_warner(aTHX_ WARN_NEWLINE, PL_warn_nl, "lstat");
1288 return PL_laststatval;
1292 Perl_do_aexec(pTHX_ SV *really, register SV **mark, register SV **sp)
1294 return do_aexec5(really, mark, sp, 0, 0);
1298 Perl_do_aexec5(pTHX_ SV *really, register SV **mark, register SV **sp,
1299 int fd, int do_report)
1301 #ifdef MACOS_TRADITIONAL
1302 Perl_croak(aTHX_ "exec? I'm not *that* kind of operating system");
1305 char *tmps = Nullch;
1309 New(401,PL_Argv, sp - mark + 1, char*);
1311 while (++mark <= sp) {
1313 *a++ = SvPVx(*mark, n_a);
1319 tmps = SvPV(really, n_a);
1320 if ((!really && *PL_Argv[0] != '/') ||
1321 (really && *tmps != '/')) /* will execvp use PATH? */
1322 TAINT_ENV(); /* testing IFS here is overkill, probably */
1323 if (really && *tmps)
1324 PerlProc_execvp(tmps,EXEC_ARGV_CAST(PL_Argv));
1326 PerlProc_execvp(PL_Argv[0],EXEC_ARGV_CAST(PL_Argv));
1327 if (ckWARN(WARN_EXEC))
1328 Perl_warner(aTHX_ WARN_EXEC, "Can't exec \"%s\": %s",
1329 (really ? tmps : PL_Argv[0]), Strerror(errno));
1333 PerlLIO_write(fd, (void*)&e, sizeof(int));
1343 Perl_do_execfree(pTHX)
1347 PL_Argv = Null(char **);
1355 #if !defined(OS2) && !defined(WIN32) && !defined(DJGPP) && !defined(EPOC) && !defined(MACOS_TRADITIONAL)
1358 Perl_do_exec(pTHX_ char *cmd)
1360 return do_exec3(cmd,0,0);
1364 Perl_do_exec3(pTHX_ char *cmd, int fd, int do_report)
1369 while (*cmd && isSPACE(*cmd))
1372 /* save an extra exec if possible */
1377 if (strnEQ(cmd,PL_cshname,PL_cshlen) &&
1378 strnEQ(cmd+PL_cshlen," -c",3)) {
1380 s = cmd+PL_cshlen+3;
1394 if (s[-1] == '\'') {
1396 PerlProc_execl(PL_cshname,"csh", flags, ncmd, (char*)0);
1405 /* see if there are shell metacharacters in it */
1407 if (*cmd == '.' && isSPACE(cmd[1]))
1410 if (strnEQ(cmd,"exec",4) && isSPACE(cmd[4]))
1413 for (s = cmd; *s && isALNUM(*s); s++) ; /* catch VAR=val gizmo */
1417 for (s = cmd; *s; s++) {
1418 if (*s != ' ' && !isALPHA(*s) &&
1419 strchr("$&*(){}[]'\";\\|?<>~`\n",*s)) {
1420 if (*s == '\n' && !s[1]) {
1424 /* handle the 2>&1 construct at the end */
1425 if (*s == '>' && s[1] == '&' && s[2] == '1'
1426 && s > cmd + 1 && s[-1] == '2' && isSPACE(s[-2])
1427 && (!s[3] || isSPACE(s[3])))
1431 while (*t && isSPACE(*t))
1433 if (!*t && (dup2(1,2) != -1)) {
1439 PerlProc_execl(PL_sh_path, "sh", "-c", cmd, (char*)0);
1444 New(402,PL_Argv, (s - cmd) / 2 + 2, char*);
1445 PL_Cmd = savepvn(cmd, s-cmd);
1447 for (s = PL_Cmd; *s;) {
1448 while (*s && isSPACE(*s)) s++;
1451 while (*s && !isSPACE(*s)) s++;
1457 PerlProc_execvp(PL_Argv[0],PL_Argv);
1458 if (errno == ENOEXEC) { /* for system V NIH syndrome */
1465 if (ckWARN(WARN_EXEC))
1466 Perl_warner(aTHX_ WARN_EXEC, "Can't exec \"%s\": %s",
1467 PL_Argv[0], Strerror(errno));
1469 PerlLIO_write(fd, (void*)&e, sizeof(int));
1478 #endif /* OS2 || WIN32 */
1481 Perl_apply(pTHX_ I32 type, register SV **mark, register SV **sp)
1485 register I32 tot = 0;
1488 SV **oldmark = mark;
1491 #define APPLY_TAINT_PROPER() \
1493 if (PL_tainted) { TAINT_PROPER(what); } \
1496 /* This is a first heuristic; it doesn't catch tainting magic. */
1498 while (++mark <= sp) {
1499 if (SvTAINTED(*mark)) {
1509 APPLY_TAINT_PROPER();
1512 APPLY_TAINT_PROPER();
1514 while (++mark <= sp) {
1515 char *name = SvPVx(*mark, n_a);
1516 APPLY_TAINT_PROPER();
1517 if (PerlLIO_chmod(name, val))
1525 APPLY_TAINT_PROPER();
1526 if (sp - mark > 2) {
1527 val = SvIVx(*++mark);
1528 val2 = SvIVx(*++mark);
1529 APPLY_TAINT_PROPER();
1531 while (++mark <= sp) {
1532 char *name = SvPVx(*mark, n_a);
1533 APPLY_TAINT_PROPER();
1534 if (PerlLIO_chown(name, val, val2))
1541 XXX Should we make lchown() directly available from perl?
1542 For now, we'll let Configure test for HAS_LCHOWN, but do
1543 nothing in the core.
1549 APPLY_TAINT_PROPER();
1552 s = SvPVx(*++mark, n_a);
1554 if (*s == 'S' && s[1] == 'I' && s[2] == 'G')
1556 if (!(val = whichsig(s)))
1557 Perl_croak(aTHX_ "Unrecognized signal name \"%s\"",s);
1561 APPLY_TAINT_PROPER();
1564 /* kill() doesn't do process groups (job trees?) under VMS */
1565 if (val < 0) val = -val;
1566 if (val == SIGKILL) {
1567 # include <starlet.h>
1568 /* Use native sys$delprc() to insure that target process is
1569 * deleted; supervisor-mode images don't pay attention to
1570 * CRTL's emulation of Unix-style signals and kill()
1572 while (++mark <= sp) {
1573 I32 proc = SvIVx(*mark);
1574 register unsigned long int __vmssts;
1575 APPLY_TAINT_PROPER();
1576 if (!((__vmssts = sys$delprc(&proc,0)) & 1)) {
1580 case SS$_NOSUCHNODE:
1581 SETERRNO(ESRCH,__vmssts);
1584 SETERRNO(EPERM,__vmssts);
1587 SETERRNO(EVMSERR,__vmssts);
1596 while (++mark <= sp) {
1597 I32 proc = SvIVx(*mark);
1598 APPLY_TAINT_PROPER();
1600 if (PerlProc_killpg(proc,val)) /* BSD */
1602 if (PerlProc_kill(-proc,val)) /* SYSV */
1608 while (++mark <= sp) {
1609 I32 proc = SvIVx(*mark);
1610 APPLY_TAINT_PROPER();
1611 if (PerlProc_kill(proc, val))
1619 APPLY_TAINT_PROPER();
1621 while (++mark <= sp) {
1622 s = SvPVx(*mark, n_a);
1623 APPLY_TAINT_PROPER();
1624 if (PL_euid || PL_unsafe) {
1628 else { /* don't let root wipe out directories without -U */
1629 if (PerlLIO_lstat(s,&PL_statbuf) < 0 || S_ISDIR(PL_statbuf.st_mode))
1641 APPLY_TAINT_PROPER();
1642 if (sp - mark > 2) {
1643 #if defined(I_UTIME) || defined(VMS)
1644 struct utimbuf utbuf;
1652 SV* accessed = *++mark;
1653 SV* modified = *++mark;
1654 void * utbufp = &utbuf;
1656 /* be like C, and if both times are undefined, let the C
1657 library figure out what to do. This usually means
1660 if ( accessed == &PL_sv_undef && modified == &PL_sv_undef )
1663 Zero(&utbuf, sizeof utbuf, char);
1665 utbuf.actime = (Time_t)SvNVx(accessed); /* time accessed */
1666 utbuf.modtime = (Time_t)SvNVx(modified); /* time modified */
1668 utbuf.actime = (Time_t)SvIVx(accessed); /* time accessed */
1669 utbuf.modtime = (Time_t)SvIVx(modified); /* time modified */
1671 APPLY_TAINT_PROPER();
1673 while (++mark <= sp) {
1674 char *name = SvPVx(*mark, n_a);
1675 APPLY_TAINT_PROPER();
1676 if (PerlLIO_utime(name, utbufp))
1687 #undef APPLY_TAINT_PROPER
1690 /* Do the permissions allow some operation? Assumes statcache already set. */
1691 #ifndef VMS /* VMS' cando is in vms.c */
1693 Perl_cando(pTHX_ Mode_t mode, Uid_t effective, register Stat_t *statbufp)
1694 /* Note: we use `effective' both for uids and gids.
1695 * Here we are betting on Uid_t being equal or wider than Gid_t. */
1698 /* [Comments and code from Len Reed]
1699 * MS-DOS "user" is similar to UNIX's "superuser," but can't write
1700 * to write-protected files. The execute permission bit is set
1701 * by the Miscrosoft C library stat() function for the following:
1706 * All files and directories are readable.
1707 * Directories and special files, e.g. "CON", cannot be
1709 * [Comment by Tom Dinger -- a directory can have the write-protect
1710 * bit set in the file system, but DOS permits changes to
1711 * the directory anyway. In addition, all bets are off
1712 * here for networked software, such as Novell and
1716 /* Atari stat() does pretty much the same thing. we set x_bit_set_in_stat
1717 * too so it will actually look into the files for magic numbers
1719 return (mode & statbufp->st_mode) ? TRUE : FALSE;
1721 #else /* ! DOSISH */
1722 if ((effective ? PL_euid : PL_uid) == 0) { /* root is special */
1723 if (mode == S_IXUSR) {
1724 if (statbufp->st_mode & 0111 || S_ISDIR(statbufp->st_mode))
1728 return TRUE; /* root reads and writes anything */
1731 if (statbufp->st_uid == (effective ? PL_euid : PL_uid) ) {
1732 if (statbufp->st_mode & mode)
1733 return TRUE; /* ok as "user" */
1735 else if (ingroup(statbufp->st_gid,effective)) {
1736 if (statbufp->st_mode & mode >> 3)
1737 return TRUE; /* ok as "group" */
1739 else if (statbufp->st_mode & mode >> 6)
1740 return TRUE; /* ok as "other" */
1742 #endif /* ! DOSISH */
1747 Perl_ingroup(pTHX_ Gid_t testgid, Uid_t effective)
1749 #ifdef MACOS_TRADITIONAL
1750 /* This is simply not correct for AppleShare, but fix it yerself. */
1753 if (testgid == (effective ? PL_egid : PL_gid))
1755 #ifdef HAS_GETGROUPS
1760 Groups_t gary[NGROUPS];
1763 anum = getgroups(NGROUPS,gary);
1765 if (gary[anum] == testgid)
1773 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
1776 Perl_do_ipcget(pTHX_ I32 optype, SV **mark, SV **sp)
1781 key = (key_t)SvNVx(*++mark);
1782 n = (optype == OP_MSGGET) ? 0 : SvIVx(*++mark);
1783 flags = SvIVx(*++mark);
1789 return msgget(key, flags);
1793 return semget(key, n, flags);
1797 return shmget(key, n, flags);
1799 #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
1801 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
1804 return -1; /* should never happen */
1808 Perl_do_ipcctl(pTHX_ I32 optype, SV **mark, SV **sp)
1812 I32 id, n, cmd, infosize, getinfo;
1815 id = SvIVx(*++mark);
1816 n = (optype == OP_SEMCTL) ? SvIVx(*++mark) : 0;
1817 cmd = SvIVx(*++mark);
1820 getinfo = (cmd == IPC_STAT);
1826 if (cmd == IPC_STAT || cmd == IPC_SET)
1827 infosize = sizeof(struct msqid_ds);
1832 if (cmd == IPC_STAT || cmd == IPC_SET)
1833 infosize = sizeof(struct shmid_ds);
1839 if (cmd == IPC_STAT || cmd == IPC_SET)
1840 infosize = sizeof(struct semid_ds);
1841 else if (cmd == GETALL || cmd == SETALL)
1843 struct semid_ds semds;
1845 #ifdef EXTRA_F_IN_SEMUN_BUF
1846 semun.buff = &semds;
1850 getinfo = (cmd == GETALL);
1851 if (Semctl(id, 0, IPC_STAT, semun) == -1)
1853 infosize = semds.sem_nsems * sizeof(short);
1854 /* "short" is technically wrong but much more portable
1855 than guessing about u_?short(_t)? */
1858 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
1862 #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
1864 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
1873 SvPV_force(astr, len);
1874 a = SvGROW(astr, infosize+1);
1878 a = SvPV(astr, len);
1879 if (len != infosize)
1880 Perl_croak(aTHX_ "Bad arg length for %s, is %lu, should be %ld",
1889 a = INT2PTR(char *,i); /* ouch */
1896 ret = msgctl(id, cmd, (struct msqid_ds *)a);
1902 union semun unsemds;
1904 #ifdef EXTRA_F_IN_SEMUN_BUF
1905 unsemds.buff = (struct semid_ds *)a;
1907 unsemds.buf = (struct semid_ds *)a;
1909 ret = Semctl(id, n, cmd, unsemds);
1911 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
1918 ret = shmctl(id, cmd, (struct shmid_ds *)a);
1922 if (getinfo && ret >= 0) {
1923 SvCUR_set(astr, infosize);
1924 *SvEND(astr) = '\0';
1931 Perl_do_msgsnd(pTHX_ SV **mark, SV **sp)
1936 I32 id, msize, flags;
1939 id = SvIVx(*++mark);
1941 flags = SvIVx(*++mark);
1942 mbuf = SvPV(mstr, len);
1943 if ((msize = len - sizeof(long)) < 0)
1944 Perl_croak(aTHX_ "Arg too short for msgsnd");
1946 return msgsnd(id, (struct msgbuf *)mbuf, msize, flags);
1948 Perl_croak(aTHX_ "msgsnd not implemented");
1953 Perl_do_msgrcv(pTHX_ SV **mark, SV **sp)
1959 I32 id, msize, flags, ret;
1962 id = SvIVx(*++mark);
1964 /* suppress warning when reading into undef var --jhi */
1966 sv_setpvn(mstr, "", 0);
1967 msize = SvIVx(*++mark);
1968 mtype = (long)SvIVx(*++mark);
1969 flags = SvIVx(*++mark);
1970 SvPV_force(mstr, len);
1971 mbuf = SvGROW(mstr, sizeof(long)+msize+1);
1974 ret = msgrcv(id, (struct msgbuf *)mbuf, msize, mtype, flags);
1976 SvCUR_set(mstr, sizeof(long)+ret);
1977 *SvEND(mstr) = '\0';
1978 #ifndef INCOMPLETE_TAINTS
1979 /* who knows who has been playing with this message? */
1985 Perl_croak(aTHX_ "msgrcv not implemented");
1990 Perl_do_semop(pTHX_ SV **mark, SV **sp)
1998 id = SvIVx(*++mark);
2000 opbuf = SvPV(opstr, opsize);
2001 if (opsize < 3 * SHORTSIZE
2002 || (opsize % (3 * SHORTSIZE))) {
2003 SETERRNO(EINVAL,LIB$_INVARG);
2007 /* We can't assume that sizeof(struct sembuf) == 3 * sizeof(short). */
2009 int nsops = opsize / (3 * sizeof (short));
2011 short *ops = (short *) opbuf;
2013 struct sembuf *temps, *t;
2016 New (0, temps, nsops, struct sembuf);
2024 result = semop(id, temps, nsops);
2038 Perl_croak(aTHX_ "semop not implemented");
2043 Perl_do_shmio(pTHX_ I32 optype, SV **mark, SV **sp)
2048 I32 id, mpos, msize;
2050 struct shmid_ds shmds;
2052 id = SvIVx(*++mark);
2054 mpos = SvIVx(*++mark);
2055 msize = SvIVx(*++mark);
2057 if (shmctl(id, IPC_STAT, &shmds) == -1)
2059 if (mpos < 0 || msize < 0 || mpos + msize > shmds.shm_segsz) {
2060 SETERRNO(EFAULT,SS$_ACCVIO); /* can't do as caller requested */
2063 shm = (char *)shmat(id, (char*)NULL, (optype == OP_SHMREAD) ? SHM_RDONLY : 0);
2064 if (shm == (char *)-1) /* I hate System V IPC, I really do */
2066 if (optype == OP_SHMREAD) {
2067 /* suppress warning when reading into undef var (tchrist 3/Mar/00) */
2069 sv_setpvn(mstr, "", 0);
2070 SvPV_force(mstr, len);
2071 mbuf = SvGROW(mstr, msize+1);
2073 Copy(shm + mpos, mbuf, msize, char);
2074 SvCUR_set(mstr, msize);
2075 *SvEND(mstr) = '\0';
2077 #ifndef INCOMPLETE_TAINTS
2078 /* who knows who has been playing with this shared memory? */
2085 mbuf = SvPV(mstr, len);
2086 if ((n = len) > msize)
2088 Copy(mbuf, shm + mpos, n, char);
2090 memzero(shm + mpos + n, msize - n);
2094 Perl_croak(aTHX_ "shm I/O not implemented");
2098 #endif /* SYSV IPC */
2101 =for apidoc start_glob
2103 Function called by C<do_readline> to spawn a glob (or do the glob inside
2104 perl on VMS). This code used to be inline, but now perl uses C<File::Glob>
2105 this glob starter is only used by miniperl during the build proccess.
2106 Moving it away shrinks pp_hot.c; shrinking pp_hot.c helps speed perl up.
2112 Perl_start_glob (pTHX_ SV *tmpglob, IO *io)
2114 SV *tmpcmd = NEWSV(55, 0);
2118 #ifdef VMS /* expand the wildcards right here, rather than opening a pipe, */
2119 /* since spawning off a process is a real performance hit */
2121 #include <descrip.h>
2122 #include <lib$routines.h>
2125 char rslt[NAM$C_MAXRSS+1+sizeof(unsigned short int)] = {'\0','\0'};
2126 char vmsspec[NAM$C_MAXRSS+1];
2127 char *rstr = rslt + sizeof(unsigned short int), *begin, *end, *cp;
2128 $DESCRIPTOR(dfltdsc,"SYS$DISK:[]*.*;");
2131 struct dsc$descriptor_s wilddsc
2132 = {0, DSC$K_DTYPE_T, DSC$K_CLASS_S, 0};
2133 struct dsc$descriptor_vs rsdsc
2134 = {sizeof rslt, DSC$K_DTYPE_VT, DSC$K_CLASS_VS, rslt};
2135 unsigned long int cxt = 0, sts = 0, ok = 1, hasdir = 0, hasver = 0, isunix = 0;
2137 /* We could find out if there's an explicit dev/dir or version
2138 by peeking into lib$find_file's internal context at
2139 ((struct NAM *)((struct FAB *)cxt)->fab$l_nam)->nam$l_fnb
2140 but that's unsupported, so I don't want to do it now and
2141 have it bite someone in the future. */
2142 cp = SvPV(tmpglob,i);
2144 if (cp[i] == ';') hasver = 1;
2146 if (sts) hasver = 1;
2150 hasdir = isunix = 1;
2153 if (cp[i] == ']' || cp[i] == '>' || cp[i] == ':') {
2158 if ((tmpfp = PerlIO_tmpfile()) != NULL) {
2160 if (!PerlLIO_stat(SvPVX(tmpglob),&st) && S_ISDIR(st.st_mode))
2161 ok = ((wilddsc.dsc$a_pointer = tovmspath(SvPVX(tmpglob),vmsspec)) != NULL);
2162 else ok = ((wilddsc.dsc$a_pointer = tovmsspec(SvPVX(tmpglob),vmsspec)) != NULL);
2163 if (ok) wilddsc.dsc$w_length = (unsigned short int) strlen(wilddsc.dsc$a_pointer);
2164 for (cp=wilddsc.dsc$a_pointer; ok && cp && *cp; cp++)
2165 if (*cp == '?') *cp = '%'; /* VMS style single-char wildcard */
2166 while (ok && ((sts = lib$find_file(&wilddsc,&rsdsc,&cxt,
2167 &dfltdsc,NULL,NULL,NULL))&1)) {
2168 end = rstr + (unsigned long int) *rslt;
2169 if (!hasver) while (*end != ';') end--;
2170 *(end++) = '\n'; *end = '\0';
2171 for (cp = rstr; *cp; cp++) *cp = _tolower(*cp);
2173 if (isunix) trim_unixpath(rstr,SvPVX(tmpglob),1);
2178 while (*(--begin) != ']' && *begin != '>') ;
2181 ok = (PerlIO_puts(tmpfp,begin) != EOF);
2183 if (cxt) (void)lib$find_file_end(&cxt);
2184 if (ok && sts != RMS$_NMF &&
2185 sts != RMS$_DNF && sts != RMS$_FNF) ok = 0;
2188 SETERRNO((sts == RMS$_SYN ? EINVAL : EVMSERR),sts);
2190 PerlIO_close(tmpfp);
2194 PerlIO_rewind(tmpfp);
2195 IoTYPE(io) = IoTYPE_RDONLY;
2196 IoIFP(io) = fp = tmpfp;
2197 IoFLAGS(io) &= ~IOf_UNTAINT; /* maybe redundant */
2202 #ifdef MACOS_TRADITIONAL
2203 sv_setpv(tmpcmd, "glob ");
2204 sv_catsv(tmpcmd, tmpglob);
2205 sv_catpv(tmpcmd, " |");
2209 sv_setpv(tmpcmd, "for a in ");
2210 sv_catsv(tmpcmd, tmpglob);
2211 sv_catpv(tmpcmd, "; do echo \"$a\\0\\c\"; done |");
2214 sv_setpv(tmpcmd, "/dev/dosglob/"); /* File System Extension */
2215 sv_catsv(tmpcmd, tmpglob);
2217 sv_setpv(tmpcmd, "perlglob ");
2218 sv_catsv(tmpcmd, tmpglob);
2219 sv_catpv(tmpcmd, " |");
2224 sv_setpvn(tmpcmd, PL_cshname, PL_cshlen);
2225 sv_catpv(tmpcmd, " -cf 'set nonomatch; glob ");
2226 sv_catsv(tmpcmd, tmpglob);
2227 sv_catpv(tmpcmd, "' 2>/dev/null |");
2229 sv_setpv(tmpcmd, "echo ");
2230 sv_catsv(tmpcmd, tmpglob);
2232 sv_catpv(tmpcmd, "|tr -s ' \t\f\r' '\\012\\012\\012\\012'|");
2234 sv_catpv(tmpcmd, "|tr -s ' \t\f\r' '\\n\\n\\n\\n'|");
2237 #endif /* !DOSISH */
2238 #endif /* MACOS_TRADITIONAL */
2239 (void)do_open(PL_last_in_gv, SvPVX(tmpcmd), SvCUR(tmpcmd),
2240 FALSE, O_RDONLY, 0, Nullfp);