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 */
1432 if (really && *tmps)
1433 PerlProc_execvp(tmps,EXEC_ARGV_CAST(PL_Argv));
1435 PerlProc_execvp(PL_Argv[0],EXEC_ARGV_CAST(PL_Argv));
1436 if (ckWARN(WARN_EXEC))
1437 Perl_warner(aTHX_ packWARN(WARN_EXEC), "Can't exec \"%s\": %s",
1438 (really ? tmps : PL_Argv[0]), Strerror(errno));
1442 PerlLIO_write(fd, (void*)&e, sizeof(int));
1452 Perl_do_execfree(pTHX)
1456 PL_Argv = Null(char **);
1464 #if !defined(OS2) && !defined(WIN32) && !defined(DJGPP) && !defined(EPOC) && !defined(MACOS_TRADITIONAL)
1467 Perl_do_exec(pTHX_ char *cmd)
1469 return do_exec3(cmd,0,0);
1473 Perl_do_exec3(pTHX_ char *cmd, int fd, int do_report)
1478 while (*cmd && isSPACE(*cmd))
1481 /* save an extra exec if possible */
1486 if (strnEQ(cmd,PL_cshname,PL_cshlen) &&
1487 strnEQ(cmd+PL_cshlen," -c",3)) {
1489 s = cmd+PL_cshlen+3;
1503 if (s[-1] == '\'') {
1505 PerlProc_execl(PL_cshname,"csh", flags, ncmd, (char*)0);
1514 /* see if there are shell metacharacters in it */
1516 if (*cmd == '.' && isSPACE(cmd[1]))
1519 if (strnEQ(cmd,"exec",4) && isSPACE(cmd[4]))
1522 for (s = cmd; *s && isALNUM(*s); s++) ; /* catch VAR=val gizmo */
1526 for (s = cmd; *s; s++) {
1527 if (*s != ' ' && !isALPHA(*s) &&
1528 strchr("$&*(){}[]'\";\\|?<>~`\n",*s)) {
1529 if (*s == '\n' && !s[1]) {
1533 /* handle the 2>&1 construct at the end */
1534 if (*s == '>' && s[1] == '&' && s[2] == '1'
1535 && s > cmd + 1 && s[-1] == '2' && isSPACE(s[-2])
1536 && (!s[3] || isSPACE(s[3])))
1540 while (*t && isSPACE(*t))
1542 if (!*t && (dup2(1,2) != -1)) {
1548 PerlProc_execl(PL_sh_path, "sh", "-c", cmd, (char*)0);
1553 New(402,PL_Argv, (s - cmd) / 2 + 2, char*);
1554 PL_Cmd = savepvn(cmd, s-cmd);
1556 for (s = PL_Cmd; *s;) {
1557 while (*s && isSPACE(*s)) s++;
1560 while (*s && !isSPACE(*s)) s++;
1566 PerlProc_execvp(PL_Argv[0],PL_Argv);
1567 if (errno == ENOEXEC) { /* for system V NIH syndrome */
1574 if (ckWARN(WARN_EXEC))
1575 Perl_warner(aTHX_ packWARN(WARN_EXEC), "Can't exec \"%s\": %s",
1576 PL_Argv[0], Strerror(errno));
1578 PerlLIO_write(fd, (void*)&e, sizeof(int));
1587 #endif /* OS2 || WIN32 */
1590 Perl_apply(pTHX_ I32 type, register SV **mark, register SV **sp)
1594 register I32 tot = 0;
1597 SV **oldmark = mark;
1600 #define APPLY_TAINT_PROPER() \
1602 if (PL_tainted) { TAINT_PROPER(what); } \
1605 /* This is a first heuristic; it doesn't catch tainting magic. */
1607 while (++mark <= sp) {
1608 if (SvTAINTED(*mark)) {
1618 APPLY_TAINT_PROPER();
1621 APPLY_TAINT_PROPER();
1623 while (++mark <= sp) {
1624 char *name = SvPVx(*mark, n_a);
1625 APPLY_TAINT_PROPER();
1626 if (PerlLIO_chmod(name, val))
1634 APPLY_TAINT_PROPER();
1635 if (sp - mark > 2) {
1636 val = SvIVx(*++mark);
1637 val2 = SvIVx(*++mark);
1638 APPLY_TAINT_PROPER();
1640 while (++mark <= sp) {
1641 char *name = SvPVx(*mark, n_a);
1642 APPLY_TAINT_PROPER();
1643 if (PerlLIO_chown(name, val, val2))
1650 XXX Should we make lchown() directly available from perl?
1651 For now, we'll let Configure test for HAS_LCHOWN, but do
1652 nothing in the core.
1658 APPLY_TAINT_PROPER();
1661 s = SvPVx(*++mark, n_a);
1663 if (*s == 'S' && s[1] == 'I' && s[2] == 'G')
1665 if ((val = whichsig(s)) < 0)
1666 Perl_croak(aTHX_ "Unrecognized signal name \"%s\"",s);
1670 APPLY_TAINT_PROPER();
1673 /* kill() doesn't do process groups (job trees?) under VMS */
1674 if (val < 0) val = -val;
1675 if (val == SIGKILL) {
1676 # include <starlet.h>
1677 /* Use native sys$delprc() to insure that target process is
1678 * deleted; supervisor-mode images don't pay attention to
1679 * CRTL's emulation of Unix-style signals and kill()
1681 while (++mark <= sp) {
1682 I32 proc = SvIVx(*mark);
1683 register unsigned long int __vmssts;
1684 APPLY_TAINT_PROPER();
1685 if (!((__vmssts = sys$delprc(&proc,0)) & 1)) {
1689 case SS$_NOSUCHNODE:
1690 SETERRNO(ESRCH,__vmssts);
1693 SETERRNO(EPERM,__vmssts);
1696 SETERRNO(EVMSERR,__vmssts);
1705 while (++mark <= sp) {
1706 I32 proc = SvIVx(*mark);
1707 APPLY_TAINT_PROPER();
1709 if (PerlProc_killpg(proc,val)) /* BSD */
1711 if (PerlProc_kill(-proc,val)) /* SYSV */
1717 while (++mark <= sp) {
1718 I32 proc = SvIVx(*mark);
1719 APPLY_TAINT_PROPER();
1720 if (PerlProc_kill(proc, val))
1728 APPLY_TAINT_PROPER();
1730 while (++mark <= sp) {
1731 s = SvPVx(*mark, n_a);
1732 APPLY_TAINT_PROPER();
1733 if (PL_euid || PL_unsafe) {
1737 else { /* don't let root wipe out directories without -U */
1738 if (PerlLIO_lstat(s,&PL_statbuf) < 0 || S_ISDIR(PL_statbuf.st_mode))
1750 APPLY_TAINT_PROPER();
1751 if (sp - mark > 2) {
1752 #if defined(I_UTIME) || defined(VMS)
1753 struct utimbuf utbuf;
1761 SV* accessed = *++mark;
1762 SV* modified = *++mark;
1763 void * utbufp = &utbuf;
1765 /* Be like C, and if both times are undefined, let the C
1766 * library figure out what to do. This usually means
1767 * "current time". */
1769 if ( accessed == &PL_sv_undef && modified == &PL_sv_undef )
1772 Zero(&utbuf, sizeof utbuf, char);
1774 utbuf.actime = (Time_t)SvNVx(accessed); /* time accessed */
1775 utbuf.modtime = (Time_t)SvNVx(modified); /* time modified */
1777 utbuf.actime = (Time_t)SvIVx(accessed); /* time accessed */
1778 utbuf.modtime = (Time_t)SvIVx(modified); /* time modified */
1781 APPLY_TAINT_PROPER();
1783 while (++mark <= sp) {
1784 char *name = SvPVx(*mark, n_a);
1785 APPLY_TAINT_PROPER();
1786 if (PerlLIO_utime(name, utbufp))
1797 #undef APPLY_TAINT_PROPER
1800 /* Do the permissions allow some operation? Assumes statcache already set. */
1801 #ifndef VMS /* VMS' cando is in vms.c */
1803 Perl_cando(pTHX_ Mode_t mode, Uid_t effective, register Stat_t *statbufp)
1804 /* Note: we use `effective' both for uids and gids.
1805 * Here we are betting on Uid_t being equal or wider than Gid_t. */
1808 /* [Comments and code from Len Reed]
1809 * MS-DOS "user" is similar to UNIX's "superuser," but can't write
1810 * to write-protected files. The execute permission bit is set
1811 * by the Miscrosoft C library stat() function for the following:
1816 * All files and directories are readable.
1817 * Directories and special files, e.g. "CON", cannot be
1819 * [Comment by Tom Dinger -- a directory can have the write-protect
1820 * bit set in the file system, but DOS permits changes to
1821 * the directory anyway. In addition, all bets are off
1822 * here for networked software, such as Novell and
1826 /* Atari stat() does pretty much the same thing. we set x_bit_set_in_stat
1827 * too so it will actually look into the files for magic numbers
1829 return (mode & statbufp->st_mode) ? TRUE : FALSE;
1831 #else /* ! DOSISH */
1832 if ((effective ? PL_euid : PL_uid) == 0) { /* root is special */
1833 if (mode == S_IXUSR) {
1834 if (statbufp->st_mode & 0111 || S_ISDIR(statbufp->st_mode))
1838 return TRUE; /* root reads and writes anything */
1841 if (statbufp->st_uid == (effective ? PL_euid : PL_uid) ) {
1842 if (statbufp->st_mode & mode)
1843 return TRUE; /* ok as "user" */
1845 else if (ingroup(statbufp->st_gid,effective)) {
1846 if (statbufp->st_mode & mode >> 3)
1847 return TRUE; /* ok as "group" */
1849 else if (statbufp->st_mode & mode >> 6)
1850 return TRUE; /* ok as "other" */
1852 #endif /* ! DOSISH */
1857 Perl_ingroup(pTHX_ Gid_t testgid, Uid_t effective)
1859 #ifdef MACOS_TRADITIONAL
1860 /* This is simply not correct for AppleShare, but fix it yerself. */
1863 if (testgid == (effective ? PL_egid : PL_gid))
1865 #ifdef HAS_GETGROUPS
1870 Groups_t gary[NGROUPS];
1873 anum = getgroups(NGROUPS,gary);
1875 if (gary[anum] == testgid)
1883 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
1886 Perl_do_ipcget(pTHX_ I32 optype, SV **mark, SV **sp)
1891 key = (key_t)SvNVx(*++mark);
1892 n = (optype == OP_MSGGET) ? 0 : SvIVx(*++mark);
1893 flags = SvIVx(*++mark);
1899 return msgget(key, flags);
1903 return semget(key, n, flags);
1907 return shmget(key, n, flags);
1909 #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
1911 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
1914 return -1; /* should never happen */
1918 Perl_do_ipcctl(pTHX_ I32 optype, SV **mark, SV **sp)
1922 I32 id, n, cmd, infosize, getinfo;
1925 id = SvIVx(*++mark);
1926 n = (optype == OP_SEMCTL) ? SvIVx(*++mark) : 0;
1927 cmd = SvIVx(*++mark);
1930 getinfo = (cmd == IPC_STAT);
1936 if (cmd == IPC_STAT || cmd == IPC_SET)
1937 infosize = sizeof(struct msqid_ds);
1942 if (cmd == IPC_STAT || cmd == IPC_SET)
1943 infosize = sizeof(struct shmid_ds);
1949 if (cmd == IPC_STAT || cmd == IPC_SET)
1950 infosize = sizeof(struct semid_ds);
1951 else if (cmd == GETALL || cmd == SETALL)
1953 struct semid_ds semds;
1955 #ifdef EXTRA_F_IN_SEMUN_BUF
1956 semun.buff = &semds;
1960 getinfo = (cmd == GETALL);
1961 if (Semctl(id, 0, IPC_STAT, semun) == -1)
1963 infosize = semds.sem_nsems * sizeof(short);
1964 /* "short" is technically wrong but much more portable
1965 than guessing about u_?short(_t)? */
1968 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
1972 #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
1974 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
1983 SvPV_force(astr, len);
1984 a = SvGROW(astr, infosize+1);
1988 a = SvPV(astr, len);
1989 if (len != infosize)
1990 Perl_croak(aTHX_ "Bad arg length for %s, is %lu, should be %ld",
1999 a = INT2PTR(char *,i); /* ouch */
2006 ret = msgctl(id, cmd, (struct msqid_ds *)a);
2012 union semun unsemds;
2014 #ifdef EXTRA_F_IN_SEMUN_BUF
2015 unsemds.buff = (struct semid_ds *)a;
2017 unsemds.buf = (struct semid_ds *)a;
2019 ret = Semctl(id, n, cmd, unsemds);
2021 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
2028 ret = shmctl(id, cmd, (struct shmid_ds *)a);
2032 if (getinfo && ret >= 0) {
2033 SvCUR_set(astr, infosize);
2034 *SvEND(astr) = '\0';
2041 Perl_do_msgsnd(pTHX_ SV **mark, SV **sp)
2046 I32 id, msize, flags;
2049 id = SvIVx(*++mark);
2051 flags = SvIVx(*++mark);
2052 mbuf = SvPV(mstr, len);
2053 if ((msize = len - sizeof(long)) < 0)
2054 Perl_croak(aTHX_ "Arg too short for msgsnd");
2056 return msgsnd(id, (struct msgbuf *)mbuf, msize, flags);
2058 Perl_croak(aTHX_ "msgsnd not implemented");
2063 Perl_do_msgrcv(pTHX_ SV **mark, SV **sp)
2069 I32 id, msize, flags, ret;
2072 id = SvIVx(*++mark);
2074 /* suppress warning when reading into undef var --jhi */
2076 sv_setpvn(mstr, "", 0);
2077 msize = SvIVx(*++mark);
2078 mtype = (long)SvIVx(*++mark);
2079 flags = SvIVx(*++mark);
2080 SvPV_force(mstr, len);
2081 mbuf = SvGROW(mstr, sizeof(long)+msize+1);
2084 ret = msgrcv(id, (struct msgbuf *)mbuf, msize, mtype, flags);
2086 SvCUR_set(mstr, sizeof(long)+ret);
2087 *SvEND(mstr) = '\0';
2088 #ifndef INCOMPLETE_TAINTS
2089 /* who knows who has been playing with this message? */
2095 Perl_croak(aTHX_ "msgrcv not implemented");
2100 Perl_do_semop(pTHX_ SV **mark, SV **sp)
2108 id = SvIVx(*++mark);
2110 opbuf = SvPV(opstr, opsize);
2111 if (opsize < 3 * SHORTSIZE
2112 || (opsize % (3 * SHORTSIZE))) {
2113 SETERRNO(EINVAL,LIB_INVARG);
2117 /* We can't assume that sizeof(struct sembuf) == 3 * sizeof(short). */
2119 int nsops = opsize / (3 * sizeof (short));
2121 short *ops = (short *) opbuf;
2123 struct sembuf *temps, *t;
2126 New (0, temps, nsops, struct sembuf);
2134 result = semop(id, temps, nsops);
2148 Perl_croak(aTHX_ "semop not implemented");
2153 Perl_do_shmio(pTHX_ I32 optype, SV **mark, SV **sp)
2158 I32 id, mpos, msize;
2160 struct shmid_ds shmds;
2162 id = SvIVx(*++mark);
2164 mpos = SvIVx(*++mark);
2165 msize = SvIVx(*++mark);
2167 if (shmctl(id, IPC_STAT, &shmds) == -1)
2169 if (mpos < 0 || msize < 0 || mpos + msize > shmds.shm_segsz) {
2170 SETERRNO(EFAULT,SS_ACCVIO); /* can't do as caller requested */
2173 shm = (char *)shmat(id, (char*)NULL, (optype == OP_SHMREAD) ? SHM_RDONLY : 0);
2174 if (shm == (char *)-1) /* I hate System V IPC, I really do */
2176 if (optype == OP_SHMREAD) {
2177 /* suppress warning when reading into undef var (tchrist 3/Mar/00) */
2179 sv_setpvn(mstr, "", 0);
2180 SvPV_force(mstr, len);
2181 mbuf = SvGROW(mstr, msize+1);
2183 Copy(shm + mpos, mbuf, msize, char);
2184 SvCUR_set(mstr, msize);
2185 *SvEND(mstr) = '\0';
2187 #ifndef INCOMPLETE_TAINTS
2188 /* who knows who has been playing with this shared memory? */
2195 mbuf = SvPV(mstr, len);
2196 if ((n = len) > msize)
2198 Copy(mbuf, shm + mpos, n, char);
2200 memzero(shm + mpos + n, msize - n);
2204 Perl_croak(aTHX_ "shm I/O not implemented");
2208 #endif /* SYSV IPC */
2213 =for apidoc start_glob
2215 Function called by C<do_readline> to spawn a glob (or do the glob inside
2216 perl on VMS). This code used to be inline, but now perl uses C<File::Glob>
2217 this glob starter is only used by miniperl during the build process.
2218 Moving it away shrinks pp_hot.c; shrinking pp_hot.c helps speed perl up.
2224 Perl_start_glob (pTHX_ SV *tmpglob, IO *io)
2226 SV *tmpcmd = NEWSV(55, 0);
2230 #ifdef VMS /* expand the wildcards right here, rather than opening a pipe, */
2231 /* since spawning off a process is a real performance hit */
2233 #include <descrip.h>
2234 #include <lib$routines.h>
2237 char rslt[NAM$C_MAXRSS+1+sizeof(unsigned short int)] = {'\0','\0'};
2238 char vmsspec[NAM$C_MAXRSS+1];
2239 char *rstr = rslt + sizeof(unsigned short int), *begin, *end, *cp;
2240 $DESCRIPTOR(dfltdsc,"SYS$DISK:[]*.*;");
2243 struct dsc$descriptor_s wilddsc
2244 = {0, DSC$K_DTYPE_T, DSC$K_CLASS_S, 0};
2245 struct dsc$descriptor_vs rsdsc
2246 = {sizeof rslt, DSC$K_DTYPE_VT, DSC$K_CLASS_VS, rslt};
2247 unsigned long int cxt = 0, sts = 0, ok = 1, hasdir = 0, hasver = 0, isunix = 0;
2249 /* We could find out if there's an explicit dev/dir or version
2250 by peeking into lib$find_file's internal context at
2251 ((struct NAM *)((struct FAB *)cxt)->fab$l_nam)->nam$l_fnb
2252 but that's unsupported, so I don't want to do it now and
2253 have it bite someone in the future. */
2254 cp = SvPV(tmpglob,i);
2256 if (cp[i] == ';') hasver = 1;
2258 if (sts) hasver = 1;
2262 hasdir = isunix = 1;
2265 if (cp[i] == ']' || cp[i] == '>' || cp[i] == ':') {
2270 if ((tmpfp = PerlIO_tmpfile()) != NULL) {
2272 if (!PerlLIO_stat(SvPVX(tmpglob),&st) && S_ISDIR(st.st_mode))
2273 ok = ((wilddsc.dsc$a_pointer = tovmspath(SvPVX(tmpglob),vmsspec)) != NULL);
2274 else ok = ((wilddsc.dsc$a_pointer = tovmsspec(SvPVX(tmpglob),vmsspec)) != NULL);
2275 if (ok) wilddsc.dsc$w_length = (unsigned short int) strlen(wilddsc.dsc$a_pointer);
2276 for (cp=wilddsc.dsc$a_pointer; ok && cp && *cp; cp++)
2277 if (*cp == '?') *cp = '%'; /* VMS style single-char wildcard */
2278 while (ok && ((sts = lib$find_file(&wilddsc,&rsdsc,&cxt,
2279 &dfltdsc,NULL,NULL,NULL))&1)) {
2280 end = rstr + (unsigned long int) *rslt;
2281 if (!hasver) while (*end != ';') end--;
2282 *(end++) = '\n'; *end = '\0';
2283 for (cp = rstr; *cp; cp++) *cp = _tolower(*cp);
2285 if (isunix) trim_unixpath(rstr,SvPVX(tmpglob),1);
2290 while (*(--begin) != ']' && *begin != '>') ;
2293 ok = (PerlIO_puts(tmpfp,begin) != EOF);
2295 if (cxt) (void)lib$find_file_end(&cxt);
2296 if (ok && sts != RMS$_NMF &&
2297 sts != RMS$_DNF && sts != RMS_FNF) ok = 0;
2300 SETERRNO((sts == RMS$_SYN ? EINVAL : EVMSERR),sts);
2302 PerlIO_close(tmpfp);
2306 PerlIO_rewind(tmpfp);
2307 IoTYPE(io) = IoTYPE_RDONLY;
2308 IoIFP(io) = fp = tmpfp;
2309 IoFLAGS(io) &= ~IOf_UNTAINT; /* maybe redundant */
2314 #ifdef MACOS_TRADITIONAL
2315 sv_setpv(tmpcmd, "glob ");
2316 sv_catsv(tmpcmd, tmpglob);
2317 sv_catpv(tmpcmd, " |");
2321 sv_setpv(tmpcmd, "for a in ");
2322 sv_catsv(tmpcmd, tmpglob);
2323 sv_catpv(tmpcmd, "; do echo \"$a\\0\\c\"; done |");
2326 sv_setpv(tmpcmd, "/dev/dosglob/"); /* File System Extension */
2327 sv_catsv(tmpcmd, tmpglob);
2329 sv_setpv(tmpcmd, "perlglob ");
2330 sv_catsv(tmpcmd, tmpglob);
2331 sv_catpv(tmpcmd, " |");
2336 sv_setpvn(tmpcmd, PL_cshname, PL_cshlen);
2337 sv_catpv(tmpcmd, " -cf 'set nonomatch; glob ");
2338 sv_catsv(tmpcmd, tmpglob);
2339 sv_catpv(tmpcmd, "' 2>/dev/null |");
2341 sv_setpv(tmpcmd, "echo ");
2342 sv_catsv(tmpcmd, tmpglob);
2344 sv_catpv(tmpcmd, "|tr -s ' \t\f\r' '\\012\\012\\012\\012'|");
2346 sv_catpv(tmpcmd, "|tr -s ' \t\f\r' '\\n\\n\\n\\n'|");
2349 #endif /* !DOSISH */
2350 #endif /* MACOS_TRADITIONAL */
2351 (void)do_open(PL_last_in_gv, SvPVX(tmpcmd), SvCUR(tmpcmd),
2352 FALSE, O_RDONLY, 0, Nullfp);