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 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);
645 PerlLIO_dup2(dupfd,ofd);
646 PerlLIO_close(dupfd);
653 fd = PerlIO_fileno(fp);
655 #if defined(HAS_FCNTL) && defined(F_SETFD)
657 int save_errno = errno;
658 fcntl(fd,F_SETFD,fd > PL_maxsysfd); /* can change errno */
664 IoFLAGS(io) &= ~IOf_NOLINE;
666 if (IoTYPE(io) == IoTYPE_SOCKET
667 || (IoTYPE(io) == IoTYPE_WRONLY && fd >= 0 && S_ISCHR(PL_statbuf.st_mode)) ) {
669 if (*s == 'I' || *s == '#')
672 if (!(IoOFP(io) = PerlIO_openn(aTHX_ type,s,fd,0,0,NULL,0,svp))) {
686 IoTYPE(io) = savetype;
691 Perl_nextargv(pTHX_ register GV *gv)
694 #ifndef FLEXFILENAMES
703 PL_argvoutgv = gv_fetchpv("ARGVOUT",TRUE,SVt_PVIO);
704 if (io && (IoFLAGS(io) & IOf_ARGV) && (IoFLAGS(io) & IOf_START)) {
705 IoFLAGS(io) &= ~IOf_START;
707 if (!PL_argvout_stack)
708 PL_argvout_stack = newAV();
709 av_push(PL_argvout_stack, SvREFCNT_inc(PL_defoutgv));
712 if (PL_filemode & (S_ISUID|S_ISGID)) {
713 PerlIO_flush(IoIFP(GvIOn(PL_argvoutgv))); /* chmod must follow last write */
715 (void)fchmod(PL_lastfd,PL_filemode);
717 (void)PerlLIO_chmod(PL_oldname,PL_filemode);
721 while (av_len(GvAV(gv)) >= 0) {
723 sv = av_shift(GvAV(gv));
725 sv_setsv(GvSV(gv),sv);
726 SvSETMAGIC(GvSV(gv));
727 PL_oldname = SvPVx(GvSV(gv), oldlen);
728 if (do_open(gv,PL_oldname,oldlen,PL_inplace!=0,O_RDONLY,0,Nullfp)) {
730 TAINT_PROPER("inplace open");
731 if (oldlen == 1 && *PL_oldname == '-') {
732 setdefout(gv_fetchpv("STDOUT",TRUE,SVt_PVIO));
733 return IoIFP(GvIOp(gv));
735 #ifndef FLEXFILENAMES
736 filedev = PL_statbuf.st_dev;
737 fileino = PL_statbuf.st_ino;
739 PL_filemode = PL_statbuf.st_mode;
740 fileuid = PL_statbuf.st_uid;
741 filegid = PL_statbuf.st_gid;
742 if (!S_ISREG(PL_filemode)) {
743 if (ckWARN_d(WARN_INPLACE))
744 Perl_warner(aTHX_ packWARN(WARN_INPLACE),
745 "Can't do inplace edit: %s is not a regular file",
751 char *star = strchr(PL_inplace, '*');
753 char *begin = PL_inplace;
754 sv_setpvn(sv, "", 0);
756 sv_catpvn(sv, begin, star - begin);
757 sv_catpvn(sv, PL_oldname, oldlen);
759 } while ((star = strchr(begin, '*')));
764 sv_catpv(sv,PL_inplace);
766 #ifndef FLEXFILENAMES
767 if ((PerlLIO_stat(SvPVX(sv),&PL_statbuf) >= 0
768 && PL_statbuf.st_dev == filedev
769 && PL_statbuf.st_ino == fileino)
771 || ((_djstat_fail_bits & _STFAIL_TRUENAME)!=0)
775 if (ckWARN_d(WARN_INPLACE))
776 Perl_warner(aTHX_ packWARN(WARN_INPLACE),
777 "Can't do inplace edit: %s would not be unique",
784 #if !defined(DOSISH) && !defined(__CYGWIN__) && !defined(EPOC)
785 if (PerlLIO_rename(PL_oldname,SvPVX(sv)) < 0) {
786 if (ckWARN_d(WARN_INPLACE))
787 Perl_warner(aTHX_ packWARN(WARN_INPLACE),
788 "Can't rename %s to %s: %s, skipping file",
789 PL_oldname, SvPVX(sv), Strerror(errno) );
795 (void)PerlLIO_unlink(SvPVX(sv));
796 (void)PerlLIO_rename(PL_oldname,SvPVX(sv));
797 do_open(gv,SvPVX(sv),SvCUR(sv),PL_inplace!=0,O_RDONLY,0,Nullfp);
800 (void)UNLINK(SvPVX(sv));
801 if (link(PL_oldname,SvPVX(sv)) < 0) {
802 if (ckWARN_d(WARN_INPLACE))
803 Perl_warner(aTHX_ packWARN(WARN_INPLACE),
804 "Can't rename %s to %s: %s, skipping file",
805 PL_oldname, SvPVX(sv), Strerror(errno) );
809 (void)UNLINK(PL_oldname);
813 #if !defined(DOSISH) && !defined(AMIGAOS)
814 # ifndef VMS /* Don't delete; use automatic file versioning */
815 if (UNLINK(PL_oldname) < 0) {
816 if (ckWARN_d(WARN_INPLACE))
817 Perl_warner(aTHX_ packWARN(WARN_INPLACE),
818 "Can't remove %s: %s, skipping file",
819 PL_oldname, Strerror(errno) );
825 Perl_croak(aTHX_ "Can't do inplace edit without backup");
829 sv_setpvn(sv,">",!PL_inplace);
830 sv_catpvn(sv,PL_oldname,oldlen);
831 SETERRNO(0,0); /* in case sprintf set errno */
833 if (!do_open(PL_argvoutgv,SvPVX(sv),SvCUR(sv),PL_inplace!=0,
834 O_WRONLY|O_CREAT|O_TRUNC,0,Nullfp))
836 if (!do_open(PL_argvoutgv,SvPVX(sv),SvCUR(sv),PL_inplace!=0,
837 O_WRONLY|O_CREAT|OPEN_EXCL,0666,Nullfp))
840 if (ckWARN_d(WARN_INPLACE))
841 Perl_warner(aTHX_ packWARN(WARN_INPLACE), "Can't do inplace edit on %s: %s",
842 PL_oldname, Strerror(errno) );
846 setdefout(PL_argvoutgv);
847 PL_lastfd = PerlIO_fileno(IoIFP(GvIOp(PL_argvoutgv)));
848 (void)PerlLIO_fstat(PL_lastfd,&PL_statbuf);
850 (void)fchmod(PL_lastfd,PL_filemode);
852 # if !(defined(WIN32) && defined(__BORLANDC__))
853 /* Borland runtime creates a readonly file! */
854 (void)PerlLIO_chmod(PL_oldname,PL_filemode);
857 if (fileuid != PL_statbuf.st_uid || filegid != PL_statbuf.st_gid) {
859 (void)fchown(PL_lastfd,fileuid,filegid);
862 (void)PerlLIO_chown(PL_oldname,fileuid,filegid);
867 return IoIFP(GvIOp(gv));
870 if (ckWARN_d(WARN_INPLACE)) {
872 if (PerlLIO_stat(PL_oldname, &PL_statbuf) >= 0
873 && !S_ISREG(PL_statbuf.st_mode))
875 Perl_warner(aTHX_ packWARN(WARN_INPLACE),
876 "Can't do inplace edit: %s is not a regular file",
880 Perl_warner(aTHX_ packWARN(WARN_INPLACE), "Can't open %s: %s",
881 PL_oldname, Strerror(eno));
885 if (io && (IoFLAGS(io) & IOf_ARGV))
886 IoFLAGS(io) |= IOf_START;
888 (void)do_close(PL_argvoutgv,FALSE);
889 if (io && (IoFLAGS(io) & IOf_ARGV)
890 && PL_argvout_stack && AvFILLp(PL_argvout_stack) >= 0)
892 GV *oldout = (GV*)av_pop(PL_argvout_stack);
894 SvREFCNT_dec(oldout);
897 setdefout(gv_fetchpv("STDOUT",TRUE,SVt_PVIO));
904 Perl_do_pipe(pTHX_ SV *sv, GV *rgv, GV *wgv)
923 if (PerlProc_pipe(fd) < 0)
925 IoIFP(rstio) = PerlIO_fdopen(fd[0], "r");
926 IoOFP(wstio) = PerlIO_fdopen(fd[1], "w");
927 IoOFP(rstio) = IoIFP(rstio);
928 IoIFP(wstio) = IoOFP(wstio);
929 IoTYPE(rstio) = IoTYPE_RDONLY;
930 IoTYPE(wstio) = IoTYPE_WRONLY;
931 if (!IoIFP(rstio) || !IoOFP(wstio)) {
932 if (IoIFP(rstio)) PerlIO_close(IoIFP(rstio));
933 else PerlLIO_close(fd[0]);
934 if (IoOFP(wstio)) PerlIO_close(IoOFP(wstio));
935 else PerlLIO_close(fd[1]);
939 sv_setsv(sv,&PL_sv_yes);
943 sv_setsv(sv,&PL_sv_undef);
948 /* explicit renamed to avoid C++ conflict -- kja */
950 Perl_do_close(pTHX_ GV *gv, bool not_implicit)
957 if (!gv || SvTYPE(gv) != SVt_PVGV) {
959 SETERRNO(EBADF,SS_IVCHAN);
963 if (!io) { /* never opened */
965 if (ckWARN(WARN_UNOPENED)) /* no check for closed here */
966 report_evil_fh(gv, io, PL_op->op_type);
967 SETERRNO(EBADF,SS_IVCHAN);
971 retval = io_close(io, not_implicit);
975 IoLINES_LEFT(io) = IoPAGE_LEN(io);
977 IoTYPE(io) = IoTYPE_CLOSED;
982 Perl_io_close(pTHX_ IO *io, bool not_implicit)
988 if (IoTYPE(io) == IoTYPE_PIPE) {
989 status = PerlProc_pclose(IoIFP(io));
991 STATUS_NATIVE_SET(status);
992 retval = (STATUS_POSIX == 0);
995 retval = (status != -1);
998 else if (IoTYPE(io) == IoTYPE_STD)
1001 if (IoOFP(io) && IoOFP(io) != IoIFP(io)) { /* a socket */
1002 retval = (PerlIO_close(IoOFP(io)) != EOF);
1003 PerlIO_close(IoIFP(io)); /* clear stdio, fd already closed */
1006 retval = (PerlIO_close(IoIFP(io)) != EOF);
1008 IoOFP(io) = IoIFP(io) = Nullfp;
1010 else if (not_implicit) {
1011 SETERRNO(EBADF,SS_IVCHAN);
1018 Perl_do_eof(pTHX_ GV *gv)
1027 else if (ckWARN(WARN_IO) && (IoTYPE(io) == IoTYPE_WRONLY))
1028 report_evil_fh(gv, io, OP_phoney_OUTPUT_ONLY);
1032 if (PerlIO_has_cntptr(IoIFP(io))) { /* (the code works without this) */
1033 if (PerlIO_get_cnt(IoIFP(io)) > 0) /* cheat a little, since */
1034 return FALSE; /* this is the most usual case */
1037 ch = PerlIO_getc(IoIFP(io));
1039 (void)PerlIO_ungetc(IoIFP(io),ch);
1043 if (PerlIO_has_cntptr(IoIFP(io)) && PerlIO_canset_cnt(IoIFP(io))) {
1044 if (PerlIO_get_cnt(IoIFP(io)) < -1)
1045 PerlIO_set_cnt(IoIFP(io),-1);
1047 if (PL_op->op_flags & OPf_SPECIAL) { /* not necessarily a real EOF yet? */
1048 if (gv != PL_argvgv || !nextargv(gv)) /* get another fp handy */
1052 return TRUE; /* normal fp, definitely end of file */
1058 Perl_do_tell(pTHX_ GV *gv)
1060 register IO *io = 0;
1061 register PerlIO *fp;
1063 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io))) {
1064 #ifdef ULTRIX_STDIO_BOTCH
1066 (void)PerlIO_seek(fp, 0L, 2); /* ultrix 1.2 workaround */
1068 return PerlIO_tell(fp);
1070 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1071 report_evil_fh(gv, io, PL_op->op_type);
1072 SETERRNO(EBADF,RMS_IFI);
1077 Perl_do_seek(pTHX_ GV *gv, Off_t pos, int whence)
1079 register IO *io = 0;
1080 register PerlIO *fp;
1082 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io))) {
1083 #ifdef ULTRIX_STDIO_BOTCH
1085 (void)PerlIO_seek(fp, 0L, 2); /* ultrix 1.2 workaround */
1087 return PerlIO_seek(fp, pos, whence) >= 0;
1089 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1090 report_evil_fh(gv, io, PL_op->op_type);
1091 SETERRNO(EBADF,RMS_IFI);
1096 Perl_do_sysseek(pTHX_ GV *gv, Off_t pos, int whence)
1098 register IO *io = 0;
1099 register PerlIO *fp;
1101 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io)))
1102 return PerlLIO_lseek(PerlIO_fileno(fp), pos, whence);
1103 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1104 report_evil_fh(gv, io, PL_op->op_type);
1105 SETERRNO(EBADF,RMS_IFI);
1110 Perl_mode_from_discipline(pTHX_ SV *discp)
1112 int mode = O_BINARY;
1115 char *s = SvPV(discp,len);
1120 if (len > 3 && strnEQ(s+1, "raw", 3)
1121 && (!s[4] || s[4] == ':' || isSPACE(s[4])))
1130 if (len > 4 && strnEQ(s+1, "crlf", 4)
1131 && (!s[5] || s[5] == ':' || isSPACE(s[5])))
1140 goto fail_discipline;
1143 else if (isSPACE(*s)) {
1150 end = strchr(s+1, ':');
1153 #ifndef PERLIO_LAYERS
1154 Perl_croak(aTHX_ "Unknown discipline '%.*s'", end-s, s);
1165 Perl_do_binmode(pTHX_ PerlIO *fp, int iotype, int mode)
1167 /* The old body of this is now in non-LAYER part of perlio.c
1168 * This is a stub for any XS code which might have been calling it.
1170 char *name = ":raw";
1171 #ifdef PERLIO_USING_CRLF
1172 if (!(mode & O_BINARY))
1175 return PerlIO_binmode(aTHX_ fp, iotype, mode, name);
1178 #if !defined(HAS_TRUNCATE) && !defined(HAS_CHSIZE) && defined(F_FREESP)
1179 /* code courtesy of William Kucharski */
1182 I32 my_chsize(fd, length)
1183 I32 fd; /* file descriptor */
1184 Off_t length; /* length to set file to */
1189 if (PerlLIO_fstat(fd, &filebuf) < 0)
1192 if (filebuf.st_size < length) {
1194 /* extend file length */
1196 if ((PerlLIO_lseek(fd, (length - 1), 0)) < 0)
1199 /* write a "0" byte */
1201 if ((PerlLIO_write(fd, "", 1)) != 1)
1205 /* truncate length */
1209 fl.l_start = length;
1210 fl.l_type = F_WRLCK; /* write lock on file space */
1213 * This relies on the UNDOCUMENTED F_FREESP argument to
1214 * fcntl(2), which truncates the file so that it ends at the
1215 * position indicated by fl.l_start.
1217 * Will minor miracles never cease?
1220 if (fcntl(fd, F_FREESP, &fl) < 0)
1227 #endif /* F_FREESP */
1230 Perl_do_print(pTHX_ register SV *sv, PerlIO *fp)
1232 register char *tmps;
1235 /* assuming fp is checked earlier */
1241 if (SvIOK(sv) && SvIVX(sv) != 0) {
1242 PerlIO_printf(fp, PL_ofmt, (NV)SvIVX(sv));
1243 return !PerlIO_error(fp);
1245 if ( (SvNOK(sv) && SvNVX(sv) != 0.0)
1246 || (looks_like_number(sv) && sv_2nv(sv) != 0.0) ) {
1247 PerlIO_printf(fp, PL_ofmt, SvNVX(sv));
1248 return !PerlIO_error(fp);
1251 switch (SvTYPE(sv)) {
1253 if (ckWARN(WARN_UNINITIALIZED))
1261 PerlIO_printf(fp, "%"UVuf, (UV)SvUVX(sv));
1263 PerlIO_printf(fp, "%"IVdf, (IV)SvIVX(sv));
1264 return !PerlIO_error(fp);
1268 if (PerlIO_isutf8(fp)) {
1270 sv_utf8_upgrade(sv = sv_mortalcopy(sv));
1272 else if (DO_UTF8(sv)) {
1273 if (!sv_utf8_downgrade((sv = sv_mortalcopy(sv)), TRUE)
1274 && ckWARN_d(WARN_UTF8))
1276 Perl_warner(aTHX_ packWARN(WARN_UTF8), "Wide character in print");
1279 tmps = SvPV(sv, len);
1282 /* To detect whether the process is about to overstep its
1283 * filesize limit we would need getrlimit(). We could then
1284 * also transparently raise the limit with setrlimit() --
1285 * but only until the system hard limit/the filesystem limit,
1286 * at which we would get EPERM. Note that when using buffered
1287 * io the write failure can be delayed until the flush/close. --jhi */
1288 if (len && (PerlIO_write(fp,tmps,len) == 0))
1290 return !PerlIO_error(fp);
1300 if (PL_op->op_flags & OPf_REF) {
1305 if (io && IoIFP(io)) {
1307 sv_setpv(PL_statname,"");
1308 PL_laststype = OP_STAT;
1309 return (PL_laststatval = PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache));
1313 return PL_laststatval;
1314 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1315 report_evil_fh(gv, io, PL_op->op_type);
1317 sv_setpv(PL_statname,"");
1318 return (PL_laststatval = -1);
1326 if (SvTYPE(sv) == SVt_PVGV) {
1330 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
1337 sv_setpv(PL_statname, s);
1338 PL_laststype = OP_STAT;
1339 PL_laststatval = PerlLIO_stat(s, &PL_statcache);
1340 if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(s, '\n'))
1341 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "stat");
1342 return PL_laststatval;
1352 if (PL_op->op_flags & OPf_REF) {
1354 if (cGVOP_gv == PL_defgv) {
1355 if (PL_laststype != OP_LSTAT)
1356 Perl_croak(aTHX_ "The stat preceding -l _ wasn't an lstat");
1357 return PL_laststatval;
1359 if (ckWARN(WARN_IO)) {
1360 Perl_warner(aTHX_ packWARN(WARN_IO), "Use of -l on filehandle %s",
1362 return (PL_laststatval = -1);
1366 PL_laststype = OP_LSTAT;
1370 if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV && ckWARN(WARN_IO)) {
1371 Perl_warner(aTHX_ packWARN(WARN_IO), "Use of -l on filehandle %s",
1372 GvENAME((GV*) SvRV(sv)));
1373 return (PL_laststatval = -1);
1375 sv_setpv(PL_statname,SvPV(sv, n_a));
1376 PL_laststatval = PerlLIO_lstat(SvPV(sv, n_a),&PL_statcache);
1377 if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(SvPV(sv, n_a), '\n'))
1378 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "lstat");
1379 return PL_laststatval;
1383 Perl_do_aexec(pTHX_ SV *really, register SV **mark, register SV **sp)
1385 return do_aexec5(really, mark, sp, 0, 0);
1389 Perl_do_aexec5(pTHX_ SV *really, register SV **mark, register SV **sp,
1390 int fd, int do_report)
1392 #ifdef MACOS_TRADITIONAL
1393 Perl_croak(aTHX_ "exec? I'm not *that* kind of operating system");
1396 char *tmps = Nullch;
1400 New(401,PL_Argv, sp - mark + 1, char*);
1402 while (++mark <= sp) {
1404 *a++ = SvPVx(*mark, n_a);
1410 tmps = SvPV(really, n_a);
1411 if ((!really && *PL_Argv[0] != '/') ||
1412 (really && *tmps != '/')) /* will execvp use PATH? */
1413 TAINT_ENV(); /* testing IFS here is overkill, probably */
1414 if (really && *tmps)
1415 PerlProc_execvp(tmps,EXEC_ARGV_CAST(PL_Argv));
1417 PerlProc_execvp(PL_Argv[0],EXEC_ARGV_CAST(PL_Argv));
1418 if (ckWARN(WARN_EXEC))
1419 Perl_warner(aTHX_ packWARN(WARN_EXEC), "Can't exec \"%s\": %s",
1420 (really ? tmps : PL_Argv[0]), Strerror(errno));
1424 PerlLIO_write(fd, (void*)&e, sizeof(int));
1434 Perl_do_execfree(pTHX)
1438 PL_Argv = Null(char **);
1446 #if !defined(OS2) && !defined(WIN32) && !defined(DJGPP) && !defined(EPOC) && !defined(MACOS_TRADITIONAL)
1449 Perl_do_exec(pTHX_ char *cmd)
1451 return do_exec3(cmd,0,0);
1455 Perl_do_exec3(pTHX_ char *cmd, int fd, int do_report)
1460 while (*cmd && isSPACE(*cmd))
1463 /* save an extra exec if possible */
1468 if (strnEQ(cmd,PL_cshname,PL_cshlen) &&
1469 strnEQ(cmd+PL_cshlen," -c",3)) {
1471 s = cmd+PL_cshlen+3;
1485 if (s[-1] == '\'') {
1487 PerlProc_execl(PL_cshname,"csh", flags, ncmd, (char*)0);
1496 /* see if there are shell metacharacters in it */
1498 if (*cmd == '.' && isSPACE(cmd[1]))
1501 if (strnEQ(cmd,"exec",4) && isSPACE(cmd[4]))
1504 for (s = cmd; *s && isALNUM(*s); s++) ; /* catch VAR=val gizmo */
1508 for (s = cmd; *s; s++) {
1509 if (*s != ' ' && !isALPHA(*s) &&
1510 strchr("$&*(){}[]'\";\\|?<>~`\n",*s)) {
1511 if (*s == '\n' && !s[1]) {
1515 /* handle the 2>&1 construct at the end */
1516 if (*s == '>' && s[1] == '&' && s[2] == '1'
1517 && s > cmd + 1 && s[-1] == '2' && isSPACE(s[-2])
1518 && (!s[3] || isSPACE(s[3])))
1522 while (*t && isSPACE(*t))
1524 if (!*t && (dup2(1,2) != -1)) {
1530 PerlProc_execl(PL_sh_path, "sh", "-c", cmd, (char*)0);
1535 New(402,PL_Argv, (s - cmd) / 2 + 2, char*);
1536 PL_Cmd = savepvn(cmd, s-cmd);
1538 for (s = PL_Cmd; *s;) {
1539 while (*s && isSPACE(*s)) s++;
1542 while (*s && !isSPACE(*s)) s++;
1548 PerlProc_execvp(PL_Argv[0],PL_Argv);
1549 if (errno == ENOEXEC) { /* for system V NIH syndrome */
1556 if (ckWARN(WARN_EXEC))
1557 Perl_warner(aTHX_ packWARN(WARN_EXEC), "Can't exec \"%s\": %s",
1558 PL_Argv[0], Strerror(errno));
1560 PerlLIO_write(fd, (void*)&e, sizeof(int));
1569 #endif /* OS2 || WIN32 */
1572 Perl_apply(pTHX_ I32 type, register SV **mark, register SV **sp)
1576 register I32 tot = 0;
1579 SV **oldmark = mark;
1582 #define APPLY_TAINT_PROPER() \
1584 if (PL_tainted) { TAINT_PROPER(what); } \
1587 /* This is a first heuristic; it doesn't catch tainting magic. */
1589 while (++mark <= sp) {
1590 if (SvTAINTED(*mark)) {
1600 APPLY_TAINT_PROPER();
1603 APPLY_TAINT_PROPER();
1605 while (++mark <= sp) {
1606 char *name = SvPVx(*mark, n_a);
1607 APPLY_TAINT_PROPER();
1608 if (PerlLIO_chmod(name, val))
1616 APPLY_TAINT_PROPER();
1617 if (sp - mark > 2) {
1618 val = SvIVx(*++mark);
1619 val2 = SvIVx(*++mark);
1620 APPLY_TAINT_PROPER();
1622 while (++mark <= sp) {
1623 char *name = SvPVx(*mark, n_a);
1624 APPLY_TAINT_PROPER();
1625 if (PerlLIO_chown(name, val, val2))
1632 XXX Should we make lchown() directly available from perl?
1633 For now, we'll let Configure test for HAS_LCHOWN, but do
1634 nothing in the core.
1640 APPLY_TAINT_PROPER();
1643 s = SvPVx(*++mark, n_a);
1645 if (*s == 'S' && s[1] == 'I' && s[2] == 'G')
1647 if (!(val = whichsig(s)))
1648 Perl_croak(aTHX_ "Unrecognized signal name \"%s\"",s);
1652 APPLY_TAINT_PROPER();
1655 /* kill() doesn't do process groups (job trees?) under VMS */
1656 if (val < 0) val = -val;
1657 if (val == SIGKILL) {
1658 # include <starlet.h>
1659 /* Use native sys$delprc() to insure that target process is
1660 * deleted; supervisor-mode images don't pay attention to
1661 * CRTL's emulation of Unix-style signals and kill()
1663 while (++mark <= sp) {
1664 I32 proc = SvIVx(*mark);
1665 register unsigned long int __vmssts;
1666 APPLY_TAINT_PROPER();
1667 if (!((__vmssts = sys$delprc(&proc,0)) & 1)) {
1671 case SS$_NOSUCHNODE:
1672 SETERRNO(ESRCH,__vmssts);
1675 SETERRNO(EPERM,__vmssts);
1678 SETERRNO(EVMSERR,__vmssts);
1687 while (++mark <= sp) {
1688 I32 proc = SvIVx(*mark);
1689 APPLY_TAINT_PROPER();
1691 if (PerlProc_killpg(proc,val)) /* BSD */
1693 if (PerlProc_kill(-proc,val)) /* SYSV */
1699 while (++mark <= sp) {
1700 I32 proc = SvIVx(*mark);
1701 APPLY_TAINT_PROPER();
1702 if (PerlProc_kill(proc, val))
1710 APPLY_TAINT_PROPER();
1712 while (++mark <= sp) {
1713 s = SvPVx(*mark, n_a);
1714 APPLY_TAINT_PROPER();
1715 if (PL_euid || PL_unsafe) {
1719 else { /* don't let root wipe out directories without -U */
1720 if (PerlLIO_lstat(s,&PL_statbuf) < 0 || S_ISDIR(PL_statbuf.st_mode))
1732 APPLY_TAINT_PROPER();
1733 if (sp - mark > 2) {
1734 #if defined(I_UTIME) || defined(VMS)
1735 struct utimbuf utbuf;
1743 SV* accessed = *++mark;
1744 SV* modified = *++mark;
1745 void * utbufp = &utbuf;
1747 /* be like C, and if both times are undefined, let the C
1748 library figure out what to do. This usually means
1751 if ( accessed == &PL_sv_undef && modified == &PL_sv_undef )
1754 Zero(&utbuf, sizeof utbuf, char);
1756 utbuf.actime = (Time_t)SvNVx(accessed); /* time accessed */
1757 utbuf.modtime = (Time_t)SvNVx(modified); /* time modified */
1759 utbuf.actime = (Time_t)SvIVx(accessed); /* time accessed */
1760 utbuf.modtime = (Time_t)SvIVx(modified); /* time modified */
1762 APPLY_TAINT_PROPER();
1764 while (++mark <= sp) {
1765 char *name = SvPVx(*mark, n_a);
1766 APPLY_TAINT_PROPER();
1767 if (PerlLIO_utime(name, utbufp))
1778 #undef APPLY_TAINT_PROPER
1781 /* Do the permissions allow some operation? Assumes statcache already set. */
1782 #ifndef VMS /* VMS' cando is in vms.c */
1784 Perl_cando(pTHX_ Mode_t mode, Uid_t effective, register Stat_t *statbufp)
1785 /* Note: we use `effective' both for uids and gids.
1786 * Here we are betting on Uid_t being equal or wider than Gid_t. */
1789 /* [Comments and code from Len Reed]
1790 * MS-DOS "user" is similar to UNIX's "superuser," but can't write
1791 * to write-protected files. The execute permission bit is set
1792 * by the Miscrosoft C library stat() function for the following:
1797 * All files and directories are readable.
1798 * Directories and special files, e.g. "CON", cannot be
1800 * [Comment by Tom Dinger -- a directory can have the write-protect
1801 * bit set in the file system, but DOS permits changes to
1802 * the directory anyway. In addition, all bets are off
1803 * here for networked software, such as Novell and
1807 /* Atari stat() does pretty much the same thing. we set x_bit_set_in_stat
1808 * too so it will actually look into the files for magic numbers
1810 return (mode & statbufp->st_mode) ? TRUE : FALSE;
1812 #else /* ! DOSISH */
1813 if ((effective ? PL_euid : PL_uid) == 0) { /* root is special */
1814 if (mode == S_IXUSR) {
1815 if (statbufp->st_mode & 0111 || S_ISDIR(statbufp->st_mode))
1819 return TRUE; /* root reads and writes anything */
1822 if (statbufp->st_uid == (effective ? PL_euid : PL_uid) ) {
1823 if (statbufp->st_mode & mode)
1824 return TRUE; /* ok as "user" */
1826 else if (ingroup(statbufp->st_gid,effective)) {
1827 if (statbufp->st_mode & mode >> 3)
1828 return TRUE; /* ok as "group" */
1830 else if (statbufp->st_mode & mode >> 6)
1831 return TRUE; /* ok as "other" */
1833 #endif /* ! DOSISH */
1838 Perl_ingroup(pTHX_ Gid_t testgid, Uid_t effective)
1840 #ifdef MACOS_TRADITIONAL
1841 /* This is simply not correct for AppleShare, but fix it yerself. */
1844 if (testgid == (effective ? PL_egid : PL_gid))
1846 #ifdef HAS_GETGROUPS
1851 Groups_t gary[NGROUPS];
1854 anum = getgroups(NGROUPS,gary);
1856 if (gary[anum] == testgid)
1864 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
1867 Perl_do_ipcget(pTHX_ I32 optype, SV **mark, SV **sp)
1872 key = (key_t)SvNVx(*++mark);
1873 n = (optype == OP_MSGGET) ? 0 : SvIVx(*++mark);
1874 flags = SvIVx(*++mark);
1880 return msgget(key, flags);
1884 return semget(key, n, flags);
1888 return shmget(key, n, flags);
1890 #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
1892 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
1895 return -1; /* should never happen */
1899 Perl_do_ipcctl(pTHX_ I32 optype, SV **mark, SV **sp)
1903 I32 id, n, cmd, infosize, getinfo;
1906 id = SvIVx(*++mark);
1907 n = (optype == OP_SEMCTL) ? SvIVx(*++mark) : 0;
1908 cmd = SvIVx(*++mark);
1911 getinfo = (cmd == IPC_STAT);
1917 if (cmd == IPC_STAT || cmd == IPC_SET)
1918 infosize = sizeof(struct msqid_ds);
1923 if (cmd == IPC_STAT || cmd == IPC_SET)
1924 infosize = sizeof(struct shmid_ds);
1930 if (cmd == IPC_STAT || cmd == IPC_SET)
1931 infosize = sizeof(struct semid_ds);
1932 else if (cmd == GETALL || cmd == SETALL)
1934 struct semid_ds semds;
1936 #ifdef EXTRA_F_IN_SEMUN_BUF
1937 semun.buff = &semds;
1941 getinfo = (cmd == GETALL);
1942 if (Semctl(id, 0, IPC_STAT, semun) == -1)
1944 infosize = semds.sem_nsems * sizeof(short);
1945 /* "short" is technically wrong but much more portable
1946 than guessing about u_?short(_t)? */
1949 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
1953 #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
1955 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
1964 SvPV_force(astr, len);
1965 a = SvGROW(astr, infosize+1);
1969 a = SvPV(astr, len);
1970 if (len != infosize)
1971 Perl_croak(aTHX_ "Bad arg length for %s, is %lu, should be %ld",
1980 a = INT2PTR(char *,i); /* ouch */
1987 ret = msgctl(id, cmd, (struct msqid_ds *)a);
1993 union semun unsemds;
1995 #ifdef EXTRA_F_IN_SEMUN_BUF
1996 unsemds.buff = (struct semid_ds *)a;
1998 unsemds.buf = (struct semid_ds *)a;
2000 ret = Semctl(id, n, cmd, unsemds);
2002 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
2009 ret = shmctl(id, cmd, (struct shmid_ds *)a);
2013 if (getinfo && ret >= 0) {
2014 SvCUR_set(astr, infosize);
2015 *SvEND(astr) = '\0';
2022 Perl_do_msgsnd(pTHX_ SV **mark, SV **sp)
2027 I32 id, msize, flags;
2030 id = SvIVx(*++mark);
2032 flags = SvIVx(*++mark);
2033 mbuf = SvPV(mstr, len);
2034 if ((msize = len - sizeof(long)) < 0)
2035 Perl_croak(aTHX_ "Arg too short for msgsnd");
2037 return msgsnd(id, (struct msgbuf *)mbuf, msize, flags);
2039 Perl_croak(aTHX_ "msgsnd not implemented");
2044 Perl_do_msgrcv(pTHX_ SV **mark, SV **sp)
2050 I32 id, msize, flags, ret;
2053 id = SvIVx(*++mark);
2055 /* suppress warning when reading into undef var --jhi */
2057 sv_setpvn(mstr, "", 0);
2058 msize = SvIVx(*++mark);
2059 mtype = (long)SvIVx(*++mark);
2060 flags = SvIVx(*++mark);
2061 SvPV_force(mstr, len);
2062 mbuf = SvGROW(mstr, sizeof(long)+msize+1);
2065 ret = msgrcv(id, (struct msgbuf *)mbuf, msize, mtype, flags);
2067 SvCUR_set(mstr, sizeof(long)+ret);
2068 *SvEND(mstr) = '\0';
2069 #ifndef INCOMPLETE_TAINTS
2070 /* who knows who has been playing with this message? */
2076 Perl_croak(aTHX_ "msgrcv not implemented");
2081 Perl_do_semop(pTHX_ SV **mark, SV **sp)
2089 id = SvIVx(*++mark);
2091 opbuf = SvPV(opstr, opsize);
2092 if (opsize < 3 * SHORTSIZE
2093 || (opsize % (3 * SHORTSIZE))) {
2094 SETERRNO(EINVAL,LIB_INVARG);
2098 /* We can't assume that sizeof(struct sembuf) == 3 * sizeof(short). */
2100 int nsops = opsize / (3 * sizeof (short));
2102 short *ops = (short *) opbuf;
2104 struct sembuf *temps, *t;
2107 New (0, temps, nsops, struct sembuf);
2115 result = semop(id, temps, nsops);
2129 Perl_croak(aTHX_ "semop not implemented");
2134 Perl_do_shmio(pTHX_ I32 optype, SV **mark, SV **sp)
2139 I32 id, mpos, msize;
2141 struct shmid_ds shmds;
2143 id = SvIVx(*++mark);
2145 mpos = SvIVx(*++mark);
2146 msize = SvIVx(*++mark);
2148 if (shmctl(id, IPC_STAT, &shmds) == -1)
2150 if (mpos < 0 || msize < 0 || mpos + msize > shmds.shm_segsz) {
2151 SETERRNO(EFAULT,SS_ACCVIO); /* can't do as caller requested */
2154 shm = (char *)shmat(id, (char*)NULL, (optype == OP_SHMREAD) ? SHM_RDONLY : 0);
2155 if (shm == (char *)-1) /* I hate System V IPC, I really do */
2157 if (optype == OP_SHMREAD) {
2158 /* suppress warning when reading into undef var (tchrist 3/Mar/00) */
2160 sv_setpvn(mstr, "", 0);
2161 SvPV_force(mstr, len);
2162 mbuf = SvGROW(mstr, msize+1);
2164 Copy(shm + mpos, mbuf, msize, char);
2165 SvCUR_set(mstr, msize);
2166 *SvEND(mstr) = '\0';
2168 #ifndef INCOMPLETE_TAINTS
2169 /* who knows who has been playing with this shared memory? */
2176 mbuf = SvPV(mstr, len);
2177 if ((n = len) > msize)
2179 Copy(mbuf, shm + mpos, n, char);
2181 memzero(shm + mpos + n, msize - n);
2185 Perl_croak(aTHX_ "shm I/O not implemented");
2189 #endif /* SYSV IPC */
2194 =for apidoc start_glob
2196 Function called by C<do_readline> to spawn a glob (or do the glob inside
2197 perl on VMS). This code used to be inline, but now perl uses C<File::Glob>
2198 this glob starter is only used by miniperl during the build process.
2199 Moving it away shrinks pp_hot.c; shrinking pp_hot.c helps speed perl up.
2205 Perl_start_glob (pTHX_ SV *tmpglob, IO *io)
2207 SV *tmpcmd = NEWSV(55, 0);
2211 #ifdef VMS /* expand the wildcards right here, rather than opening a pipe, */
2212 /* since spawning off a process is a real performance hit */
2214 #include <descrip.h>
2215 #include <lib$routines.h>
2218 char rslt[NAM$C_MAXRSS+1+sizeof(unsigned short int)] = {'\0','\0'};
2219 char vmsspec[NAM$C_MAXRSS+1];
2220 char *rstr = rslt + sizeof(unsigned short int), *begin, *end, *cp;
2221 $DESCRIPTOR(dfltdsc,"SYS$DISK:[]*.*;");
2224 struct dsc$descriptor_s wilddsc
2225 = {0, DSC$K_DTYPE_T, DSC$K_CLASS_S, 0};
2226 struct dsc$descriptor_vs rsdsc
2227 = {sizeof rslt, DSC$K_DTYPE_VT, DSC$K_CLASS_VS, rslt};
2228 unsigned long int cxt = 0, sts = 0, ok = 1, hasdir = 0, hasver = 0, isunix = 0;
2230 /* We could find out if there's an explicit dev/dir or version
2231 by peeking into lib$find_file's internal context at
2232 ((struct NAM *)((struct FAB *)cxt)->fab$l_nam)->nam$l_fnb
2233 but that's unsupported, so I don't want to do it now and
2234 have it bite someone in the future. */
2235 cp = SvPV(tmpglob,i);
2237 if (cp[i] == ';') hasver = 1;
2239 if (sts) hasver = 1;
2243 hasdir = isunix = 1;
2246 if (cp[i] == ']' || cp[i] == '>' || cp[i] == ':') {
2251 if ((tmpfp = PerlIO_tmpfile()) != NULL) {
2253 if (!PerlLIO_stat(SvPVX(tmpglob),&st) && S_ISDIR(st.st_mode))
2254 ok = ((wilddsc.dsc$a_pointer = tovmspath(SvPVX(tmpglob),vmsspec)) != NULL);
2255 else ok = ((wilddsc.dsc$a_pointer = tovmsspec(SvPVX(tmpglob),vmsspec)) != NULL);
2256 if (ok) wilddsc.dsc$w_length = (unsigned short int) strlen(wilddsc.dsc$a_pointer);
2257 for (cp=wilddsc.dsc$a_pointer; ok && cp && *cp; cp++)
2258 if (*cp == '?') *cp = '%'; /* VMS style single-char wildcard */
2259 while (ok && ((sts = lib$find_file(&wilddsc,&rsdsc,&cxt,
2260 &dfltdsc,NULL,NULL,NULL))&1)) {
2261 end = rstr + (unsigned long int) *rslt;
2262 if (!hasver) while (*end != ';') end--;
2263 *(end++) = '\n'; *end = '\0';
2264 for (cp = rstr; *cp; cp++) *cp = _tolower(*cp);
2266 if (isunix) trim_unixpath(rstr,SvPVX(tmpglob),1);
2271 while (*(--begin) != ']' && *begin != '>') ;
2274 ok = (PerlIO_puts(tmpfp,begin) != EOF);
2276 if (cxt) (void)lib$find_file_end(&cxt);
2277 if (ok && sts != RMS$_NMF &&
2278 sts != RMS$_DNF && sts != RMS_FNF) ok = 0;
2281 SETERRNO((sts == RMS$_SYN ? EINVAL : EVMSERR),sts);
2283 PerlIO_close(tmpfp);
2287 PerlIO_rewind(tmpfp);
2288 IoTYPE(io) = IoTYPE_RDONLY;
2289 IoIFP(io) = fp = tmpfp;
2290 IoFLAGS(io) &= ~IOf_UNTAINT; /* maybe redundant */
2295 #ifdef MACOS_TRADITIONAL
2296 sv_setpv(tmpcmd, "glob ");
2297 sv_catsv(tmpcmd, tmpglob);
2298 sv_catpv(tmpcmd, " |");
2302 sv_setpv(tmpcmd, "for a in ");
2303 sv_catsv(tmpcmd, tmpglob);
2304 sv_catpv(tmpcmd, "; do echo \"$a\\0\\c\"; done |");
2307 sv_setpv(tmpcmd, "/dev/dosglob/"); /* File System Extension */
2308 sv_catsv(tmpcmd, tmpglob);
2310 sv_setpv(tmpcmd, "perlglob ");
2311 sv_catsv(tmpcmd, tmpglob);
2312 sv_catpv(tmpcmd, " |");
2317 sv_setpvn(tmpcmd, PL_cshname, PL_cshlen);
2318 sv_catpv(tmpcmd, " -cf 'set nonomatch; glob ");
2319 sv_catsv(tmpcmd, tmpglob);
2320 sv_catpv(tmpcmd, "' 2>/dev/null |");
2322 sv_setpv(tmpcmd, "echo ");
2323 sv_catsv(tmpcmd, tmpglob);
2325 sv_catpv(tmpcmd, "|tr -s ' \t\f\r' '\\012\\012\\012\\012'|");
2327 sv_catpv(tmpcmd, "|tr -s ' \t\f\r' '\\n\\n\\n\\n'|");
2330 #endif /* !DOSISH */
2331 #endif /* MACOS_TRADITIONAL */
2332 (void)do_open(PL_last_in_gv, SvPVX(tmpcmd), SvCUR(tmpcmd),
2333 FALSE, O_RDONLY, 0, Nullfp);