3 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
4 * 2000, 2001, 2002, 2003, by Larry Wall and others
6 * You may distribute under the terms of either the GNU General Public
7 * License or the Artistic License, as specified in the README file.
12 * "Far below them they saw the white waters pour into a foaming bowl, and
13 * then swirl darkly about a deep oval basin in the rocks, until they found
14 * their way out again through a narrow gate, and flowed away, fuming and
15 * chattering, into calmer and more level reaches."
19 #define PERL_IN_DOIO_C
22 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
31 # ifndef HAS_SHMAT_PROTOTYPE
32 extern Shmat_t shmat (int, char *, int);
38 # if defined(_MSC_VER) || defined(__MINGW32__)
39 # include <sys/utime.h>
46 # define OPEN_EXCL O_EXCL
51 #if !defined(NSIG) || defined(M_UNIX) || defined(M_XENIX)
56 Perl_do_open(pTHX_ GV *gv, register char *name, I32 len, int as_raw,
57 int rawmode, int rawperm, PerlIO *supplied_fp)
59 return do_openn(gv, name, len, as_raw, rawmode, rawperm,
60 supplied_fp, (SV **) NULL, 0);
64 Perl_do_open9(pTHX_ GV *gv, register char *name, I32 len, int as_raw,
65 int rawmode, int rawperm, PerlIO *supplied_fp, SV *svs,
68 return do_openn(gv, name, len, as_raw, rawmode, rawperm,
69 supplied_fp, &svs, 1);
73 Perl_do_openn(pTHX_ GV *gv, register char *name, I32 len, int as_raw,
74 int rawmode, int rawperm, PerlIO *supplied_fp, SV **svp,
77 register IO *io = GvIOn(gv);
78 PerlIO *saveifp = Nullfp;
79 PerlIO *saveofp = Nullfp;
81 char savetype = IoTYPE_CLOSED;
86 bool was_fdopen = FALSE;
87 bool in_raw = 0, in_crlf = 0, out_raw = 0, out_crlf = 0;
89 char mode[8]; /* stdio file mode ("r\0", "rb\0", "r+b\0" etc.) */
92 Zero(mode,sizeof(mode),char);
93 PL_forkprocess = 1; /* assume true if no fork */
95 /* Collect default raw/crlf info from the op */
96 if (PL_op && PL_op->op_type == OP_OPEN) {
97 /* set up disciplines */
98 U8 flags = PL_op->op_private;
99 in_raw = (flags & OPpOPEN_IN_RAW);
100 in_crlf = (flags & OPpOPEN_IN_CRLF);
101 out_raw = (flags & OPpOPEN_OUT_RAW);
102 out_crlf = (flags & OPpOPEN_OUT_CRLF);
105 /* If currently open - close before we re-open */
107 fd = PerlIO_fileno(IoIFP(io));
108 if (IoTYPE(io) == IoTYPE_STD) {
109 /* This is a clone of one of STD* handles */
112 else if (fd >= 0 && fd <= PL_maxsysfd) {
113 /* This is one of the original STD* handles */
116 savetype = IoTYPE(io);
120 else if (IoTYPE(io) == IoTYPE_PIPE)
121 result = PerlProc_pclose(IoIFP(io));
122 else if (IoIFP(io) != IoOFP(io)) {
124 result = PerlIO_close(IoOFP(io));
125 PerlIO_close(IoIFP(io)); /* clear stdio, fd already closed */
128 result = PerlIO_close(IoIFP(io));
131 result = PerlIO_close(IoIFP(io));
132 if (result == EOF && fd > PL_maxsysfd) {
133 /* Why is this not Perl_warn*() call ? */
134 PerlIO_printf(Perl_error_log,
135 "Warning: unable to close filehandle %s properly.\n",
138 IoOFP(io) = IoIFP(io) = Nullfp;
142 /* sysopen style args, i.e. integer mode and permissions */
146 #ifdef O_APPEND /* Not fully portable. */
149 #ifdef O_TRUNC /* Not fully portable. */
154 O_WRONLY|O_RDWR|O_CREAT|appendtrunc;
158 Perl_croak(aTHX_ "panic: sysopen with multiple args");
166 It might be (in OS/390 and Mac OS Classic it is)
172 This means that simple & with O_RDWR would look
173 like O_RDONLY is present. Therefore we have to
176 if ((ismodifying = (rawmode & modifyingmode))) {
177 if ((ismodifying & O_WRONLY) == O_WRONLY ||
178 (ismodifying & O_RDWR) == O_RDWR ||
179 (ismodifying & (O_CREAT|appendtrunc)))
180 TAINT_PROPER("sysopen");
182 mode[ix++] = '#'; /* Marker to openn to use numeric "sysopen" */
184 #if defined(USE_64_BIT_RAWIO) && defined(O_LARGEFILE)
185 rawmode |= O_LARGEFILE; /* Transparently largefiley. */
188 IoTYPE(io) = PerlIO_intmode2str(rawmode, &mode[ix], &writing);
190 namesv = sv_2mortal(newSVpvn(name,strlen(name)));
194 fp = PerlIO_openn(aTHX_ type, mode, -1, rawmode, rawperm, NULL, num_svs, svp);
197 /* Regular (non-sys) open */
202 PerlIO *that_fp = NULL;
204 type = savepvn(name, len);
208 /* Lose leading and trailing white space */
210 for (; isSPACE(*type); type++) ;
211 while (tend > type && isSPACE(tend[-1]))
215 /* New style explict name, type is just mode and discipline/layer info */
218 if (SvROK(*svp) && !strchr(name,'&')) {
220 Perl_warner(aTHX_ packWARN(WARN_IO),
221 "Can't open a reference");
222 SETERRNO(EINVAL, LIB_INVARG);
225 #endif /* USE_STDIO */
226 name = SvOK(*svp) ? SvPV(*svp, l) : "";
228 name = savepvn(name, len);
236 if ((*type == IoTYPE_RDWR) && /* scary */
237 (*(type+1) == IoTYPE_RDONLY || *(type+1) == IoTYPE_WRONLY) &&
238 ((!num_svs || (tend > type+1 && tend[-1] != IoTYPE_PIPE)))) {
239 TAINT_PROPER("open");
244 if (*type == IoTYPE_PIPE) {
246 if (type[1] != IoTYPE_STD) {
248 Perl_croak(aTHX_ "Unknown open() mode '%.*s'", (int)olen, oname);
253 for (type++; isSPACE(*type); type++) ;
259 /* command is missing 19990114 */
260 if (ckWARN(WARN_PIPE))
261 Perl_warner(aTHX_ packWARN(WARN_PIPE), "Missing command in piped open");
265 if (strNE(name,"-") || num_svs)
267 TAINT_PROPER("piped open");
268 if (!num_svs && name[len-1] == '|') {
270 if (ckWARN(WARN_PIPE))
271 Perl_warner(aTHX_ packWARN(WARN_PIPE), "Can't open bidirectional pipe");
280 fp = PerlProc_popen_list(mode, num_svs, svp);
283 fp = PerlProc_popen(name,mode);
287 if (PerlIO_apply_layers(aTHX_ fp, mode, type) != 0) {
293 else if (*type == IoTYPE_WRONLY) {
294 TAINT_PROPER("open");
296 if (*type == IoTYPE_WRONLY) {
297 /* Two IoTYPE_WRONLYs in a row make for an IoTYPE_APPEND. */
298 mode[0] = IoTYPE(io) = IoTYPE_APPEND;
313 dodup = PERLIO_DUP_FD;
319 if (!num_svs && !*type && supplied_fp) {
320 /* "<+&" etc. is used by typemaps */
325 Perl_croak(aTHX_ "More than one argument to '%c&' open",IoTYPE(io));
328 for (; isSPACE(*type); type++) ;
329 if (num_svs && (SvIOK(*svp) || (SvPOK(*svp) && looks_like_number(*svp)))) {
333 else if (isDIGIT(*type)) {
339 thatio = sv_2io(*svp);
343 thatgv = gv_fetchpv(type,FALSE,SVt_PVIO);
344 thatio = GvIO(thatgv);
348 SETERRNO(EINVAL,SS_IVCHAN);
352 if ((that_fp = IoIFP(thatio))) {
353 /* Flush stdio buffer before dup. --mjd
354 * Unfortunately SEEK_CURing 0 seems to
355 * be optimized away on most platforms;
356 * only Solaris and Linux seem to flush
359 /* sfio fails to clear error on next
360 sfwrite, contrary to documentation.
362 if (PerlIO_seek(that_fp, 0, SEEK_CUR) == -1)
363 PerlIO_clearerr(that_fp);
365 /* On the other hand, do all platforms
366 * take gracefully to flushing a read-only
367 * filehandle? Perhaps we should do
368 * fsetpos(src)+fgetpos(dst)? --nik */
369 PerlIO_flush(that_fp);
370 fd = PerlIO_fileno(that_fp);
371 /* When dup()ing STDIN, STDOUT or STDERR
372 * explicitly set appropriate access mode */
373 if (that_fp == PerlIO_stdout()
374 || that_fp == PerlIO_stderr())
375 IoTYPE(io) = IoTYPE_WRONLY;
376 else if (that_fp == PerlIO_stdin())
377 IoTYPE(io) = IoTYPE_RDONLY;
378 /* When dup()ing a socket, say result is
380 else if (IoTYPE(thatio) == IoTYPE_SOCKET)
381 IoTYPE(io) = IoTYPE_SOCKET;
389 fp = PerlIO_fdupopen(aTHX_ that_fp, NULL, dodup);
393 fd = PerlLIO_dup(fd);
396 if (!(fp = PerlIO_openn(aTHX_ type,mode,fd,0,0,NULL,num_svs,svp))) {
405 for (; isSPACE(*type); type++) ;
406 if (*type == IoTYPE_STD && (!type[1] || isSPACE(type[1]) || type[1] == ':')) {
409 fp = PerlIO_stdout();
410 IoTYPE(io) = IoTYPE_STD;
412 Perl_croak(aTHX_ "More than one argument to '>%c' open",IoTYPE_STD);
417 namesv = sv_2mortal(newSVpvn(type,strlen(type)));
422 fp = PerlIO_openn(aTHX_ type,mode,-1,0,0,NULL,num_svs,svp);
426 else if (*type == IoTYPE_RDONLY) {
428 for (type++; isSPACE(*type); type++) ;
438 if (*type == IoTYPE_STD && (!type[1] || isSPACE(type[1]) || type[1] == ':')) {
442 IoTYPE(io) = IoTYPE_STD;
444 Perl_croak(aTHX_ "More than one argument to '<%c' open",IoTYPE_STD);
449 namesv = sv_2mortal(newSVpvn(type,strlen(type)));
454 fp = PerlIO_openn(aTHX_ type,mode,-1,0,0,NULL,num_svs,svp);
457 else if ((num_svs && type[0] == IoTYPE_STD && type[1] == IoTYPE_PIPE) ||
458 (!num_svs && tend > type+1 && tend[-1] == IoTYPE_PIPE)) {
460 type += 2; /* skip over '-|' */
464 while (tend > type && isSPACE(tend[-1]))
467 for (; isSPACE(*type); type++) ;
472 /* command is missing 19990114 */
473 if (ckWARN(WARN_PIPE))
474 Perl_warner(aTHX_ packWARN(WARN_PIPE), "Missing command in piped open");
478 if (strNE(name,"-") || num_svs)
480 TAINT_PROPER("piped open");
487 fp = PerlProc_popen_list(mode,num_svs,svp);
490 fp = PerlProc_popen(name,mode);
492 IoTYPE(io) = IoTYPE_PIPE;
494 for (; isSPACE(*type); type++) ;
496 if (PerlIO_apply_layers(aTHX_ fp, mode, type) != 0) {
506 IoTYPE(io) = IoTYPE_RDONLY;
508 for (; isSPACE(*name); name++) ;
514 if (strEQ(name,"-")) {
516 IoTYPE(io) = IoTYPE_STD;
520 namesv = sv_2mortal(newSVpvn(type,strlen(type)));
525 fp = PerlIO_openn(aTHX_ type,mode,-1,0,0,NULL,num_svs,svp);
530 if (ckWARN(WARN_NEWLINE) && IoTYPE(io) == IoTYPE_RDONLY && strchr(name, '\n'))
531 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "open");
535 if (ckWARN(WARN_IO)) {
536 if ((IoTYPE(io) == IoTYPE_RDONLY) &&
537 (fp == PerlIO_stdout() || fp == PerlIO_stderr())) {
538 Perl_warner(aTHX_ packWARN(WARN_IO),
539 "Filehandle STD%s reopened as %s only for input",
540 ((fp == PerlIO_stdout()) ? "OUT" : "ERR"),
543 else if ((IoTYPE(io) == IoTYPE_WRONLY) && fp == PerlIO_stdin()) {
544 Perl_warner(aTHX_ packWARN(WARN_IO),
545 "Filehandle STDIN reopened as %s only for output",
550 fd = PerlIO_fileno(fp);
551 /* If there is no fd (e.g. PerlIO::scalar) assume it isn't a
552 * socket - this covers PerlIO::scalar - otherwise unless we "know" the
553 * type probe for socket-ness.
555 if (IoTYPE(io) && IoTYPE(io) != IoTYPE_PIPE && IoTYPE(io) != IoTYPE_STD && fd >= 0) {
556 if (PerlLIO_fstat(fd,&PL_statbuf) < 0) {
557 /* If PerlIO claims to have fd we had better be able to fstat() it. */
558 (void) PerlIO_close(fp);
562 if (S_ISSOCK(PL_statbuf.st_mode))
563 IoTYPE(io) = IoTYPE_SOCKET; /* in case a socket was passed in to us */
567 !(PL_statbuf.st_mode & S_IFMT)
571 && IoTYPE(io) != IoTYPE_WRONLY /* Dups of STD* filehandles already have */
572 && IoTYPE(io) != IoTYPE_RDONLY /* type so they aren't marked as sockets */
573 ) { /* on OS's that return 0 on fstat()ed pipe */
575 Sock_size_t buflen = sizeof tmpbuf;
576 if (PerlSock_getsockname(fd, (struct sockaddr *)tmpbuf, &buflen) >= 0
577 || errno != ENOTSOCK)
578 IoTYPE(io) = IoTYPE_SOCKET; /* some OS's return 0 on fstat()ed socket */
579 /* but some return 0 for streams too, sigh */
581 #endif /* HAS_SOCKET */
582 #endif /* !PERL_MICRO */
586 * If this is a standard handle we discard all the layer stuff
587 * and just dup the fd into whatever was on the handle before !
590 if (saveifp) { /* must use old fp? */
591 /* If fd is less that PL_maxsysfd i.e. STDIN..STDERR
592 then dup the new fileno down
595 PerlIO_flush(saveofp); /* emulate PerlIO_close() */
596 if (saveofp != saveifp) { /* was a socket? */
597 PerlIO_close(saveofp);
601 /* Still a small can-of-worms here if (say) PerlIO::scalar
602 is assigned to (say) STDOUT - for now let dup2() fail
603 and provide the error
605 if (PerlLIO_dup2(fd, savefd) < 0) {
606 (void)PerlIO_close(fp);
610 if (savefd != PerlIO_fileno(PerlIO_stdin())) {
611 char newname[FILENAME_MAX+1];
612 if (PerlIO_getname(fp, newname)) {
613 if (fd == PerlIO_fileno(PerlIO_stdout()))
614 Perl_vmssetuserlnm(aTHX_ "SYS$OUTPUT", newname);
615 if (fd == PerlIO_fileno(PerlIO_stderr()))
616 Perl_vmssetuserlnm(aTHX_ "SYS$ERROR", newname);
622 /* PL_fdpid isn't used on Windows, so avoid this useless work.
623 * XXX Probably the same for a lot of other places. */
629 sv = *av_fetch(PL_fdpid,fd,TRUE);
630 (void)SvUPGRADE(sv, SVt_IV);
633 sv = *av_fetch(PL_fdpid,savefd,TRUE);
634 (void)SvUPGRADE(sv, SVt_IV);
641 /* need to close fp without closing underlying fd */
642 int ofd = PerlIO_fileno(fp);
643 int dupfd = PerlLIO_dup(ofd);
644 #if defined(HAS_FCNTL) && defined(F_SETFD)
645 /* Assume if we have F_SETFD we have F_GETFD */
646 int coe = fcntl(ofd,F_GETFD);
649 PerlLIO_dup2(dupfd,ofd);
650 #if defined(HAS_FCNTL) && defined(F_SETFD)
651 /* The dup trick has lost close-on-exec on ofd */
652 fcntl(ofd,F_SETFD, coe);
654 PerlLIO_close(dupfd);
661 fd = PerlIO_fileno(fp);
663 #if defined(HAS_FCNTL) && defined(F_SETFD)
665 int save_errno = errno;
666 fcntl(fd,F_SETFD,fd > PL_maxsysfd); /* can change errno */
672 IoFLAGS(io) &= ~IOf_NOLINE;
674 if (IoTYPE(io) == IoTYPE_SOCKET
675 || (IoTYPE(io) == IoTYPE_WRONLY && fd >= 0 && S_ISCHR(PL_statbuf.st_mode)) ) {
677 if (*s == 'I' || *s == '#')
680 if (!(IoOFP(io) = PerlIO_openn(aTHX_ type,s,fd,0,0,NULL,0,svp))) {
694 IoTYPE(io) = savetype;
699 Perl_nextargv(pTHX_ register GV *gv)
702 #ifndef FLEXFILENAMES
711 PL_argvoutgv = gv_fetchpv("ARGVOUT",TRUE,SVt_PVIO);
712 if (io && (IoFLAGS(io) & IOf_ARGV) && (IoFLAGS(io) & IOf_START)) {
713 IoFLAGS(io) &= ~IOf_START;
715 if (!PL_argvout_stack)
716 PL_argvout_stack = newAV();
717 av_push(PL_argvout_stack, SvREFCNT_inc(PL_defoutgv));
720 if (PL_filemode & (S_ISUID|S_ISGID)) {
721 PerlIO_flush(IoIFP(GvIOn(PL_argvoutgv))); /* chmod must follow last write */
723 (void)fchmod(PL_lastfd,PL_filemode);
725 (void)PerlLIO_chmod(PL_oldname,PL_filemode);
731 while (av_len(GvAV(gv)) >= 0) {
733 sv = av_shift(GvAV(gv));
735 sv_setsv(GvSV(gv),sv);
736 SvSETMAGIC(GvSV(gv));
737 PL_oldname = SvPVx(GvSV(gv), oldlen);
738 if (do_open(gv,PL_oldname,oldlen,PL_inplace!=0,O_RDONLY,0,Nullfp)) {
740 TAINT_PROPER("inplace open");
741 if (oldlen == 1 && *PL_oldname == '-') {
742 setdefout(gv_fetchpv("STDOUT",TRUE,SVt_PVIO));
743 return IoIFP(GvIOp(gv));
745 #ifndef FLEXFILENAMES
746 filedev = PL_statbuf.st_dev;
747 fileino = PL_statbuf.st_ino;
749 PL_filemode = PL_statbuf.st_mode;
750 fileuid = PL_statbuf.st_uid;
751 filegid = PL_statbuf.st_gid;
752 if (!S_ISREG(PL_filemode)) {
753 if (ckWARN_d(WARN_INPLACE))
754 Perl_warner(aTHX_ packWARN(WARN_INPLACE),
755 "Can't do inplace edit: %s is not a regular file",
761 char *star = strchr(PL_inplace, '*');
763 char *begin = PL_inplace;
764 sv_setpvn(sv, "", 0);
766 sv_catpvn(sv, begin, star - begin);
767 sv_catpvn(sv, PL_oldname, oldlen);
769 } while ((star = strchr(begin, '*')));
774 sv_catpv(sv,PL_inplace);
776 #ifndef FLEXFILENAMES
777 if ((PerlLIO_stat(SvPVX(sv),&PL_statbuf) >= 0
778 && PL_statbuf.st_dev == filedev
779 && PL_statbuf.st_ino == fileino)
781 || ((_djstat_fail_bits & _STFAIL_TRUENAME)!=0)
785 if (ckWARN_d(WARN_INPLACE))
786 Perl_warner(aTHX_ packWARN(WARN_INPLACE),
787 "Can't do inplace edit: %"SVf" would not be unique",
794 #if !defined(DOSISH) && !defined(__CYGWIN__) && !defined(EPOC)
795 if (PerlLIO_rename(PL_oldname,SvPVX(sv)) < 0) {
796 if (ckWARN_d(WARN_INPLACE))
797 Perl_warner(aTHX_ packWARN(WARN_INPLACE),
798 "Can't rename %s to %"SVf": %s, skipping file",
799 PL_oldname, sv, Strerror(errno) );
805 (void)PerlLIO_unlink(SvPVX(sv));
806 (void)PerlLIO_rename(PL_oldname,SvPVX(sv));
807 do_open(gv,SvPVX(sv),SvCUR(sv),PL_inplace!=0,O_RDONLY,0,Nullfp);
810 (void)UNLINK(SvPVX(sv));
811 if (link(PL_oldname,SvPVX(sv)) < 0) {
812 if (ckWARN_d(WARN_INPLACE))
813 Perl_warner(aTHX_ packWARN(WARN_INPLACE),
814 "Can't rename %s to %"SVf": %s, skipping file",
815 PL_oldname, sv, Strerror(errno) );
819 (void)UNLINK(PL_oldname);
823 #if !defined(DOSISH) && !defined(AMIGAOS)
824 # ifndef VMS /* Don't delete; use automatic file versioning */
825 if (UNLINK(PL_oldname) < 0) {
826 if (ckWARN_d(WARN_INPLACE))
827 Perl_warner(aTHX_ packWARN(WARN_INPLACE),
828 "Can't remove %s: %s, skipping file",
829 PL_oldname, Strerror(errno) );
835 Perl_croak(aTHX_ "Can't do inplace edit without backup");
839 sv_setpvn(sv,">",!PL_inplace);
840 sv_catpvn(sv,PL_oldname,oldlen);
841 SETERRNO(0,0); /* in case sprintf set errno */
843 if (!do_open(PL_argvoutgv,SvPVX(sv),SvCUR(sv),PL_inplace!=0,
844 O_WRONLY|O_CREAT|O_TRUNC,0,Nullfp))
846 if (!do_open(PL_argvoutgv,SvPVX(sv),SvCUR(sv),PL_inplace!=0,
847 O_WRONLY|O_CREAT|OPEN_EXCL,0666,Nullfp))
850 if (ckWARN_d(WARN_INPLACE))
851 Perl_warner(aTHX_ packWARN(WARN_INPLACE), "Can't do inplace edit on %s: %s",
852 PL_oldname, Strerror(errno) );
856 setdefout(PL_argvoutgv);
857 PL_lastfd = PerlIO_fileno(IoIFP(GvIOp(PL_argvoutgv)));
858 (void)PerlLIO_fstat(PL_lastfd,&PL_statbuf);
860 (void)fchmod(PL_lastfd,PL_filemode);
862 # if !(defined(WIN32) && defined(__BORLANDC__))
863 /* Borland runtime creates a readonly file! */
864 (void)PerlLIO_chmod(PL_oldname,PL_filemode);
867 if (fileuid != PL_statbuf.st_uid || filegid != PL_statbuf.st_gid) {
869 (void)fchown(PL_lastfd,fileuid,filegid);
872 (void)PerlLIO_chown(PL_oldname,fileuid,filegid);
877 return IoIFP(GvIOp(gv));
880 if (ckWARN_d(WARN_INPLACE)) {
882 if (PerlLIO_stat(PL_oldname, &PL_statbuf) >= 0
883 && !S_ISREG(PL_statbuf.st_mode))
885 Perl_warner(aTHX_ packWARN(WARN_INPLACE),
886 "Can't do inplace edit: %s is not a regular file",
890 Perl_warner(aTHX_ packWARN(WARN_INPLACE), "Can't open %s: %s",
891 PL_oldname, Strerror(eno));
895 if (io && (IoFLAGS(io) & IOf_ARGV))
896 IoFLAGS(io) |= IOf_START;
898 (void)do_close(PL_argvoutgv,FALSE);
899 if (io && (IoFLAGS(io) & IOf_ARGV)
900 && PL_argvout_stack && AvFILLp(PL_argvout_stack) >= 0)
902 GV *oldout = (GV*)av_pop(PL_argvout_stack);
904 SvREFCNT_dec(oldout);
907 setdefout(gv_fetchpv("STDOUT",TRUE,SVt_PVIO));
914 Perl_do_pipe(pTHX_ SV *sv, GV *rgv, GV *wgv)
933 if (PerlProc_pipe(fd) < 0)
935 IoIFP(rstio) = PerlIO_fdopen(fd[0], "r"PIPESOCK_MODE);
936 IoOFP(wstio) = PerlIO_fdopen(fd[1], "w"PIPESOCK_MODE);
937 IoOFP(rstio) = IoIFP(rstio);
938 IoIFP(wstio) = IoOFP(wstio);
939 IoTYPE(rstio) = IoTYPE_RDONLY;
940 IoTYPE(wstio) = IoTYPE_WRONLY;
941 if (!IoIFP(rstio) || !IoOFP(wstio)) {
942 if (IoIFP(rstio)) PerlIO_close(IoIFP(rstio));
943 else PerlLIO_close(fd[0]);
944 if (IoOFP(wstio)) PerlIO_close(IoOFP(wstio));
945 else PerlLIO_close(fd[1]);
949 sv_setsv(sv,&PL_sv_yes);
953 sv_setsv(sv,&PL_sv_undef);
958 /* explicit renamed to avoid C++ conflict -- kja */
960 Perl_do_close(pTHX_ GV *gv, bool not_implicit)
967 if (!gv || SvTYPE(gv) != SVt_PVGV) {
969 SETERRNO(EBADF,SS_IVCHAN);
973 if (!io) { /* never opened */
975 if (ckWARN(WARN_UNOPENED)) /* no check for closed here */
976 report_evil_fh(gv, io, PL_op->op_type);
977 SETERRNO(EBADF,SS_IVCHAN);
981 retval = io_close(io, not_implicit);
985 IoLINES_LEFT(io) = IoPAGE_LEN(io);
987 IoTYPE(io) = IoTYPE_CLOSED;
992 Perl_io_close(pTHX_ IO *io, bool not_implicit)
998 if (IoTYPE(io) == IoTYPE_PIPE) {
999 status = PerlProc_pclose(IoIFP(io));
1001 STATUS_NATIVE_SET(status);
1002 retval = (STATUS_POSIX == 0);
1005 retval = (status != -1);
1008 else if (IoTYPE(io) == IoTYPE_STD)
1011 if (IoOFP(io) && IoOFP(io) != IoIFP(io)) { /* a socket */
1012 retval = (PerlIO_close(IoOFP(io)) != EOF);
1013 PerlIO_close(IoIFP(io)); /* clear stdio, fd already closed */
1016 retval = (PerlIO_close(IoIFP(io)) != EOF);
1018 IoOFP(io) = IoIFP(io) = Nullfp;
1020 else if (not_implicit) {
1021 SETERRNO(EBADF,SS_IVCHAN);
1028 Perl_do_eof(pTHX_ GV *gv)
1037 else if (ckWARN(WARN_IO) && (IoTYPE(io) == IoTYPE_WRONLY))
1038 report_evil_fh(gv, io, OP_phoney_OUTPUT_ONLY);
1043 if (PerlIO_has_cntptr(IoIFP(io))) { /* (the code works without this) */
1044 if (PerlIO_get_cnt(IoIFP(io)) > 0) /* cheat a little, since */
1045 return FALSE; /* this is the most usual case */
1048 saverrno = errno; /* getc and ungetc can stomp on errno */
1049 ch = PerlIO_getc(IoIFP(io));
1051 (void)PerlIO_ungetc(IoIFP(io),ch);
1057 if (PerlIO_has_cntptr(IoIFP(io)) && PerlIO_canset_cnt(IoIFP(io))) {
1058 if (PerlIO_get_cnt(IoIFP(io)) < -1)
1059 PerlIO_set_cnt(IoIFP(io),-1);
1061 if (PL_op->op_flags & OPf_SPECIAL) { /* not necessarily a real EOF yet? */
1062 if (gv != PL_argvgv || !nextargv(gv)) /* get another fp handy */
1066 return TRUE; /* normal fp, definitely end of file */
1072 Perl_do_tell(pTHX_ GV *gv)
1074 register IO *io = 0;
1075 register PerlIO *fp;
1077 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io))) {
1078 #ifdef ULTRIX_STDIO_BOTCH
1080 (void)PerlIO_seek(fp, 0L, 2); /* ultrix 1.2 workaround */
1082 return PerlIO_tell(fp);
1084 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1085 report_evil_fh(gv, io, PL_op->op_type);
1086 SETERRNO(EBADF,RMS_IFI);
1091 Perl_do_seek(pTHX_ GV *gv, Off_t pos, int whence)
1093 register IO *io = 0;
1094 register PerlIO *fp;
1096 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io))) {
1097 #ifdef ULTRIX_STDIO_BOTCH
1099 (void)PerlIO_seek(fp, 0L, 2); /* ultrix 1.2 workaround */
1101 return PerlIO_seek(fp, pos, whence) >= 0;
1103 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1104 report_evil_fh(gv, io, PL_op->op_type);
1105 SETERRNO(EBADF,RMS_IFI);
1110 Perl_do_sysseek(pTHX_ GV *gv, Off_t pos, int whence)
1112 register IO *io = 0;
1113 register PerlIO *fp;
1115 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io)))
1116 return PerlLIO_lseek(PerlIO_fileno(fp), pos, whence);
1117 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1118 report_evil_fh(gv, io, PL_op->op_type);
1119 SETERRNO(EBADF,RMS_IFI);
1124 Perl_mode_from_discipline(pTHX_ SV *discp)
1126 int mode = O_BINARY;
1129 char *s = SvPV(discp,len);
1134 if (len > 3 && strnEQ(s+1, "raw", 3)
1135 && (!s[4] || s[4] == ':' || isSPACE(s[4])))
1144 if (len > 4 && strnEQ(s+1, "crlf", 4)
1145 && (!s[5] || s[5] == ':' || isSPACE(s[5])))
1154 goto fail_discipline;
1157 else if (isSPACE(*s)) {
1164 end = strchr(s+1, ':');
1167 #ifndef PERLIO_LAYERS
1168 Perl_croak(aTHX_ "Unknown discipline '%.*s'", end-s, s);
1179 Perl_do_binmode(pTHX_ PerlIO *fp, int iotype, int mode)
1181 /* The old body of this is now in non-LAYER part of perlio.c
1182 * This is a stub for any XS code which might have been calling it.
1184 char *name = ":raw";
1185 #ifdef PERLIO_USING_CRLF
1186 if (!(mode & O_BINARY))
1189 return PerlIO_binmode(aTHX_ fp, iotype, mode, name);
1192 #if !defined(HAS_TRUNCATE) && !defined(HAS_CHSIZE) && defined(F_FREESP)
1193 /* code courtesy of William Kucharski */
1196 I32 my_chsize(fd, length)
1197 I32 fd; /* file descriptor */
1198 Off_t length; /* length to set file to */
1203 if (PerlLIO_fstat(fd, &filebuf) < 0)
1206 if (filebuf.st_size < length) {
1208 /* extend file length */
1210 if ((PerlLIO_lseek(fd, (length - 1), 0)) < 0)
1213 /* write a "0" byte */
1215 if ((PerlLIO_write(fd, "", 1)) != 1)
1219 /* truncate length */
1223 fl.l_start = length;
1224 fl.l_type = F_WRLCK; /* write lock on file space */
1227 * This relies on the UNDOCUMENTED F_FREESP argument to
1228 * fcntl(2), which truncates the file so that it ends at the
1229 * position indicated by fl.l_start.
1231 * Will minor miracles never cease?
1234 if (fcntl(fd, F_FREESP, &fl) < 0)
1241 #endif /* F_FREESP */
1244 Perl_do_print(pTHX_ register SV *sv, PerlIO *fp)
1246 register char *tmps;
1249 /* assuming fp is checked earlier */
1255 if (SvIOK(sv) && SvIVX(sv) != 0) {
1256 PerlIO_printf(fp, PL_ofmt, (NV)SvIVX(sv));
1257 return !PerlIO_error(fp);
1259 if ( (SvNOK(sv) && SvNVX(sv) != 0.0)
1260 || (looks_like_number(sv) && sv_2nv(sv) != 0.0) ) {
1261 PerlIO_printf(fp, PL_ofmt, SvNVX(sv));
1262 return !PerlIO_error(fp);
1265 switch (SvTYPE(sv)) {
1267 if (ckWARN(WARN_UNINITIALIZED))
1275 PerlIO_printf(fp, "%"UVuf, (UV)SvUVX(sv));
1277 PerlIO_printf(fp, "%"IVdf, (IV)SvIVX(sv));
1278 return !PerlIO_error(fp);
1282 if (PerlIO_isutf8(fp)) {
1284 sv_utf8_upgrade_flags(sv = sv_mortalcopy(sv),
1285 SV_GMAGIC|SV_UTF8_NO_ENCODING);
1287 else if (DO_UTF8(sv)) {
1288 if (!sv_utf8_downgrade((sv = sv_mortalcopy(sv)), TRUE)
1289 && ckWARN_d(WARN_UTF8))
1291 Perl_warner(aTHX_ packWARN(WARN_UTF8), "Wide character in print");
1294 tmps = SvPV(sv, len);
1297 /* To detect whether the process is about to overstep its
1298 * filesize limit we would need getrlimit(). We could then
1299 * also transparently raise the limit with setrlimit() --
1300 * but only until the system hard limit/the filesystem limit,
1301 * at which we would get EPERM. Note that when using buffered
1302 * io the write failure can be delayed until the flush/close. --jhi */
1303 if (len && (PerlIO_write(fp,tmps,len) == 0))
1305 return !PerlIO_error(fp);
1315 if (PL_op->op_flags & OPf_REF) {
1320 if (io && IoIFP(io)) {
1322 sv_setpv(PL_statname,"");
1323 PL_laststype = OP_STAT;
1324 return (PL_laststatval = PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache));
1328 return PL_laststatval;
1329 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1330 report_evil_fh(gv, io, PL_op->op_type);
1332 sv_setpv(PL_statname,"");
1333 return (PL_laststatval = -1);
1341 if (SvTYPE(sv) == SVt_PVGV) {
1345 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
1352 sv_setpvn(PL_statname, s, len);
1353 s = SvPVX(PL_statname); /* s now NUL-terminated */
1354 PL_laststype = OP_STAT;
1355 PL_laststatval = PerlLIO_stat(s, &PL_statcache);
1356 if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(s, '\n'))
1357 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "stat");
1358 return PL_laststatval;
1368 if (PL_op->op_flags & OPf_REF) {
1370 if (cGVOP_gv == PL_defgv) {
1371 if (PL_laststype != OP_LSTAT)
1372 Perl_croak(aTHX_ "The stat preceding -l _ wasn't an lstat");
1373 return PL_laststatval;
1375 if (ckWARN(WARN_IO)) {
1376 Perl_warner(aTHX_ packWARN(WARN_IO), "Use of -l on filehandle %s",
1378 return (PL_laststatval = -1);
1382 PL_laststype = OP_LSTAT;
1386 if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV && ckWARN(WARN_IO)) {
1387 Perl_warner(aTHX_ packWARN(WARN_IO), "Use of -l on filehandle %s",
1388 GvENAME((GV*) SvRV(sv)));
1389 return (PL_laststatval = -1);
1391 sv_setpv(PL_statname,SvPV(sv, n_a));
1392 PL_laststatval = PerlLIO_lstat(SvPV(sv, n_a),&PL_statcache);
1393 if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(SvPV(sv, n_a), '\n'))
1394 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "lstat");
1395 return PL_laststatval;
1400 Perl_do_aexec(pTHX_ SV *really, register SV **mark, register SV **sp)
1402 return do_aexec5(really, mark, sp, 0, 0);
1407 Perl_do_aexec5(pTHX_ SV *really, register SV **mark, register SV **sp,
1408 int fd, int do_report)
1410 #ifdef MACOS_TRADITIONAL
1411 Perl_croak(aTHX_ "exec? I'm not *that* kind of operating system");
1414 char *tmps = Nullch;
1418 New(401,PL_Argv, sp - mark + 1, char*);
1420 while (++mark <= sp) {
1422 *a++ = SvPVx(*mark, n_a);
1428 tmps = SvPV(really, n_a);
1429 if ((!really && *PL_Argv[0] != '/') ||
1430 (really && *tmps != '/')) /* will execvp use PATH? */
1431 TAINT_ENV(); /* testing IFS here is overkill, probably */
1433 if (really && *tmps)
1434 PerlProc_execvp(tmps,EXEC_ARGV_CAST(PL_Argv));
1436 PerlProc_execvp(PL_Argv[0],EXEC_ARGV_CAST(PL_Argv));
1438 if (ckWARN(WARN_EXEC))
1439 Perl_warner(aTHX_ packWARN(WARN_EXEC), "Can't exec \"%s\": %s",
1440 (really ? tmps : PL_Argv[0]), Strerror(errno));
1444 PerlLIO_write(fd, (void*)&e, sizeof(int));
1454 Perl_do_execfree(pTHX)
1458 PL_Argv = Null(char **);
1466 #if !defined(OS2) && !defined(WIN32) && !defined(DJGPP) && !defined(EPOC) && !defined(MACOS_TRADITIONAL)
1469 Perl_do_exec(pTHX_ char *cmd)
1471 return do_exec3(cmd,0,0);
1475 Perl_do_exec3(pTHX_ char *cmd, int fd, int do_report)
1480 while (*cmd && isSPACE(*cmd))
1483 /* save an extra exec if possible */
1488 if (strnEQ(cmd,PL_cshname,PL_cshlen) &&
1489 strnEQ(cmd+PL_cshlen," -c",3)) {
1491 s = cmd+PL_cshlen+3;
1505 if (s[-1] == '\'') {
1508 PerlProc_execl(PL_cshname,"csh", flags, ncmd, (char*)0);
1518 /* see if there are shell metacharacters in it */
1520 if (*cmd == '.' && isSPACE(cmd[1]))
1523 if (strnEQ(cmd,"exec",4) && isSPACE(cmd[4]))
1526 for (s = cmd; *s && isALNUM(*s); s++) ; /* catch VAR=val gizmo */
1530 for (s = cmd; *s; s++) {
1531 if (*s != ' ' && !isALPHA(*s) &&
1532 strchr("$&*(){}[]'\";\\|?<>~`\n",*s)) {
1533 if (*s == '\n' && !s[1]) {
1537 /* handle the 2>&1 construct at the end */
1538 if (*s == '>' && s[1] == '&' && s[2] == '1'
1539 && s > cmd + 1 && s[-1] == '2' && isSPACE(s[-2])
1540 && (!s[3] || isSPACE(s[3])))
1544 while (*t && isSPACE(*t))
1546 if (!*t && (dup2(1,2) != -1)) {
1553 PerlProc_execl(PL_sh_path, "sh", "-c", cmd, (char*)0);
1559 New(402,PL_Argv, (s - cmd) / 2 + 2, char*);
1560 PL_Cmd = savepvn(cmd, s-cmd);
1562 for (s = PL_Cmd; *s;) {
1563 while (*s && isSPACE(*s)) s++;
1566 while (*s && !isSPACE(*s)) s++;
1573 PerlProc_execvp(PL_Argv[0],PL_Argv);
1575 if (errno == ENOEXEC) { /* for system V NIH syndrome */
1582 if (ckWARN(WARN_EXEC))
1583 Perl_warner(aTHX_ packWARN(WARN_EXEC), "Can't exec \"%s\": %s",
1584 PL_Argv[0], Strerror(errno));
1586 PerlLIO_write(fd, (void*)&e, sizeof(int));
1595 #endif /* OS2 || WIN32 */
1598 Perl_apply(pTHX_ I32 type, register SV **mark, register SV **sp)
1602 register I32 tot = 0;
1605 SV **oldmark = mark;
1608 #define APPLY_TAINT_PROPER() \
1610 if (PL_tainted) { TAINT_PROPER(what); } \
1613 /* This is a first heuristic; it doesn't catch tainting magic. */
1615 while (++mark <= sp) {
1616 if (SvTAINTED(*mark)) {
1626 APPLY_TAINT_PROPER();
1629 APPLY_TAINT_PROPER();
1631 while (++mark <= sp) {
1632 char *name = SvPVx(*mark, n_a);
1633 APPLY_TAINT_PROPER();
1634 if (PerlLIO_chmod(name, val))
1642 APPLY_TAINT_PROPER();
1643 if (sp - mark > 2) {
1644 val = SvIVx(*++mark);
1645 val2 = SvIVx(*++mark);
1646 APPLY_TAINT_PROPER();
1648 while (++mark <= sp) {
1649 char *name = SvPVx(*mark, n_a);
1650 APPLY_TAINT_PROPER();
1651 if (PerlLIO_chown(name, val, val2))
1658 XXX Should we make lchown() directly available from perl?
1659 For now, we'll let Configure test for HAS_LCHOWN, but do
1660 nothing in the core.
1666 APPLY_TAINT_PROPER();
1669 s = SvPVx(*++mark, n_a);
1671 if (*s == 'S' && s[1] == 'I' && s[2] == 'G')
1673 if ((val = whichsig(s)) < 0)
1674 Perl_croak(aTHX_ "Unrecognized signal name \"%s\"",s);
1678 APPLY_TAINT_PROPER();
1681 /* kill() doesn't do process groups (job trees?) under VMS */
1682 if (val < 0) val = -val;
1683 if (val == SIGKILL) {
1684 # include <starlet.h>
1685 /* Use native sys$delprc() to insure that target process is
1686 * deleted; supervisor-mode images don't pay attention to
1687 * CRTL's emulation of Unix-style signals and kill()
1689 while (++mark <= sp) {
1690 I32 proc = SvIVx(*mark);
1691 register unsigned long int __vmssts;
1692 APPLY_TAINT_PROPER();
1693 if (!((__vmssts = sys$delprc(&proc,0)) & 1)) {
1697 case SS$_NOSUCHNODE:
1698 SETERRNO(ESRCH,__vmssts);
1701 SETERRNO(EPERM,__vmssts);
1704 SETERRNO(EVMSERR,__vmssts);
1713 while (++mark <= sp) {
1714 I32 proc = SvIVx(*mark);
1715 APPLY_TAINT_PROPER();
1717 if (PerlProc_killpg(proc,val)) /* BSD */
1719 if (PerlProc_kill(-proc,val)) /* SYSV */
1725 while (++mark <= sp) {
1726 I32 proc = SvIVx(*mark);
1727 APPLY_TAINT_PROPER();
1728 if (PerlProc_kill(proc, val))
1736 APPLY_TAINT_PROPER();
1738 while (++mark <= sp) {
1739 s = SvPVx(*mark, n_a);
1740 APPLY_TAINT_PROPER();
1741 if (PL_euid || PL_unsafe) {
1745 else { /* don't let root wipe out directories without -U */
1746 if (PerlLIO_lstat(s,&PL_statbuf) < 0 || S_ISDIR(PL_statbuf.st_mode))
1758 APPLY_TAINT_PROPER();
1759 if (sp - mark > 2) {
1760 #if defined(I_UTIME) || defined(VMS)
1761 struct utimbuf utbuf;
1769 SV* accessed = *++mark;
1770 SV* modified = *++mark;
1771 void * utbufp = &utbuf;
1773 /* Be like C, and if both times are undefined, let the C
1774 * library figure out what to do. This usually means
1775 * "current time". */
1777 if ( accessed == &PL_sv_undef && modified == &PL_sv_undef )
1780 Zero(&utbuf, sizeof utbuf, char);
1782 utbuf.actime = (Time_t)SvNVx(accessed); /* time accessed */
1783 utbuf.modtime = (Time_t)SvNVx(modified); /* time modified */
1785 utbuf.actime = (Time_t)SvIVx(accessed); /* time accessed */
1786 utbuf.modtime = (Time_t)SvIVx(modified); /* time modified */
1789 APPLY_TAINT_PROPER();
1791 while (++mark <= sp) {
1792 char *name = SvPVx(*mark, n_a);
1793 APPLY_TAINT_PROPER();
1794 if (PerlLIO_utime(name, utbufp))
1805 #undef APPLY_TAINT_PROPER
1808 /* Do the permissions allow some operation? Assumes statcache already set. */
1809 #ifndef VMS /* VMS' cando is in vms.c */
1811 Perl_cando(pTHX_ Mode_t mode, Uid_t effective, register Stat_t *statbufp)
1812 /* Note: we use `effective' both for uids and gids.
1813 * Here we are betting on Uid_t being equal or wider than Gid_t. */
1816 /* [Comments and code from Len Reed]
1817 * MS-DOS "user" is similar to UNIX's "superuser," but can't write
1818 * to write-protected files. The execute permission bit is set
1819 * by the Miscrosoft C library stat() function for the following:
1824 * All files and directories are readable.
1825 * Directories and special files, e.g. "CON", cannot be
1827 * [Comment by Tom Dinger -- a directory can have the write-protect
1828 * bit set in the file system, but DOS permits changes to
1829 * the directory anyway. In addition, all bets are off
1830 * here for networked software, such as Novell and
1834 /* Atari stat() does pretty much the same thing. we set x_bit_set_in_stat
1835 * too so it will actually look into the files for magic numbers
1837 return (mode & statbufp->st_mode) ? TRUE : FALSE;
1839 #else /* ! DOSISH */
1840 if ((effective ? PL_euid : PL_uid) == 0) { /* root is special */
1841 if (mode == S_IXUSR) {
1842 if (statbufp->st_mode & 0111 || S_ISDIR(statbufp->st_mode))
1846 return TRUE; /* root reads and writes anything */
1849 if (statbufp->st_uid == (effective ? PL_euid : PL_uid) ) {
1850 if (statbufp->st_mode & mode)
1851 return TRUE; /* ok as "user" */
1853 else if (ingroup(statbufp->st_gid,effective)) {
1854 if (statbufp->st_mode & mode >> 3)
1855 return TRUE; /* ok as "group" */
1857 else if (statbufp->st_mode & mode >> 6)
1858 return TRUE; /* ok as "other" */
1860 #endif /* ! DOSISH */
1865 Perl_ingroup(pTHX_ Gid_t testgid, Uid_t effective)
1867 #ifdef MACOS_TRADITIONAL
1868 /* This is simply not correct for AppleShare, but fix it yerself. */
1871 if (testgid == (effective ? PL_egid : PL_gid))
1873 #ifdef HAS_GETGROUPS
1878 Groups_t gary[NGROUPS];
1881 anum = getgroups(NGROUPS,gary);
1883 if (gary[anum] == testgid)
1891 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
1894 Perl_do_ipcget(pTHX_ I32 optype, SV **mark, SV **sp)
1899 key = (key_t)SvNVx(*++mark);
1900 n = (optype == OP_MSGGET) ? 0 : SvIVx(*++mark);
1901 flags = SvIVx(*++mark);
1907 return msgget(key, flags);
1911 return semget(key, n, flags);
1915 return shmget(key, n, flags);
1917 #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
1919 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
1922 return -1; /* should never happen */
1926 Perl_do_ipcctl(pTHX_ I32 optype, SV **mark, SV **sp)
1930 I32 id, n, cmd, infosize, getinfo;
1933 id = SvIVx(*++mark);
1934 n = (optype == OP_SEMCTL) ? SvIVx(*++mark) : 0;
1935 cmd = SvIVx(*++mark);
1938 getinfo = (cmd == IPC_STAT);
1944 if (cmd == IPC_STAT || cmd == IPC_SET)
1945 infosize = sizeof(struct msqid_ds);
1950 if (cmd == IPC_STAT || cmd == IPC_SET)
1951 infosize = sizeof(struct shmid_ds);
1957 if (cmd == IPC_STAT || cmd == IPC_SET)
1958 infosize = sizeof(struct semid_ds);
1959 else if (cmd == GETALL || cmd == SETALL)
1961 struct semid_ds semds;
1963 #ifdef EXTRA_F_IN_SEMUN_BUF
1964 semun.buff = &semds;
1968 getinfo = (cmd == GETALL);
1969 if (Semctl(id, 0, IPC_STAT, semun) == -1)
1971 infosize = semds.sem_nsems * sizeof(short);
1972 /* "short" is technically wrong but much more portable
1973 than guessing about u_?short(_t)? */
1976 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
1980 #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
1982 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
1991 SvPV_force(astr, len);
1992 a = SvGROW(astr, infosize+1);
1996 a = SvPV(astr, len);
1997 if (len != infosize)
1998 Perl_croak(aTHX_ "Bad arg length for %s, is %lu, should be %ld",
2007 a = INT2PTR(char *,i); /* ouch */
2014 ret = msgctl(id, cmd, (struct msqid_ds *)a);
2020 union semun unsemds;
2022 #ifdef EXTRA_F_IN_SEMUN_BUF
2023 unsemds.buff = (struct semid_ds *)a;
2025 unsemds.buf = (struct semid_ds *)a;
2027 ret = Semctl(id, n, cmd, unsemds);
2029 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
2036 ret = shmctl(id, cmd, (struct shmid_ds *)a);
2040 if (getinfo && ret >= 0) {
2041 SvCUR_set(astr, infosize);
2042 *SvEND(astr) = '\0';
2049 Perl_do_msgsnd(pTHX_ SV **mark, SV **sp)
2054 I32 id, msize, flags;
2057 id = SvIVx(*++mark);
2059 flags = SvIVx(*++mark);
2060 mbuf = SvPV(mstr, len);
2061 if ((msize = len - sizeof(long)) < 0)
2062 Perl_croak(aTHX_ "Arg too short for msgsnd");
2064 return msgsnd(id, (struct msgbuf *)mbuf, msize, flags);
2066 Perl_croak(aTHX_ "msgsnd not implemented");
2071 Perl_do_msgrcv(pTHX_ SV **mark, SV **sp)
2077 I32 id, msize, flags, ret;
2080 id = SvIVx(*++mark);
2082 /* suppress warning when reading into undef var --jhi */
2084 sv_setpvn(mstr, "", 0);
2085 msize = SvIVx(*++mark);
2086 mtype = (long)SvIVx(*++mark);
2087 flags = SvIVx(*++mark);
2088 SvPV_force(mstr, len);
2089 mbuf = SvGROW(mstr, sizeof(long)+msize+1);
2092 ret = msgrcv(id, (struct msgbuf *)mbuf, msize, mtype, flags);
2094 SvCUR_set(mstr, sizeof(long)+ret);
2095 *SvEND(mstr) = '\0';
2096 #ifndef INCOMPLETE_TAINTS
2097 /* who knows who has been playing with this message? */
2103 Perl_croak(aTHX_ "msgrcv not implemented");
2108 Perl_do_semop(pTHX_ SV **mark, SV **sp)
2116 id = SvIVx(*++mark);
2118 opbuf = SvPV(opstr, opsize);
2119 if (opsize < 3 * SHORTSIZE
2120 || (opsize % (3 * SHORTSIZE))) {
2121 SETERRNO(EINVAL,LIB_INVARG);
2125 /* We can't assume that sizeof(struct sembuf) == 3 * sizeof(short). */
2127 int nsops = opsize / (3 * sizeof (short));
2129 short *ops = (short *) opbuf;
2131 struct sembuf *temps, *t;
2134 New (0, temps, nsops, struct sembuf);
2142 result = semop(id, temps, nsops);
2156 Perl_croak(aTHX_ "semop not implemented");
2161 Perl_do_shmio(pTHX_ I32 optype, SV **mark, SV **sp)
2166 I32 id, mpos, msize;
2168 struct shmid_ds shmds;
2170 id = SvIVx(*++mark);
2172 mpos = SvIVx(*++mark);
2173 msize = SvIVx(*++mark);
2175 if (shmctl(id, IPC_STAT, &shmds) == -1)
2177 if (mpos < 0 || msize < 0 || mpos + msize > shmds.shm_segsz) {
2178 SETERRNO(EFAULT,SS_ACCVIO); /* can't do as caller requested */
2181 shm = (char *)shmat(id, (char*)NULL, (optype == OP_SHMREAD) ? SHM_RDONLY : 0);
2182 if (shm == (char *)-1) /* I hate System V IPC, I really do */
2184 if (optype == OP_SHMREAD) {
2185 /* suppress warning when reading into undef var (tchrist 3/Mar/00) */
2187 sv_setpvn(mstr, "", 0);
2188 SvPV_force(mstr, len);
2189 mbuf = SvGROW(mstr, msize+1);
2191 Copy(shm + mpos, mbuf, msize, char);
2192 SvCUR_set(mstr, msize);
2193 *SvEND(mstr) = '\0';
2195 #ifndef INCOMPLETE_TAINTS
2196 /* who knows who has been playing with this shared memory? */
2203 mbuf = SvPV(mstr, len);
2204 if ((n = len) > msize)
2206 Copy(mbuf, shm + mpos, n, char);
2208 memzero(shm + mpos + n, msize - n);
2212 Perl_croak(aTHX_ "shm I/O not implemented");
2216 #endif /* SYSV IPC */
2221 =for apidoc start_glob
2223 Function called by C<do_readline> to spawn a glob (or do the glob inside
2224 perl on VMS). This code used to be inline, but now perl uses C<File::Glob>
2225 this glob starter is only used by miniperl during the build process.
2226 Moving it away shrinks pp_hot.c; shrinking pp_hot.c helps speed perl up.
2232 Perl_start_glob (pTHX_ SV *tmpglob, IO *io)
2234 SV *tmpcmd = NEWSV(55, 0);
2238 #ifdef VMS /* expand the wildcards right here, rather than opening a pipe, */
2239 /* since spawning off a process is a real performance hit */
2241 #include <descrip.h>
2242 #include <lib$routines.h>
2245 char rslt[NAM$C_MAXRSS+1+sizeof(unsigned short int)] = {'\0','\0'};
2246 char vmsspec[NAM$C_MAXRSS+1];
2247 char *rstr = rslt + sizeof(unsigned short int), *begin, *end, *cp;
2248 $DESCRIPTOR(dfltdsc,"SYS$DISK:[]*.*;");
2251 struct dsc$descriptor_s wilddsc
2252 = {0, DSC$K_DTYPE_T, DSC$K_CLASS_S, 0};
2253 struct dsc$descriptor_vs rsdsc
2254 = {sizeof rslt, DSC$K_DTYPE_VT, DSC$K_CLASS_VS, rslt};
2255 unsigned long int cxt = 0, sts = 0, ok = 1, hasdir = 0, hasver = 0, isunix = 0;
2257 /* We could find out if there's an explicit dev/dir or version
2258 by peeking into lib$find_file's internal context at
2259 ((struct NAM *)((struct FAB *)cxt)->fab$l_nam)->nam$l_fnb
2260 but that's unsupported, so I don't want to do it now and
2261 have it bite someone in the future. */
2262 cp = SvPV(tmpglob,i);
2264 if (cp[i] == ';') hasver = 1;
2266 if (sts) hasver = 1;
2270 hasdir = isunix = 1;
2273 if (cp[i] == ']' || cp[i] == '>' || cp[i] == ':') {
2278 if ((tmpfp = PerlIO_tmpfile()) != NULL) {
2280 if (!PerlLIO_stat(SvPVX(tmpglob),&st) && S_ISDIR(st.st_mode))
2281 ok = ((wilddsc.dsc$a_pointer = tovmspath(SvPVX(tmpglob),vmsspec)) != NULL);
2282 else ok = ((wilddsc.dsc$a_pointer = tovmsspec(SvPVX(tmpglob),vmsspec)) != NULL);
2283 if (ok) wilddsc.dsc$w_length = (unsigned short int) strlen(wilddsc.dsc$a_pointer);
2284 for (cp=wilddsc.dsc$a_pointer; ok && cp && *cp; cp++)
2285 if (*cp == '?') *cp = '%'; /* VMS style single-char wildcard */
2286 while (ok && ((sts = lib$find_file(&wilddsc,&rsdsc,&cxt,
2287 &dfltdsc,NULL,NULL,NULL))&1)) {
2288 end = rstr + (unsigned long int) *rslt;
2289 if (!hasver) while (*end != ';') end--;
2290 *(end++) = '\n'; *end = '\0';
2291 for (cp = rstr; *cp; cp++) *cp = _tolower(*cp);
2293 if (isunix) trim_unixpath(rstr,SvPVX(tmpglob),1);
2298 while (*(--begin) != ']' && *begin != '>') ;
2301 ok = (PerlIO_puts(tmpfp,begin) != EOF);
2303 if (cxt) (void)lib$find_file_end(&cxt);
2304 if (ok && sts != RMS$_NMF &&
2305 sts != RMS$_DNF && sts != RMS_FNF) ok = 0;
2308 SETERRNO((sts == RMS$_SYN ? EINVAL : EVMSERR),sts);
2310 PerlIO_close(tmpfp);
2314 PerlIO_rewind(tmpfp);
2315 IoTYPE(io) = IoTYPE_RDONLY;
2316 IoIFP(io) = fp = tmpfp;
2317 IoFLAGS(io) &= ~IOf_UNTAINT; /* maybe redundant */
2322 #ifdef MACOS_TRADITIONAL
2323 sv_setpv(tmpcmd, "glob ");
2324 sv_catsv(tmpcmd, tmpglob);
2325 sv_catpv(tmpcmd, " |");
2329 sv_setpv(tmpcmd, "for a in ");
2330 sv_catsv(tmpcmd, tmpglob);
2331 sv_catpv(tmpcmd, "; do echo \"$a\\0\\c\"; done |");
2334 sv_setpv(tmpcmd, "/dev/dosglob/"); /* File System Extension */
2335 sv_catsv(tmpcmd, tmpglob);
2337 sv_setpv(tmpcmd, "perlglob ");
2338 sv_catsv(tmpcmd, tmpglob);
2339 sv_catpv(tmpcmd, " |");
2344 sv_setpvn(tmpcmd, PL_cshname, PL_cshlen);
2345 sv_catpv(tmpcmd, " -cf 'set nonomatch; glob ");
2346 sv_catsv(tmpcmd, tmpglob);
2347 sv_catpv(tmpcmd, "' 2>/dev/null |");
2349 sv_setpv(tmpcmd, "echo ");
2350 sv_catsv(tmpcmd, tmpglob);
2352 sv_catpv(tmpcmd, "|tr -s ' \t\f\r' '\\012\\012\\012\\012'|");
2354 sv_catpv(tmpcmd, "|tr -s ' \t\f\r' '\\n\\n\\n\\n'|");
2357 #endif /* !DOSISH */
2358 #endif /* MACOS_TRADITIONAL */
2359 (void)do_open(PL_last_in_gv, SvPVX(tmpcmd), SvCUR(tmpcmd),
2360 FALSE, O_RDONLY, 0, Nullfp);