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));
327 for (; isSPACE(*type); type++) ;
328 if (num_svs && (SvIOK(*svp) || (SvPOK(*svp) && looks_like_number(*svp)))) {
332 else if (isDIGIT(*type)) {
338 thatio = sv_2io(*svp);
342 thatgv = gv_fetchpv(type,FALSE,SVt_PVIO);
343 thatio = GvIO(thatgv);
347 SETERRNO(EINVAL,SS_IVCHAN);
351 if ((that_fp = IoIFP(thatio))) {
352 /* Flush stdio buffer before dup. --mjd
353 * Unfortunately SEEK_CURing 0 seems to
354 * be optimized away on most platforms;
355 * only Solaris and Linux seem to flush
358 /* sfio fails to clear error on next
359 sfwrite, contrary to documentation.
361 if (PerlIO_seek(that_fp, 0, SEEK_CUR) == -1)
362 PerlIO_clearerr(that_fp);
364 /* On the other hand, do all platforms
365 * take gracefully to flushing a read-only
366 * filehandle? Perhaps we should do
367 * fsetpos(src)+fgetpos(dst)? --nik */
368 PerlIO_flush(that_fp);
369 fd = PerlIO_fileno(that_fp);
370 /* When dup()ing STDIN, STDOUT or STDERR
371 * explicitly set appropriate access mode */
372 if (that_fp == PerlIO_stdout()
373 || that_fp == PerlIO_stderr())
374 IoTYPE(io) = IoTYPE_WRONLY;
375 else if (that_fp == PerlIO_stdin())
376 IoTYPE(io) = IoTYPE_RDONLY;
377 /* When dup()ing a socket, say result is
379 else if (IoTYPE(thatio) == IoTYPE_SOCKET)
380 IoTYPE(io) = IoTYPE_SOCKET;
388 fp = PerlIO_fdupopen(aTHX_ that_fp, NULL, dodup);
392 fd = PerlLIO_dup(fd);
395 if (!(fp = PerlIO_openn(aTHX_ type,mode,fd,0,0,NULL,num_svs,svp))) {
404 for (; isSPACE(*type); type++) ;
405 if (*type == IoTYPE_STD && (!type[1] || isSPACE(type[1]) || type[1] == ':')) {
408 fp = PerlIO_stdout();
409 IoTYPE(io) = IoTYPE_STD;
411 Perl_croak(aTHX_ "More than one argument to '>%c' open",IoTYPE_STD);
416 namesv = sv_2mortal(newSVpvn(type,strlen(type)));
421 fp = PerlIO_openn(aTHX_ type,mode,-1,0,0,NULL,num_svs,svp);
425 else if (*type == IoTYPE_RDONLY) {
427 for (type++; isSPACE(*type); type++) ;
437 if (*type == IoTYPE_STD && (!type[1] || isSPACE(type[1]) || type[1] == ':')) {
441 IoTYPE(io) = IoTYPE_STD;
443 Perl_croak(aTHX_ "More than one argument to '<%c' open",IoTYPE_STD);
448 namesv = sv_2mortal(newSVpvn(type,strlen(type)));
453 fp = PerlIO_openn(aTHX_ type,mode,-1,0,0,NULL,num_svs,svp);
456 else if ((num_svs && type[0] == IoTYPE_STD && type[1] == IoTYPE_PIPE) ||
457 (!num_svs && tend > type+1 && tend[-1] == IoTYPE_PIPE)) {
459 type += 2; /* skip over '-|' */
463 while (tend > type && isSPACE(tend[-1]))
466 for (; isSPACE(*type); type++) ;
471 /* command is missing 19990114 */
472 if (ckWARN(WARN_PIPE))
473 Perl_warner(aTHX_ packWARN(WARN_PIPE), "Missing command in piped open");
477 if (strNE(name,"-") || num_svs)
479 TAINT_PROPER("piped open");
486 fp = PerlProc_popen_list(mode,num_svs,svp);
489 fp = PerlProc_popen(name,mode);
491 IoTYPE(io) = IoTYPE_PIPE;
493 for (; isSPACE(*type); type++) ;
495 if (PerlIO_apply_layers(aTHX_ fp, mode, type) != 0) {
505 IoTYPE(io) = IoTYPE_RDONLY;
507 for (; isSPACE(*name); name++) ;
513 if (strEQ(name,"-")) {
515 IoTYPE(io) = IoTYPE_STD;
519 namesv = sv_2mortal(newSVpvn(type,strlen(type)));
524 fp = PerlIO_openn(aTHX_ type,mode,-1,0,0,NULL,num_svs,svp);
529 if (ckWARN(WARN_NEWLINE) && IoTYPE(io) == IoTYPE_RDONLY && strchr(name, '\n'))
530 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "open");
534 if (ckWARN(WARN_IO)) {
535 if ((IoTYPE(io) == IoTYPE_RDONLY) &&
536 (fp == PerlIO_stdout() || fp == PerlIO_stderr())) {
537 Perl_warner(aTHX_ packWARN(WARN_IO),
538 "Filehandle STD%s reopened as %s only for input",
539 ((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 reopened as %s only for output",
549 fd = PerlIO_fileno(fp);
550 /* If there is no fd (e.g. PerlIO::scalar) assume it isn't a
551 * socket - this covers PerlIO::scalar - otherwise unless we "know" the
552 * type probe for socket-ness.
554 if (IoTYPE(io) && IoTYPE(io) != IoTYPE_PIPE && IoTYPE(io) != IoTYPE_STD && fd >= 0) {
555 if (PerlLIO_fstat(fd,&PL_statbuf) < 0) {
556 /* If PerlIO claims to have fd we had better be able to fstat() it. */
557 (void) PerlIO_close(fp);
561 if (S_ISSOCK(PL_statbuf.st_mode))
562 IoTYPE(io) = IoTYPE_SOCKET; /* in case a socket was passed in to us */
566 !(PL_statbuf.st_mode & S_IFMT)
570 && IoTYPE(io) != IoTYPE_WRONLY /* Dups of STD* filehandles already have */
571 && IoTYPE(io) != IoTYPE_RDONLY /* type so they aren't marked as sockets */
572 ) { /* on OS's that return 0 on fstat()ed pipe */
574 Sock_size_t buflen = sizeof tmpbuf;
575 if (PerlSock_getsockname(fd, (struct sockaddr *)tmpbuf, &buflen) >= 0
576 || errno != ENOTSOCK)
577 IoTYPE(io) = IoTYPE_SOCKET; /* some OS's return 0 on fstat()ed socket */
578 /* but some return 0 for streams too, sigh */
580 #endif /* HAS_SOCKET */
581 #endif /* !PERL_MICRO */
585 * If this is a standard handle we discard all the layer stuff
586 * and just dup the fd into whatever was on the handle before !
589 if (saveifp) { /* must use old fp? */
590 /* If fd is less that PL_maxsysfd i.e. STDIN..STDERR
591 then dup the new fileno down
594 PerlIO_flush(saveofp); /* emulate PerlIO_close() */
595 if (saveofp != saveifp) { /* was a socket? */
596 PerlIO_close(saveofp);
600 /* Still a small can-of-worms here if (say) PerlIO::scalar
601 is assigned to (say) STDOUT - for now let dup2() fail
602 and provide the error
604 if (PerlLIO_dup2(fd, savefd) < 0) {
605 (void)PerlIO_close(fp);
609 if (savefd != PerlIO_fileno(PerlIO_stdin())) {
610 char newname[FILENAME_MAX+1];
611 if (PerlIO_getname(fp, newname)) {
612 if (fd == PerlIO_fileno(PerlIO_stdout()))
613 Perl_vmssetuserlnm(aTHX_ "SYS$OUTPUT", newname);
614 if (fd == PerlIO_fileno(PerlIO_stderr()))
615 Perl_vmssetuserlnm(aTHX_ "SYS$ERROR", newname);
621 /* PL_fdpid isn't used on Windows, so avoid this useless work.
622 * XXX Probably the same for a lot of other places. */
628 sv = *av_fetch(PL_fdpid,fd,TRUE);
629 (void)SvUPGRADE(sv, SVt_IV);
632 sv = *av_fetch(PL_fdpid,savefd,TRUE);
633 (void)SvUPGRADE(sv, SVt_IV);
640 /* need to close fp without closing underlying fd */
641 int ofd = PerlIO_fileno(fp);
642 int dupfd = PerlLIO_dup(ofd);
644 PerlLIO_dup2(dupfd,ofd);
645 PerlLIO_close(dupfd);
652 fd = PerlIO_fileno(fp);
654 #if defined(HAS_FCNTL) && defined(F_SETFD)
656 int save_errno = errno;
657 fcntl(fd,F_SETFD,fd > PL_maxsysfd); /* can change errno */
663 IoFLAGS(io) &= ~IOf_NOLINE;
665 if (IoTYPE(io) == IoTYPE_SOCKET
666 || (IoTYPE(io) == IoTYPE_WRONLY && fd >= 0 && S_ISCHR(PL_statbuf.st_mode)) ) {
668 if (*s == 'I' || *s == '#')
671 if (!(IoOFP(io) = PerlIO_openn(aTHX_ type,s,fd,0,0,NULL,0,svp))) {
685 IoTYPE(io) = savetype;
690 Perl_nextargv(pTHX_ register GV *gv)
693 #ifndef FLEXFILENAMES
702 PL_argvoutgv = gv_fetchpv("ARGVOUT",TRUE,SVt_PVIO);
703 if (io && (IoFLAGS(io) & IOf_ARGV) && (IoFLAGS(io) & IOf_START)) {
704 IoFLAGS(io) &= ~IOf_START;
706 if (!PL_argvout_stack)
707 PL_argvout_stack = newAV();
708 av_push(PL_argvout_stack, SvREFCNT_inc(PL_defoutgv));
711 if (PL_filemode & (S_ISUID|S_ISGID)) {
712 PerlIO_flush(IoIFP(GvIOn(PL_argvoutgv))); /* chmod must follow last write */
714 (void)fchmod(PL_lastfd,PL_filemode);
716 (void)PerlLIO_chmod(PL_oldname,PL_filemode);
720 while (av_len(GvAV(gv)) >= 0) {
722 sv = av_shift(GvAV(gv));
724 sv_setsv(GvSV(gv),sv);
725 SvSETMAGIC(GvSV(gv));
726 PL_oldname = SvPVx(GvSV(gv), oldlen);
727 if (do_open(gv,PL_oldname,oldlen,PL_inplace!=0,O_RDONLY,0,Nullfp)) {
729 TAINT_PROPER("inplace open");
730 if (oldlen == 1 && *PL_oldname == '-') {
731 setdefout(gv_fetchpv("STDOUT",TRUE,SVt_PVIO));
732 return IoIFP(GvIOp(gv));
734 #ifndef FLEXFILENAMES
735 filedev = PL_statbuf.st_dev;
736 fileino = PL_statbuf.st_ino;
738 PL_filemode = PL_statbuf.st_mode;
739 fileuid = PL_statbuf.st_uid;
740 filegid = PL_statbuf.st_gid;
741 if (!S_ISREG(PL_filemode)) {
742 if (ckWARN_d(WARN_INPLACE))
743 Perl_warner(aTHX_ packWARN(WARN_INPLACE),
744 "Can't do inplace edit: %s is not a regular file",
750 char *star = strchr(PL_inplace, '*');
752 char *begin = PL_inplace;
753 sv_setpvn(sv, "", 0);
755 sv_catpvn(sv, begin, star - begin);
756 sv_catpvn(sv, PL_oldname, oldlen);
758 } while ((star = strchr(begin, '*')));
763 sv_catpv(sv,PL_inplace);
765 #ifndef FLEXFILENAMES
766 if ((PerlLIO_stat(SvPVX(sv),&PL_statbuf) >= 0
767 && PL_statbuf.st_dev == filedev
768 && PL_statbuf.st_ino == fileino)
770 || ((_djstat_fail_bits & _STFAIL_TRUENAME)!=0)
774 if (ckWARN_d(WARN_INPLACE))
775 Perl_warner(aTHX_ packWARN(WARN_INPLACE),
776 "Can't do inplace edit: %"SVf" would not be unique",
783 #if !defined(DOSISH) && !defined(__CYGWIN__) && !defined(EPOC)
784 if (PerlLIO_rename(PL_oldname,SvPVX(sv)) < 0) {
785 if (ckWARN_d(WARN_INPLACE))
786 Perl_warner(aTHX_ packWARN(WARN_INPLACE),
787 "Can't rename %s to %"SVf": %s, skipping file",
788 PL_oldname, sv, Strerror(errno) );
794 (void)PerlLIO_unlink(SvPVX(sv));
795 (void)PerlLIO_rename(PL_oldname,SvPVX(sv));
796 do_open(gv,SvPVX(sv),SvCUR(sv),PL_inplace!=0,O_RDONLY,0,Nullfp);
799 (void)UNLINK(SvPVX(sv));
800 if (link(PL_oldname,SvPVX(sv)) < 0) {
801 if (ckWARN_d(WARN_INPLACE))
802 Perl_warner(aTHX_ packWARN(WARN_INPLACE),
803 "Can't rename %s to %"SVf": %s, skipping file",
804 PL_oldname, sv, Strerror(errno) );
808 (void)UNLINK(PL_oldname);
812 #if !defined(DOSISH) && !defined(AMIGAOS)
813 # ifndef VMS /* Don't delete; use automatic file versioning */
814 if (UNLINK(PL_oldname) < 0) {
815 if (ckWARN_d(WARN_INPLACE))
816 Perl_warner(aTHX_ packWARN(WARN_INPLACE),
817 "Can't remove %s: %s, skipping file",
818 PL_oldname, Strerror(errno) );
824 Perl_croak(aTHX_ "Can't do inplace edit without backup");
828 sv_setpvn(sv,">",!PL_inplace);
829 sv_catpvn(sv,PL_oldname,oldlen);
830 SETERRNO(0,0); /* in case sprintf set errno */
832 if (!do_open(PL_argvoutgv,SvPVX(sv),SvCUR(sv),PL_inplace!=0,
833 O_WRONLY|O_CREAT|O_TRUNC,0,Nullfp))
835 if (!do_open(PL_argvoutgv,SvPVX(sv),SvCUR(sv),PL_inplace!=0,
836 O_WRONLY|O_CREAT|OPEN_EXCL,0666,Nullfp))
839 if (ckWARN_d(WARN_INPLACE))
840 Perl_warner(aTHX_ packWARN(WARN_INPLACE), "Can't do inplace edit on %s: %s",
841 PL_oldname, Strerror(errno) );
845 setdefout(PL_argvoutgv);
846 PL_lastfd = PerlIO_fileno(IoIFP(GvIOp(PL_argvoutgv)));
847 (void)PerlLIO_fstat(PL_lastfd,&PL_statbuf);
849 (void)fchmod(PL_lastfd,PL_filemode);
851 # if !(defined(WIN32) && defined(__BORLANDC__))
852 /* Borland runtime creates a readonly file! */
853 (void)PerlLIO_chmod(PL_oldname,PL_filemode);
856 if (fileuid != PL_statbuf.st_uid || filegid != PL_statbuf.st_gid) {
858 (void)fchown(PL_lastfd,fileuid,filegid);
861 (void)PerlLIO_chown(PL_oldname,fileuid,filegid);
866 return IoIFP(GvIOp(gv));
869 if (ckWARN_d(WARN_INPLACE)) {
871 if (PerlLIO_stat(PL_oldname, &PL_statbuf) >= 0
872 && !S_ISREG(PL_statbuf.st_mode))
874 Perl_warner(aTHX_ packWARN(WARN_INPLACE),
875 "Can't do inplace edit: %s is not a regular file",
879 Perl_warner(aTHX_ packWARN(WARN_INPLACE), "Can't open %s: %s",
880 PL_oldname, Strerror(eno));
884 if (io && (IoFLAGS(io) & IOf_ARGV))
885 IoFLAGS(io) |= IOf_START;
887 (void)do_close(PL_argvoutgv,FALSE);
888 if (io && (IoFLAGS(io) & IOf_ARGV)
889 && PL_argvout_stack && AvFILLp(PL_argvout_stack) >= 0)
891 GV *oldout = (GV*)av_pop(PL_argvout_stack);
893 SvREFCNT_dec(oldout);
896 setdefout(gv_fetchpv("STDOUT",TRUE,SVt_PVIO));
903 Perl_do_pipe(pTHX_ SV *sv, GV *rgv, GV *wgv)
922 if (PerlProc_pipe(fd) < 0)
924 IoIFP(rstio) = PerlIO_fdopen(fd[0], "r"PIPESOCK_MODE);
925 IoOFP(wstio) = PerlIO_fdopen(fd[1], "w"PIPESOCK_MODE);
926 IoOFP(rstio) = IoIFP(rstio);
927 IoIFP(wstio) = IoOFP(wstio);
928 IoTYPE(rstio) = IoTYPE_RDONLY;
929 IoTYPE(wstio) = IoTYPE_WRONLY;
930 if (!IoIFP(rstio) || !IoOFP(wstio)) {
931 if (IoIFP(rstio)) PerlIO_close(IoIFP(rstio));
932 else PerlLIO_close(fd[0]);
933 if (IoOFP(wstio)) PerlIO_close(IoOFP(wstio));
934 else PerlLIO_close(fd[1]);
938 sv_setsv(sv,&PL_sv_yes);
942 sv_setsv(sv,&PL_sv_undef);
947 /* explicit renamed to avoid C++ conflict -- kja */
949 Perl_do_close(pTHX_ GV *gv, bool not_implicit)
956 if (!gv || SvTYPE(gv) != SVt_PVGV) {
958 SETERRNO(EBADF,SS_IVCHAN);
962 if (!io) { /* never opened */
964 if (ckWARN(WARN_UNOPENED)) /* no check for closed here */
965 report_evil_fh(gv, io, PL_op->op_type);
966 SETERRNO(EBADF,SS_IVCHAN);
970 retval = io_close(io, not_implicit);
974 IoLINES_LEFT(io) = IoPAGE_LEN(io);
976 IoTYPE(io) = IoTYPE_CLOSED;
981 Perl_io_close(pTHX_ IO *io, bool not_implicit)
987 if (IoTYPE(io) == IoTYPE_PIPE) {
988 status = PerlProc_pclose(IoIFP(io));
990 STATUS_NATIVE_SET(status);
991 retval = (STATUS_POSIX == 0);
994 retval = (status != -1);
997 else if (IoTYPE(io) == IoTYPE_STD)
1000 if (IoOFP(io) && IoOFP(io) != IoIFP(io)) { /* a socket */
1001 retval = (PerlIO_close(IoOFP(io)) != EOF);
1002 PerlIO_close(IoIFP(io)); /* clear stdio, fd already closed */
1005 retval = (PerlIO_close(IoIFP(io)) != EOF);
1007 IoOFP(io) = IoIFP(io) = Nullfp;
1009 else if (not_implicit) {
1010 SETERRNO(EBADF,SS_IVCHAN);
1017 Perl_do_eof(pTHX_ GV *gv)
1026 else if (ckWARN(WARN_IO) && (IoTYPE(io) == IoTYPE_WRONLY))
1027 report_evil_fh(gv, io, OP_phoney_OUTPUT_ONLY);
1031 if (PerlIO_has_cntptr(IoIFP(io))) { /* (the code works without this) */
1032 if (PerlIO_get_cnt(IoIFP(io)) > 0) /* cheat a little, since */
1033 return FALSE; /* this is the most usual case */
1036 ch = PerlIO_getc(IoIFP(io));
1038 (void)PerlIO_ungetc(IoIFP(io),ch);
1042 if (PerlIO_has_cntptr(IoIFP(io)) && PerlIO_canset_cnt(IoIFP(io))) {
1043 if (PerlIO_get_cnt(IoIFP(io)) < -1)
1044 PerlIO_set_cnt(IoIFP(io),-1);
1046 if (PL_op->op_flags & OPf_SPECIAL) { /* not necessarily a real EOF yet? */
1047 if (gv != PL_argvgv || !nextargv(gv)) /* get another fp handy */
1051 return TRUE; /* normal fp, definitely end of file */
1057 Perl_do_tell(pTHX_ GV *gv)
1059 register IO *io = 0;
1060 register PerlIO *fp;
1062 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io))) {
1063 #ifdef ULTRIX_STDIO_BOTCH
1065 (void)PerlIO_seek(fp, 0L, 2); /* ultrix 1.2 workaround */
1067 return PerlIO_tell(fp);
1069 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1070 report_evil_fh(gv, io, PL_op->op_type);
1071 SETERRNO(EBADF,RMS_IFI);
1076 Perl_do_seek(pTHX_ GV *gv, Off_t pos, int whence)
1078 register IO *io = 0;
1079 register PerlIO *fp;
1081 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io))) {
1082 #ifdef ULTRIX_STDIO_BOTCH
1084 (void)PerlIO_seek(fp, 0L, 2); /* ultrix 1.2 workaround */
1086 return PerlIO_seek(fp, pos, whence) >= 0;
1088 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1089 report_evil_fh(gv, io, PL_op->op_type);
1090 SETERRNO(EBADF,RMS_IFI);
1095 Perl_do_sysseek(pTHX_ GV *gv, Off_t pos, int whence)
1097 register IO *io = 0;
1098 register PerlIO *fp;
1100 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io)))
1101 return PerlLIO_lseek(PerlIO_fileno(fp), pos, whence);
1102 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1103 report_evil_fh(gv, io, PL_op->op_type);
1104 SETERRNO(EBADF,RMS_IFI);
1109 Perl_mode_from_discipline(pTHX_ SV *discp)
1111 int mode = O_BINARY;
1114 char *s = SvPV(discp,len);
1119 if (len > 3 && strnEQ(s+1, "raw", 3)
1120 && (!s[4] || s[4] == ':' || isSPACE(s[4])))
1129 if (len > 4 && strnEQ(s+1, "crlf", 4)
1130 && (!s[5] || s[5] == ':' || isSPACE(s[5])))
1139 goto fail_discipline;
1142 else if (isSPACE(*s)) {
1149 end = strchr(s+1, ':');
1152 #ifndef PERLIO_LAYERS
1153 Perl_croak(aTHX_ "Unknown discipline '%.*s'", end-s, s);
1164 Perl_do_binmode(pTHX_ PerlIO *fp, int iotype, int mode)
1166 /* The old body of this is now in non-LAYER part of perlio.c
1167 * This is a stub for any XS code which might have been calling it.
1169 char *name = ":raw";
1170 #ifdef PERLIO_USING_CRLF
1171 if (!(mode & O_BINARY))
1174 return PerlIO_binmode(aTHX_ fp, iotype, mode, name);
1177 #if !defined(HAS_TRUNCATE) && !defined(HAS_CHSIZE) && defined(F_FREESP)
1178 /* code courtesy of William Kucharski */
1181 I32 my_chsize(fd, length)
1182 I32 fd; /* file descriptor */
1183 Off_t length; /* length to set file to */
1188 if (PerlLIO_fstat(fd, &filebuf) < 0)
1191 if (filebuf.st_size < length) {
1193 /* extend file length */
1195 if ((PerlLIO_lseek(fd, (length - 1), 0)) < 0)
1198 /* write a "0" byte */
1200 if ((PerlLIO_write(fd, "", 1)) != 1)
1204 /* truncate length */
1208 fl.l_start = length;
1209 fl.l_type = F_WRLCK; /* write lock on file space */
1212 * This relies on the UNDOCUMENTED F_FREESP argument to
1213 * fcntl(2), which truncates the file so that it ends at the
1214 * position indicated by fl.l_start.
1216 * Will minor miracles never cease?
1219 if (fcntl(fd, F_FREESP, &fl) < 0)
1226 #endif /* F_FREESP */
1229 Perl_do_print(pTHX_ register SV *sv, PerlIO *fp)
1231 register char *tmps;
1234 /* assuming fp is checked earlier */
1240 if (SvIOK(sv) && SvIVX(sv) != 0) {
1241 PerlIO_printf(fp, PL_ofmt, (NV)SvIVX(sv));
1242 return !PerlIO_error(fp);
1244 if ( (SvNOK(sv) && SvNVX(sv) != 0.0)
1245 || (looks_like_number(sv) && sv_2nv(sv) != 0.0) ) {
1246 PerlIO_printf(fp, PL_ofmt, SvNVX(sv));
1247 return !PerlIO_error(fp);
1250 switch (SvTYPE(sv)) {
1252 if (ckWARN(WARN_UNINITIALIZED))
1260 PerlIO_printf(fp, "%"UVuf, (UV)SvUVX(sv));
1262 PerlIO_printf(fp, "%"IVdf, (IV)SvIVX(sv));
1263 return !PerlIO_error(fp);
1267 if (PerlIO_isutf8(fp)) {
1269 sv_utf8_upgrade_flags(sv = sv_mortalcopy(sv),
1270 SV_GMAGIC|SV_UTF8_NO_ENCODING);
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
1749 * "current time". */
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 */
1763 APPLY_TAINT_PROPER();
1765 while (++mark <= sp) {
1766 char *name = SvPVx(*mark, n_a);
1767 APPLY_TAINT_PROPER();
1768 if (PerlLIO_utime(name, utbufp))
1779 #undef APPLY_TAINT_PROPER
1782 /* Do the permissions allow some operation? Assumes statcache already set. */
1783 #ifndef VMS /* VMS' cando is in vms.c */
1785 Perl_cando(pTHX_ Mode_t mode, Uid_t effective, register Stat_t *statbufp)
1786 /* Note: we use `effective' both for uids and gids.
1787 * Here we are betting on Uid_t being equal or wider than Gid_t. */
1790 /* [Comments and code from Len Reed]
1791 * MS-DOS "user" is similar to UNIX's "superuser," but can't write
1792 * to write-protected files. The execute permission bit is set
1793 * by the Miscrosoft C library stat() function for the following:
1798 * All files and directories are readable.
1799 * Directories and special files, e.g. "CON", cannot be
1801 * [Comment by Tom Dinger -- a directory can have the write-protect
1802 * bit set in the file system, but DOS permits changes to
1803 * the directory anyway. In addition, all bets are off
1804 * here for networked software, such as Novell and
1808 /* Atari stat() does pretty much the same thing. we set x_bit_set_in_stat
1809 * too so it will actually look into the files for magic numbers
1811 return (mode & statbufp->st_mode) ? TRUE : FALSE;
1813 #else /* ! DOSISH */
1814 if ((effective ? PL_euid : PL_uid) == 0) { /* root is special */
1815 if (mode == S_IXUSR) {
1816 if (statbufp->st_mode & 0111 || S_ISDIR(statbufp->st_mode))
1820 return TRUE; /* root reads and writes anything */
1823 if (statbufp->st_uid == (effective ? PL_euid : PL_uid) ) {
1824 if (statbufp->st_mode & mode)
1825 return TRUE; /* ok as "user" */
1827 else if (ingroup(statbufp->st_gid,effective)) {
1828 if (statbufp->st_mode & mode >> 3)
1829 return TRUE; /* ok as "group" */
1831 else if (statbufp->st_mode & mode >> 6)
1832 return TRUE; /* ok as "other" */
1834 #endif /* ! DOSISH */
1839 Perl_ingroup(pTHX_ Gid_t testgid, Uid_t effective)
1841 #ifdef MACOS_TRADITIONAL
1842 /* This is simply not correct for AppleShare, but fix it yerself. */
1845 if (testgid == (effective ? PL_egid : PL_gid))
1847 #ifdef HAS_GETGROUPS
1852 Groups_t gary[NGROUPS];
1855 anum = getgroups(NGROUPS,gary);
1857 if (gary[anum] == testgid)
1865 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
1868 Perl_do_ipcget(pTHX_ I32 optype, SV **mark, SV **sp)
1873 key = (key_t)SvNVx(*++mark);
1874 n = (optype == OP_MSGGET) ? 0 : SvIVx(*++mark);
1875 flags = SvIVx(*++mark);
1881 return msgget(key, flags);
1885 return semget(key, n, flags);
1889 return shmget(key, n, flags);
1891 #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
1893 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
1896 return -1; /* should never happen */
1900 Perl_do_ipcctl(pTHX_ I32 optype, SV **mark, SV **sp)
1904 I32 id, n, cmd, infosize, getinfo;
1907 id = SvIVx(*++mark);
1908 n = (optype == OP_SEMCTL) ? SvIVx(*++mark) : 0;
1909 cmd = SvIVx(*++mark);
1912 getinfo = (cmd == IPC_STAT);
1918 if (cmd == IPC_STAT || cmd == IPC_SET)
1919 infosize = sizeof(struct msqid_ds);
1924 if (cmd == IPC_STAT || cmd == IPC_SET)
1925 infosize = sizeof(struct shmid_ds);
1931 if (cmd == IPC_STAT || cmd == IPC_SET)
1932 infosize = sizeof(struct semid_ds);
1933 else if (cmd == GETALL || cmd == SETALL)
1935 struct semid_ds semds;
1937 #ifdef EXTRA_F_IN_SEMUN_BUF
1938 semun.buff = &semds;
1942 getinfo = (cmd == GETALL);
1943 if (Semctl(id, 0, IPC_STAT, semun) == -1)
1945 infosize = semds.sem_nsems * sizeof(short);
1946 /* "short" is technically wrong but much more portable
1947 than guessing about u_?short(_t)? */
1950 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
1954 #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
1956 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
1965 SvPV_force(astr, len);
1966 a = SvGROW(astr, infosize+1);
1970 a = SvPV(astr, len);
1971 if (len != infosize)
1972 Perl_croak(aTHX_ "Bad arg length for %s, is %lu, should be %ld",
1981 a = INT2PTR(char *,i); /* ouch */
1988 ret = msgctl(id, cmd, (struct msqid_ds *)a);
1994 union semun unsemds;
1996 #ifdef EXTRA_F_IN_SEMUN_BUF
1997 unsemds.buff = (struct semid_ds *)a;
1999 unsemds.buf = (struct semid_ds *)a;
2001 ret = Semctl(id, n, cmd, unsemds);
2003 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
2010 ret = shmctl(id, cmd, (struct shmid_ds *)a);
2014 if (getinfo && ret >= 0) {
2015 SvCUR_set(astr, infosize);
2016 *SvEND(astr) = '\0';
2023 Perl_do_msgsnd(pTHX_ SV **mark, SV **sp)
2028 I32 id, msize, flags;
2031 id = SvIVx(*++mark);
2033 flags = SvIVx(*++mark);
2034 mbuf = SvPV(mstr, len);
2035 if ((msize = len - sizeof(long)) < 0)
2036 Perl_croak(aTHX_ "Arg too short for msgsnd");
2038 return msgsnd(id, (struct msgbuf *)mbuf, msize, flags);
2040 Perl_croak(aTHX_ "msgsnd not implemented");
2045 Perl_do_msgrcv(pTHX_ SV **mark, SV **sp)
2051 I32 id, msize, flags, ret;
2054 id = SvIVx(*++mark);
2056 /* suppress warning when reading into undef var --jhi */
2058 sv_setpvn(mstr, "", 0);
2059 msize = SvIVx(*++mark);
2060 mtype = (long)SvIVx(*++mark);
2061 flags = SvIVx(*++mark);
2062 SvPV_force(mstr, len);
2063 mbuf = SvGROW(mstr, sizeof(long)+msize+1);
2066 ret = msgrcv(id, (struct msgbuf *)mbuf, msize, mtype, flags);
2068 SvCUR_set(mstr, sizeof(long)+ret);
2069 *SvEND(mstr) = '\0';
2070 #ifndef INCOMPLETE_TAINTS
2071 /* who knows who has been playing with this message? */
2077 Perl_croak(aTHX_ "msgrcv not implemented");
2082 Perl_do_semop(pTHX_ SV **mark, SV **sp)
2090 id = SvIVx(*++mark);
2092 opbuf = SvPV(opstr, opsize);
2093 if (opsize < 3 * SHORTSIZE
2094 || (opsize % (3 * SHORTSIZE))) {
2095 SETERRNO(EINVAL,LIB_INVARG);
2099 /* We can't assume that sizeof(struct sembuf) == 3 * sizeof(short). */
2101 int nsops = opsize / (3 * sizeof (short));
2103 short *ops = (short *) opbuf;
2105 struct sembuf *temps, *t;
2108 New (0, temps, nsops, struct sembuf);
2116 result = semop(id, temps, nsops);
2130 Perl_croak(aTHX_ "semop not implemented");
2135 Perl_do_shmio(pTHX_ I32 optype, SV **mark, SV **sp)
2140 I32 id, mpos, msize;
2142 struct shmid_ds shmds;
2144 id = SvIVx(*++mark);
2146 mpos = SvIVx(*++mark);
2147 msize = SvIVx(*++mark);
2149 if (shmctl(id, IPC_STAT, &shmds) == -1)
2151 if (mpos < 0 || msize < 0 || mpos + msize > shmds.shm_segsz) {
2152 SETERRNO(EFAULT,SS_ACCVIO); /* can't do as caller requested */
2155 shm = (char *)shmat(id, (char*)NULL, (optype == OP_SHMREAD) ? SHM_RDONLY : 0);
2156 if (shm == (char *)-1) /* I hate System V IPC, I really do */
2158 if (optype == OP_SHMREAD) {
2159 /* suppress warning when reading into undef var (tchrist 3/Mar/00) */
2161 sv_setpvn(mstr, "", 0);
2162 SvPV_force(mstr, len);
2163 mbuf = SvGROW(mstr, msize+1);
2165 Copy(shm + mpos, mbuf, msize, char);
2166 SvCUR_set(mstr, msize);
2167 *SvEND(mstr) = '\0';
2169 #ifndef INCOMPLETE_TAINTS
2170 /* who knows who has been playing with this shared memory? */
2177 mbuf = SvPV(mstr, len);
2178 if ((n = len) > msize)
2180 Copy(mbuf, shm + mpos, n, char);
2182 memzero(shm + mpos + n, msize - n);
2186 Perl_croak(aTHX_ "shm I/O not implemented");
2190 #endif /* SYSV IPC */
2195 =for apidoc start_glob
2197 Function called by C<do_readline> to spawn a glob (or do the glob inside
2198 perl on VMS). This code used to be inline, but now perl uses C<File::Glob>
2199 this glob starter is only used by miniperl during the build process.
2200 Moving it away shrinks pp_hot.c; shrinking pp_hot.c helps speed perl up.
2206 Perl_start_glob (pTHX_ SV *tmpglob, IO *io)
2208 SV *tmpcmd = NEWSV(55, 0);
2212 #ifdef VMS /* expand the wildcards right here, rather than opening a pipe, */
2213 /* since spawning off a process is a real performance hit */
2215 #include <descrip.h>
2216 #include <lib$routines.h>
2219 char rslt[NAM$C_MAXRSS+1+sizeof(unsigned short int)] = {'\0','\0'};
2220 char vmsspec[NAM$C_MAXRSS+1];
2221 char *rstr = rslt + sizeof(unsigned short int), *begin, *end, *cp;
2222 $DESCRIPTOR(dfltdsc,"SYS$DISK:[]*.*;");
2225 struct dsc$descriptor_s wilddsc
2226 = {0, DSC$K_DTYPE_T, DSC$K_CLASS_S, 0};
2227 struct dsc$descriptor_vs rsdsc
2228 = {sizeof rslt, DSC$K_DTYPE_VT, DSC$K_CLASS_VS, rslt};
2229 unsigned long int cxt = 0, sts = 0, ok = 1, hasdir = 0, hasver = 0, isunix = 0;
2231 /* We could find out if there's an explicit dev/dir or version
2232 by peeking into lib$find_file's internal context at
2233 ((struct NAM *)((struct FAB *)cxt)->fab$l_nam)->nam$l_fnb
2234 but that's unsupported, so I don't want to do it now and
2235 have it bite someone in the future. */
2236 cp = SvPV(tmpglob,i);
2238 if (cp[i] == ';') hasver = 1;
2240 if (sts) hasver = 1;
2244 hasdir = isunix = 1;
2247 if (cp[i] == ']' || cp[i] == '>' || cp[i] == ':') {
2252 if ((tmpfp = PerlIO_tmpfile()) != NULL) {
2254 if (!PerlLIO_stat(SvPVX(tmpglob),&st) && S_ISDIR(st.st_mode))
2255 ok = ((wilddsc.dsc$a_pointer = tovmspath(SvPVX(tmpglob),vmsspec)) != NULL);
2256 else ok = ((wilddsc.dsc$a_pointer = tovmsspec(SvPVX(tmpglob),vmsspec)) != NULL);
2257 if (ok) wilddsc.dsc$w_length = (unsigned short int) strlen(wilddsc.dsc$a_pointer);
2258 for (cp=wilddsc.dsc$a_pointer; ok && cp && *cp; cp++)
2259 if (*cp == '?') *cp = '%'; /* VMS style single-char wildcard */
2260 while (ok && ((sts = lib$find_file(&wilddsc,&rsdsc,&cxt,
2261 &dfltdsc,NULL,NULL,NULL))&1)) {
2262 end = rstr + (unsigned long int) *rslt;
2263 if (!hasver) while (*end != ';') end--;
2264 *(end++) = '\n'; *end = '\0';
2265 for (cp = rstr; *cp; cp++) *cp = _tolower(*cp);
2267 if (isunix) trim_unixpath(rstr,SvPVX(tmpglob),1);
2272 while (*(--begin) != ']' && *begin != '>') ;
2275 ok = (PerlIO_puts(tmpfp,begin) != EOF);
2277 if (cxt) (void)lib$find_file_end(&cxt);
2278 if (ok && sts != RMS$_NMF &&
2279 sts != RMS$_DNF && sts != RMS_FNF) ok = 0;
2282 SETERRNO((sts == RMS$_SYN ? EINVAL : EVMSERR),sts);
2284 PerlIO_close(tmpfp);
2288 PerlIO_rewind(tmpfp);
2289 IoTYPE(io) = IoTYPE_RDONLY;
2290 IoIFP(io) = fp = tmpfp;
2291 IoFLAGS(io) &= ~IOf_UNTAINT; /* maybe redundant */
2296 #ifdef MACOS_TRADITIONAL
2297 sv_setpv(tmpcmd, "glob ");
2298 sv_catsv(tmpcmd, tmpglob);
2299 sv_catpv(tmpcmd, " |");
2303 sv_setpv(tmpcmd, "for a in ");
2304 sv_catsv(tmpcmd, tmpglob);
2305 sv_catpv(tmpcmd, "; do echo \"$a\\0\\c\"; done |");
2308 sv_setpv(tmpcmd, "/dev/dosglob/"); /* File System Extension */
2309 sv_catsv(tmpcmd, tmpglob);
2311 sv_setpv(tmpcmd, "perlglob ");
2312 sv_catsv(tmpcmd, tmpglob);
2313 sv_catpv(tmpcmd, " |");
2318 sv_setpvn(tmpcmd, PL_cshname, PL_cshlen);
2319 sv_catpv(tmpcmd, " -cf 'set nonomatch; glob ");
2320 sv_catsv(tmpcmd, tmpglob);
2321 sv_catpv(tmpcmd, "' 2>/dev/null |");
2323 sv_setpv(tmpcmd, "echo ");
2324 sv_catsv(tmpcmd, tmpglob);
2326 sv_catpv(tmpcmd, "|tr -s ' \t\f\r' '\\012\\012\\012\\012'|");
2328 sv_catpv(tmpcmd, "|tr -s ' \t\f\r' '\\n\\n\\n\\n'|");
2331 #endif /* !DOSISH */
2332 #endif /* MACOS_TRADITIONAL */
2333 (void)do_open(PL_last_in_gv, SvPVX(tmpcmd), SvCUR(tmpcmd),
2334 FALSE, O_RDONLY, 0, Nullfp);