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 */
216 name = SvOK(*svp) ? SvPV(*svp, l) : "";
218 name = savepvn(name, len);
226 if ((*type == IoTYPE_RDWR) && /* scary */
227 (*(type+1) == IoTYPE_RDONLY || *(type+1) == IoTYPE_WRONLY) &&
228 ((!num_svs || (tend > type+1 && tend[-1] != IoTYPE_PIPE)))) {
229 TAINT_PROPER("open");
234 if (*type == IoTYPE_PIPE) {
236 if (type[1] != IoTYPE_STD) {
238 Perl_croak(aTHX_ "Unknown open() mode '%.*s'", (int)olen, oname);
243 for (type++; isSPACE(*type); type++) ;
249 /* command is missing 19990114 */
250 if (ckWARN(WARN_PIPE))
251 Perl_warner(aTHX_ packWARN(WARN_PIPE), "Missing command in piped open");
255 if (strNE(name,"-") || num_svs)
257 TAINT_PROPER("piped open");
258 if (!num_svs && name[len-1] == '|') {
260 if (ckWARN(WARN_PIPE))
261 Perl_warner(aTHX_ packWARN(WARN_PIPE), "Can't open bidirectional pipe");
270 fp = PerlProc_popen_list(mode, num_svs, svp);
273 fp = PerlProc_popen(name,mode);
276 else if (*type == IoTYPE_WRONLY) {
277 TAINT_PROPER("open");
279 if (*type == IoTYPE_WRONLY) {
280 /* Two IoTYPE_WRONLYs in a row make for an IoTYPE_APPEND. */
281 mode[0] = IoTYPE(io) = IoTYPE_APPEND;
296 dodup = PERLIO_DUP_FD;
302 if (!num_svs && !*type && supplied_fp) {
303 /* "<+&" etc. is used by typemaps */
308 Perl_croak(aTHX_ "More than one argument to '%c&' open",IoTYPE(io));
310 if (num_svs && SvIOK(*svp)) {
313 else if (isDIGIT(*type)) {
315 for (; isSPACE(*type); type++) ;
321 thatio = sv_2io(*svp);
326 for (; isSPACE(*type); type++) ;
327 thatgv = gv_fetchpv(type,FALSE,SVt_PVIO);
328 thatio = GvIO(thatgv);
332 SETERRNO(EINVAL,SS$_IVCHAN);
336 if ((that_fp = IoIFP(thatio))) {
337 /* Flush stdio buffer before dup. --mjd
338 * Unfortunately SEEK_CURing 0 seems to
339 * be optimized away on most platforms;
340 * only Solaris and Linux seem to flush
343 /* sfio fails to clear error on next
344 sfwrite, contrary to documentation.
346 if (PerlIO_seek(that_fp, 0, SEEK_CUR) == -1)
347 PerlIO_clearerr(that_fp);
349 /* On the other hand, do all platforms
350 * take gracefully to flushing a read-only
351 * filehandle? Perhaps we should do
352 * fsetpos(src)+fgetpos(dst)? --nik */
353 PerlIO_flush(that_fp);
354 fd = PerlIO_fileno(that_fp);
355 /* When dup()ing STDIN, STDOUT or STDERR
356 * explicitly set appropriate access mode */
357 if (that_fp == PerlIO_stdout()
358 || that_fp == PerlIO_stderr())
359 IoTYPE(io) = IoTYPE_WRONLY;
360 else if (that_fp == PerlIO_stdin())
361 IoTYPE(io) = IoTYPE_RDONLY;
362 /* When dup()ing a socket, say result is
364 else if (IoTYPE(thatio) == IoTYPE_SOCKET)
365 IoTYPE(io) = IoTYPE_SOCKET;
373 fp = PerlIO_fdupopen(aTHX_ that_fp, NULL, dodup);
377 fd = PerlLIO_dup(fd);
380 if (!(fp = PerlIO_openn(aTHX_ type,mode,fd,0,0,NULL,num_svs,svp))) {
389 for (; isSPACE(*type); type++) ;
390 if (*type == IoTYPE_STD && (!type[1] || isSPACE(type[1]) || type[1] == ':')) {
393 fp = PerlIO_stdout();
394 IoTYPE(io) = IoTYPE_STD;
396 Perl_croak(aTHX_ "More than one argument to '>%c' open",IoTYPE_STD);
401 namesv = sv_2mortal(newSVpvn(type,strlen(type)));
406 fp = PerlIO_openn(aTHX_ type,mode,-1,0,0,NULL,num_svs,svp);
410 else if (*type == IoTYPE_RDONLY) {
412 for (type++; isSPACE(*type); type++) ;
422 if (*type == IoTYPE_STD && (!type[1] || isSPACE(type[1]) || type[1] == ':')) {
426 IoTYPE(io) = IoTYPE_STD;
428 Perl_croak(aTHX_ "More than one argument to '<%c' open",IoTYPE_STD);
433 namesv = sv_2mortal(newSVpvn(type,strlen(type)));
438 fp = PerlIO_openn(aTHX_ type,mode,-1,0,0,NULL,num_svs,svp);
441 else if ((num_svs && type[0] == IoTYPE_STD && type[1] == IoTYPE_PIPE) ||
442 (!num_svs && tend > type+1 && tend[-1] == IoTYPE_PIPE)) {
444 type += 2; /* skip over '-|' */
448 while (tend > type && isSPACE(tend[-1]))
451 for (; isSPACE(*type); type++) ;
456 /* command is missing 19990114 */
457 if (ckWARN(WARN_PIPE))
458 Perl_warner(aTHX_ packWARN(WARN_PIPE), "Missing command in piped open");
462 if (strNE(name,"-") || num_svs)
464 TAINT_PROPER("piped open");
471 fp = PerlProc_popen_list(mode,num_svs,svp);
474 fp = PerlProc_popen(name,mode);
476 IoTYPE(io) = IoTYPE_PIPE;
482 IoTYPE(io) = IoTYPE_RDONLY;
484 for (; isSPACE(*name); name++) ;
490 if (strEQ(name,"-")) {
492 IoTYPE(io) = IoTYPE_STD;
496 namesv = sv_2mortal(newSVpvn(type,strlen(type)));
501 fp = PerlIO_openn(aTHX_ type,mode,-1,0,0,NULL,num_svs,svp);
506 if (ckWARN(WARN_NEWLINE) && IoTYPE(io) == IoTYPE_RDONLY && strchr(name, '\n'))
507 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "open");
511 if (ckWARN(WARN_IO)) {
512 if ((IoTYPE(io) == IoTYPE_RDONLY) &&
513 (fp == PerlIO_stdout() || fp == PerlIO_stderr())) {
514 Perl_warner(aTHX_ packWARN(WARN_IO),
515 "Filehandle STD%s opened only for input",
516 (fp == PerlIO_stdout()) ? "OUT" : "ERR");
518 else if ((IoTYPE(io) == IoTYPE_WRONLY) && fp == PerlIO_stdin()) {
519 Perl_warner(aTHX_ packWARN(WARN_IO),
520 "Filehandle STDIN opened only for output");
524 fd = PerlIO_fileno(fp);
525 /* If there is no fd (e.g. PerlIO::Scalar) assume it isn't a
526 * socket - this covers PerlIO::Scalar - otherwise unless we "know" the
527 * type probe for socket-ness.
529 if (IoTYPE(io) && IoTYPE(io) != IoTYPE_PIPE && IoTYPE(io) != IoTYPE_STD && fd >= 0) {
530 if (PerlLIO_fstat(fd,&PL_statbuf) < 0) {
531 /* If PerlIO claims to have fd we had better be able to fstat() it. */
532 (void) PerlIO_close(fp);
536 if (S_ISSOCK(PL_statbuf.st_mode))
537 IoTYPE(io) = IoTYPE_SOCKET; /* in case a socket was passed in to us */
541 !(PL_statbuf.st_mode & S_IFMT)
545 && IoTYPE(io) != IoTYPE_WRONLY /* Dups of STD* filehandles already have */
546 && IoTYPE(io) != IoTYPE_RDONLY /* type so they aren't marked as sockets */
547 ) { /* on OS's that return 0 on fstat()ed pipe */
549 Sock_size_t buflen = sizeof tmpbuf;
550 if (PerlSock_getsockname(fd, (struct sockaddr *)tmpbuf, &buflen) >= 0
551 || errno != ENOTSOCK)
552 IoTYPE(io) = IoTYPE_SOCKET; /* some OS's return 0 on fstat()ed socket */
553 /* but some return 0 for streams too, sigh */
555 #endif /* HAS_SOCKET */
556 #endif /* !PERL_MICRO */
560 * If this is a standard handle we discard all the layer stuff
561 * and just dup the fd into whatever was on the handle before !
564 if (saveifp) { /* must use old fp? */
565 /* If fd is less that PL_maxsysfd i.e. STDIN..STDERR
566 then dup the new fileno down
569 PerlIO_flush(saveofp); /* emulate PerlIO_close() */
570 if (saveofp != saveifp) { /* was a socket? */
571 PerlIO_close(saveofp);
575 /* Still a small can-of-worms here if (say) PerlIO::Scalar
576 is assigned to (say) STDOUT - for now let dup2() fail
577 and provide the error
579 if (PerlLIO_dup2(fd, savefd) < 0) {
580 (void)PerlIO_close(fp);
584 if (savefd != PerlIO_fileno(PerlIO_stdin())) {
585 char newname[FILENAME_MAX+1];
586 if (PerlIO_getname(fp, newname)) {
587 if (fd == PerlIO_fileno(PerlIO_stdout()))
588 Perl_vmssetuserlnm(aTHX_ "SYS$OUTPUT", newname);
589 if (fd == PerlIO_fileno(PerlIO_stderr()))
590 Perl_vmssetuserlnm(aTHX_ "SYS$ERROR", newname);
596 /* PL_fdpid isn't used on Windows, so avoid this useless work.
597 * XXX Probably the same for a lot of other places. */
603 sv = *av_fetch(PL_fdpid,fd,TRUE);
604 (void)SvUPGRADE(sv, SVt_IV);
607 sv = *av_fetch(PL_fdpid,savefd,TRUE);
608 (void)SvUPGRADE(sv, SVt_IV);
615 /* need to close fp without closing underlying fd */
616 int ofd = PerlIO_fileno(fp);
617 int dupfd = PerlLIO_dup(ofd);
619 PerlLIO_dup2(dupfd,ofd);
620 PerlLIO_close(dupfd);
627 fd = PerlIO_fileno(fp);
629 #if defined(HAS_FCNTL) && defined(F_SETFD)
631 int save_errno = errno;
632 fcntl(fd,F_SETFD,fd > PL_maxsysfd); /* can change errno */
638 IoFLAGS(io) &= ~IOf_NOLINE;
640 if (IoTYPE(io) == IoTYPE_SOCKET
641 || (IoTYPE(io) == IoTYPE_WRONLY && fd >= 0 && S_ISCHR(PL_statbuf.st_mode)) ) {
643 if (!(IoOFP(io) = PerlIO_openn(aTHX_ type,mode,fd,0,0,NULL,num_svs,svp))) {
657 IoTYPE(io) = savetype;
662 Perl_nextargv(pTHX_ register GV *gv)
665 #ifndef FLEXFILENAMES
674 PL_argvoutgv = gv_fetchpv("ARGVOUT",TRUE,SVt_PVIO);
675 if (io && (IoFLAGS(io) & IOf_ARGV) && (IoFLAGS(io) & IOf_START)) {
676 IoFLAGS(io) &= ~IOf_START;
678 if (!PL_argvout_stack)
679 PL_argvout_stack = newAV();
680 av_push(PL_argvout_stack, SvREFCNT_inc(PL_defoutgv));
683 if (PL_filemode & (S_ISUID|S_ISGID)) {
684 PerlIO_flush(IoIFP(GvIOn(PL_argvoutgv))); /* chmod must follow last write */
686 (void)fchmod(PL_lastfd,PL_filemode);
688 (void)PerlLIO_chmod(PL_oldname,PL_filemode);
692 while (av_len(GvAV(gv)) >= 0) {
694 sv = av_shift(GvAV(gv));
696 sv_setsv(GvSV(gv),sv);
697 SvSETMAGIC(GvSV(gv));
698 PL_oldname = SvPVx(GvSV(gv), oldlen);
699 if (do_open(gv,PL_oldname,oldlen,PL_inplace!=0,O_RDONLY,0,Nullfp)) {
701 TAINT_PROPER("inplace open");
702 if (oldlen == 1 && *PL_oldname == '-') {
703 setdefout(gv_fetchpv("STDOUT",TRUE,SVt_PVIO));
704 return IoIFP(GvIOp(gv));
706 #ifndef FLEXFILENAMES
707 filedev = PL_statbuf.st_dev;
708 fileino = PL_statbuf.st_ino;
710 PL_filemode = PL_statbuf.st_mode;
711 fileuid = PL_statbuf.st_uid;
712 filegid = PL_statbuf.st_gid;
713 if (!S_ISREG(PL_filemode)) {
714 if (ckWARN_d(WARN_INPLACE))
715 Perl_warner(aTHX_ packWARN(WARN_INPLACE),
716 "Can't do inplace edit: %s is not a regular file",
722 char *star = strchr(PL_inplace, '*');
724 char *begin = PL_inplace;
725 sv_setpvn(sv, "", 0);
727 sv_catpvn(sv, begin, star - begin);
728 sv_catpvn(sv, PL_oldname, oldlen);
730 } while ((star = strchr(begin, '*')));
735 sv_catpv(sv,PL_inplace);
737 #ifndef FLEXFILENAMES
738 if ((PerlLIO_stat(SvPVX(sv),&PL_statbuf) >= 0
739 && PL_statbuf.st_dev == filedev
740 && PL_statbuf.st_ino == fileino)
742 || ((_djstat_fail_bits & _STFAIL_TRUENAME)!=0)
746 if (ckWARN_d(WARN_INPLACE))
747 Perl_warner(aTHX_ packWARN(WARN_INPLACE),
748 "Can't do inplace edit: %s would not be unique",
755 #if !defined(DOSISH) && !defined(__CYGWIN__)
756 if (PerlLIO_rename(PL_oldname,SvPVX(sv)) < 0) {
757 if (ckWARN_d(WARN_INPLACE))
758 Perl_warner(aTHX_ packWARN(WARN_INPLACE),
759 "Can't rename %s to %s: %s, skipping file",
760 PL_oldname, SvPVX(sv), Strerror(errno) );
766 (void)PerlLIO_unlink(SvPVX(sv));
767 (void)PerlLIO_rename(PL_oldname,SvPVX(sv));
768 do_open(gv,SvPVX(sv),SvCUR(sv),PL_inplace!=0,O_RDONLY,0,Nullfp);
771 (void)UNLINK(SvPVX(sv));
772 if (link(PL_oldname,SvPVX(sv)) < 0) {
773 if (ckWARN_d(WARN_INPLACE))
774 Perl_warner(aTHX_ packWARN(WARN_INPLACE),
775 "Can't rename %s to %s: %s, skipping file",
776 PL_oldname, SvPVX(sv), Strerror(errno) );
780 (void)UNLINK(PL_oldname);
784 #if !defined(DOSISH) && !defined(AMIGAOS)
785 # ifndef VMS /* Don't delete; use automatic file versioning */
786 if (UNLINK(PL_oldname) < 0) {
787 if (ckWARN_d(WARN_INPLACE))
788 Perl_warner(aTHX_ packWARN(WARN_INPLACE),
789 "Can't remove %s: %s, skipping file",
790 PL_oldname, Strerror(errno) );
796 Perl_croak(aTHX_ "Can't do inplace edit without backup");
800 sv_setpvn(sv,">",!PL_inplace);
801 sv_catpvn(sv,PL_oldname,oldlen);
802 SETERRNO(0,0); /* in case sprintf set errno */
804 if (!do_open(PL_argvoutgv,SvPVX(sv),SvCUR(sv),PL_inplace!=0,
805 O_WRONLY|O_CREAT|O_TRUNC,0,Nullfp))
807 if (!do_open(PL_argvoutgv,SvPVX(sv),SvCUR(sv),PL_inplace!=0,
808 O_WRONLY|O_CREAT|OPEN_EXCL,0666,Nullfp))
811 if (ckWARN_d(WARN_INPLACE))
812 Perl_warner(aTHX_ packWARN(WARN_INPLACE), "Can't do inplace edit on %s: %s",
813 PL_oldname, Strerror(errno) );
817 setdefout(PL_argvoutgv);
818 PL_lastfd = PerlIO_fileno(IoIFP(GvIOp(PL_argvoutgv)));
819 (void)PerlLIO_fstat(PL_lastfd,&PL_statbuf);
821 (void)fchmod(PL_lastfd,PL_filemode);
823 # if !(defined(WIN32) && defined(__BORLANDC__))
824 /* Borland runtime creates a readonly file! */
825 (void)PerlLIO_chmod(PL_oldname,PL_filemode);
828 if (fileuid != PL_statbuf.st_uid || filegid != PL_statbuf.st_gid) {
830 (void)fchown(PL_lastfd,fileuid,filegid);
833 (void)PerlLIO_chown(PL_oldname,fileuid,filegid);
838 return IoIFP(GvIOp(gv));
841 if (ckWARN_d(WARN_INPLACE)) {
843 if (PerlLIO_stat(PL_oldname, &PL_statbuf) >= 0
844 && !S_ISREG(PL_statbuf.st_mode))
846 Perl_warner(aTHX_ packWARN(WARN_INPLACE),
847 "Can't do inplace edit: %s is not a regular file",
851 Perl_warner(aTHX_ packWARN(WARN_INPLACE), "Can't open %s: %s",
852 PL_oldname, Strerror(eno));
856 if (io && (IoFLAGS(io) & IOf_ARGV))
857 IoFLAGS(io) |= IOf_START;
859 (void)do_close(PL_argvoutgv,FALSE);
860 if (io && (IoFLAGS(io) & IOf_ARGV)
861 && PL_argvout_stack && AvFILLp(PL_argvout_stack) >= 0)
863 GV *oldout = (GV*)av_pop(PL_argvout_stack);
865 SvREFCNT_dec(oldout);
868 setdefout(gv_fetchpv("STDOUT",TRUE,SVt_PVIO));
875 Perl_do_pipe(pTHX_ SV *sv, GV *rgv, GV *wgv)
894 if (PerlProc_pipe(fd) < 0)
896 IoIFP(rstio) = PerlIO_fdopen(fd[0], "r");
897 IoOFP(wstio) = PerlIO_fdopen(fd[1], "w");
898 IoIFP(wstio) = IoOFP(wstio);
899 IoTYPE(rstio) = IoTYPE_RDONLY;
900 IoTYPE(wstio) = IoTYPE_WRONLY;
901 if (!IoIFP(rstio) || !IoOFP(wstio)) {
902 if (IoIFP(rstio)) PerlIO_close(IoIFP(rstio));
903 else PerlLIO_close(fd[0]);
904 if (IoOFP(wstio)) PerlIO_close(IoOFP(wstio));
905 else PerlLIO_close(fd[1]);
909 sv_setsv(sv,&PL_sv_yes);
913 sv_setsv(sv,&PL_sv_undef);
918 /* explicit renamed to avoid C++ conflict -- kja */
920 Perl_do_close(pTHX_ GV *gv, bool not_implicit)
927 if (!gv || SvTYPE(gv) != SVt_PVGV) {
929 SETERRNO(EBADF,SS$_IVCHAN);
933 if (!io) { /* never opened */
935 if (ckWARN(WARN_UNOPENED)) /* no check for closed here */
936 report_evil_fh(gv, io, PL_op->op_type);
937 SETERRNO(EBADF,SS$_IVCHAN);
941 retval = io_close(io, not_implicit);
945 IoLINES_LEFT(io) = IoPAGE_LEN(io);
947 IoTYPE(io) = IoTYPE_CLOSED;
952 Perl_io_close(pTHX_ IO *io, bool not_implicit)
958 if (IoTYPE(io) == IoTYPE_PIPE) {
959 status = PerlProc_pclose(IoIFP(io));
961 STATUS_NATIVE_SET(status);
962 retval = (STATUS_POSIX == 0);
965 retval = (status != -1);
968 else if (IoTYPE(io) == IoTYPE_STD)
971 if (IoOFP(io) && IoOFP(io) != IoIFP(io)) { /* a socket */
972 retval = (PerlIO_close(IoOFP(io)) != EOF);
973 PerlIO_close(IoIFP(io)); /* clear stdio, fd already closed */
976 retval = (PerlIO_close(IoIFP(io)) != EOF);
978 IoOFP(io) = IoIFP(io) = Nullfp;
980 else if (not_implicit) {
981 SETERRNO(EBADF,SS$_IVCHAN);
988 Perl_do_eof(pTHX_ GV *gv)
997 else if (ckWARN(WARN_IO) && (IoTYPE(io) == IoTYPE_WRONLY))
998 report_evil_fh(gv, io, OP_phoney_OUTPUT_ONLY);
1002 if (PerlIO_has_cntptr(IoIFP(io))) { /* (the code works without this) */
1003 if (PerlIO_get_cnt(IoIFP(io)) > 0) /* cheat a little, since */
1004 return FALSE; /* this is the most usual case */
1007 ch = PerlIO_getc(IoIFP(io));
1009 (void)PerlIO_ungetc(IoIFP(io),ch);
1013 if (PerlIO_has_cntptr(IoIFP(io)) && PerlIO_canset_cnt(IoIFP(io))) {
1014 if (PerlIO_get_cnt(IoIFP(io)) < -1)
1015 PerlIO_set_cnt(IoIFP(io),-1);
1017 if (PL_op->op_flags & OPf_SPECIAL) { /* not necessarily a real EOF yet? */
1018 if (gv != PL_argvgv || !nextargv(gv)) /* get another fp handy */
1022 return TRUE; /* normal fp, definitely end of file */
1028 Perl_do_tell(pTHX_ GV *gv)
1030 register IO *io = 0;
1031 register PerlIO *fp;
1033 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io))) {
1034 #ifdef ULTRIX_STDIO_BOTCH
1036 (void)PerlIO_seek(fp, 0L, 2); /* ultrix 1.2 workaround */
1038 return PerlIO_tell(fp);
1040 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1041 report_evil_fh(gv, io, PL_op->op_type);
1042 SETERRNO(EBADF,RMS$_IFI);
1047 Perl_do_seek(pTHX_ GV *gv, Off_t pos, int whence)
1049 register IO *io = 0;
1050 register PerlIO *fp;
1052 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io))) {
1053 #ifdef ULTRIX_STDIO_BOTCH
1055 (void)PerlIO_seek(fp, 0L, 2); /* ultrix 1.2 workaround */
1057 return PerlIO_seek(fp, pos, whence) >= 0;
1059 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1060 report_evil_fh(gv, io, PL_op->op_type);
1061 SETERRNO(EBADF,RMS$_IFI);
1066 Perl_do_sysseek(pTHX_ GV *gv, Off_t pos, int whence)
1068 register IO *io = 0;
1069 register PerlIO *fp;
1071 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io)))
1072 return PerlLIO_lseek(PerlIO_fileno(fp), pos, whence);
1073 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1074 report_evil_fh(gv, io, PL_op->op_type);
1075 SETERRNO(EBADF,RMS$_IFI);
1080 Perl_mode_from_discipline(pTHX_ SV *discp)
1082 int mode = O_BINARY;
1085 char *s = SvPV(discp,len);
1090 if (len > 3 && strnEQ(s+1, "raw", 3)
1091 && (!s[4] || s[4] == ':' || isSPACE(s[4])))
1100 if (len > 4 && strnEQ(s+1, "crlf", 4)
1101 && (!s[5] || s[5] == ':' || isSPACE(s[5])))
1110 goto fail_discipline;
1113 else if (isSPACE(*s)) {
1120 end = strchr(s+1, ':');
1123 #ifndef PERLIO_LAYERS
1124 Perl_croak(aTHX_ "Unknown discipline '%.*s'", end-s, s);
1135 Perl_do_binmode(pTHX_ PerlIO *fp, int iotype, int mode)
1137 /* The old body of this is now in non-LAYER part of perlio.c
1138 * This is a stub for any XS code which might have been calling it.
1140 char *name = ":raw";
1141 #ifdef PERLIO_USING_CRLF
1142 if (!(mode & O_BINARY))
1145 return PerlIO_binmode(aTHX_ fp, iotype, mode, name);
1148 #if !defined(HAS_TRUNCATE) && !defined(HAS_CHSIZE) && defined(F_FREESP)
1149 /* code courtesy of William Kucharski */
1152 I32 my_chsize(fd, length)
1153 I32 fd; /* file descriptor */
1154 Off_t length; /* length to set file to */
1157 struct stat filebuf;
1159 if (PerlLIO_fstat(fd, &filebuf) < 0)
1162 if (filebuf.st_size < length) {
1164 /* extend file length */
1166 if ((PerlLIO_lseek(fd, (length - 1), 0)) < 0)
1169 /* write a "0" byte */
1171 if ((PerlLIO_write(fd, "", 1)) != 1)
1175 /* truncate length */
1179 fl.l_start = length;
1180 fl.l_type = F_WRLCK; /* write lock on file space */
1183 * This relies on the UNDOCUMENTED F_FREESP argument to
1184 * fcntl(2), which truncates the file so that it ends at the
1185 * position indicated by fl.l_start.
1187 * Will minor miracles never cease?
1190 if (fcntl(fd, F_FREESP, &fl) < 0)
1197 #endif /* F_FREESP */
1200 Perl_do_print(pTHX_ register SV *sv, PerlIO *fp)
1202 register char *tmps;
1205 /* assuming fp is checked earlier */
1211 if (SvIOK(sv) && SvIVX(sv) != 0) {
1212 PerlIO_printf(fp, PL_ofmt, (NV)SvIVX(sv));
1213 return !PerlIO_error(fp);
1215 if ( (SvNOK(sv) && SvNVX(sv) != 0.0)
1216 || (looks_like_number(sv) && sv_2nv(sv) != 0.0) ) {
1217 PerlIO_printf(fp, PL_ofmt, SvNVX(sv));
1218 return !PerlIO_error(fp);
1221 switch (SvTYPE(sv)) {
1223 if (ckWARN(WARN_UNINITIALIZED))
1231 PerlIO_printf(fp, "%"UVuf, (UV)SvUVX(sv));
1233 PerlIO_printf(fp, "%"IVdf, (IV)SvIVX(sv));
1234 return !PerlIO_error(fp);
1238 if (PerlIO_isutf8(fp)) {
1240 sv_utf8_upgrade(sv = sv_mortalcopy(sv));
1242 else if (DO_UTF8(sv)) {
1243 if (!sv_utf8_downgrade((sv = sv_mortalcopy(sv)), TRUE)
1244 && ckWARN_d(WARN_UTF8))
1246 Perl_warner(aTHX_ packWARN(WARN_UTF8), "Wide character in print");
1249 tmps = SvPV(sv, len);
1252 /* To detect whether the process is about to overstep its
1253 * filesize limit we would need getrlimit(). We could then
1254 * also transparently raise the limit with setrlimit() --
1255 * but only until the system hard limit/the filesystem limit,
1256 * at which we would get EPERM. Note that when using buffered
1257 * io the write failure can be delayed until the flush/close. --jhi */
1258 if (len && (PerlIO_write(fp,tmps,len) == 0))
1260 return !PerlIO_error(fp);
1270 if (PL_op->op_flags & OPf_REF) {
1275 if (io && IoIFP(io)) {
1277 sv_setpv(PL_statname,"");
1278 PL_laststype = OP_STAT;
1279 return (PL_laststatval = PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache));
1283 return PL_laststatval;
1284 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1285 report_evil_fh(gv, io, PL_op->op_type);
1287 sv_setpv(PL_statname,"");
1288 return (PL_laststatval = -1);
1296 if (SvTYPE(sv) == SVt_PVGV) {
1300 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
1307 sv_setpv(PL_statname, s);
1308 PL_laststype = OP_STAT;
1309 PL_laststatval = PerlLIO_stat(s, &PL_statcache);
1310 if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(s, '\n'))
1311 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "stat");
1312 return PL_laststatval;
1322 if (PL_op->op_flags & OPf_REF) {
1324 if (cGVOP_gv == PL_defgv) {
1325 if (PL_laststype != OP_LSTAT)
1326 Perl_croak(aTHX_ "The stat preceding -l _ wasn't an lstat");
1327 return PL_laststatval;
1329 if (ckWARN(WARN_IO)) {
1330 Perl_warner(aTHX_ packWARN(WARN_IO), "Use of -l on filehandle %s",
1332 return (PL_laststatval = -1);
1336 PL_laststype = OP_LSTAT;
1340 if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV && ckWARN(WARN_IO)) {
1341 Perl_warner(aTHX_ packWARN(WARN_IO), "Use of -l on filehandle %s",
1342 GvENAME((GV*) SvRV(sv)));
1343 return (PL_laststatval = -1);
1345 sv_setpv(PL_statname,SvPV(sv, n_a));
1346 PL_laststatval = PerlLIO_lstat(SvPV(sv, n_a),&PL_statcache);
1347 if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(SvPV(sv, n_a), '\n'))
1348 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "lstat");
1349 return PL_laststatval;
1353 Perl_do_aexec(pTHX_ SV *really, register SV **mark, register SV **sp)
1355 return do_aexec5(really, mark, sp, 0, 0);
1359 Perl_do_aexec5(pTHX_ SV *really, register SV **mark, register SV **sp,
1360 int fd, int do_report)
1362 #ifdef MACOS_TRADITIONAL
1363 Perl_croak(aTHX_ "exec? I'm not *that* kind of operating system");
1366 char *tmps = Nullch;
1370 New(401,PL_Argv, sp - mark + 1, char*);
1372 while (++mark <= sp) {
1374 *a++ = SvPVx(*mark, n_a);
1380 tmps = SvPV(really, n_a);
1381 if ((!really && *PL_Argv[0] != '/') ||
1382 (really && *tmps != '/')) /* will execvp use PATH? */
1383 TAINT_ENV(); /* testing IFS here is overkill, probably */
1384 if (really && *tmps)
1385 PerlProc_execvp(tmps,EXEC_ARGV_CAST(PL_Argv));
1387 PerlProc_execvp(PL_Argv[0],EXEC_ARGV_CAST(PL_Argv));
1388 if (ckWARN(WARN_EXEC))
1389 Perl_warner(aTHX_ packWARN(WARN_EXEC), "Can't exec \"%s\": %s",
1390 (really ? tmps : PL_Argv[0]), Strerror(errno));
1394 PerlLIO_write(fd, (void*)&e, sizeof(int));
1404 Perl_do_execfree(pTHX)
1408 PL_Argv = Null(char **);
1416 #if !defined(OS2) && !defined(WIN32) && !defined(DJGPP) && !defined(EPOC) && !defined(MACOS_TRADITIONAL)
1419 Perl_do_exec(pTHX_ char *cmd)
1421 return do_exec3(cmd,0,0);
1425 Perl_do_exec3(pTHX_ char *cmd, int fd, int do_report)
1430 while (*cmd && isSPACE(*cmd))
1433 /* save an extra exec if possible */
1438 if (strnEQ(cmd,PL_cshname,PL_cshlen) &&
1439 strnEQ(cmd+PL_cshlen," -c",3)) {
1441 s = cmd+PL_cshlen+3;
1455 if (s[-1] == '\'') {
1457 PerlProc_execl(PL_cshname,"csh", flags, ncmd, (char*)0);
1466 /* see if there are shell metacharacters in it */
1468 if (*cmd == '.' && isSPACE(cmd[1]))
1471 if (strnEQ(cmd,"exec",4) && isSPACE(cmd[4]))
1474 for (s = cmd; *s && isALNUM(*s); s++) ; /* catch VAR=val gizmo */
1478 for (s = cmd; *s; s++) {
1479 if (*s != ' ' && !isALPHA(*s) &&
1480 strchr("$&*(){}[]'\";\\|?<>~`\n",*s)) {
1481 if (*s == '\n' && !s[1]) {
1485 /* handle the 2>&1 construct at the end */
1486 if (*s == '>' && s[1] == '&' && s[2] == '1'
1487 && s > cmd + 1 && s[-1] == '2' && isSPACE(s[-2])
1488 && (!s[3] || isSPACE(s[3])))
1492 while (*t && isSPACE(*t))
1494 if (!*t && (dup2(1,2) != -1)) {
1500 PerlProc_execl(PL_sh_path, "sh", "-c", cmd, (char*)0);
1505 New(402,PL_Argv, (s - cmd) / 2 + 2, char*);
1506 PL_Cmd = savepvn(cmd, s-cmd);
1508 for (s = PL_Cmd; *s;) {
1509 while (*s && isSPACE(*s)) s++;
1512 while (*s && !isSPACE(*s)) s++;
1518 PerlProc_execvp(PL_Argv[0],PL_Argv);
1519 if (errno == ENOEXEC) { /* for system V NIH syndrome */
1526 if (ckWARN(WARN_EXEC))
1527 Perl_warner(aTHX_ packWARN(WARN_EXEC), "Can't exec \"%s\": %s",
1528 PL_Argv[0], Strerror(errno));
1530 PerlLIO_write(fd, (void*)&e, sizeof(int));
1539 #endif /* OS2 || WIN32 */
1542 Perl_apply(pTHX_ I32 type, register SV **mark, register SV **sp)
1546 register I32 tot = 0;
1549 SV **oldmark = mark;
1552 #define APPLY_TAINT_PROPER() \
1554 if (PL_tainted) { TAINT_PROPER(what); } \
1557 /* This is a first heuristic; it doesn't catch tainting magic. */
1559 while (++mark <= sp) {
1560 if (SvTAINTED(*mark)) {
1570 APPLY_TAINT_PROPER();
1573 APPLY_TAINT_PROPER();
1575 while (++mark <= sp) {
1576 char *name = SvPVx(*mark, n_a);
1577 APPLY_TAINT_PROPER();
1578 if (PerlLIO_chmod(name, val))
1586 APPLY_TAINT_PROPER();
1587 if (sp - mark > 2) {
1588 val = SvIVx(*++mark);
1589 val2 = SvIVx(*++mark);
1590 APPLY_TAINT_PROPER();
1592 while (++mark <= sp) {
1593 char *name = SvPVx(*mark, n_a);
1594 APPLY_TAINT_PROPER();
1595 if (PerlLIO_chown(name, val, val2))
1602 XXX Should we make lchown() directly available from perl?
1603 For now, we'll let Configure test for HAS_LCHOWN, but do
1604 nothing in the core.
1610 APPLY_TAINT_PROPER();
1613 s = SvPVx(*++mark, n_a);
1615 if (*s == 'S' && s[1] == 'I' && s[2] == 'G')
1617 if (!(val = whichsig(s)))
1618 Perl_croak(aTHX_ "Unrecognized signal name \"%s\"",s);
1622 APPLY_TAINT_PROPER();
1625 /* kill() doesn't do process groups (job trees?) under VMS */
1626 if (val < 0) val = -val;
1627 if (val == SIGKILL) {
1628 # include <starlet.h>
1629 /* Use native sys$delprc() to insure that target process is
1630 * deleted; supervisor-mode images don't pay attention to
1631 * CRTL's emulation of Unix-style signals and kill()
1633 while (++mark <= sp) {
1634 I32 proc = SvIVx(*mark);
1635 register unsigned long int __vmssts;
1636 APPLY_TAINT_PROPER();
1637 if (!((__vmssts = sys$delprc(&proc,0)) & 1)) {
1641 case SS$_NOSUCHNODE:
1642 SETERRNO(ESRCH,__vmssts);
1645 SETERRNO(EPERM,__vmssts);
1648 SETERRNO(EVMSERR,__vmssts);
1657 while (++mark <= sp) {
1658 I32 proc = SvIVx(*mark);
1659 APPLY_TAINT_PROPER();
1661 if (PerlProc_killpg(proc,val)) /* BSD */
1663 if (PerlProc_kill(-proc,val)) /* SYSV */
1669 while (++mark <= sp) {
1670 I32 proc = SvIVx(*mark);
1671 APPLY_TAINT_PROPER();
1672 if (PerlProc_kill(proc, val))
1680 APPLY_TAINT_PROPER();
1682 while (++mark <= sp) {
1683 s = SvPVx(*mark, n_a);
1684 APPLY_TAINT_PROPER();
1685 if (PL_euid || PL_unsafe) {
1689 else { /* don't let root wipe out directories without -U */
1690 if (PerlLIO_lstat(s,&PL_statbuf) < 0 || S_ISDIR(PL_statbuf.st_mode))
1702 APPLY_TAINT_PROPER();
1703 if (sp - mark > 2) {
1704 #if defined(I_UTIME) || defined(VMS)
1705 struct utimbuf utbuf;
1713 SV* accessed = *++mark;
1714 SV* modified = *++mark;
1715 void * utbufp = &utbuf;
1717 /* be like C, and if both times are undefined, let the C
1718 library figure out what to do. This usually means
1721 if ( accessed == &PL_sv_undef && modified == &PL_sv_undef )
1724 Zero(&utbuf, sizeof utbuf, char);
1726 utbuf.actime = (Time_t)SvNVx(accessed); /* time accessed */
1727 utbuf.modtime = (Time_t)SvNVx(modified); /* time modified */
1729 utbuf.actime = (Time_t)SvIVx(accessed); /* time accessed */
1730 utbuf.modtime = (Time_t)SvIVx(modified); /* time modified */
1732 APPLY_TAINT_PROPER();
1734 while (++mark <= sp) {
1735 char *name = SvPVx(*mark, n_a);
1736 APPLY_TAINT_PROPER();
1737 if (PerlLIO_utime(name, utbufp))
1748 #undef APPLY_TAINT_PROPER
1751 /* Do the permissions allow some operation? Assumes statcache already set. */
1752 #ifndef VMS /* VMS' cando is in vms.c */
1754 Perl_cando(pTHX_ Mode_t mode, Uid_t effective, register Stat_t *statbufp)
1755 /* Note: we use `effective' both for uids and gids.
1756 * Here we are betting on Uid_t being equal or wider than Gid_t. */
1759 /* [Comments and code from Len Reed]
1760 * MS-DOS "user" is similar to UNIX's "superuser," but can't write
1761 * to write-protected files. The execute permission bit is set
1762 * by the Miscrosoft C library stat() function for the following:
1767 * All files and directories are readable.
1768 * Directories and special files, e.g. "CON", cannot be
1770 * [Comment by Tom Dinger -- a directory can have the write-protect
1771 * bit set in the file system, but DOS permits changes to
1772 * the directory anyway. In addition, all bets are off
1773 * here for networked software, such as Novell and
1777 /* Atari stat() does pretty much the same thing. we set x_bit_set_in_stat
1778 * too so it will actually look into the files for magic numbers
1780 return (mode & statbufp->st_mode) ? TRUE : FALSE;
1782 #else /* ! DOSISH */
1783 if ((effective ? PL_euid : PL_uid) == 0) { /* root is special */
1784 if (mode == S_IXUSR) {
1785 if (statbufp->st_mode & 0111 || S_ISDIR(statbufp->st_mode))
1789 return TRUE; /* root reads and writes anything */
1792 if (statbufp->st_uid == (effective ? PL_euid : PL_uid) ) {
1793 if (statbufp->st_mode & mode)
1794 return TRUE; /* ok as "user" */
1796 else if (ingroup(statbufp->st_gid,effective)) {
1797 if (statbufp->st_mode & mode >> 3)
1798 return TRUE; /* ok as "group" */
1800 else if (statbufp->st_mode & mode >> 6)
1801 return TRUE; /* ok as "other" */
1803 #endif /* ! DOSISH */
1808 Perl_ingroup(pTHX_ Gid_t testgid, Uid_t effective)
1810 #ifdef MACOS_TRADITIONAL
1811 /* This is simply not correct for AppleShare, but fix it yerself. */
1814 if (testgid == (effective ? PL_egid : PL_gid))
1816 #ifdef HAS_GETGROUPS
1821 Groups_t gary[NGROUPS];
1824 anum = getgroups(NGROUPS,gary);
1826 if (gary[anum] == testgid)
1834 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
1837 Perl_do_ipcget(pTHX_ I32 optype, SV **mark, SV **sp)
1842 key = (key_t)SvNVx(*++mark);
1843 n = (optype == OP_MSGGET) ? 0 : SvIVx(*++mark);
1844 flags = SvIVx(*++mark);
1850 return msgget(key, flags);
1854 return semget(key, n, flags);
1858 return shmget(key, n, flags);
1860 #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
1862 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
1865 return -1; /* should never happen */
1869 Perl_do_ipcctl(pTHX_ I32 optype, SV **mark, SV **sp)
1873 I32 id, n, cmd, infosize, getinfo;
1876 id = SvIVx(*++mark);
1877 n = (optype == OP_SEMCTL) ? SvIVx(*++mark) : 0;
1878 cmd = SvIVx(*++mark);
1881 getinfo = (cmd == IPC_STAT);
1887 if (cmd == IPC_STAT || cmd == IPC_SET)
1888 infosize = sizeof(struct msqid_ds);
1893 if (cmd == IPC_STAT || cmd == IPC_SET)
1894 infosize = sizeof(struct shmid_ds);
1900 if (cmd == IPC_STAT || cmd == IPC_SET)
1901 infosize = sizeof(struct semid_ds);
1902 else if (cmd == GETALL || cmd == SETALL)
1904 struct semid_ds semds;
1906 #ifdef EXTRA_F_IN_SEMUN_BUF
1907 semun.buff = &semds;
1911 getinfo = (cmd == GETALL);
1912 if (Semctl(id, 0, IPC_STAT, semun) == -1)
1914 infosize = semds.sem_nsems * sizeof(short);
1915 /* "short" is technically wrong but much more portable
1916 than guessing about u_?short(_t)? */
1919 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
1923 #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
1925 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
1934 SvPV_force(astr, len);
1935 a = SvGROW(astr, infosize+1);
1939 a = SvPV(astr, len);
1940 if (len != infosize)
1941 Perl_croak(aTHX_ "Bad arg length for %s, is %lu, should be %ld",
1950 a = INT2PTR(char *,i); /* ouch */
1957 ret = msgctl(id, cmd, (struct msqid_ds *)a);
1963 union semun unsemds;
1965 #ifdef EXTRA_F_IN_SEMUN_BUF
1966 unsemds.buff = (struct semid_ds *)a;
1968 unsemds.buf = (struct semid_ds *)a;
1970 ret = Semctl(id, n, cmd, unsemds);
1972 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
1979 ret = shmctl(id, cmd, (struct shmid_ds *)a);
1983 if (getinfo && ret >= 0) {
1984 SvCUR_set(astr, infosize);
1985 *SvEND(astr) = '\0';
1992 Perl_do_msgsnd(pTHX_ SV **mark, SV **sp)
1997 I32 id, msize, flags;
2000 id = SvIVx(*++mark);
2002 flags = SvIVx(*++mark);
2003 mbuf = SvPV(mstr, len);
2004 if ((msize = len - sizeof(long)) < 0)
2005 Perl_croak(aTHX_ "Arg too short for msgsnd");
2007 return msgsnd(id, (struct msgbuf *)mbuf, msize, flags);
2009 Perl_croak(aTHX_ "msgsnd not implemented");
2014 Perl_do_msgrcv(pTHX_ SV **mark, SV **sp)
2020 I32 id, msize, flags, ret;
2023 id = SvIVx(*++mark);
2025 /* suppress warning when reading into undef var --jhi */
2027 sv_setpvn(mstr, "", 0);
2028 msize = SvIVx(*++mark);
2029 mtype = (long)SvIVx(*++mark);
2030 flags = SvIVx(*++mark);
2031 SvPV_force(mstr, len);
2032 mbuf = SvGROW(mstr, sizeof(long)+msize+1);
2035 ret = msgrcv(id, (struct msgbuf *)mbuf, msize, mtype, flags);
2037 SvCUR_set(mstr, sizeof(long)+ret);
2038 *SvEND(mstr) = '\0';
2039 #ifndef INCOMPLETE_TAINTS
2040 /* who knows who has been playing with this message? */
2046 Perl_croak(aTHX_ "msgrcv not implemented");
2051 Perl_do_semop(pTHX_ SV **mark, SV **sp)
2059 id = SvIVx(*++mark);
2061 opbuf = SvPV(opstr, opsize);
2062 if (opsize < 3 * SHORTSIZE
2063 || (opsize % (3 * SHORTSIZE))) {
2064 SETERRNO(EINVAL,LIB$_INVARG);
2068 /* We can't assume that sizeof(struct sembuf) == 3 * sizeof(short). */
2070 int nsops = opsize / (3 * sizeof (short));
2072 short *ops = (short *) opbuf;
2074 struct sembuf *temps, *t;
2077 New (0, temps, nsops, struct sembuf);
2085 result = semop(id, temps, nsops);
2099 Perl_croak(aTHX_ "semop not implemented");
2104 Perl_do_shmio(pTHX_ I32 optype, SV **mark, SV **sp)
2109 I32 id, mpos, msize;
2111 struct shmid_ds shmds;
2113 id = SvIVx(*++mark);
2115 mpos = SvIVx(*++mark);
2116 msize = SvIVx(*++mark);
2118 if (shmctl(id, IPC_STAT, &shmds) == -1)
2120 if (mpos < 0 || msize < 0 || mpos + msize > shmds.shm_segsz) {
2121 SETERRNO(EFAULT,SS$_ACCVIO); /* can't do as caller requested */
2124 shm = (char *)shmat(id, (char*)NULL, (optype == OP_SHMREAD) ? SHM_RDONLY : 0);
2125 if (shm == (char *)-1) /* I hate System V IPC, I really do */
2127 if (optype == OP_SHMREAD) {
2128 /* suppress warning when reading into undef var (tchrist 3/Mar/00) */
2130 sv_setpvn(mstr, "", 0);
2131 SvPV_force(mstr, len);
2132 mbuf = SvGROW(mstr, msize+1);
2134 Copy(shm + mpos, mbuf, msize, char);
2135 SvCUR_set(mstr, msize);
2136 *SvEND(mstr) = '\0';
2138 #ifndef INCOMPLETE_TAINTS
2139 /* who knows who has been playing with this shared memory? */
2146 mbuf = SvPV(mstr, len);
2147 if ((n = len) > msize)
2149 Copy(mbuf, shm + mpos, n, char);
2151 memzero(shm + mpos + n, msize - n);
2155 Perl_croak(aTHX_ "shm I/O not implemented");
2159 #endif /* SYSV IPC */
2164 =for apidoc start_glob
2166 Function called by C<do_readline> to spawn a glob (or do the glob inside
2167 perl on VMS). This code used to be inline, but now perl uses C<File::Glob>
2168 this glob starter is only used by miniperl during the build process.
2169 Moving it away shrinks pp_hot.c; shrinking pp_hot.c helps speed perl up.
2175 Perl_start_glob (pTHX_ SV *tmpglob, IO *io)
2177 SV *tmpcmd = NEWSV(55, 0);
2181 #ifdef VMS /* expand the wildcards right here, rather than opening a pipe, */
2182 /* since spawning off a process is a real performance hit */
2184 #include <descrip.h>
2185 #include <lib$routines.h>
2188 char rslt[NAM$C_MAXRSS+1+sizeof(unsigned short int)] = {'\0','\0'};
2189 char vmsspec[NAM$C_MAXRSS+1];
2190 char *rstr = rslt + sizeof(unsigned short int), *begin, *end, *cp;
2191 $DESCRIPTOR(dfltdsc,"SYS$DISK:[]*.*;");
2194 struct dsc$descriptor_s wilddsc
2195 = {0, DSC$K_DTYPE_T, DSC$K_CLASS_S, 0};
2196 struct dsc$descriptor_vs rsdsc
2197 = {sizeof rslt, DSC$K_DTYPE_VT, DSC$K_CLASS_VS, rslt};
2198 unsigned long int cxt = 0, sts = 0, ok = 1, hasdir = 0, hasver = 0, isunix = 0;
2200 /* We could find out if there's an explicit dev/dir or version
2201 by peeking into lib$find_file's internal context at
2202 ((struct NAM *)((struct FAB *)cxt)->fab$l_nam)->nam$l_fnb
2203 but that's unsupported, so I don't want to do it now and
2204 have it bite someone in the future. */
2205 cp = SvPV(tmpglob,i);
2207 if (cp[i] == ';') hasver = 1;
2209 if (sts) hasver = 1;
2213 hasdir = isunix = 1;
2216 if (cp[i] == ']' || cp[i] == '>' || cp[i] == ':') {
2221 if ((tmpfp = PerlIO_tmpfile()) != NULL) {
2223 if (!PerlLIO_stat(SvPVX(tmpglob),&st) && S_ISDIR(st.st_mode))
2224 ok = ((wilddsc.dsc$a_pointer = tovmspath(SvPVX(tmpglob),vmsspec)) != NULL);
2225 else ok = ((wilddsc.dsc$a_pointer = tovmsspec(SvPVX(tmpglob),vmsspec)) != NULL);
2226 if (ok) wilddsc.dsc$w_length = (unsigned short int) strlen(wilddsc.dsc$a_pointer);
2227 for (cp=wilddsc.dsc$a_pointer; ok && cp && *cp; cp++)
2228 if (*cp == '?') *cp = '%'; /* VMS style single-char wildcard */
2229 while (ok && ((sts = lib$find_file(&wilddsc,&rsdsc,&cxt,
2230 &dfltdsc,NULL,NULL,NULL))&1)) {
2231 end = rstr + (unsigned long int) *rslt;
2232 if (!hasver) while (*end != ';') end--;
2233 *(end++) = '\n'; *end = '\0';
2234 for (cp = rstr; *cp; cp++) *cp = _tolower(*cp);
2236 if (isunix) trim_unixpath(rstr,SvPVX(tmpglob),1);
2241 while (*(--begin) != ']' && *begin != '>') ;
2244 ok = (PerlIO_puts(tmpfp,begin) != EOF);
2246 if (cxt) (void)lib$find_file_end(&cxt);
2247 if (ok && sts != RMS$_NMF &&
2248 sts != RMS$_DNF && sts != RMS$_FNF) ok = 0;
2251 SETERRNO((sts == RMS$_SYN ? EINVAL : EVMSERR),sts);
2253 PerlIO_close(tmpfp);
2257 PerlIO_rewind(tmpfp);
2258 IoTYPE(io) = IoTYPE_RDONLY;
2259 IoIFP(io) = fp = tmpfp;
2260 IoFLAGS(io) &= ~IOf_UNTAINT; /* maybe redundant */
2265 #ifdef MACOS_TRADITIONAL
2266 sv_setpv(tmpcmd, "glob ");
2267 sv_catsv(tmpcmd, tmpglob);
2268 sv_catpv(tmpcmd, " |");
2272 sv_setpv(tmpcmd, "for a in ");
2273 sv_catsv(tmpcmd, tmpglob);
2274 sv_catpv(tmpcmd, "; do echo \"$a\\0\\c\"; done |");
2277 sv_setpv(tmpcmd, "/dev/dosglob/"); /* File System Extension */
2278 sv_catsv(tmpcmd, tmpglob);
2280 sv_setpv(tmpcmd, "perlglob ");
2281 sv_catsv(tmpcmd, tmpglob);
2282 sv_catpv(tmpcmd, " |");
2287 sv_setpvn(tmpcmd, PL_cshname, PL_cshlen);
2288 sv_catpv(tmpcmd, " -cf 'set nonomatch; glob ");
2289 sv_catsv(tmpcmd, tmpglob);
2290 sv_catpv(tmpcmd, "' 2>/dev/null |");
2292 sv_setpv(tmpcmd, "echo ");
2293 sv_catsv(tmpcmd, tmpglob);
2295 sv_catpv(tmpcmd, "|tr -s ' \t\f\r' '\\012\\012\\012\\012'|");
2297 sv_catpv(tmpcmd, "|tr -s ' \t\f\r' '\\n\\n\\n\\n'|");
2300 #endif /* !DOSISH */
2301 #endif /* MACOS_TRADITIONAL */
2302 (void)do_open(PL_last_in_gv, SvPVX(tmpcmd), SvCUR(tmpcmd),
2303 FALSE, O_RDONLY, 0, Nullfp);