3 * Copyright (c) 1991-2002, 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 */
145 #ifdef O_APPEND /* Not fully portable. */
148 #ifdef O_TRUNC /* Not fully portable. */
153 O_WRONLY|O_RDWR|O_CREAT|appendtrunc;
157 Perl_croak(aTHX_ "panic: sysopen with multiple args");
165 It might be (in OS/390 and Mac OS Classic it is)
171 This means that simple & with O_RDWR would look
172 like O_RDONLY is present. Therefore we have to
175 if ((ismodifying = (rawmode & modifyingmode))) {
176 if ((ismodifying & O_WRONLY) == O_WRONLY ||
177 (ismodifying & O_RDWR) == O_RDWR ||
178 (ismodifying & (O_CREAT|appendtrunc)))
179 TAINT_PROPER("sysopen");
181 mode[ix++] = '#'; /* Marker to openn to use numeric "sysopen" */
183 #if defined(USE_64_BIT_RAWIO) && defined(O_LARGEFILE)
184 rawmode |= O_LARGEFILE; /* Transparently largefiley. */
187 IoTYPE(io) = PerlIO_intmode2str(rawmode, &mode[ix], &writing);
189 namesv = sv_2mortal(newSVpvn(name,strlen(name)));
193 fp = PerlIO_openn(aTHX_ type, mode, -1, rawmode, rawperm, NULL, num_svs, svp);
196 /* Regular (non-sys) open */
201 PerlIO *that_fp = NULL;
203 type = savepvn(name, len);
207 /* Lose leading and trailing white space */
209 for (; isSPACE(*type); type++) ;
210 while (tend > type && isSPACE(tend[-1]))
214 /* New style explict name, type is just mode and discipline/layer info */
217 if (SvROK(*svp) && !strchr(name,'&')) {
219 Perl_warner(aTHX_ packWARN(WARN_IO),
220 "Can't open a reference");
221 SETERRNO(EINVAL, LIB$_INVARG);
224 #endif /* USE_STDIO */
225 name = SvOK(*svp) ? SvPV(*svp, l) : "";
227 name = savepvn(name, len);
235 if ((*type == IoTYPE_RDWR) && /* scary */
236 (*(type+1) == IoTYPE_RDONLY || *(type+1) == IoTYPE_WRONLY) &&
237 ((!num_svs || (tend > type+1 && tend[-1] != IoTYPE_PIPE)))) {
238 TAINT_PROPER("open");
243 if (*type == IoTYPE_PIPE) {
245 if (type[1] != IoTYPE_STD) {
247 Perl_croak(aTHX_ "Unknown open() mode '%.*s'", (int)olen, oname);
252 for (type++; isSPACE(*type); type++) ;
258 /* command is missing 19990114 */
259 if (ckWARN(WARN_PIPE))
260 Perl_warner(aTHX_ packWARN(WARN_PIPE), "Missing command in piped open");
264 if (strNE(name,"-") || num_svs)
266 TAINT_PROPER("piped open");
267 if (!num_svs && name[len-1] == '|') {
269 if (ckWARN(WARN_PIPE))
270 Perl_warner(aTHX_ packWARN(WARN_PIPE), "Can't open bidirectional pipe");
279 fp = PerlProc_popen_list(mode, num_svs, svp);
282 fp = PerlProc_popen(name,mode);
286 if (PerlIO_apply_layers(aTHX_ fp, mode, type) != 0) {
292 else if (*type == IoTYPE_WRONLY) {
293 TAINT_PROPER("open");
295 if (*type == IoTYPE_WRONLY) {
296 /* Two IoTYPE_WRONLYs in a row make for an IoTYPE_APPEND. */
297 mode[0] = IoTYPE(io) = IoTYPE_APPEND;
312 dodup = PERLIO_DUP_FD;
318 if (!num_svs && !*type && supplied_fp) {
319 /* "<+&" etc. is used by typemaps */
324 Perl_croak(aTHX_ "More than one argument to '%c&' open",IoTYPE(io));
326 if (num_svs && (SvIOK(*svp) || (SvPOK(*svp) && looks_like_number(*svp)))) {
329 else if (isDIGIT(*type)) {
331 for (; isSPACE(*type); type++) ;
337 thatio = sv_2io(*svp);
342 for (; isSPACE(*type); type++) ;
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 opened only for input",
540 (fp == PerlIO_stdout()) ? "OUT" : "ERR");
542 else if ((IoTYPE(io) == IoTYPE_WRONLY) && fp == PerlIO_stdin()) {
543 Perl_warner(aTHX_ packWARN(WARN_IO),
544 "Filehandle STDIN opened only for output");
548 fd = PerlIO_fileno(fp);
549 /* If there is no fd (e.g. PerlIO::Scalar) assume it isn't a
550 * socket - this covers PerlIO::Scalar - otherwise unless we "know" the
551 * type probe for socket-ness.
553 if (IoTYPE(io) && IoTYPE(io) != IoTYPE_PIPE && IoTYPE(io) != IoTYPE_STD && fd >= 0) {
554 if (PerlLIO_fstat(fd,&PL_statbuf) < 0) {
555 /* If PerlIO claims to have fd we had better be able to fstat() it. */
556 (void) PerlIO_close(fp);
560 if (S_ISSOCK(PL_statbuf.st_mode))
561 IoTYPE(io) = IoTYPE_SOCKET; /* in case a socket was passed in to us */
565 !(PL_statbuf.st_mode & S_IFMT)
569 && IoTYPE(io) != IoTYPE_WRONLY /* Dups of STD* filehandles already have */
570 && IoTYPE(io) != IoTYPE_RDONLY /* type so they aren't marked as sockets */
571 ) { /* on OS's that return 0 on fstat()ed pipe */
573 Sock_size_t buflen = sizeof tmpbuf;
574 if (PerlSock_getsockname(fd, (struct sockaddr *)tmpbuf, &buflen) >= 0
575 || errno != ENOTSOCK)
576 IoTYPE(io) = IoTYPE_SOCKET; /* some OS's return 0 on fstat()ed socket */
577 /* but some return 0 for streams too, sigh */
579 #endif /* HAS_SOCKET */
580 #endif /* !PERL_MICRO */
584 * If this is a standard handle we discard all the layer stuff
585 * and just dup the fd into whatever was on the handle before !
588 if (saveifp) { /* must use old fp? */
589 /* If fd is less that PL_maxsysfd i.e. STDIN..STDERR
590 then dup the new fileno down
593 PerlIO_flush(saveofp); /* emulate PerlIO_close() */
594 if (saveofp != saveifp) { /* was a socket? */
595 PerlIO_close(saveofp);
599 /* Still a small can-of-worms here if (say) PerlIO::Scalar
600 is assigned to (say) STDOUT - for now let dup2() fail
601 and provide the error
603 if (PerlLIO_dup2(fd, savefd) < 0) {
604 (void)PerlIO_close(fp);
608 if (savefd != PerlIO_fileno(PerlIO_stdin())) {
609 char newname[FILENAME_MAX+1];
610 if (PerlIO_getname(fp, newname)) {
611 if (fd == PerlIO_fileno(PerlIO_stdout()))
612 Perl_vmssetuserlnm(aTHX_ "SYS$OUTPUT", newname);
613 if (fd == PerlIO_fileno(PerlIO_stderr()))
614 Perl_vmssetuserlnm(aTHX_ "SYS$ERROR", newname);
620 /* PL_fdpid isn't used on Windows, so avoid this useless work.
621 * XXX Probably the same for a lot of other places. */
627 sv = *av_fetch(PL_fdpid,fd,TRUE);
628 (void)SvUPGRADE(sv, SVt_IV);
631 sv = *av_fetch(PL_fdpid,savefd,TRUE);
632 (void)SvUPGRADE(sv, SVt_IV);
639 /* need to close fp without closing underlying fd */
640 int ofd = PerlIO_fileno(fp);
641 int dupfd = PerlLIO_dup(ofd);
643 PerlLIO_dup2(dupfd,ofd);
644 PerlLIO_close(dupfd);
651 fd = PerlIO_fileno(fp);
653 #if defined(HAS_FCNTL) && defined(F_SETFD)
655 int save_errno = errno;
656 fcntl(fd,F_SETFD,fd > PL_maxsysfd); /* can change errno */
662 IoFLAGS(io) &= ~IOf_NOLINE;
664 if (IoTYPE(io) == IoTYPE_SOCKET
665 || (IoTYPE(io) == IoTYPE_WRONLY && fd >= 0 && S_ISCHR(PL_statbuf.st_mode)) ) {
667 if (!(IoOFP(io) = PerlIO_openn(aTHX_ type,mode,fd,0,0,NULL,0,svp))) {
681 IoTYPE(io) = savetype;
686 Perl_nextargv(pTHX_ register GV *gv)
689 #ifndef FLEXFILENAMES
698 PL_argvoutgv = gv_fetchpv("ARGVOUT",TRUE,SVt_PVIO);
699 if (io && (IoFLAGS(io) & IOf_ARGV) && (IoFLAGS(io) & IOf_START)) {
700 IoFLAGS(io) &= ~IOf_START;
702 if (!PL_argvout_stack)
703 PL_argvout_stack = newAV();
704 av_push(PL_argvout_stack, SvREFCNT_inc(PL_defoutgv));
707 if (PL_filemode & (S_ISUID|S_ISGID)) {
708 PerlIO_flush(IoIFP(GvIOn(PL_argvoutgv))); /* chmod must follow last write */
710 (void)fchmod(PL_lastfd,PL_filemode);
712 (void)PerlLIO_chmod(PL_oldname,PL_filemode);
716 while (av_len(GvAV(gv)) >= 0) {
718 sv = av_shift(GvAV(gv));
720 sv_setsv(GvSV(gv),sv);
721 SvSETMAGIC(GvSV(gv));
722 PL_oldname = SvPVx(GvSV(gv), oldlen);
723 if (do_open(gv,PL_oldname,oldlen,PL_inplace!=0,O_RDONLY,0,Nullfp)) {
725 TAINT_PROPER("inplace open");
726 if (oldlen == 1 && *PL_oldname == '-') {
727 setdefout(gv_fetchpv("STDOUT",TRUE,SVt_PVIO));
728 return IoIFP(GvIOp(gv));
730 #ifndef FLEXFILENAMES
731 filedev = PL_statbuf.st_dev;
732 fileino = PL_statbuf.st_ino;
734 PL_filemode = PL_statbuf.st_mode;
735 fileuid = PL_statbuf.st_uid;
736 filegid = PL_statbuf.st_gid;
737 if (!S_ISREG(PL_filemode)) {
738 if (ckWARN_d(WARN_INPLACE))
739 Perl_warner(aTHX_ packWARN(WARN_INPLACE),
740 "Can't do inplace edit: %s is not a regular file",
746 char *star = strchr(PL_inplace, '*');
748 char *begin = PL_inplace;
749 sv_setpvn(sv, "", 0);
751 sv_catpvn(sv, begin, star - begin);
752 sv_catpvn(sv, PL_oldname, oldlen);
754 } while ((star = strchr(begin, '*')));
759 sv_catpv(sv,PL_inplace);
761 #ifndef FLEXFILENAMES
762 if ((PerlLIO_stat(SvPVX(sv),&PL_statbuf) >= 0
763 && PL_statbuf.st_dev == filedev
764 && PL_statbuf.st_ino == fileino)
766 || ((_djstat_fail_bits & _STFAIL_TRUENAME)!=0)
770 if (ckWARN_d(WARN_INPLACE))
771 Perl_warner(aTHX_ packWARN(WARN_INPLACE),
772 "Can't do inplace edit: %s would not be unique",
779 #if !defined(DOSISH) && !defined(__CYGWIN__) && !defined(EPOC)
780 if (PerlLIO_rename(PL_oldname,SvPVX(sv)) < 0) {
781 if (ckWARN_d(WARN_INPLACE))
782 Perl_warner(aTHX_ packWARN(WARN_INPLACE),
783 "Can't rename %s to %s: %s, skipping file",
784 PL_oldname, SvPVX(sv), Strerror(errno) );
790 (void)PerlLIO_unlink(SvPVX(sv));
791 (void)PerlLIO_rename(PL_oldname,SvPVX(sv));
792 do_open(gv,SvPVX(sv),SvCUR(sv),PL_inplace!=0,O_RDONLY,0,Nullfp);
795 (void)UNLINK(SvPVX(sv));
796 if (link(PL_oldname,SvPVX(sv)) < 0) {
797 if (ckWARN_d(WARN_INPLACE))
798 Perl_warner(aTHX_ packWARN(WARN_INPLACE),
799 "Can't rename %s to %s: %s, skipping file",
800 PL_oldname, SvPVX(sv), Strerror(errno) );
804 (void)UNLINK(PL_oldname);
808 #if !defined(DOSISH) && !defined(AMIGAOS)
809 # ifndef VMS /* Don't delete; use automatic file versioning */
810 if (UNLINK(PL_oldname) < 0) {
811 if (ckWARN_d(WARN_INPLACE))
812 Perl_warner(aTHX_ packWARN(WARN_INPLACE),
813 "Can't remove %s: %s, skipping file",
814 PL_oldname, Strerror(errno) );
820 Perl_croak(aTHX_ "Can't do inplace edit without backup");
824 sv_setpvn(sv,">",!PL_inplace);
825 sv_catpvn(sv,PL_oldname,oldlen);
826 SETERRNO(0,0); /* in case sprintf set errno */
828 if (!do_open(PL_argvoutgv,SvPVX(sv),SvCUR(sv),PL_inplace!=0,
829 O_WRONLY|O_CREAT|O_TRUNC,0,Nullfp))
831 if (!do_open(PL_argvoutgv,SvPVX(sv),SvCUR(sv),PL_inplace!=0,
832 O_WRONLY|O_CREAT|OPEN_EXCL,0666,Nullfp))
835 if (ckWARN_d(WARN_INPLACE))
836 Perl_warner(aTHX_ packWARN(WARN_INPLACE), "Can't do inplace edit on %s: %s",
837 PL_oldname, Strerror(errno) );
841 setdefout(PL_argvoutgv);
842 PL_lastfd = PerlIO_fileno(IoIFP(GvIOp(PL_argvoutgv)));
843 (void)PerlLIO_fstat(PL_lastfd,&PL_statbuf);
845 (void)fchmod(PL_lastfd,PL_filemode);
847 # if !(defined(WIN32) && defined(__BORLANDC__))
848 /* Borland runtime creates a readonly file! */
849 (void)PerlLIO_chmod(PL_oldname,PL_filemode);
852 if (fileuid != PL_statbuf.st_uid || filegid != PL_statbuf.st_gid) {
854 (void)fchown(PL_lastfd,fileuid,filegid);
857 (void)PerlLIO_chown(PL_oldname,fileuid,filegid);
862 return IoIFP(GvIOp(gv));
865 if (ckWARN_d(WARN_INPLACE)) {
867 if (PerlLIO_stat(PL_oldname, &PL_statbuf) >= 0
868 && !S_ISREG(PL_statbuf.st_mode))
870 Perl_warner(aTHX_ packWARN(WARN_INPLACE),
871 "Can't do inplace edit: %s is not a regular file",
875 Perl_warner(aTHX_ packWARN(WARN_INPLACE), "Can't open %s: %s",
876 PL_oldname, Strerror(eno));
880 if (io && (IoFLAGS(io) & IOf_ARGV))
881 IoFLAGS(io) |= IOf_START;
883 (void)do_close(PL_argvoutgv,FALSE);
884 if (io && (IoFLAGS(io) & IOf_ARGV)
885 && PL_argvout_stack && AvFILLp(PL_argvout_stack) >= 0)
887 GV *oldout = (GV*)av_pop(PL_argvout_stack);
889 SvREFCNT_dec(oldout);
892 setdefout(gv_fetchpv("STDOUT",TRUE,SVt_PVIO));
899 Perl_do_pipe(pTHX_ SV *sv, GV *rgv, GV *wgv)
918 if (PerlProc_pipe(fd) < 0)
920 IoIFP(rstio) = PerlIO_fdopen(fd[0], "r");
921 IoOFP(wstio) = PerlIO_fdopen(fd[1], "w");
922 IoOFP(rstio) = IoIFP(rstio);
923 IoIFP(wstio) = IoOFP(wstio);
924 IoTYPE(rstio) = IoTYPE_RDONLY;
925 IoTYPE(wstio) = IoTYPE_WRONLY;
926 if (!IoIFP(rstio) || !IoOFP(wstio)) {
927 if (IoIFP(rstio)) PerlIO_close(IoIFP(rstio));
928 else PerlLIO_close(fd[0]);
929 if (IoOFP(wstio)) PerlIO_close(IoOFP(wstio));
930 else PerlLIO_close(fd[1]);
934 sv_setsv(sv,&PL_sv_yes);
938 sv_setsv(sv,&PL_sv_undef);
943 /* explicit renamed to avoid C++ conflict -- kja */
945 Perl_do_close(pTHX_ GV *gv, bool not_implicit)
952 if (!gv || SvTYPE(gv) != SVt_PVGV) {
954 SETERRNO(EBADF,SS$_IVCHAN);
958 if (!io) { /* never opened */
960 if (ckWARN(WARN_UNOPENED)) /* no check for closed here */
961 report_evil_fh(gv, io, PL_op->op_type);
962 SETERRNO(EBADF,SS$_IVCHAN);
966 retval = io_close(io, not_implicit);
970 IoLINES_LEFT(io) = IoPAGE_LEN(io);
972 IoTYPE(io) = IoTYPE_CLOSED;
977 Perl_io_close(pTHX_ IO *io, bool not_implicit)
983 if (IoTYPE(io) == IoTYPE_PIPE) {
984 status = PerlProc_pclose(IoIFP(io));
986 STATUS_NATIVE_SET(status);
987 retval = (STATUS_POSIX == 0);
990 retval = (status != -1);
993 else if (IoTYPE(io) == IoTYPE_STD)
996 if (IoOFP(io) && IoOFP(io) != IoIFP(io)) { /* a socket */
997 retval = (PerlIO_close(IoOFP(io)) != EOF);
998 PerlIO_close(IoIFP(io)); /* clear stdio, fd already closed */
1001 retval = (PerlIO_close(IoIFP(io)) != EOF);
1003 IoOFP(io) = IoIFP(io) = Nullfp;
1005 else if (not_implicit) {
1006 SETERRNO(EBADF,SS$_IVCHAN);
1013 Perl_do_eof(pTHX_ GV *gv)
1022 else if (ckWARN(WARN_IO) && (IoTYPE(io) == IoTYPE_WRONLY))
1023 report_evil_fh(gv, io, OP_phoney_OUTPUT_ONLY);
1027 if (PerlIO_has_cntptr(IoIFP(io))) { /* (the code works without this) */
1028 if (PerlIO_get_cnt(IoIFP(io)) > 0) /* cheat a little, since */
1029 return FALSE; /* this is the most usual case */
1032 ch = PerlIO_getc(IoIFP(io));
1034 (void)PerlIO_ungetc(IoIFP(io),ch);
1038 if (PerlIO_has_cntptr(IoIFP(io)) && PerlIO_canset_cnt(IoIFP(io))) {
1039 if (PerlIO_get_cnt(IoIFP(io)) < -1)
1040 PerlIO_set_cnt(IoIFP(io),-1);
1042 if (PL_op->op_flags & OPf_SPECIAL) { /* not necessarily a real EOF yet? */
1043 if (gv != PL_argvgv || !nextargv(gv)) /* get another fp handy */
1047 return TRUE; /* normal fp, definitely end of file */
1053 Perl_do_tell(pTHX_ GV *gv)
1055 register IO *io = 0;
1056 register PerlIO *fp;
1058 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io))) {
1059 #ifdef ULTRIX_STDIO_BOTCH
1061 (void)PerlIO_seek(fp, 0L, 2); /* ultrix 1.2 workaround */
1063 return PerlIO_tell(fp);
1065 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1066 report_evil_fh(gv, io, PL_op->op_type);
1067 SETERRNO(EBADF,RMS$_IFI);
1072 Perl_do_seek(pTHX_ GV *gv, Off_t pos, int whence)
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_seek(fp, pos, whence) >= 0;
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_sysseek(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 return PerlLIO_lseek(PerlIO_fileno(fp), pos, whence);
1098 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1099 report_evil_fh(gv, io, PL_op->op_type);
1100 SETERRNO(EBADF,RMS$_IFI);
1105 Perl_mode_from_discipline(pTHX_ SV *discp)
1107 int mode = O_BINARY;
1110 char *s = SvPV(discp,len);
1115 if (len > 3 && strnEQ(s+1, "raw", 3)
1116 && (!s[4] || s[4] == ':' || isSPACE(s[4])))
1125 if (len > 4 && strnEQ(s+1, "crlf", 4)
1126 && (!s[5] || s[5] == ':' || isSPACE(s[5])))
1135 goto fail_discipline;
1138 else if (isSPACE(*s)) {
1145 end = strchr(s+1, ':');
1148 #ifndef PERLIO_LAYERS
1149 Perl_croak(aTHX_ "Unknown discipline '%.*s'", end-s, s);
1160 Perl_do_binmode(pTHX_ PerlIO *fp, int iotype, int mode)
1162 /* The old body of this is now in non-LAYER part of perlio.c
1163 * This is a stub for any XS code which might have been calling it.
1165 char *name = ":raw";
1166 #ifdef PERLIO_USING_CRLF
1167 if (!(mode & O_BINARY))
1170 return PerlIO_binmode(aTHX_ fp, iotype, mode, name);
1173 #if !defined(HAS_TRUNCATE) && !defined(HAS_CHSIZE) && defined(F_FREESP)
1174 /* code courtesy of William Kucharski */
1177 I32 my_chsize(fd, length)
1178 I32 fd; /* file descriptor */
1179 Off_t length; /* length to set file to */
1184 if (PerlLIO_fstat(fd, &filebuf) < 0)
1187 if (filebuf.st_size < length) {
1189 /* extend file length */
1191 if ((PerlLIO_lseek(fd, (length - 1), 0)) < 0)
1194 /* write a "0" byte */
1196 if ((PerlLIO_write(fd, "", 1)) != 1)
1200 /* truncate length */
1204 fl.l_start = length;
1205 fl.l_type = F_WRLCK; /* write lock on file space */
1208 * This relies on the UNDOCUMENTED F_FREESP argument to
1209 * fcntl(2), which truncates the file so that it ends at the
1210 * position indicated by fl.l_start.
1212 * Will minor miracles never cease?
1215 if (fcntl(fd, F_FREESP, &fl) < 0)
1222 #endif /* F_FREESP */
1225 Perl_do_print(pTHX_ register SV *sv, PerlIO *fp)
1227 register char *tmps;
1230 /* assuming fp is checked earlier */
1236 if (SvIOK(sv) && SvIVX(sv) != 0) {
1237 PerlIO_printf(fp, PL_ofmt, (NV)SvIVX(sv));
1238 return !PerlIO_error(fp);
1240 if ( (SvNOK(sv) && SvNVX(sv) != 0.0)
1241 || (looks_like_number(sv) && sv_2nv(sv) != 0.0) ) {
1242 PerlIO_printf(fp, PL_ofmt, SvNVX(sv));
1243 return !PerlIO_error(fp);
1246 switch (SvTYPE(sv)) {
1248 if (ckWARN(WARN_UNINITIALIZED))
1256 PerlIO_printf(fp, "%"UVuf, (UV)SvUVX(sv));
1258 PerlIO_printf(fp, "%"IVdf, (IV)SvIVX(sv));
1259 return !PerlIO_error(fp);
1263 if (PerlIO_isutf8(fp)) {
1265 sv_utf8_upgrade(sv = sv_mortalcopy(sv));
1267 else if (DO_UTF8(sv)) {
1268 if (!sv_utf8_downgrade((sv = sv_mortalcopy(sv)), TRUE)
1269 && ckWARN_d(WARN_UTF8))
1271 Perl_warner(aTHX_ packWARN(WARN_UTF8), "Wide character in print");
1274 tmps = SvPV(sv, len);
1277 /* To detect whether the process is about to overstep its
1278 * filesize limit we would need getrlimit(). We could then
1279 * also transparently raise the limit with setrlimit() --
1280 * but only until the system hard limit/the filesystem limit,
1281 * at which we would get EPERM. Note that when using buffered
1282 * io the write failure can be delayed until the flush/close. --jhi */
1283 if (len && (PerlIO_write(fp,tmps,len) == 0))
1285 return !PerlIO_error(fp);
1295 if (PL_op->op_flags & OPf_REF) {
1300 if (io && IoIFP(io)) {
1302 sv_setpv(PL_statname,"");
1303 PL_laststype = OP_STAT;
1304 return (PL_laststatval = PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache));
1308 return PL_laststatval;
1309 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1310 report_evil_fh(gv, io, PL_op->op_type);
1312 sv_setpv(PL_statname,"");
1313 return (PL_laststatval = -1);
1321 if (SvTYPE(sv) == SVt_PVGV) {
1325 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
1332 sv_setpv(PL_statname, s);
1333 PL_laststype = OP_STAT;
1334 PL_laststatval = PerlLIO_stat(s, &PL_statcache);
1335 if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(s, '\n'))
1336 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "stat");
1337 return PL_laststatval;
1347 if (PL_op->op_flags & OPf_REF) {
1349 if (cGVOP_gv == PL_defgv) {
1350 if (PL_laststype != OP_LSTAT)
1351 Perl_croak(aTHX_ "The stat preceding -l _ wasn't an lstat");
1352 return PL_laststatval;
1354 if (ckWARN(WARN_IO)) {
1355 Perl_warner(aTHX_ packWARN(WARN_IO), "Use of -l on filehandle %s",
1357 return (PL_laststatval = -1);
1361 PL_laststype = OP_LSTAT;
1365 if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV && ckWARN(WARN_IO)) {
1366 Perl_warner(aTHX_ packWARN(WARN_IO), "Use of -l on filehandle %s",
1367 GvENAME((GV*) SvRV(sv)));
1368 return (PL_laststatval = -1);
1370 sv_setpv(PL_statname,SvPV(sv, n_a));
1371 PL_laststatval = PerlLIO_lstat(SvPV(sv, n_a),&PL_statcache);
1372 if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(SvPV(sv, n_a), '\n'))
1373 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "lstat");
1374 return PL_laststatval;
1378 Perl_do_aexec(pTHX_ SV *really, register SV **mark, register SV **sp)
1380 return do_aexec5(really, mark, sp, 0, 0);
1384 Perl_do_aexec5(pTHX_ SV *really, register SV **mark, register SV **sp,
1385 int fd, int do_report)
1387 #ifdef MACOS_TRADITIONAL
1388 Perl_croak(aTHX_ "exec? I'm not *that* kind of operating system");
1391 char *tmps = Nullch;
1395 New(401,PL_Argv, sp - mark + 1, char*);
1397 while (++mark <= sp) {
1399 *a++ = SvPVx(*mark, n_a);
1405 tmps = SvPV(really, n_a);
1406 if ((!really && *PL_Argv[0] != '/') ||
1407 (really && *tmps != '/')) /* will execvp use PATH? */
1408 TAINT_ENV(); /* testing IFS here is overkill, probably */
1409 if (really && *tmps)
1410 PerlProc_execvp(tmps,EXEC_ARGV_CAST(PL_Argv));
1412 PerlProc_execvp(PL_Argv[0],EXEC_ARGV_CAST(PL_Argv));
1413 if (ckWARN(WARN_EXEC))
1414 Perl_warner(aTHX_ packWARN(WARN_EXEC), "Can't exec \"%s\": %s",
1415 (really ? tmps : PL_Argv[0]), Strerror(errno));
1419 PerlLIO_write(fd, (void*)&e, sizeof(int));
1429 Perl_do_execfree(pTHX)
1433 PL_Argv = Null(char **);
1441 #if !defined(OS2) && !defined(WIN32) && !defined(DJGPP) && !defined(EPOC) && !defined(MACOS_TRADITIONAL)
1444 Perl_do_exec(pTHX_ char *cmd)
1446 return do_exec3(cmd,0,0);
1450 Perl_do_exec3(pTHX_ char *cmd, int fd, int do_report)
1455 while (*cmd && isSPACE(*cmd))
1458 /* save an extra exec if possible */
1463 if (strnEQ(cmd,PL_cshname,PL_cshlen) &&
1464 strnEQ(cmd+PL_cshlen," -c",3)) {
1466 s = cmd+PL_cshlen+3;
1480 if (s[-1] == '\'') {
1482 PerlProc_execl(PL_cshname,"csh", flags, ncmd, (char*)0);
1491 /* see if there are shell metacharacters in it */
1493 if (*cmd == '.' && isSPACE(cmd[1]))
1496 if (strnEQ(cmd,"exec",4) && isSPACE(cmd[4]))
1499 for (s = cmd; *s && isALNUM(*s); s++) ; /* catch VAR=val gizmo */
1503 for (s = cmd; *s; s++) {
1504 if (*s != ' ' && !isALPHA(*s) &&
1505 strchr("$&*(){}[]'\";\\|?<>~`\n",*s)) {
1506 if (*s == '\n' && !s[1]) {
1510 /* handle the 2>&1 construct at the end */
1511 if (*s == '>' && s[1] == '&' && s[2] == '1'
1512 && s > cmd + 1 && s[-1] == '2' && isSPACE(s[-2])
1513 && (!s[3] || isSPACE(s[3])))
1517 while (*t && isSPACE(*t))
1519 if (!*t && (dup2(1,2) != -1)) {
1525 PerlProc_execl(PL_sh_path, "sh", "-c", cmd, (char*)0);
1530 New(402,PL_Argv, (s - cmd) / 2 + 2, char*);
1531 PL_Cmd = savepvn(cmd, s-cmd);
1533 for (s = PL_Cmd; *s;) {
1534 while (*s && isSPACE(*s)) s++;
1537 while (*s && !isSPACE(*s)) s++;
1543 PerlProc_execvp(PL_Argv[0],PL_Argv);
1544 if (errno == ENOEXEC) { /* for system V NIH syndrome */
1551 if (ckWARN(WARN_EXEC))
1552 Perl_warner(aTHX_ packWARN(WARN_EXEC), "Can't exec \"%s\": %s",
1553 PL_Argv[0], Strerror(errno));
1555 PerlLIO_write(fd, (void*)&e, sizeof(int));
1564 #endif /* OS2 || WIN32 */
1567 Perl_apply(pTHX_ I32 type, register SV **mark, register SV **sp)
1571 register I32 tot = 0;
1574 SV **oldmark = mark;
1577 #define APPLY_TAINT_PROPER() \
1579 if (PL_tainted) { TAINT_PROPER(what); } \
1582 /* This is a first heuristic; it doesn't catch tainting magic. */
1584 while (++mark <= sp) {
1585 if (SvTAINTED(*mark)) {
1595 APPLY_TAINT_PROPER();
1598 APPLY_TAINT_PROPER();
1600 while (++mark <= sp) {
1601 char *name = SvPVx(*mark, n_a);
1602 APPLY_TAINT_PROPER();
1603 if (PerlLIO_chmod(name, val))
1611 APPLY_TAINT_PROPER();
1612 if (sp - mark > 2) {
1613 val = SvIVx(*++mark);
1614 val2 = SvIVx(*++mark);
1615 APPLY_TAINT_PROPER();
1617 while (++mark <= sp) {
1618 char *name = SvPVx(*mark, n_a);
1619 APPLY_TAINT_PROPER();
1620 if (PerlLIO_chown(name, val, val2))
1627 XXX Should we make lchown() directly available from perl?
1628 For now, we'll let Configure test for HAS_LCHOWN, but do
1629 nothing in the core.
1635 APPLY_TAINT_PROPER();
1638 s = SvPVx(*++mark, n_a);
1640 if (*s == 'S' && s[1] == 'I' && s[2] == 'G')
1642 if (!(val = whichsig(s)))
1643 Perl_croak(aTHX_ "Unrecognized signal name \"%s\"",s);
1647 APPLY_TAINT_PROPER();
1650 /* kill() doesn't do process groups (job trees?) under VMS */
1651 if (val < 0) val = -val;
1652 if (val == SIGKILL) {
1653 # include <starlet.h>
1654 /* Use native sys$delprc() to insure that target process is
1655 * deleted; supervisor-mode images don't pay attention to
1656 * CRTL's emulation of Unix-style signals and kill()
1658 while (++mark <= sp) {
1659 I32 proc = SvIVx(*mark);
1660 register unsigned long int __vmssts;
1661 APPLY_TAINT_PROPER();
1662 if (!((__vmssts = sys$delprc(&proc,0)) & 1)) {
1666 case SS$_NOSUCHNODE:
1667 SETERRNO(ESRCH,__vmssts);
1670 SETERRNO(EPERM,__vmssts);
1673 SETERRNO(EVMSERR,__vmssts);
1682 while (++mark <= sp) {
1683 I32 proc = SvIVx(*mark);
1684 APPLY_TAINT_PROPER();
1686 if (PerlProc_killpg(proc,val)) /* BSD */
1688 if (PerlProc_kill(-proc,val)) /* SYSV */
1694 while (++mark <= sp) {
1695 I32 proc = SvIVx(*mark);
1696 APPLY_TAINT_PROPER();
1697 if (PerlProc_kill(proc, val))
1705 APPLY_TAINT_PROPER();
1707 while (++mark <= sp) {
1708 s = SvPVx(*mark, n_a);
1709 APPLY_TAINT_PROPER();
1710 if (PL_euid || PL_unsafe) {
1714 else { /* don't let root wipe out directories without -U */
1715 if (PerlLIO_lstat(s,&PL_statbuf) < 0 || S_ISDIR(PL_statbuf.st_mode))
1727 APPLY_TAINT_PROPER();
1728 if (sp - mark > 2) {
1729 #if defined(I_UTIME) || defined(VMS)
1730 struct utimbuf utbuf;
1738 SV* accessed = *++mark;
1739 SV* modified = *++mark;
1740 void * utbufp = &utbuf;
1742 /* be like C, and if both times are undefined, let the C
1743 library figure out what to do. This usually means
1746 if ( accessed == &PL_sv_undef && modified == &PL_sv_undef )
1749 Zero(&utbuf, sizeof utbuf, char);
1751 utbuf.actime = (Time_t)SvNVx(accessed); /* time accessed */
1752 utbuf.modtime = (Time_t)SvNVx(modified); /* time modified */
1754 utbuf.actime = (Time_t)SvIVx(accessed); /* time accessed */
1755 utbuf.modtime = (Time_t)SvIVx(modified); /* time modified */
1757 APPLY_TAINT_PROPER();
1759 while (++mark <= sp) {
1760 char *name = SvPVx(*mark, n_a);
1761 APPLY_TAINT_PROPER();
1762 if (PerlLIO_utime(name, utbufp))
1773 #undef APPLY_TAINT_PROPER
1776 /* Do the permissions allow some operation? Assumes statcache already set. */
1777 #ifndef VMS /* VMS' cando is in vms.c */
1779 Perl_cando(pTHX_ Mode_t mode, Uid_t effective, register Stat_t *statbufp)
1780 /* Note: we use `effective' both for uids and gids.
1781 * Here we are betting on Uid_t being equal or wider than Gid_t. */
1784 /* [Comments and code from Len Reed]
1785 * MS-DOS "user" is similar to UNIX's "superuser," but can't write
1786 * to write-protected files. The execute permission bit is set
1787 * by the Miscrosoft C library stat() function for the following:
1792 * All files and directories are readable.
1793 * Directories and special files, e.g. "CON", cannot be
1795 * [Comment by Tom Dinger -- a directory can have the write-protect
1796 * bit set in the file system, but DOS permits changes to
1797 * the directory anyway. In addition, all bets are off
1798 * here for networked software, such as Novell and
1802 /* Atari stat() does pretty much the same thing. we set x_bit_set_in_stat
1803 * too so it will actually look into the files for magic numbers
1805 return (mode & statbufp->st_mode) ? TRUE : FALSE;
1807 #else /* ! DOSISH */
1808 if ((effective ? PL_euid : PL_uid) == 0) { /* root is special */
1809 if (mode == S_IXUSR) {
1810 if (statbufp->st_mode & 0111 || S_ISDIR(statbufp->st_mode))
1814 return TRUE; /* root reads and writes anything */
1817 if (statbufp->st_uid == (effective ? PL_euid : PL_uid) ) {
1818 if (statbufp->st_mode & mode)
1819 return TRUE; /* ok as "user" */
1821 else if (ingroup(statbufp->st_gid,effective)) {
1822 if (statbufp->st_mode & mode >> 3)
1823 return TRUE; /* ok as "group" */
1825 else if (statbufp->st_mode & mode >> 6)
1826 return TRUE; /* ok as "other" */
1828 #endif /* ! DOSISH */
1833 Perl_ingroup(pTHX_ Gid_t testgid, Uid_t effective)
1835 #ifdef MACOS_TRADITIONAL
1836 /* This is simply not correct for AppleShare, but fix it yerself. */
1839 if (testgid == (effective ? PL_egid : PL_gid))
1841 #ifdef HAS_GETGROUPS
1846 Groups_t gary[NGROUPS];
1849 anum = getgroups(NGROUPS,gary);
1851 if (gary[anum] == testgid)
1859 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
1862 Perl_do_ipcget(pTHX_ I32 optype, SV **mark, SV **sp)
1867 key = (key_t)SvNVx(*++mark);
1868 n = (optype == OP_MSGGET) ? 0 : SvIVx(*++mark);
1869 flags = SvIVx(*++mark);
1875 return msgget(key, flags);
1879 return semget(key, n, flags);
1883 return shmget(key, n, flags);
1885 #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
1887 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
1890 return -1; /* should never happen */
1894 Perl_do_ipcctl(pTHX_ I32 optype, SV **mark, SV **sp)
1898 I32 id, n, cmd, infosize, getinfo;
1901 id = SvIVx(*++mark);
1902 n = (optype == OP_SEMCTL) ? SvIVx(*++mark) : 0;
1903 cmd = SvIVx(*++mark);
1906 getinfo = (cmd == IPC_STAT);
1912 if (cmd == IPC_STAT || cmd == IPC_SET)
1913 infosize = sizeof(struct msqid_ds);
1918 if (cmd == IPC_STAT || cmd == IPC_SET)
1919 infosize = sizeof(struct shmid_ds);
1925 if (cmd == IPC_STAT || cmd == IPC_SET)
1926 infosize = sizeof(struct semid_ds);
1927 else if (cmd == GETALL || cmd == SETALL)
1929 struct semid_ds semds;
1931 #ifdef EXTRA_F_IN_SEMUN_BUF
1932 semun.buff = &semds;
1936 getinfo = (cmd == GETALL);
1937 if (Semctl(id, 0, IPC_STAT, semun) == -1)
1939 infosize = semds.sem_nsems * sizeof(short);
1940 /* "short" is technically wrong but much more portable
1941 than guessing about u_?short(_t)? */
1944 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
1948 #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
1950 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
1959 SvPV_force(astr, len);
1960 a = SvGROW(astr, infosize+1);
1964 a = SvPV(astr, len);
1965 if (len != infosize)
1966 Perl_croak(aTHX_ "Bad arg length for %s, is %lu, should be %ld",
1975 a = INT2PTR(char *,i); /* ouch */
1982 ret = msgctl(id, cmd, (struct msqid_ds *)a);
1988 union semun unsemds;
1990 #ifdef EXTRA_F_IN_SEMUN_BUF
1991 unsemds.buff = (struct semid_ds *)a;
1993 unsemds.buf = (struct semid_ds *)a;
1995 ret = Semctl(id, n, cmd, unsemds);
1997 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
2004 ret = shmctl(id, cmd, (struct shmid_ds *)a);
2008 if (getinfo && ret >= 0) {
2009 SvCUR_set(astr, infosize);
2010 *SvEND(astr) = '\0';
2017 Perl_do_msgsnd(pTHX_ SV **mark, SV **sp)
2022 I32 id, msize, flags;
2025 id = SvIVx(*++mark);
2027 flags = SvIVx(*++mark);
2028 mbuf = SvPV(mstr, len);
2029 if ((msize = len - sizeof(long)) < 0)
2030 Perl_croak(aTHX_ "Arg too short for msgsnd");
2032 return msgsnd(id, (struct msgbuf *)mbuf, msize, flags);
2034 Perl_croak(aTHX_ "msgsnd not implemented");
2039 Perl_do_msgrcv(pTHX_ SV **mark, SV **sp)
2045 I32 id, msize, flags, ret;
2048 id = SvIVx(*++mark);
2050 /* suppress warning when reading into undef var --jhi */
2052 sv_setpvn(mstr, "", 0);
2053 msize = SvIVx(*++mark);
2054 mtype = (long)SvIVx(*++mark);
2055 flags = SvIVx(*++mark);
2056 SvPV_force(mstr, len);
2057 mbuf = SvGROW(mstr, sizeof(long)+msize+1);
2060 ret = msgrcv(id, (struct msgbuf *)mbuf, msize, mtype, flags);
2062 SvCUR_set(mstr, sizeof(long)+ret);
2063 *SvEND(mstr) = '\0';
2064 #ifndef INCOMPLETE_TAINTS
2065 /* who knows who has been playing with this message? */
2071 Perl_croak(aTHX_ "msgrcv not implemented");
2076 Perl_do_semop(pTHX_ SV **mark, SV **sp)
2084 id = SvIVx(*++mark);
2086 opbuf = SvPV(opstr, opsize);
2087 if (opsize < 3 * SHORTSIZE
2088 || (opsize % (3 * SHORTSIZE))) {
2089 SETERRNO(EINVAL,LIB$_INVARG);
2093 /* We can't assume that sizeof(struct sembuf) == 3 * sizeof(short). */
2095 int nsops = opsize / (3 * sizeof (short));
2097 short *ops = (short *) opbuf;
2099 struct sembuf *temps, *t;
2102 New (0, temps, nsops, struct sembuf);
2110 result = semop(id, temps, nsops);
2124 Perl_croak(aTHX_ "semop not implemented");
2129 Perl_do_shmio(pTHX_ I32 optype, SV **mark, SV **sp)
2134 I32 id, mpos, msize;
2136 struct shmid_ds shmds;
2138 id = SvIVx(*++mark);
2140 mpos = SvIVx(*++mark);
2141 msize = SvIVx(*++mark);
2143 if (shmctl(id, IPC_STAT, &shmds) == -1)
2145 if (mpos < 0 || msize < 0 || mpos + msize > shmds.shm_segsz) {
2146 SETERRNO(EFAULT,SS$_ACCVIO); /* can't do as caller requested */
2149 shm = (char *)shmat(id, (char*)NULL, (optype == OP_SHMREAD) ? SHM_RDONLY : 0);
2150 if (shm == (char *)-1) /* I hate System V IPC, I really do */
2152 if (optype == OP_SHMREAD) {
2153 /* suppress warning when reading into undef var (tchrist 3/Mar/00) */
2155 sv_setpvn(mstr, "", 0);
2156 SvPV_force(mstr, len);
2157 mbuf = SvGROW(mstr, msize+1);
2159 Copy(shm + mpos, mbuf, msize, char);
2160 SvCUR_set(mstr, msize);
2161 *SvEND(mstr) = '\0';
2163 #ifndef INCOMPLETE_TAINTS
2164 /* who knows who has been playing with this shared memory? */
2171 mbuf = SvPV(mstr, len);
2172 if ((n = len) > msize)
2174 Copy(mbuf, shm + mpos, n, char);
2176 memzero(shm + mpos + n, msize - n);
2180 Perl_croak(aTHX_ "shm I/O not implemented");
2184 #endif /* SYSV IPC */
2189 =for apidoc start_glob
2191 Function called by C<do_readline> to spawn a glob (or do the glob inside
2192 perl on VMS). This code used to be inline, but now perl uses C<File::Glob>
2193 this glob starter is only used by miniperl during the build process.
2194 Moving it away shrinks pp_hot.c; shrinking pp_hot.c helps speed perl up.
2200 Perl_start_glob (pTHX_ SV *tmpglob, IO *io)
2202 SV *tmpcmd = NEWSV(55, 0);
2206 #ifdef VMS /* expand the wildcards right here, rather than opening a pipe, */
2207 /* since spawning off a process is a real performance hit */
2209 #include <descrip.h>
2210 #include <lib$routines.h>
2213 char rslt[NAM$C_MAXRSS+1+sizeof(unsigned short int)] = {'\0','\0'};
2214 char vmsspec[NAM$C_MAXRSS+1];
2215 char *rstr = rslt + sizeof(unsigned short int), *begin, *end, *cp;
2216 $DESCRIPTOR(dfltdsc,"SYS$DISK:[]*.*;");
2219 struct dsc$descriptor_s wilddsc
2220 = {0, DSC$K_DTYPE_T, DSC$K_CLASS_S, 0};
2221 struct dsc$descriptor_vs rsdsc
2222 = {sizeof rslt, DSC$K_DTYPE_VT, DSC$K_CLASS_VS, rslt};
2223 unsigned long int cxt = 0, sts = 0, ok = 1, hasdir = 0, hasver = 0, isunix = 0;
2225 /* We could find out if there's an explicit dev/dir or version
2226 by peeking into lib$find_file's internal context at
2227 ((struct NAM *)((struct FAB *)cxt)->fab$l_nam)->nam$l_fnb
2228 but that's unsupported, so I don't want to do it now and
2229 have it bite someone in the future. */
2230 cp = SvPV(tmpglob,i);
2232 if (cp[i] == ';') hasver = 1;
2234 if (sts) hasver = 1;
2238 hasdir = isunix = 1;
2241 if (cp[i] == ']' || cp[i] == '>' || cp[i] == ':') {
2246 if ((tmpfp = PerlIO_tmpfile()) != NULL) {
2248 if (!PerlLIO_stat(SvPVX(tmpglob),&st) && S_ISDIR(st.st_mode))
2249 ok = ((wilddsc.dsc$a_pointer = tovmspath(SvPVX(tmpglob),vmsspec)) != NULL);
2250 else ok = ((wilddsc.dsc$a_pointer = tovmsspec(SvPVX(tmpglob),vmsspec)) != NULL);
2251 if (ok) wilddsc.dsc$w_length = (unsigned short int) strlen(wilddsc.dsc$a_pointer);
2252 for (cp=wilddsc.dsc$a_pointer; ok && cp && *cp; cp++)
2253 if (*cp == '?') *cp = '%'; /* VMS style single-char wildcard */
2254 while (ok && ((sts = lib$find_file(&wilddsc,&rsdsc,&cxt,
2255 &dfltdsc,NULL,NULL,NULL))&1)) {
2256 end = rstr + (unsigned long int) *rslt;
2257 if (!hasver) while (*end != ';') end--;
2258 *(end++) = '\n'; *end = '\0';
2259 for (cp = rstr; *cp; cp++) *cp = _tolower(*cp);
2261 if (isunix) trim_unixpath(rstr,SvPVX(tmpglob),1);
2266 while (*(--begin) != ']' && *begin != '>') ;
2269 ok = (PerlIO_puts(tmpfp,begin) != EOF);
2271 if (cxt) (void)lib$find_file_end(&cxt);
2272 if (ok && sts != RMS$_NMF &&
2273 sts != RMS$_DNF && sts != RMS$_FNF) ok = 0;
2276 SETERRNO((sts == RMS$_SYN ? EINVAL : EVMSERR),sts);
2278 PerlIO_close(tmpfp);
2282 PerlIO_rewind(tmpfp);
2283 IoTYPE(io) = IoTYPE_RDONLY;
2284 IoIFP(io) = fp = tmpfp;
2285 IoFLAGS(io) &= ~IOf_UNTAINT; /* maybe redundant */
2290 #ifdef MACOS_TRADITIONAL
2291 sv_setpv(tmpcmd, "glob ");
2292 sv_catsv(tmpcmd, tmpglob);
2293 sv_catpv(tmpcmd, " |");
2297 sv_setpv(tmpcmd, "for a in ");
2298 sv_catsv(tmpcmd, tmpglob);
2299 sv_catpv(tmpcmd, "; do echo \"$a\\0\\c\"; done |");
2302 sv_setpv(tmpcmd, "/dev/dosglob/"); /* File System Extension */
2303 sv_catsv(tmpcmd, tmpglob);
2305 sv_setpv(tmpcmd, "perlglob ");
2306 sv_catsv(tmpcmd, tmpglob);
2307 sv_catpv(tmpcmd, " |");
2312 sv_setpvn(tmpcmd, PL_cshname, PL_cshlen);
2313 sv_catpv(tmpcmd, " -cf 'set nonomatch; glob ");
2314 sv_catsv(tmpcmd, tmpglob);
2315 sv_catpv(tmpcmd, "' 2>/dev/null |");
2317 sv_setpv(tmpcmd, "echo ");
2318 sv_catsv(tmpcmd, tmpglob);
2320 sv_catpv(tmpcmd, "|tr -s ' \t\f\r' '\\012\\012\\012\\012'|");
2322 sv_catpv(tmpcmd, "|tr -s ' \t\f\r' '\\n\\n\\n\\n'|");
2325 #endif /* !DOSISH */
2326 #endif /* MACOS_TRADITIONAL */
2327 (void)do_open(PL_last_in_gv, SvPVX(tmpcmd), SvCUR(tmpcmd),
2328 FALSE, O_RDONLY, 0, Nullfp);