3 * Copyright (c) 1991-2001, 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 */
144 Perl_croak(aTHX_ "panic: sysopen with multiple args");
146 if (rawmode & (O_WRONLY|O_RDWR|O_CREAT
147 #ifdef O_APPEND /* Not fully portable. */
150 #ifdef O_TRUNC /* Not fully portable. */
154 TAINT_PROPER("sysopen");
155 mode[ix++] = '#'; /* Marker to openn to use numeric "sysopen" */
157 #if defined(USE_64_BIT_RAWIO) && defined(O_LARGEFILE)
158 rawmode |= O_LARGEFILE; /* Transparently largefiley. */
161 IoTYPE(io) = PerlIO_intmode2str(rawmode, &mode[ix], &writing);
163 namesv = sv_2mortal(newSVpvn(name,strlen(name)));
167 fp = PerlIO_openn(aTHX_ type, mode, -1, rawmode, rawperm, NULL, num_svs, svp);
170 /* Regular (non-sys) open */
176 type = savepvn(name, len);
179 /* Loose trailing white space */
180 while (tend > type && isSPACE(tend[-1]))
183 /* New style explict name, type is just mode and discipline/layer info */
185 name = SvOK(*svp) ? SvPV(*svp, l) : "";
187 name = savepvn(name, len);
190 for (; isSPACE(*type); type++) ;
197 if ((*type == IoTYPE_RDWR) && /* scary */
198 (*(type+1) == IoTYPE_RDONLY || *(type+1) == IoTYPE_WRONLY) &&
199 ((!num_svs || (tend > type+1 && tend[-1] != IoTYPE_PIPE)))) {
200 TAINT_PROPER("open");
205 if (*type == IoTYPE_PIPE) {
207 if (type[1] != IoTYPE_STD) {
209 Perl_croak(aTHX_ "Unknown open() mode '%.*s'", (int)olen, oname);
214 for (type++; isSPACE(*type); type++) ;
220 /* command is missing 19990114 */
221 if (ckWARN(WARN_PIPE))
222 Perl_warner(aTHX_ WARN_PIPE, "Missing command in piped open");
226 if (strNE(name,"-") || num_svs)
228 TAINT_PROPER("piped open");
229 if (!num_svs && name[len-1] == '|') {
231 if (ckWARN(WARN_PIPE))
232 Perl_warner(aTHX_ WARN_PIPE, "Can't open bidirectional pipe");
241 fp = PerlProc_popen_list(mode, num_svs, svp);
244 fp = PerlProc_popen(name,mode);
247 else if (*type == IoTYPE_WRONLY) {
248 TAINT_PROPER("open");
250 if (*type == IoTYPE_WRONLY) {
251 /* Two IoTYPE_WRONLYs in a row make for an IoTYPE_APPEND. */
252 mode[0] = IoTYPE(io) = IoTYPE_APPEND;
273 if (!num_svs && !*type && supplied_fp) {
274 /* "<+&" etc. is used by typemaps */
279 Perl_croak(aTHX_ "More than one argument to '%c&' open",IoTYPE(io));
281 if (num_svs && SvIOK(*svp)) {
284 else if (isDIGIT(*type)) {
286 for (; isSPACE(*type); type++) ;
292 thatio = sv_2io(*svp);
297 for (; isSPACE(*type); type++) ;
298 thatgv = gv_fetchpv(type,FALSE,SVt_PVIO);
299 thatio = GvIO(thatgv);
303 SETERRNO(EINVAL,SS$_IVCHAN);
308 PerlIO *fp = IoIFP(thatio);
309 /* Flush stdio buffer before dup. --mjd
310 * Unfortunately SEEK_CURing 0 seems to
311 * be optimized away on most platforms;
312 * only Solaris and Linux seem to flush
315 /* sfio fails to clear error on next
316 sfwrite, contrary to documentation.
318 if (PerlIO_seek(fp, 0, SEEK_CUR) == -1)
321 /* On the other hand, do all platforms
322 * take gracefully to flushing a read-only
323 * filehandle? Perhaps we should do
324 * fsetpos(src)+fgetpos(dst)? --nik */
326 fd = PerlIO_fileno(fp);
327 /* When dup()ing STDIN, STDOUT or STDERR
328 * explicitly set appropriate access mode */
329 if (IoIFP(thatio) == PerlIO_stdout()
330 || IoIFP(thatio) == PerlIO_stderr())
331 IoTYPE(io) = IoTYPE_WRONLY;
332 else if (IoIFP(thatio) == PerlIO_stdin())
333 IoTYPE(io) = IoTYPE_RDONLY;
334 /* When dup()ing a socket, say result is
336 else if (IoTYPE(thatio) == IoTYPE_SOCKET)
337 IoTYPE(io) = IoTYPE_SOCKET;
343 fd = PerlLIO_dup(fd);
348 if (!(fp = PerlIO_openn(aTHX_ type,mode,fd,0,0,NULL,num_svs,svp))) {
356 Perl_croak(aTHX_ "More than one argument to '>' open");
359 for (; isSPACE(*type); type++) ;
360 if (*type == IoTYPE_STD && (!type[1] || isSPACE(type[1]) || type[1] == ':')) {
363 fp = PerlIO_stdout();
364 IoTYPE(io) = IoTYPE_STD;
368 namesv = sv_2mortal(newSVpvn(type,strlen(type)));
373 fp = PerlIO_openn(aTHX_ type,mode,-1,0,0,NULL,num_svs,svp);
377 else if (*type == IoTYPE_RDONLY) {
379 Perl_croak(aTHX_ "More than one argument to '<' open");
382 for (type++; isSPACE(*type); type++) ;
392 if (*type == IoTYPE_STD && (!type[1] || isSPACE(type[1]) || type[1] == ':')) {
396 IoTYPE(io) = IoTYPE_STD;
400 namesv = sv_2mortal(newSVpvn(type,strlen(type)));
405 fp = PerlIO_openn(aTHX_ type,mode,-1,0,0,NULL,num_svs,svp);
408 else if ((num_svs && type[0] == IoTYPE_STD && type[1] == IoTYPE_PIPE) ||
409 (!num_svs && tend > type+1 && tend[-1] == IoTYPE_PIPE)) {
411 type += 2; /* skip over '-|' */
415 while (tend > type && isSPACE(tend[-1]))
418 for (; isSPACE(*type); type++) ;
423 /* command is missing 19990114 */
424 if (ckWARN(WARN_PIPE))
425 Perl_warner(aTHX_ WARN_PIPE, "Missing command in piped open");
429 if (strNE(name,"-") || num_svs)
431 TAINT_PROPER("piped open");
438 fp = PerlProc_popen_list(mode,num_svs,svp);
441 fp = PerlProc_popen(name,mode);
443 IoTYPE(io) = IoTYPE_PIPE;
449 IoTYPE(io) = IoTYPE_RDONLY;
451 for (; isSPACE(*name); name++) ;
457 if (strEQ(name,"-")) {
459 IoTYPE(io) = IoTYPE_STD;
463 namesv = sv_2mortal(newSVpvn(type,strlen(type)));
468 fp = PerlIO_openn(aTHX_ type,mode,-1,0,0,NULL,num_svs,svp);
473 if (ckWARN(WARN_NEWLINE) && IoTYPE(io) == IoTYPE_RDONLY && strchr(name, '\n'))
474 Perl_warner(aTHX_ WARN_NEWLINE, PL_warn_nl, "open");
478 if (ckWARN(WARN_IO)) {
479 if ((IoTYPE(io) == IoTYPE_RDONLY) &&
480 (fp == PerlIO_stdout() || fp == PerlIO_stderr())) {
481 Perl_warner(aTHX_ WARN_IO,
482 "Filehandle STD%s opened only for input",
483 (fp == PerlIO_stdout()) ? "OUT" : "ERR");
485 else if ((IoTYPE(io) == IoTYPE_WRONLY) && fp == PerlIO_stdin()) {
486 Perl_warner(aTHX_ WARN_IO,
487 "Filehandle STDIN opened only for output");
491 if (IoTYPE(io) && IoTYPE(io) != IoTYPE_PIPE && IoTYPE(io) != IoTYPE_STD &&
492 /* FIXME: This next term is a hack to avoid fileno on PerlIO::Scalar */
493 !(num_svs && SvROK(*svp))) {
494 if (PerlLIO_fstat(PerlIO_fileno(fp),&PL_statbuf) < 0) {
495 (void)PerlIO_close(fp);
499 if (S_ISSOCK(PL_statbuf.st_mode))
500 IoTYPE(io) = IoTYPE_SOCKET; /* in case a socket was passed in to us */
504 !(PL_statbuf.st_mode & S_IFMT)
508 && IoTYPE(io) != IoTYPE_WRONLY /* Dups of STD* filehandles already have */
509 && IoTYPE(io) != IoTYPE_RDONLY /* type so they aren't marked as sockets */
510 ) { /* on OS's that return 0 on fstat()ed pipe */
512 Sock_size_t buflen = sizeof tmpbuf;
513 if (PerlSock_getsockname(PerlIO_fileno(fp), (struct sockaddr *)tmpbuf,
515 || errno != ENOTSOCK)
516 IoTYPE(io) = IoTYPE_SOCKET; /* some OS's return 0 on fstat()ed socket */
517 /* but some return 0 for streams too, sigh */
519 #endif /* !PERL_MICRO */
522 if (saveifp) { /* must use old fp? */
523 /* If fd is less that PL_maxsysfd i.e. STDIN..STDERR
524 then dup the new fileno down
526 fd = PerlIO_fileno(fp);
528 PerlIO_flush(saveofp); /* emulate PerlIO_close() */
529 if (saveofp != saveifp) { /* was a socket? */
530 PerlIO_close(saveofp);
536 if (PerlLIO_dup2(fd, savefd) < 0) {
537 (void)PerlIO_close(fp);
541 if (savefd != PerlIO_fileno(PerlIO_stdin())) {
542 char newname[FILENAME_MAX+1];
543 if (PerlIO_getname(fp, newname)) {
544 if (fd == PerlIO_fileno(PerlIO_stdout())) Perl_vmssetuserlnm(aTHX_ "SYS$OUTPUT", newname);
545 if (fd == PerlIO_fileno(PerlIO_stderr())) Perl_vmssetuserlnm(aTHX_ "SYS$ERROR", newname);
550 sv = *av_fetch(PL_fdpid,fd,TRUE);
551 (void)SvUPGRADE(sv, SVt_IV);
554 sv = *av_fetch(PL_fdpid,savefd,TRUE);
556 (void)SvUPGRADE(sv, SVt_IV);
564 #if defined(HAS_FCNTL) && defined(F_SETFD)
566 int save_errno = errno;
567 fd = PerlIO_fileno(fp);
568 fcntl(fd,F_SETFD,fd > PL_maxsysfd); /* can change errno */
574 IoFLAGS(io) &= ~IOf_NOLINE;
576 if (IoTYPE(io) == IoTYPE_SOCKET
577 || (IoTYPE(io) == IoTYPE_WRONLY && S_ISCHR(PL_statbuf.st_mode)) ) {
579 if (!(IoOFP(io) = PerlIO_openn(aTHX_ type,mode,PerlIO_fileno(fp),0,0,NULL,num_svs,svp))) {
593 IoTYPE(io) = savetype;
598 Perl_nextargv(pTHX_ register GV *gv)
601 #ifndef FLEXFILENAMES
610 PL_argvoutgv = gv_fetchpv("ARGVOUT",TRUE,SVt_PVIO);
611 if (io && (IoFLAGS(io) & IOf_ARGV) && (IoFLAGS(io) & IOf_START)) {
612 IoFLAGS(io) &= ~IOf_START;
614 if (!PL_argvout_stack)
615 PL_argvout_stack = newAV();
616 av_push(PL_argvout_stack, SvREFCNT_inc(PL_defoutgv));
619 if (PL_filemode & (S_ISUID|S_ISGID)) {
620 PerlIO_flush(IoIFP(GvIOn(PL_argvoutgv))); /* chmod must follow last write */
622 (void)fchmod(PL_lastfd,PL_filemode);
624 (void)PerlLIO_chmod(PL_oldname,PL_filemode);
628 while (av_len(GvAV(gv)) >= 0) {
630 sv = av_shift(GvAV(gv));
632 sv_setsv(GvSV(gv),sv);
633 SvSETMAGIC(GvSV(gv));
634 PL_oldname = SvPVx(GvSV(gv), oldlen);
635 if (do_open(gv,PL_oldname,oldlen,PL_inplace!=0,O_RDONLY,0,Nullfp)) {
637 TAINT_PROPER("inplace open");
638 if (oldlen == 1 && *PL_oldname == '-') {
639 setdefout(gv_fetchpv("STDOUT",TRUE,SVt_PVIO));
640 return IoIFP(GvIOp(gv));
642 #ifndef FLEXFILENAMES
643 filedev = PL_statbuf.st_dev;
644 fileino = PL_statbuf.st_ino;
646 PL_filemode = PL_statbuf.st_mode;
647 fileuid = PL_statbuf.st_uid;
648 filegid = PL_statbuf.st_gid;
649 if (!S_ISREG(PL_filemode)) {
650 if (ckWARN_d(WARN_INPLACE))
651 Perl_warner(aTHX_ WARN_INPLACE,
652 "Can't do inplace edit: %s is not a regular file",
658 char *star = strchr(PL_inplace, '*');
660 char *begin = PL_inplace;
661 sv_setpvn(sv, "", 0);
663 sv_catpvn(sv, begin, star - begin);
664 sv_catpvn(sv, PL_oldname, oldlen);
666 } while ((star = strchr(begin, '*')));
671 sv_catpv(sv,PL_inplace);
673 #ifndef FLEXFILENAMES
674 if (PerlLIO_stat(SvPVX(sv),&PL_statbuf) >= 0
675 && PL_statbuf.st_dev == filedev
676 && PL_statbuf.st_ino == fileino
678 || (_djstat_fail_bits & _STFAIL_TRUENAME)!=0
682 if (ckWARN_d(WARN_INPLACE))
683 Perl_warner(aTHX_ WARN_INPLACE,
684 "Can't do inplace edit: %s would not be unique",
691 #if !defined(DOSISH) && !defined(__CYGWIN__)
692 if (PerlLIO_rename(PL_oldname,SvPVX(sv)) < 0) {
693 if (ckWARN_d(WARN_INPLACE))
694 Perl_warner(aTHX_ WARN_INPLACE,
695 "Can't rename %s to %s: %s, skipping file",
696 PL_oldname, SvPVX(sv), Strerror(errno) );
702 (void)PerlLIO_unlink(SvPVX(sv));
703 (void)PerlLIO_rename(PL_oldname,SvPVX(sv));
704 do_open(gv,SvPVX(sv),SvCUR(sv),PL_inplace!=0,O_RDONLY,0,Nullfp);
707 (void)UNLINK(SvPVX(sv));
708 if (link(PL_oldname,SvPVX(sv)) < 0) {
709 if (ckWARN_d(WARN_INPLACE))
710 Perl_warner(aTHX_ WARN_INPLACE,
711 "Can't rename %s to %s: %s, skipping file",
712 PL_oldname, SvPVX(sv), Strerror(errno) );
716 (void)UNLINK(PL_oldname);
720 #if !defined(DOSISH) && !defined(AMIGAOS)
721 # ifndef VMS /* Don't delete; use automatic file versioning */
722 if (UNLINK(PL_oldname) < 0) {
723 if (ckWARN_d(WARN_INPLACE))
724 Perl_warner(aTHX_ WARN_INPLACE,
725 "Can't remove %s: %s, skipping file",
726 PL_oldname, Strerror(errno) );
732 Perl_croak(aTHX_ "Can't do inplace edit without backup");
736 sv_setpvn(sv,">",!PL_inplace);
737 sv_catpvn(sv,PL_oldname,oldlen);
738 SETERRNO(0,0); /* in case sprintf set errno */
740 if (!do_open(PL_argvoutgv,SvPVX(sv),SvCUR(sv),PL_inplace!=0,
741 O_WRONLY|O_CREAT|O_TRUNC,0,Nullfp))
743 if (!do_open(PL_argvoutgv,SvPVX(sv),SvCUR(sv),PL_inplace!=0,
744 O_WRONLY|O_CREAT|OPEN_EXCL,0666,Nullfp))
747 if (ckWARN_d(WARN_INPLACE))
748 Perl_warner(aTHX_ WARN_INPLACE, "Can't do inplace edit on %s: %s",
749 PL_oldname, Strerror(errno) );
753 setdefout(PL_argvoutgv);
754 PL_lastfd = PerlIO_fileno(IoIFP(GvIOp(PL_argvoutgv)));
755 (void)PerlLIO_fstat(PL_lastfd,&PL_statbuf);
757 (void)fchmod(PL_lastfd,PL_filemode);
759 # if !(defined(WIN32) && defined(__BORLANDC__))
760 /* Borland runtime creates a readonly file! */
761 (void)PerlLIO_chmod(PL_oldname,PL_filemode);
764 if (fileuid != PL_statbuf.st_uid || filegid != PL_statbuf.st_gid) {
766 (void)fchown(PL_lastfd,fileuid,filegid);
769 (void)PerlLIO_chown(PL_oldname,fileuid,filegid);
774 return IoIFP(GvIOp(gv));
777 if (ckWARN_d(WARN_INPLACE)) {
779 if (PerlLIO_stat(PL_oldname, &PL_statbuf) >= 0
780 && !S_ISREG(PL_statbuf.st_mode))
782 Perl_warner(aTHX_ WARN_INPLACE,
783 "Can't do inplace edit: %s is not a regular file",
787 Perl_warner(aTHX_ WARN_INPLACE, "Can't open %s: %s",
788 PL_oldname, Strerror(eno));
792 if (io && (IoFLAGS(io) & IOf_ARGV))
793 IoFLAGS(io) |= IOf_START;
795 (void)do_close(PL_argvoutgv,FALSE);
796 if (io && (IoFLAGS(io) & IOf_ARGV)
797 && PL_argvout_stack && AvFILLp(PL_argvout_stack) >= 0)
799 GV *oldout = (GV*)av_pop(PL_argvout_stack);
801 SvREFCNT_dec(oldout);
804 setdefout(gv_fetchpv("STDOUT",TRUE,SVt_PVIO));
811 Perl_do_pipe(pTHX_ SV *sv, GV *rgv, GV *wgv)
830 if (PerlProc_pipe(fd) < 0)
832 IoIFP(rstio) = PerlIO_fdopen(fd[0], "r");
833 IoOFP(wstio) = PerlIO_fdopen(fd[1], "w");
834 IoIFP(wstio) = IoOFP(wstio);
835 IoTYPE(rstio) = IoTYPE_RDONLY;
836 IoTYPE(wstio) = IoTYPE_WRONLY;
837 if (!IoIFP(rstio) || !IoOFP(wstio)) {
838 if (IoIFP(rstio)) PerlIO_close(IoIFP(rstio));
839 else PerlLIO_close(fd[0]);
840 if (IoOFP(wstio)) PerlIO_close(IoOFP(wstio));
841 else PerlLIO_close(fd[1]);
845 sv_setsv(sv,&PL_sv_yes);
849 sv_setsv(sv,&PL_sv_undef);
854 /* explicit renamed to avoid C++ conflict -- kja */
856 Perl_do_close(pTHX_ GV *gv, bool not_implicit)
863 if (!gv || SvTYPE(gv) != SVt_PVGV) {
865 SETERRNO(EBADF,SS$_IVCHAN);
869 if (!io) { /* never opened */
871 if (ckWARN(WARN_UNOPENED)) /* no check for closed here */
872 report_evil_fh(gv, io, PL_op->op_type);
873 SETERRNO(EBADF,SS$_IVCHAN);
877 retval = io_close(io, not_implicit);
881 IoLINES_LEFT(io) = IoPAGE_LEN(io);
883 IoTYPE(io) = IoTYPE_CLOSED;
888 Perl_io_close(pTHX_ IO *io, bool not_implicit)
894 if (IoTYPE(io) == IoTYPE_PIPE) {
895 status = PerlProc_pclose(IoIFP(io));
897 STATUS_NATIVE_SET(status);
898 retval = (STATUS_POSIX == 0);
901 retval = (status != -1);
904 else if (IoTYPE(io) == IoTYPE_STD)
907 if (IoOFP(io) && IoOFP(io) != IoIFP(io)) { /* a socket */
908 retval = (PerlIO_close(IoOFP(io)) != EOF);
909 PerlIO_close(IoIFP(io)); /* clear stdio, fd already closed */
912 retval = (PerlIO_close(IoIFP(io)) != EOF);
914 IoOFP(io) = IoIFP(io) = Nullfp;
916 else if (not_implicit) {
917 SETERRNO(EBADF,SS$_IVCHAN);
924 Perl_do_eof(pTHX_ GV *gv)
933 else if (ckWARN(WARN_IO) && (IoTYPE(io) == IoTYPE_WRONLY))
935 /* integrate to report_evil_fh()? */
938 SV* sv = sv_newmortal();
939 gv_efullname4(sv, gv, Nullch, FALSE);
940 name = SvPV_nolen(sv);
943 Perl_warner(aTHX_ WARN_IO,
944 "Filehandle %s opened only for output", name);
946 Perl_warner(aTHX_ WARN_IO,
947 "Filehandle opened only for output");
952 if (PerlIO_has_cntptr(IoIFP(io))) { /* (the code works without this) */
953 if (PerlIO_get_cnt(IoIFP(io)) > 0) /* cheat a little, since */
954 return FALSE; /* this is the most usual case */
957 ch = PerlIO_getc(IoIFP(io));
959 (void)PerlIO_ungetc(IoIFP(io),ch);
963 if (PerlIO_has_cntptr(IoIFP(io)) && PerlIO_canset_cnt(IoIFP(io))) {
964 if (PerlIO_get_cnt(IoIFP(io)) < -1)
965 PerlIO_set_cnt(IoIFP(io),-1);
967 if (PL_op->op_flags & OPf_SPECIAL) { /* not necessarily a real EOF yet? */
968 if (!nextargv(PL_argvgv)) /* get another fp handy */
972 return TRUE; /* normal fp, definitely end of file */
978 Perl_do_tell(pTHX_ GV *gv)
983 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io))) {
984 #ifdef ULTRIX_STDIO_BOTCH
986 (void)PerlIO_seek(fp, 0L, 2); /* ultrix 1.2 workaround */
988 return PerlIO_tell(fp);
990 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
991 report_evil_fh(gv, io, PL_op->op_type);
992 SETERRNO(EBADF,RMS$_IFI);
997 Perl_do_seek(pTHX_ GV *gv, Off_t pos, int whence)
1000 register PerlIO *fp;
1002 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io))) {
1003 #ifdef ULTRIX_STDIO_BOTCH
1005 (void)PerlIO_seek(fp, 0L, 2); /* ultrix 1.2 workaround */
1007 return PerlIO_seek(fp, pos, whence) >= 0;
1009 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1010 report_evil_fh(gv, io, PL_op->op_type);
1011 SETERRNO(EBADF,RMS$_IFI);
1016 Perl_do_sysseek(pTHX_ GV *gv, Off_t pos, int whence)
1018 register IO *io = 0;
1019 register PerlIO *fp;
1021 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io)))
1022 return PerlLIO_lseek(PerlIO_fileno(fp), pos, whence);
1023 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1024 report_evil_fh(gv, io, PL_op->op_type);
1025 SETERRNO(EBADF,RMS$_IFI);
1030 Perl_mode_from_discipline(pTHX_ SV *discp)
1032 int mode = O_BINARY;
1035 char *s = SvPV(discp,len);
1040 if (len > 3 && strnEQ(s+1, "raw", 3)
1041 && (!s[4] || s[4] == ':' || isSPACE(s[4])))
1050 if (len > 4 && strnEQ(s+1, "crlf", 4)
1051 && (!s[5] || s[5] == ':' || isSPACE(s[5])))
1060 goto fail_discipline;
1063 else if (isSPACE(*s)) {
1070 end = strchr(s+1, ':');
1073 #ifndef PERLIO_LAYERS
1074 Perl_croak(aTHX_ "Unknown discipline '%.*s'", end-s, s);
1085 Perl_do_binmode(pTHX_ PerlIO *fp, int iotype, int mode)
1087 /* The old body of this is now in non-LAYER part of perlio.c
1088 * This is a stub for any XS code which might have been calling it.
1090 char *name = (O_BINARY != O_TEXT && !(mode & O_BINARY)) ? ":crlf" : ":raw";
1091 return PerlIO_binmode(aTHX_ fp, iotype, mode, name);
1094 #if !defined(HAS_TRUNCATE) && !defined(HAS_CHSIZE) && defined(F_FREESP)
1095 /* code courtesy of William Kucharski */
1098 I32 my_chsize(fd, length)
1099 I32 fd; /* file descriptor */
1100 Off_t length; /* length to set file to */
1103 struct stat filebuf;
1105 if (PerlLIO_fstat(fd, &filebuf) < 0)
1108 if (filebuf.st_size < length) {
1110 /* extend file length */
1112 if ((PerlLIO_lseek(fd, (length - 1), 0)) < 0)
1115 /* write a "0" byte */
1117 if ((PerlLIO_write(fd, "", 1)) != 1)
1121 /* truncate length */
1125 fl.l_start = length;
1126 fl.l_type = F_WRLCK; /* write lock on file space */
1129 * This relies on the UNDOCUMENTED F_FREESP argument to
1130 * fcntl(2), which truncates the file so that it ends at the
1131 * position indicated by fl.l_start.
1133 * Will minor miracles never cease?
1136 if (fcntl(fd, F_FREESP, &fl) < 0)
1143 #endif /* F_FREESP */
1146 Perl_do_print(pTHX_ register SV *sv, PerlIO *fp)
1148 register char *tmps;
1151 /* assuming fp is checked earlier */
1157 if (SvIOK(sv) && SvIVX(sv) != 0) {
1158 PerlIO_printf(fp, PL_ofmt, (NV)SvIVX(sv));
1159 return !PerlIO_error(fp);
1161 if ( (SvNOK(sv) && SvNVX(sv) != 0.0)
1162 || (looks_like_number(sv) && sv_2nv(sv) != 0.0) ) {
1163 PerlIO_printf(fp, PL_ofmt, SvNVX(sv));
1164 return !PerlIO_error(fp);
1167 switch (SvTYPE(sv)) {
1169 if (ckWARN(WARN_UNINITIALIZED))
1177 PerlIO_printf(fp, "%"UVuf, (UV)SvUVX(sv));
1179 PerlIO_printf(fp, "%"IVdf, (IV)SvIVX(sv));
1180 return !PerlIO_error(fp);
1184 if (PerlIO_isutf8(fp)) {
1186 sv_utf8_upgrade(sv = sv_mortalcopy(sv));
1188 else if (DO_UTF8(sv)) {
1189 if (!sv_utf8_downgrade((sv = sv_mortalcopy(sv)), TRUE)
1190 && ckWARN(WARN_UTF8))
1192 Perl_warner(aTHX_ WARN_UTF8, "Wide character in print");
1195 tmps = SvPV(sv, len);
1198 /* To detect whether the process is about to overstep its
1199 * filesize limit we would need getrlimit(). We could then
1200 * also transparently raise the limit with setrlimit() --
1201 * but only until the system hard limit/the filesystem limit,
1202 * at which we would get EPERM. Note that when using buffered
1203 * io the write failure can be delayed until the flush/close. --jhi */
1204 if (len && (PerlIO_write(fp,tmps,len) == 0))
1206 return !PerlIO_error(fp);
1216 if (PL_op->op_flags & OPf_REF) {
1221 if (io && IoIFP(io)) {
1223 sv_setpv(PL_statname,"");
1224 PL_laststype = OP_STAT;
1225 return (PL_laststatval = PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache));
1229 return PL_laststatval;
1230 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1231 report_evil_fh(gv, io, PL_op->op_type);
1233 sv_setpv(PL_statname,"");
1234 return (PL_laststatval = -1);
1242 if (SvTYPE(sv) == SVt_PVGV) {
1246 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
1253 sv_setpv(PL_statname, s);
1254 PL_laststype = OP_STAT;
1255 PL_laststatval = PerlLIO_stat(s, &PL_statcache);
1256 if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(s, '\n'))
1257 Perl_warner(aTHX_ WARN_NEWLINE, PL_warn_nl, "stat");
1258 return PL_laststatval;
1268 if (PL_op->op_flags & OPf_REF) {
1270 if (cGVOP_gv == PL_defgv) {
1271 if (PL_laststype != OP_LSTAT)
1272 Perl_croak(aTHX_ "The stat preceding -l _ wasn't an lstat");
1273 return PL_laststatval;
1275 Perl_croak(aTHX_ "You can't use -l on a filehandle");
1278 PL_laststype = OP_LSTAT;
1282 sv_setpv(PL_statname,SvPV(sv, n_a));
1283 PL_laststatval = PerlLIO_lstat(SvPV(sv, n_a),&PL_statcache);
1284 if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(SvPV(sv, n_a), '\n'))
1285 Perl_warner(aTHX_ WARN_NEWLINE, PL_warn_nl, "lstat");
1286 return PL_laststatval;
1290 Perl_do_aexec(pTHX_ SV *really, register SV **mark, register SV **sp)
1292 return do_aexec5(really, mark, sp, 0, 0);
1296 Perl_do_aexec5(pTHX_ SV *really, register SV **mark, register SV **sp,
1297 int fd, int do_report)
1299 #ifdef MACOS_TRADITIONAL
1300 Perl_croak(aTHX_ "exec? I'm not *that* kind of operating system");
1303 char *tmps = Nullch;
1307 New(401,PL_Argv, sp - mark + 1, char*);
1309 while (++mark <= sp) {
1311 *a++ = SvPVx(*mark, n_a);
1317 tmps = SvPV(really, n_a);
1318 if ((!really && *PL_Argv[0] != '/') ||
1319 (really && *tmps != '/')) /* will execvp use PATH? */
1320 TAINT_ENV(); /* testing IFS here is overkill, probably */
1321 if (really && *tmps)
1322 PerlProc_execvp(tmps,EXEC_ARGV_CAST(PL_Argv));
1324 PerlProc_execvp(PL_Argv[0],EXEC_ARGV_CAST(PL_Argv));
1325 if (ckWARN(WARN_EXEC))
1326 Perl_warner(aTHX_ WARN_EXEC, "Can't exec \"%s\": %s",
1327 (really ? tmps : PL_Argv[0]), Strerror(errno));
1331 PerlLIO_write(fd, (void*)&e, sizeof(int));
1341 Perl_do_execfree(pTHX)
1345 PL_Argv = Null(char **);
1353 #if !defined(OS2) && !defined(WIN32) && !defined(DJGPP) && !defined(EPOC) && !defined(MACOS_TRADITIONAL)
1356 Perl_do_exec(pTHX_ char *cmd)
1358 return do_exec3(cmd,0,0);
1362 Perl_do_exec3(pTHX_ char *cmd, int fd, int do_report)
1367 while (*cmd && isSPACE(*cmd))
1370 /* save an extra exec if possible */
1375 if (strnEQ(cmd,PL_cshname,PL_cshlen) &&
1376 strnEQ(cmd+PL_cshlen," -c",3)) {
1378 s = cmd+PL_cshlen+3;
1392 if (s[-1] == '\'') {
1394 PerlProc_execl(PL_cshname,"csh", flags, ncmd, (char*)0);
1403 /* see if there are shell metacharacters in it */
1405 if (*cmd == '.' && isSPACE(cmd[1]))
1408 if (strnEQ(cmd,"exec",4) && isSPACE(cmd[4]))
1411 for (s = cmd; *s && isALNUM(*s); s++) ; /* catch VAR=val gizmo */
1415 for (s = cmd; *s; s++) {
1416 if (*s != ' ' && !isALPHA(*s) &&
1417 strchr("$&*(){}[]'\";\\|?<>~`\n",*s)) {
1418 if (*s == '\n' && !s[1]) {
1422 /* handle the 2>&1 construct at the end */
1423 if (*s == '>' && s[1] == '&' && s[2] == '1'
1424 && s > cmd + 1 && s[-1] == '2' && isSPACE(s[-2])
1425 && (!s[3] || isSPACE(s[3])))
1429 while (*t && isSPACE(*t))
1431 if (!*t && (dup2(1,2) != -1)) {
1437 PerlProc_execl(PL_sh_path, "sh", "-c", cmd, (char*)0);
1442 New(402,PL_Argv, (s - cmd) / 2 + 2, char*);
1443 PL_Cmd = savepvn(cmd, s-cmd);
1445 for (s = PL_Cmd; *s;) {
1446 while (*s && isSPACE(*s)) s++;
1449 while (*s && !isSPACE(*s)) s++;
1455 PerlProc_execvp(PL_Argv[0],PL_Argv);
1456 if (errno == ENOEXEC) { /* for system V NIH syndrome */
1463 if (ckWARN(WARN_EXEC))
1464 Perl_warner(aTHX_ WARN_EXEC, "Can't exec \"%s\": %s",
1465 PL_Argv[0], Strerror(errno));
1467 PerlLIO_write(fd, (void*)&e, sizeof(int));
1476 #endif /* OS2 || WIN32 */
1479 Perl_apply(pTHX_ I32 type, register SV **mark, register SV **sp)
1483 register I32 tot = 0;
1486 SV **oldmark = mark;
1489 #define APPLY_TAINT_PROPER() \
1491 if (PL_tainted) { TAINT_PROPER(what); } \
1494 /* This is a first heuristic; it doesn't catch tainting magic. */
1496 while (++mark <= sp) {
1497 if (SvTAINTED(*mark)) {
1507 APPLY_TAINT_PROPER();
1510 APPLY_TAINT_PROPER();
1512 while (++mark <= sp) {
1513 char *name = SvPVx(*mark, n_a);
1514 APPLY_TAINT_PROPER();
1515 if (PerlLIO_chmod(name, val))
1523 APPLY_TAINT_PROPER();
1524 if (sp - mark > 2) {
1525 val = SvIVx(*++mark);
1526 val2 = SvIVx(*++mark);
1527 APPLY_TAINT_PROPER();
1529 while (++mark <= sp) {
1530 char *name = SvPVx(*mark, n_a);
1531 APPLY_TAINT_PROPER();
1532 if (PerlLIO_chown(name, val, val2))
1539 XXX Should we make lchown() directly available from perl?
1540 For now, we'll let Configure test for HAS_LCHOWN, but do
1541 nothing in the core.
1547 APPLY_TAINT_PROPER();
1550 s = SvPVx(*++mark, n_a);
1552 if (*s == 'S' && s[1] == 'I' && s[2] == 'G')
1554 if (!(val = whichsig(s)))
1555 Perl_croak(aTHX_ "Unrecognized signal name \"%s\"",s);
1559 APPLY_TAINT_PROPER();
1562 /* kill() doesn't do process groups (job trees?) under VMS */
1563 if (val < 0) val = -val;
1564 if (val == SIGKILL) {
1565 # include <starlet.h>
1566 /* Use native sys$delprc() to insure that target process is
1567 * deleted; supervisor-mode images don't pay attention to
1568 * CRTL's emulation of Unix-style signals and kill()
1570 while (++mark <= sp) {
1571 I32 proc = SvIVx(*mark);
1572 register unsigned long int __vmssts;
1573 APPLY_TAINT_PROPER();
1574 if (!((__vmssts = sys$delprc(&proc,0)) & 1)) {
1578 case SS$_NOSUCHNODE:
1579 SETERRNO(ESRCH,__vmssts);
1582 SETERRNO(EPERM,__vmssts);
1585 SETERRNO(EVMSERR,__vmssts);
1594 while (++mark <= sp) {
1595 I32 proc = SvIVx(*mark);
1596 APPLY_TAINT_PROPER();
1598 if (PerlProc_killpg(proc,val)) /* BSD */
1600 if (PerlProc_kill(-proc,val)) /* SYSV */
1606 while (++mark <= sp) {
1607 I32 proc = SvIVx(*mark);
1608 APPLY_TAINT_PROPER();
1609 if (PerlProc_kill(proc, val))
1617 APPLY_TAINT_PROPER();
1619 while (++mark <= sp) {
1620 s = SvPVx(*mark, n_a);
1621 APPLY_TAINT_PROPER();
1622 if (PL_euid || PL_unsafe) {
1626 else { /* don't let root wipe out directories without -U */
1627 if (PerlLIO_lstat(s,&PL_statbuf) < 0 || S_ISDIR(PL_statbuf.st_mode))
1639 APPLY_TAINT_PROPER();
1640 if (sp - mark > 2) {
1641 #if defined(I_UTIME) || defined(VMS)
1642 struct utimbuf utbuf;
1650 SV* accessed = *++mark;
1651 SV* modified = *++mark;
1652 void * utbufp = &utbuf;
1654 /* be like C, and if both times are undefined, let the C
1655 library figure out what to do. This usually means
1658 if ( accessed == &PL_sv_undef && modified == &PL_sv_undef )
1661 Zero(&utbuf, sizeof utbuf, char);
1663 utbuf.actime = (Time_t)SvNVx(accessed); /* time accessed */
1664 utbuf.modtime = (Time_t)SvNVx(modified); /* time modified */
1666 utbuf.actime = (Time_t)SvIVx(accessed); /* time accessed */
1667 utbuf.modtime = (Time_t)SvIVx(modified); /* time modified */
1669 APPLY_TAINT_PROPER();
1671 while (++mark <= sp) {
1672 char *name = SvPVx(*mark, n_a);
1673 APPLY_TAINT_PROPER();
1674 if (PerlLIO_utime(name, utbufp))
1685 #undef APPLY_TAINT_PROPER
1688 /* Do the permissions allow some operation? Assumes statcache already set. */
1689 #ifndef VMS /* VMS' cando is in vms.c */
1691 Perl_cando(pTHX_ Mode_t mode, Uid_t effective, register Stat_t *statbufp)
1692 /* Note: we use `effective' both for uids and gids.
1693 * Here we are betting on Uid_t being equal or wider than Gid_t. */
1696 /* [Comments and code from Len Reed]
1697 * MS-DOS "user" is similar to UNIX's "superuser," but can't write
1698 * to write-protected files. The execute permission bit is set
1699 * by the Miscrosoft C library stat() function for the following:
1704 * All files and directories are readable.
1705 * Directories and special files, e.g. "CON", cannot be
1707 * [Comment by Tom Dinger -- a directory can have the write-protect
1708 * bit set in the file system, but DOS permits changes to
1709 * the directory anyway. In addition, all bets are off
1710 * here for networked software, such as Novell and
1714 /* Atari stat() does pretty much the same thing. we set x_bit_set_in_stat
1715 * too so it will actually look into the files for magic numbers
1717 return (mode & statbufp->st_mode) ? TRUE : FALSE;
1719 #else /* ! DOSISH */
1720 if ((effective ? PL_euid : PL_uid) == 0) { /* root is special */
1721 if (mode == S_IXUSR) {
1722 if (statbufp->st_mode & 0111 || S_ISDIR(statbufp->st_mode))
1726 return TRUE; /* root reads and writes anything */
1729 if (statbufp->st_uid == (effective ? PL_euid : PL_uid) ) {
1730 if (statbufp->st_mode & mode)
1731 return TRUE; /* ok as "user" */
1733 else if (ingroup(statbufp->st_gid,effective)) {
1734 if (statbufp->st_mode & mode >> 3)
1735 return TRUE; /* ok as "group" */
1737 else if (statbufp->st_mode & mode >> 6)
1738 return TRUE; /* ok as "other" */
1740 #endif /* ! DOSISH */
1745 Perl_ingroup(pTHX_ Gid_t testgid, Uid_t effective)
1747 #ifdef MACOS_TRADITIONAL
1748 /* This is simply not correct for AppleShare, but fix it yerself. */
1751 if (testgid == (effective ? PL_egid : PL_gid))
1753 #ifdef HAS_GETGROUPS
1758 Groups_t gary[NGROUPS];
1761 anum = getgroups(NGROUPS,gary);
1763 if (gary[anum] == testgid)
1771 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
1774 Perl_do_ipcget(pTHX_ I32 optype, SV **mark, SV **sp)
1779 key = (key_t)SvNVx(*++mark);
1780 n = (optype == OP_MSGGET) ? 0 : SvIVx(*++mark);
1781 flags = SvIVx(*++mark);
1787 return msgget(key, flags);
1791 return semget(key, n, flags);
1795 return shmget(key, n, flags);
1797 #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
1799 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
1802 return -1; /* should never happen */
1806 Perl_do_ipcctl(pTHX_ I32 optype, SV **mark, SV **sp)
1810 I32 id, n, cmd, infosize, getinfo;
1813 id = SvIVx(*++mark);
1814 n = (optype == OP_SEMCTL) ? SvIVx(*++mark) : 0;
1815 cmd = SvIVx(*++mark);
1818 getinfo = (cmd == IPC_STAT);
1824 if (cmd == IPC_STAT || cmd == IPC_SET)
1825 infosize = sizeof(struct msqid_ds);
1830 if (cmd == IPC_STAT || cmd == IPC_SET)
1831 infosize = sizeof(struct shmid_ds);
1837 if (cmd == IPC_STAT || cmd == IPC_SET)
1838 infosize = sizeof(struct semid_ds);
1839 else if (cmd == GETALL || cmd == SETALL)
1841 struct semid_ds semds;
1843 #ifdef EXTRA_F_IN_SEMUN_BUF
1844 semun.buff = &semds;
1848 getinfo = (cmd == GETALL);
1849 if (Semctl(id, 0, IPC_STAT, semun) == -1)
1851 infosize = semds.sem_nsems * sizeof(short);
1852 /* "short" is technically wrong but much more portable
1853 than guessing about u_?short(_t)? */
1856 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
1860 #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
1862 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
1871 SvPV_force(astr, len);
1872 a = SvGROW(astr, infosize+1);
1876 a = SvPV(astr, len);
1877 if (len != infosize)
1878 Perl_croak(aTHX_ "Bad arg length for %s, is %lu, should be %ld",
1887 a = INT2PTR(char *,i); /* ouch */
1894 ret = msgctl(id, cmd, (struct msqid_ds *)a);
1900 union semun unsemds;
1902 #ifdef EXTRA_F_IN_SEMUN_BUF
1903 unsemds.buff = (struct semid_ds *)a;
1905 unsemds.buf = (struct semid_ds *)a;
1907 ret = Semctl(id, n, cmd, unsemds);
1909 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
1916 ret = shmctl(id, cmd, (struct shmid_ds *)a);
1920 if (getinfo && ret >= 0) {
1921 SvCUR_set(astr, infosize);
1922 *SvEND(astr) = '\0';
1929 Perl_do_msgsnd(pTHX_ SV **mark, SV **sp)
1934 I32 id, msize, flags;
1937 id = SvIVx(*++mark);
1939 flags = SvIVx(*++mark);
1940 mbuf = SvPV(mstr, len);
1941 if ((msize = len - sizeof(long)) < 0)
1942 Perl_croak(aTHX_ "Arg too short for msgsnd");
1944 return msgsnd(id, (struct msgbuf *)mbuf, msize, flags);
1946 Perl_croak(aTHX_ "msgsnd not implemented");
1951 Perl_do_msgrcv(pTHX_ SV **mark, SV **sp)
1957 I32 id, msize, flags, ret;
1960 id = SvIVx(*++mark);
1962 /* suppress warning when reading into undef var --jhi */
1964 sv_setpvn(mstr, "", 0);
1965 msize = SvIVx(*++mark);
1966 mtype = (long)SvIVx(*++mark);
1967 flags = SvIVx(*++mark);
1968 SvPV_force(mstr, len);
1969 mbuf = SvGROW(mstr, sizeof(long)+msize+1);
1972 ret = msgrcv(id, (struct msgbuf *)mbuf, msize, mtype, flags);
1974 SvCUR_set(mstr, sizeof(long)+ret);
1975 *SvEND(mstr) = '\0';
1976 #ifndef INCOMPLETE_TAINTS
1977 /* who knows who has been playing with this message? */
1983 Perl_croak(aTHX_ "msgrcv not implemented");
1988 Perl_do_semop(pTHX_ SV **mark, SV **sp)
1996 id = SvIVx(*++mark);
1998 opbuf = SvPV(opstr, opsize);
1999 if (opsize < 3 * SHORTSIZE
2000 || (opsize % (3 * SHORTSIZE))) {
2001 SETERRNO(EINVAL,LIB$_INVARG);
2005 /* We can't assume that sizeof(struct sembuf) == 3 * sizeof(short). */
2007 int nsops = opsize / (3 * sizeof (short));
2009 short *ops = (short *) opbuf;
2011 struct sembuf *temps, *t;
2014 New (0, temps, nsops, struct sembuf);
2022 result = semop(id, temps, nsops);
2036 Perl_croak(aTHX_ "semop not implemented");
2041 Perl_do_shmio(pTHX_ I32 optype, SV **mark, SV **sp)
2046 I32 id, mpos, msize;
2048 struct shmid_ds shmds;
2050 id = SvIVx(*++mark);
2052 mpos = SvIVx(*++mark);
2053 msize = SvIVx(*++mark);
2055 if (shmctl(id, IPC_STAT, &shmds) == -1)
2057 if (mpos < 0 || msize < 0 || mpos + msize > shmds.shm_segsz) {
2058 SETERRNO(EFAULT,SS$_ACCVIO); /* can't do as caller requested */
2061 shm = (char *)shmat(id, (char*)NULL, (optype == OP_SHMREAD) ? SHM_RDONLY : 0);
2062 if (shm == (char *)-1) /* I hate System V IPC, I really do */
2064 if (optype == OP_SHMREAD) {
2065 /* suppress warning when reading into undef var (tchrist 3/Mar/00) */
2067 sv_setpvn(mstr, "", 0);
2068 SvPV_force(mstr, len);
2069 mbuf = SvGROW(mstr, msize+1);
2071 Copy(shm + mpos, mbuf, msize, char);
2072 SvCUR_set(mstr, msize);
2073 *SvEND(mstr) = '\0';
2075 #ifndef INCOMPLETE_TAINTS
2076 /* who knows who has been playing with this shared memory? */
2083 mbuf = SvPV(mstr, len);
2084 if ((n = len) > msize)
2086 Copy(mbuf, shm + mpos, n, char);
2088 memzero(shm + mpos + n, msize - n);
2092 Perl_croak(aTHX_ "shm I/O not implemented");
2096 #endif /* SYSV IPC */
2099 =for apidoc start_glob
2101 Function called by C<do_readline> to spawn a glob (or do the glob inside
2102 perl on VMS). This code used to be inline, but now perl uses C<File::Glob>
2103 this glob starter is only used by miniperl during the build proccess.
2104 Moving it away shrinks pp_hot.c; shrinking pp_hot.c helps speed perl up.
2110 Perl_start_glob (pTHX_ SV *tmpglob, IO *io)
2112 SV *tmpcmd = NEWSV(55, 0);
2116 #ifdef VMS /* expand the wildcards right here, rather than opening a pipe, */
2117 /* since spawning off a process is a real performance hit */
2119 #include <descrip.h>
2120 #include <lib$routines.h>
2123 char rslt[NAM$C_MAXRSS+1+sizeof(unsigned short int)] = {'\0','\0'};
2124 char vmsspec[NAM$C_MAXRSS+1];
2125 char *rstr = rslt + sizeof(unsigned short int), *begin, *end, *cp;
2126 $DESCRIPTOR(dfltdsc,"SYS$DISK:[]*.*;");
2129 struct dsc$descriptor_s wilddsc
2130 = {0, DSC$K_DTYPE_T, DSC$K_CLASS_S, 0};
2131 struct dsc$descriptor_vs rsdsc
2132 = {sizeof rslt, DSC$K_DTYPE_VT, DSC$K_CLASS_VS, rslt};
2133 unsigned long int cxt = 0, sts = 0, ok = 1, hasdir = 0, hasver = 0, isunix = 0;
2135 /* We could find out if there's an explicit dev/dir or version
2136 by peeking into lib$find_file's internal context at
2137 ((struct NAM *)((struct FAB *)cxt)->fab$l_nam)->nam$l_fnb
2138 but that's unsupported, so I don't want to do it now and
2139 have it bite someone in the future. */
2140 cp = SvPV(tmpglob,i);
2142 if (cp[i] == ';') hasver = 1;
2144 if (sts) hasver = 1;
2148 hasdir = isunix = 1;
2151 if (cp[i] == ']' || cp[i] == '>' || cp[i] == ':') {
2156 if ((tmpfp = PerlIO_tmpfile()) != NULL) {
2158 if (!PerlLIO_stat(SvPVX(tmpglob),&st) && S_ISDIR(st.st_mode))
2159 ok = ((wilddsc.dsc$a_pointer = tovmspath(SvPVX(tmpglob),vmsspec)) != NULL);
2160 else ok = ((wilddsc.dsc$a_pointer = tovmsspec(SvPVX(tmpglob),vmsspec)) != NULL);
2161 if (ok) wilddsc.dsc$w_length = (unsigned short int) strlen(wilddsc.dsc$a_pointer);
2162 for (cp=wilddsc.dsc$a_pointer; ok && cp && *cp; cp++)
2163 if (*cp == '?') *cp = '%'; /* VMS style single-char wildcard */
2164 while (ok && ((sts = lib$find_file(&wilddsc,&rsdsc,&cxt,
2165 &dfltdsc,NULL,NULL,NULL))&1)) {
2166 end = rstr + (unsigned long int) *rslt;
2167 if (!hasver) while (*end != ';') end--;
2168 *(end++) = '\n'; *end = '\0';
2169 for (cp = rstr; *cp; cp++) *cp = _tolower(*cp);
2171 if (isunix) trim_unixpath(rstr,SvPVX(tmpglob),1);
2176 while (*(--begin) != ']' && *begin != '>') ;
2179 ok = (PerlIO_puts(tmpfp,begin) != EOF);
2181 if (cxt) (void)lib$find_file_end(&cxt);
2182 if (ok && sts != RMS$_NMF &&
2183 sts != RMS$_DNF && sts != RMS$_FNF) ok = 0;
2186 SETERRNO((sts == RMS$_SYN ? EINVAL : EVMSERR),sts);
2188 PerlIO_close(tmpfp);
2192 PerlIO_rewind(tmpfp);
2193 IoTYPE(io) = IoTYPE_RDONLY;
2194 IoIFP(io) = fp = tmpfp;
2195 IoFLAGS(io) &= ~IOf_UNTAINT; /* maybe redundant */
2200 #ifdef MACOS_TRADITIONAL
2201 sv_setpv(tmpcmd, "glob ");
2202 sv_catsv(tmpcmd, tmpglob);
2203 sv_catpv(tmpcmd, " |");
2207 sv_setpv(tmpcmd, "for a in ");
2208 sv_catsv(tmpcmd, tmpglob);
2209 sv_catpv(tmpcmd, "; do echo \"$a\\0\\c\"; done |");
2212 sv_setpv(tmpcmd, "/dev/dosglob/"); /* File System Extension */
2213 sv_catsv(tmpcmd, tmpglob);
2215 sv_setpv(tmpcmd, "perlglob ");
2216 sv_catsv(tmpcmd, tmpglob);
2217 sv_catpv(tmpcmd, " |");
2222 sv_setpvn(tmpcmd, PL_cshname, PL_cshlen);
2223 sv_catpv(tmpcmd, " -cf 'set nonomatch; glob ");
2224 sv_catsv(tmpcmd, tmpglob);
2225 sv_catpv(tmpcmd, "' 2>/dev/null |");
2227 sv_setpv(tmpcmd, "echo ");
2228 sv_catsv(tmpcmd, tmpglob);
2230 sv_catpv(tmpcmd, "|tr -s ' \t\f\r' '\\012\\012\\012\\012'|");
2232 sv_catpv(tmpcmd, "|tr -s ' \t\f\r' '\\n\\n\\n\\n'|");
2235 #endif /* !DOSISH */
2236 #endif /* MACOS_TRADITIONAL */
2237 (void)do_open(PL_last_in_gv, SvPVX(tmpcmd), SvCUR(tmpcmd),
2238 FALSE, O_RDONLY, 0, Nullfp);