3 * Copyright (c) 1991-2003, 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_setpvn(PL_statname, s, len);
1338 s = SvPVX(PL_statname); /* s now NUL-terminated */
1339 PL_laststype = OP_STAT;
1340 PL_laststatval = PerlLIO_stat(s, &PL_statcache);
1341 if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(s, '\n'))
1342 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "stat");
1343 return PL_laststatval;
1353 if (PL_op->op_flags & OPf_REF) {
1355 if (cGVOP_gv == PL_defgv) {
1356 if (PL_laststype != OP_LSTAT)
1357 Perl_croak(aTHX_ "The stat preceding -l _ wasn't an lstat");
1358 return PL_laststatval;
1360 if (ckWARN(WARN_IO)) {
1361 Perl_warner(aTHX_ packWARN(WARN_IO), "Use of -l on filehandle %s",
1363 return (PL_laststatval = -1);
1367 PL_laststype = OP_LSTAT;
1371 if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV && ckWARN(WARN_IO)) {
1372 Perl_warner(aTHX_ packWARN(WARN_IO), "Use of -l on filehandle %s",
1373 GvENAME((GV*) SvRV(sv)));
1374 return (PL_laststatval = -1);
1376 sv_setpv(PL_statname,SvPV(sv, n_a));
1377 PL_laststatval = PerlLIO_lstat(SvPV(sv, n_a),&PL_statcache);
1378 if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(SvPV(sv, n_a), '\n'))
1379 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "lstat");
1380 return PL_laststatval;
1384 Perl_do_aexec(pTHX_ SV *really, register SV **mark, register SV **sp)
1386 return do_aexec5(really, mark, sp, 0, 0);
1390 Perl_do_aexec5(pTHX_ SV *really, register SV **mark, register SV **sp,
1391 int fd, int do_report)
1393 #ifdef MACOS_TRADITIONAL
1394 Perl_croak(aTHX_ "exec? I'm not *that* kind of operating system");
1397 char *tmps = Nullch;
1401 New(401,PL_Argv, sp - mark + 1, char*);
1403 while (++mark <= sp) {
1405 *a++ = SvPVx(*mark, n_a);
1411 tmps = SvPV(really, n_a);
1412 if ((!really && *PL_Argv[0] != '/') ||
1413 (really && *tmps != '/')) /* will execvp use PATH? */
1414 TAINT_ENV(); /* testing IFS here is overkill, probably */
1415 if (really && *tmps)
1416 PerlProc_execvp(tmps,EXEC_ARGV_CAST(PL_Argv));
1418 PerlProc_execvp(PL_Argv[0],EXEC_ARGV_CAST(PL_Argv));
1419 if (ckWARN(WARN_EXEC))
1420 Perl_warner(aTHX_ packWARN(WARN_EXEC), "Can't exec \"%s\": %s",
1421 (really ? tmps : PL_Argv[0]), Strerror(errno));
1425 PerlLIO_write(fd, (void*)&e, sizeof(int));
1435 Perl_do_execfree(pTHX)
1439 PL_Argv = Null(char **);
1447 #if !defined(OS2) && !defined(WIN32) && !defined(DJGPP) && !defined(EPOC) && !defined(MACOS_TRADITIONAL)
1450 Perl_do_exec(pTHX_ char *cmd)
1452 return do_exec3(cmd,0,0);
1456 Perl_do_exec3(pTHX_ char *cmd, int fd, int do_report)
1461 while (*cmd && isSPACE(*cmd))
1464 /* save an extra exec if possible */
1469 if (strnEQ(cmd,PL_cshname,PL_cshlen) &&
1470 strnEQ(cmd+PL_cshlen," -c",3)) {
1472 s = cmd+PL_cshlen+3;
1486 if (s[-1] == '\'') {
1488 PerlProc_execl(PL_cshname,"csh", flags, ncmd, (char*)0);
1497 /* see if there are shell metacharacters in it */
1499 if (*cmd == '.' && isSPACE(cmd[1]))
1502 if (strnEQ(cmd,"exec",4) && isSPACE(cmd[4]))
1505 for (s = cmd; *s && isALNUM(*s); s++) ; /* catch VAR=val gizmo */
1509 for (s = cmd; *s; s++) {
1510 if (*s != ' ' && !isALPHA(*s) &&
1511 strchr("$&*(){}[]'\";\\|?<>~`\n",*s)) {
1512 if (*s == '\n' && !s[1]) {
1516 /* handle the 2>&1 construct at the end */
1517 if (*s == '>' && s[1] == '&' && s[2] == '1'
1518 && s > cmd + 1 && s[-1] == '2' && isSPACE(s[-2])
1519 && (!s[3] || isSPACE(s[3])))
1523 while (*t && isSPACE(*t))
1525 if (!*t && (dup2(1,2) != -1)) {
1531 PerlProc_execl(PL_sh_path, "sh", "-c", cmd, (char*)0);
1536 New(402,PL_Argv, (s - cmd) / 2 + 2, char*);
1537 PL_Cmd = savepvn(cmd, s-cmd);
1539 for (s = PL_Cmd; *s;) {
1540 while (*s && isSPACE(*s)) s++;
1543 while (*s && !isSPACE(*s)) s++;
1549 PerlProc_execvp(PL_Argv[0],PL_Argv);
1550 if (errno == ENOEXEC) { /* for system V NIH syndrome */
1557 if (ckWARN(WARN_EXEC))
1558 Perl_warner(aTHX_ packWARN(WARN_EXEC), "Can't exec \"%s\": %s",
1559 PL_Argv[0], Strerror(errno));
1561 PerlLIO_write(fd, (void*)&e, sizeof(int));
1570 #endif /* OS2 || WIN32 */
1573 Perl_apply(pTHX_ I32 type, register SV **mark, register SV **sp)
1577 register I32 tot = 0;
1580 SV **oldmark = mark;
1583 #define APPLY_TAINT_PROPER() \
1585 if (PL_tainted) { TAINT_PROPER(what); } \
1588 /* This is a first heuristic; it doesn't catch tainting magic. */
1590 while (++mark <= sp) {
1591 if (SvTAINTED(*mark)) {
1601 APPLY_TAINT_PROPER();
1604 APPLY_TAINT_PROPER();
1606 while (++mark <= sp) {
1607 char *name = SvPVx(*mark, n_a);
1608 APPLY_TAINT_PROPER();
1609 if (PerlLIO_chmod(name, val))
1617 APPLY_TAINT_PROPER();
1618 if (sp - mark > 2) {
1619 val = SvIVx(*++mark);
1620 val2 = SvIVx(*++mark);
1621 APPLY_TAINT_PROPER();
1623 while (++mark <= sp) {
1624 char *name = SvPVx(*mark, n_a);
1625 APPLY_TAINT_PROPER();
1626 if (PerlLIO_chown(name, val, val2))
1633 XXX Should we make lchown() directly available from perl?
1634 For now, we'll let Configure test for HAS_LCHOWN, but do
1635 nothing in the core.
1641 APPLY_TAINT_PROPER();
1644 s = SvPVx(*++mark, n_a);
1646 if (*s == 'S' && s[1] == 'I' && s[2] == 'G')
1648 if ((val = whichsig(s)) < 0)
1649 Perl_croak(aTHX_ "Unrecognized signal name \"%s\"",s);
1653 APPLY_TAINT_PROPER();
1656 /* kill() doesn't do process groups (job trees?) under VMS */
1657 if (val < 0) val = -val;
1658 if (val == SIGKILL) {
1659 # include <starlet.h>
1660 /* Use native sys$delprc() to insure that target process is
1661 * deleted; supervisor-mode images don't pay attention to
1662 * CRTL's emulation of Unix-style signals and kill()
1664 while (++mark <= sp) {
1665 I32 proc = SvIVx(*mark);
1666 register unsigned long int __vmssts;
1667 APPLY_TAINT_PROPER();
1668 if (!((__vmssts = sys$delprc(&proc,0)) & 1)) {
1672 case SS$_NOSUCHNODE:
1673 SETERRNO(ESRCH,__vmssts);
1676 SETERRNO(EPERM,__vmssts);
1679 SETERRNO(EVMSERR,__vmssts);
1688 while (++mark <= sp) {
1689 I32 proc = SvIVx(*mark);
1690 APPLY_TAINT_PROPER();
1692 if (PerlProc_killpg(proc,val)) /* BSD */
1694 if (PerlProc_kill(-proc,val)) /* SYSV */
1700 while (++mark <= sp) {
1701 I32 proc = SvIVx(*mark);
1702 APPLY_TAINT_PROPER();
1703 if (PerlProc_kill(proc, val))
1711 APPLY_TAINT_PROPER();
1713 while (++mark <= sp) {
1714 s = SvPVx(*mark, n_a);
1715 APPLY_TAINT_PROPER();
1716 if (PL_euid || PL_unsafe) {
1720 else { /* don't let root wipe out directories without -U */
1721 if (PerlLIO_lstat(s,&PL_statbuf) < 0 || S_ISDIR(PL_statbuf.st_mode))
1733 APPLY_TAINT_PROPER();
1734 if (sp - mark > 2) {
1735 #if defined(I_UTIME) || defined(VMS)
1736 struct utimbuf utbuf;
1744 SV* accessed = *++mark;
1745 SV* modified = *++mark;
1746 void * utbufp = &utbuf;
1748 /* Be like C, and if both times are undefined, let the C
1749 * library figure out what to do. This usually means
1750 * "current time". */
1752 if ( accessed == &PL_sv_undef && modified == &PL_sv_undef )
1755 Zero(&utbuf, sizeof utbuf, char);
1757 utbuf.actime = (Time_t)SvNVx(accessed); /* time accessed */
1758 utbuf.modtime = (Time_t)SvNVx(modified); /* time modified */
1760 utbuf.actime = (Time_t)SvIVx(accessed); /* time accessed */
1761 utbuf.modtime = (Time_t)SvIVx(modified); /* time modified */
1764 APPLY_TAINT_PROPER();
1766 while (++mark <= sp) {
1767 char *name = SvPVx(*mark, n_a);
1768 APPLY_TAINT_PROPER();
1769 if (PerlLIO_utime(name, utbufp))
1780 #undef APPLY_TAINT_PROPER
1783 /* Do the permissions allow some operation? Assumes statcache already set. */
1784 #ifndef VMS /* VMS' cando is in vms.c */
1786 Perl_cando(pTHX_ Mode_t mode, Uid_t effective, register Stat_t *statbufp)
1787 /* Note: we use `effective' both for uids and gids.
1788 * Here we are betting on Uid_t being equal or wider than Gid_t. */
1791 /* [Comments and code from Len Reed]
1792 * MS-DOS "user" is similar to UNIX's "superuser," but can't write
1793 * to write-protected files. The execute permission bit is set
1794 * by the Miscrosoft C library stat() function for the following:
1799 * All files and directories are readable.
1800 * Directories and special files, e.g. "CON", cannot be
1802 * [Comment by Tom Dinger -- a directory can have the write-protect
1803 * bit set in the file system, but DOS permits changes to
1804 * the directory anyway. In addition, all bets are off
1805 * here for networked software, such as Novell and
1809 /* Atari stat() does pretty much the same thing. we set x_bit_set_in_stat
1810 * too so it will actually look into the files for magic numbers
1812 return (mode & statbufp->st_mode) ? TRUE : FALSE;
1814 #else /* ! DOSISH */
1815 if ((effective ? PL_euid : PL_uid) == 0) { /* root is special */
1816 if (mode == S_IXUSR) {
1817 if (statbufp->st_mode & 0111 || S_ISDIR(statbufp->st_mode))
1821 return TRUE; /* root reads and writes anything */
1824 if (statbufp->st_uid == (effective ? PL_euid : PL_uid) ) {
1825 if (statbufp->st_mode & mode)
1826 return TRUE; /* ok as "user" */
1828 else if (ingroup(statbufp->st_gid,effective)) {
1829 if (statbufp->st_mode & mode >> 3)
1830 return TRUE; /* ok as "group" */
1832 else if (statbufp->st_mode & mode >> 6)
1833 return TRUE; /* ok as "other" */
1835 #endif /* ! DOSISH */
1840 Perl_ingroup(pTHX_ Gid_t testgid, Uid_t effective)
1842 #ifdef MACOS_TRADITIONAL
1843 /* This is simply not correct for AppleShare, but fix it yerself. */
1846 if (testgid == (effective ? PL_egid : PL_gid))
1848 #ifdef HAS_GETGROUPS
1853 Groups_t gary[NGROUPS];
1856 anum = getgroups(NGROUPS,gary);
1858 if (gary[anum] == testgid)
1866 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
1869 Perl_do_ipcget(pTHX_ I32 optype, SV **mark, SV **sp)
1874 key = (key_t)SvNVx(*++mark);
1875 n = (optype == OP_MSGGET) ? 0 : SvIVx(*++mark);
1876 flags = SvIVx(*++mark);
1882 return msgget(key, flags);
1886 return semget(key, n, flags);
1890 return shmget(key, n, flags);
1892 #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
1894 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
1897 return -1; /* should never happen */
1901 Perl_do_ipcctl(pTHX_ I32 optype, SV **mark, SV **sp)
1905 I32 id, n, cmd, infosize, getinfo;
1908 id = SvIVx(*++mark);
1909 n = (optype == OP_SEMCTL) ? SvIVx(*++mark) : 0;
1910 cmd = SvIVx(*++mark);
1913 getinfo = (cmd == IPC_STAT);
1919 if (cmd == IPC_STAT || cmd == IPC_SET)
1920 infosize = sizeof(struct msqid_ds);
1925 if (cmd == IPC_STAT || cmd == IPC_SET)
1926 infosize = sizeof(struct shmid_ds);
1932 if (cmd == IPC_STAT || cmd == IPC_SET)
1933 infosize = sizeof(struct semid_ds);
1934 else if (cmd == GETALL || cmd == SETALL)
1936 struct semid_ds semds;
1938 #ifdef EXTRA_F_IN_SEMUN_BUF
1939 semun.buff = &semds;
1943 getinfo = (cmd == GETALL);
1944 if (Semctl(id, 0, IPC_STAT, semun) == -1)
1946 infosize = semds.sem_nsems * sizeof(short);
1947 /* "short" is technically wrong but much more portable
1948 than guessing about u_?short(_t)? */
1951 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
1955 #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
1957 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
1966 SvPV_force(astr, len);
1967 a = SvGROW(astr, infosize+1);
1971 a = SvPV(astr, len);
1972 if (len != infosize)
1973 Perl_croak(aTHX_ "Bad arg length for %s, is %lu, should be %ld",
1982 a = INT2PTR(char *,i); /* ouch */
1989 ret = msgctl(id, cmd, (struct msqid_ds *)a);
1995 union semun unsemds;
1997 #ifdef EXTRA_F_IN_SEMUN_BUF
1998 unsemds.buff = (struct semid_ds *)a;
2000 unsemds.buf = (struct semid_ds *)a;
2002 ret = Semctl(id, n, cmd, unsemds);
2004 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
2011 ret = shmctl(id, cmd, (struct shmid_ds *)a);
2015 if (getinfo && ret >= 0) {
2016 SvCUR_set(astr, infosize);
2017 *SvEND(astr) = '\0';
2024 Perl_do_msgsnd(pTHX_ SV **mark, SV **sp)
2029 I32 id, msize, flags;
2032 id = SvIVx(*++mark);
2034 flags = SvIVx(*++mark);
2035 mbuf = SvPV(mstr, len);
2036 if ((msize = len - sizeof(long)) < 0)
2037 Perl_croak(aTHX_ "Arg too short for msgsnd");
2039 return msgsnd(id, (struct msgbuf *)mbuf, msize, flags);
2041 Perl_croak(aTHX_ "msgsnd not implemented");
2046 Perl_do_msgrcv(pTHX_ SV **mark, SV **sp)
2052 I32 id, msize, flags, ret;
2055 id = SvIVx(*++mark);
2057 /* suppress warning when reading into undef var --jhi */
2059 sv_setpvn(mstr, "", 0);
2060 msize = SvIVx(*++mark);
2061 mtype = (long)SvIVx(*++mark);
2062 flags = SvIVx(*++mark);
2063 SvPV_force(mstr, len);
2064 mbuf = SvGROW(mstr, sizeof(long)+msize+1);
2067 ret = msgrcv(id, (struct msgbuf *)mbuf, msize, mtype, flags);
2069 SvCUR_set(mstr, sizeof(long)+ret);
2070 *SvEND(mstr) = '\0';
2071 #ifndef INCOMPLETE_TAINTS
2072 /* who knows who has been playing with this message? */
2078 Perl_croak(aTHX_ "msgrcv not implemented");
2083 Perl_do_semop(pTHX_ SV **mark, SV **sp)
2091 id = SvIVx(*++mark);
2093 opbuf = SvPV(opstr, opsize);
2094 if (opsize < 3 * SHORTSIZE
2095 || (opsize % (3 * SHORTSIZE))) {
2096 SETERRNO(EINVAL,LIB_INVARG);
2100 /* We can't assume that sizeof(struct sembuf) == 3 * sizeof(short). */
2102 int nsops = opsize / (3 * sizeof (short));
2104 short *ops = (short *) opbuf;
2106 struct sembuf *temps, *t;
2109 New (0, temps, nsops, struct sembuf);
2117 result = semop(id, temps, nsops);
2131 Perl_croak(aTHX_ "semop not implemented");
2136 Perl_do_shmio(pTHX_ I32 optype, SV **mark, SV **sp)
2141 I32 id, mpos, msize;
2143 struct shmid_ds shmds;
2145 id = SvIVx(*++mark);
2147 mpos = SvIVx(*++mark);
2148 msize = SvIVx(*++mark);
2150 if (shmctl(id, IPC_STAT, &shmds) == -1)
2152 if (mpos < 0 || msize < 0 || mpos + msize > shmds.shm_segsz) {
2153 SETERRNO(EFAULT,SS_ACCVIO); /* can't do as caller requested */
2156 shm = (char *)shmat(id, (char*)NULL, (optype == OP_SHMREAD) ? SHM_RDONLY : 0);
2157 if (shm == (char *)-1) /* I hate System V IPC, I really do */
2159 if (optype == OP_SHMREAD) {
2160 /* suppress warning when reading into undef var (tchrist 3/Mar/00) */
2162 sv_setpvn(mstr, "", 0);
2163 SvPV_force(mstr, len);
2164 mbuf = SvGROW(mstr, msize+1);
2166 Copy(shm + mpos, mbuf, msize, char);
2167 SvCUR_set(mstr, msize);
2168 *SvEND(mstr) = '\0';
2170 #ifndef INCOMPLETE_TAINTS
2171 /* who knows who has been playing with this shared memory? */
2178 mbuf = SvPV(mstr, len);
2179 if ((n = len) > msize)
2181 Copy(mbuf, shm + mpos, n, char);
2183 memzero(shm + mpos + n, msize - n);
2187 Perl_croak(aTHX_ "shm I/O not implemented");
2191 #endif /* SYSV IPC */
2196 =for apidoc start_glob
2198 Function called by C<do_readline> to spawn a glob (or do the glob inside
2199 perl on VMS). This code used to be inline, but now perl uses C<File::Glob>
2200 this glob starter is only used by miniperl during the build process.
2201 Moving it away shrinks pp_hot.c; shrinking pp_hot.c helps speed perl up.
2207 Perl_start_glob (pTHX_ SV *tmpglob, IO *io)
2209 SV *tmpcmd = NEWSV(55, 0);
2213 #ifdef VMS /* expand the wildcards right here, rather than opening a pipe, */
2214 /* since spawning off a process is a real performance hit */
2216 #include <descrip.h>
2217 #include <lib$routines.h>
2220 char rslt[NAM$C_MAXRSS+1+sizeof(unsigned short int)] = {'\0','\0'};
2221 char vmsspec[NAM$C_MAXRSS+1];
2222 char *rstr = rslt + sizeof(unsigned short int), *begin, *end, *cp;
2223 $DESCRIPTOR(dfltdsc,"SYS$DISK:[]*.*;");
2226 struct dsc$descriptor_s wilddsc
2227 = {0, DSC$K_DTYPE_T, DSC$K_CLASS_S, 0};
2228 struct dsc$descriptor_vs rsdsc
2229 = {sizeof rslt, DSC$K_DTYPE_VT, DSC$K_CLASS_VS, rslt};
2230 unsigned long int cxt = 0, sts = 0, ok = 1, hasdir = 0, hasver = 0, isunix = 0;
2232 /* We could find out if there's an explicit dev/dir or version
2233 by peeking into lib$find_file's internal context at
2234 ((struct NAM *)((struct FAB *)cxt)->fab$l_nam)->nam$l_fnb
2235 but that's unsupported, so I don't want to do it now and
2236 have it bite someone in the future. */
2237 cp = SvPV(tmpglob,i);
2239 if (cp[i] == ';') hasver = 1;
2241 if (sts) hasver = 1;
2245 hasdir = isunix = 1;
2248 if (cp[i] == ']' || cp[i] == '>' || cp[i] == ':') {
2253 if ((tmpfp = PerlIO_tmpfile()) != NULL) {
2255 if (!PerlLIO_stat(SvPVX(tmpglob),&st) && S_ISDIR(st.st_mode))
2256 ok = ((wilddsc.dsc$a_pointer = tovmspath(SvPVX(tmpglob),vmsspec)) != NULL);
2257 else ok = ((wilddsc.dsc$a_pointer = tovmsspec(SvPVX(tmpglob),vmsspec)) != NULL);
2258 if (ok) wilddsc.dsc$w_length = (unsigned short int) strlen(wilddsc.dsc$a_pointer);
2259 for (cp=wilddsc.dsc$a_pointer; ok && cp && *cp; cp++)
2260 if (*cp == '?') *cp = '%'; /* VMS style single-char wildcard */
2261 while (ok && ((sts = lib$find_file(&wilddsc,&rsdsc,&cxt,
2262 &dfltdsc,NULL,NULL,NULL))&1)) {
2263 end = rstr + (unsigned long int) *rslt;
2264 if (!hasver) while (*end != ';') end--;
2265 *(end++) = '\n'; *end = '\0';
2266 for (cp = rstr; *cp; cp++) *cp = _tolower(*cp);
2268 if (isunix) trim_unixpath(rstr,SvPVX(tmpglob),1);
2273 while (*(--begin) != ']' && *begin != '>') ;
2276 ok = (PerlIO_puts(tmpfp,begin) != EOF);
2278 if (cxt) (void)lib$find_file_end(&cxt);
2279 if (ok && sts != RMS$_NMF &&
2280 sts != RMS$_DNF && sts != RMS_FNF) ok = 0;
2283 SETERRNO((sts == RMS$_SYN ? EINVAL : EVMSERR),sts);
2285 PerlIO_close(tmpfp);
2289 PerlIO_rewind(tmpfp);
2290 IoTYPE(io) = IoTYPE_RDONLY;
2291 IoIFP(io) = fp = tmpfp;
2292 IoFLAGS(io) &= ~IOf_UNTAINT; /* maybe redundant */
2297 #ifdef MACOS_TRADITIONAL
2298 sv_setpv(tmpcmd, "glob ");
2299 sv_catsv(tmpcmd, tmpglob);
2300 sv_catpv(tmpcmd, " |");
2304 sv_setpv(tmpcmd, "for a in ");
2305 sv_catsv(tmpcmd, tmpglob);
2306 sv_catpv(tmpcmd, "; do echo \"$a\\0\\c\"; done |");
2309 sv_setpv(tmpcmd, "/dev/dosglob/"); /* File System Extension */
2310 sv_catsv(tmpcmd, tmpglob);
2312 sv_setpv(tmpcmd, "perlglob ");
2313 sv_catsv(tmpcmd, tmpglob);
2314 sv_catpv(tmpcmd, " |");
2319 sv_setpvn(tmpcmd, PL_cshname, PL_cshlen);
2320 sv_catpv(tmpcmd, " -cf 'set nonomatch; glob ");
2321 sv_catsv(tmpcmd, tmpglob);
2322 sv_catpv(tmpcmd, "' 2>/dev/null |");
2324 sv_setpv(tmpcmd, "echo ");
2325 sv_catsv(tmpcmd, tmpglob);
2327 sv_catpv(tmpcmd, "|tr -s ' \t\f\r' '\\012\\012\\012\\012'|");
2329 sv_catpv(tmpcmd, "|tr -s ' \t\f\r' '\\n\\n\\n\\n'|");
2332 #endif /* !DOSISH */
2333 #endif /* MACOS_TRADITIONAL */
2334 (void)do_open(PL_last_in_gv, SvPVX(tmpcmd), SvCUR(tmpcmd),
2335 FALSE, O_RDONLY, 0, Nullfp);