X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=doio.c;h=175b6b065a8cfbaf207546fd09900efefa2fe229;hb=8227f81cbd3d53a745747c4247824562383badae;hp=2b8bbf9fd34cee21d3a332583f33cddbad39e9fc;hpb=463ee0b2acbd047c27e8b5393cdd8398881824c5;p=p5sagit%2Fp5-mst-13.2.git diff --git a/doio.c b/doio.c index 2b8bbf9..175b6b0 100644 --- a/doio.c +++ b/doio.c @@ -1,54 +1,17 @@ -/* $RCSfile: doio.c,v $$Revision: 4.1 $$Date: 92/08/07 17:19:42 $ +/* doio.c * - * Copyright (c) 1991, Larry Wall + * Copyright (c) 1991-1994, Larry Wall * * You may distribute under the terms of either the GNU General Public * License or the Artistic License, as specified in the README file. * - * $Log: doio.c,v $ - * Revision 4.1 92/08/07 17:19:42 lwall - * Stage 6 Snapshot - * - * Revision 4.0.1.6 92/06/11 21:08:16 lwall - * patch34: some systems don't declare h_errno extern in header files - * - * Revision 4.0.1.5 92/06/08 13:00:21 lwall - * patch20: some machines don't define ENOTSOCK in errno.h - * patch20: new warnings for failed use of stat operators on filenames with \n - * patch20: wait failed when STDOUT or STDERR reopened to a pipe - * patch20: end of file latch not reset on reopen of STDIN - * patch20: seek(HANDLE, 0, 1) went to eof because of ancient Ultrix workaround - * patch20: fixed memory leak on system() for vfork() machines - * patch20: get*by* routines now return something useful in a scalar context - * patch20: h_errno now accessible via $? - * - * Revision 4.0.1.4 91/11/05 16:51:43 lwall - * patch11: prepared for ctype implementations that don't define isascii() - * patch11: perl mistook some streams for sockets because they return mode 0 too - * patch11: reopening STDIN, STDOUT and STDERR failed on some machines - * patch11: certain perl errors should set EBADF so that $! looks better - * patch11: truncate on a closed filehandle could dump - * patch11: stats of _ forgot whether prior stat was actually lstat - * patch11: -T returned true on NFS directory - * - * Revision 4.0.1.3 91/06/10 01:21:19 lwall - * patch10: read didn't work from character special files open for writing - * patch10: close-on-exec wrongly set on system file descriptors - * - * Revision 4.0.1.2 91/06/07 10:53:39 lwall - * patch4: new copyright notice - * patch4: system fd's are now treated specially - * patch4: added $^F variable to specify maximum system fd, default 2 - * patch4: character special files now opened with bidirectional stdio buffers - * patch4: taintchecks could improperly modify parent in vfork() - * patch4: many, many itty-bitty portability fixes - * - * Revision 4.0.1.1 91/04/11 17:41:06 lwall - * patch1: hopefully straightened out some of the Xenix mess - * - * Revision 4.0 91/03/20 01:07:06 lwall - * 4.0 baseline. - * + */ + +/* + * "Far below them they saw the white waters pour into a foaming bowl, and + * then swirl darkly about a deep oval basin in the rocks, until they found + * their way out again through a narrow gate, and flowed away, fuming and + * chattering, into calmer and more level reaches." */ #include "EXTERN.h" @@ -64,6 +27,9 @@ #endif #ifdef HAS_SHM #include +# ifndef HAS_SHMAT_PROTOTYPE + extern Shmat_t shmat _((int, char *, int)); +# endif #endif #endif @@ -77,139 +43,194 @@ #include #endif +#if !defined(NSIG) || defined(M_UNIX) || defined(M_XENIX) +#include +#endif + +/* XXX If this causes problems, set i_unistd=undef in the hint file. */ +#ifdef I_UNISTD +# include +#endif + +#if defined(HAS_SOCKET) && !defined(VMS) /* VMS handles sockets via vmsish.h */ +# include +# include +# ifndef ENOTSOCK +# ifdef I_NET_ERRNO +# include +# endif +# endif +#endif + bool -do_open(gv,name,len) +do_open(gv,name,len,as_raw,rawmode,rawperm,supplied_fp) GV *gv; register char *name; I32 len; +int as_raw; +int rawmode, rawperm; +PerlIO *supplied_fp; { - FILE *fp; - register IO *io = GvIO(gv); - char *myname = savestr(name); - int result; - int fd; - int writing = 0; - char mode[3]; /* stdio file mode ("r\0" or "r+\0") */ - FILE *saveifp = Nullfp; - FILE *saveofp = Nullfp; + register IO *io = GvIOn(gv); + PerlIO *saveifp = Nullfp; + PerlIO *saveofp = Nullfp; char savetype = ' '; + int writing = 0; + PerlIO *fp; + int fd; + int result; - mode[0] = mode[1] = mode[2] = '\0'; - name = myname; forkprocess = 1; /* assume true if no fork */ - while (len && isSPACE(name[len-1])) - name[--len] = '\0'; - if (!io) - io = GvIO(gv) = newIO(); - else if (io->ifp) { - fd = fileno(io->ifp); - if (io->type == '-') + + if (IoIFP(io)) { + fd = PerlIO_fileno(IoIFP(io)); + if (IoTYPE(io) == '-') result = 0; else if (fd <= maxsysfd) { - saveifp = io->ifp; - saveofp = io->ofp; - savetype = io->type; + saveifp = IoIFP(io); + saveofp = IoOFP(io); + savetype = IoTYPE(io); result = 0; } - else if (io->type == '|') - result = my_pclose(io->ifp); - else if (io->ifp != io->ofp) { - if (io->ofp) { - result = fclose(io->ofp); - fclose(io->ifp); /* clear stdio, fd already closed */ + else if (IoTYPE(io) == '|') + result = my_pclose(IoIFP(io)); + else if (IoIFP(io) != IoOFP(io)) { + if (IoOFP(io)) { + result = PerlIO_close(IoOFP(io)); + PerlIO_close(IoIFP(io)); /* clear stdio, fd already closed */ } else - result = fclose(io->ifp); + result = PerlIO_close(IoIFP(io)); } else - result = fclose(io->ifp); + result = PerlIO_close(IoIFP(io)); if (result == EOF && fd > maxsysfd) - fprintf(stderr,"Warning: unable to close filehandle %s properly.\n", + PerlIO_printf(PerlIO_stderr(), "Warning: unable to close filehandle %s properly.\n", GvENAME(gv)); - io->ofp = io->ifp = Nullfp; - } - if (*name == '+' && len > 1 && name[len-1] != '|') { /* scary */ - mode[1] = *name++; - mode[2] = '\0'; - --len; - writing = 1; + IoOFP(io) = IoIFP(io) = Nullfp; } - else { - mode[1] = '\0'; - } - io->type = *name; - if (*name == '|') { - /*SUPPRESS 530*/ - for (name++; isSPACE(*name); name++) ; - if (strNE(name,"-")) - TAINT_ENV(); - TAINT_PROPER("piped open"); - fp = my_popen(name,"w"); - writing = 1; + + if (as_raw) { + result = rawmode & 3; + IoTYPE(io) = "<>++"[result]; + writing = (result > 0); + fd = open(name, rawmode, rawperm); + if (fd == -1) + fp = NULL; + else { + fp = PerlIO_fdopen(fd, ((result == 0) ? "r" + : (result == 1) ? "w" + : "r+")); + if (!fp) + close(fd); + } } - else if (*name == '>') { - TAINT_PROPER("open"); - name++; - if (*name == '>') { - mode[0] = io->type = 'a'; - name++; + else { + char *myname; + char mode[3]; /* stdio file mode ("r\0" or "r+\0") */ + int dodup; + + myname = savepvn(name, len); + SAVEFREEPV(myname); + name = myname; + while (len && isSPACE(name[len-1])) + name[--len] = '\0'; + + mode[0] = mode[1] = mode[2] = '\0'; + IoTYPE(io) = *name; + if (*name == '+' && len > 1 && name[len-1] != '|') { /* scary */ + mode[1] = *name++; + --len; + writing = 1; } - else - mode[0] = 'w'; - writing = 1; - if (*name == '&') { - duplicity: + + if (*name == '|') { + /*SUPPRESS 530*/ + for (name++; isSPACE(*name); name++) ; + if (strNE(name,"-")) + TAINT_ENV(); + TAINT_PROPER("piped open"); + if (dowarn && name[strlen(name)-1] == '|') + warn("Can't do bidirectional pipe"); + fp = my_popen(name,"w"); + writing = 1; + } + else if (*name == '>') { + TAINT_PROPER("open"); name++; - while (isSPACE(*name)) + if (*name == '>') { + mode[0] = IoTYPE(io) = 'a'; name++; - if (isDIGIT(*name)) - fd = atoi(name); - else { - gv = gv_fetchpv(name,FALSE); - if (!gv || !GvIO(gv)) { + } + else + mode[0] = 'w'; + writing = 1; + + if (*name == '&') { + duplicity: + dodup = 1; + name++; + if (*name == '=') { + dodup = 0; + name++; + } + if (!*name && supplied_fp) + fp = supplied_fp; + else { + /*SUPPRESS 530*/ + for (; isSPACE(*name); name++) ; + if (isDIGIT(*name)) + fd = atoi(name); + else { + IO* thatio; + gv = gv_fetchpv(name,FALSE,SVt_PVIO); + thatio = GvIO(gv); + if (!thatio) { #ifdef EINVAL - errno = EINVAL; + SETERRNO(EINVAL,SS$_IVCHAN); #endif - goto say_false; - } - if (GvIO(gv) && GvIO(gv)->ifp) { - fd = fileno(GvIO(gv)->ifp); - if (GvIO(gv)->type == 's') - io->type = 's'; + goto say_false; + } + if (IoIFP(thatio)) { + fd = PerlIO_fileno(IoIFP(thatio)); + if (IoTYPE(thatio) == 's') + IoTYPE(io) = 's'; + } + else + fd = -1; + } + if (dodup) + fd = dup(fd); + if (!(fp = PerlIO_fdopen(fd,mode))) { + if (dodup) + close(fd); + } } - else - fd = -1; - } - if (!(fp = fdopen(fd = dup(fd),mode))) { - close(fd); } - } - else { - while (isSPACE(*name)) - name++; - if (strEQ(name,"-")) { - fp = stdout; - io->type = '-'; - } - else { - fp = fopen(name,mode); + else { + /*SUPPRESS 530*/ + for (; isSPACE(*name); name++) ; + if (strEQ(name,"-")) { + fp = PerlIO_stdout(); + IoTYPE(io) = '-'; + } + else { + fp = PerlIO_open(name,mode); + } } } - } - else { - if (*name == '<') { + else if (*name == '<') { + /*SUPPRESS 530*/ + for (name++; isSPACE(*name); name++) ; mode[0] = 'r'; - name++; - while (isSPACE(*name)) - name++; if (*name == '&') goto duplicity; if (strEQ(name,"-")) { - fp = stdin; - io->type = '-'; + fp = PerlIO_stdin(); + IoTYPE(io) = '-'; } else - fp = fopen(name,mode); + fp = PerlIO_open(name,mode); } else if (name[len-1] == '|') { name[--len] = '\0'; @@ -221,35 +242,33 @@ I32 len; TAINT_ENV(); TAINT_PROPER("piped open"); fp = my_popen(name,"r"); - io->type = '|'; + IoTYPE(io) = '|'; } else { - io->type = '<'; + IoTYPE(io) = '<'; /*SUPPRESS 530*/ for (; isSPACE(*name); name++) ; if (strEQ(name,"-")) { - fp = stdin; - io->type = '-'; + fp = PerlIO_stdin(); + IoTYPE(io) = '-'; } else - fp = fopen(name,"r"); + fp = PerlIO_open(name,"r"); } } if (!fp) { - if (dowarn && io->type == '<' && strchr(name, '\n')) + if (dowarn && IoTYPE(io) == '<' && strchr(name, '\n')) warn(warn_nl, "open"); - Safefree(myname); goto say_false; } - Safefree(myname); - if (io->type && - io->type != '|' && io->type != '-') { - if (fstat(fileno(fp),&statbuf) < 0) { - (void)fclose(fp); + if (IoTYPE(io) && + IoTYPE(io) != '|' && IoTYPE(io) != '-') { + if (Fstat(PerlIO_fileno(fp),&statbuf) < 0) { + (void)PerlIO_close(fp); goto say_false; } if (S_ISSOCK(statbuf.st_mode)) - io->type = 's'; /* in case a socket was passed in to us */ + IoTYPE(io) = 's'; /* in case a socket was passed in to us */ #ifdef HAS_SOCKET else if ( #ifdef S_IFMT @@ -258,69 +277,69 @@ I32 len; !statbuf.st_mode #endif ) { - I32 buflen = sizeof tokenbuf; - if (getsockname(fileno(fp), tokenbuf, &buflen) >= 0 + int buflen = sizeof tokenbuf; + if (getsockname(PerlIO_fileno(fp), (struct sockaddr *)tokenbuf, &buflen) >= 0 || errno != ENOTSOCK) - io->type = 's'; /* some OS's return 0 on fstat()ed socket */ + IoTYPE(io) = 's'; /* some OS's return 0 on fstat()ed socket */ /* but some return 0 for streams too, sigh */ } #endif } if (saveifp) { /* must use old fp? */ - fd = fileno(saveifp); + fd = PerlIO_fileno(saveifp); if (saveofp) { - fflush(saveofp); /* emulate fclose() */ + PerlIO_flush(saveofp); /* emulate PerlIO_close() */ if (saveofp != saveifp) { /* was a socket? */ - fclose(saveofp); + PerlIO_close(saveofp); if (fd > 2) Safefree(saveofp); } } - if (fd != fileno(fp)) { + if (fd != PerlIO_fileno(fp)) { int pid; SV *sv; - dup2(fileno(fp), fd); - sv = *av_fetch(fdpid,fileno(fp),TRUE); - SvUPGRADE(sv, SVt_IV); + dup2(PerlIO_fileno(fp), fd); + sv = *av_fetch(fdpid,PerlIO_fileno(fp),TRUE); + (void)SvUPGRADE(sv, SVt_IV); pid = SvIVX(sv); SvIVX(sv) = 0; sv = *av_fetch(fdpid,fd,TRUE); - SvUPGRADE(sv, SVt_IV); + (void)SvUPGRADE(sv, SVt_IV); SvIVX(sv) = pid; - fclose(fp); + PerlIO_close(fp); } fp = saveifp; - clearerr(fp); + PerlIO_clearerr(fp); } -#if defined(HAS_FCNTL) && defined(FFt_SETFD) - fd = fileno(fp); - fcntl(fd,FFt_SETFD,fd > maxsysfd); +#if defined(HAS_FCNTL) && defined(F_SETFD) + fd = PerlIO_fileno(fp); + fcntl(fd,F_SETFD,fd > maxsysfd); #endif - io->ifp = fp; + IoIFP(io) = fp; if (writing) { - if (io->type == 's' - || (io->type == '>' && S_ISCHR(statbuf.st_mode)) ) { - if (!(io->ofp = fdopen(fileno(fp),"w"))) { - fclose(fp); - io->ifp = Nullfp; + if (IoTYPE(io) == 's' + || (IoTYPE(io) == '>' && S_ISCHR(statbuf.st_mode)) ) { + if (!(IoOFP(io) = PerlIO_fdopen(PerlIO_fileno(fp),"w"))) { + PerlIO_close(fp); + IoIFP(io) = Nullfp; goto say_false; } } else - io->ofp = fp; + IoOFP(io) = fp; } return TRUE; say_false: - io->ifp = saveifp; - io->ofp = saveofp; - io->type = savetype; + IoIFP(io) = saveifp; + IoOFP(io) = saveofp; + IoTYPE(io) = savetype; return FALSE; } -FILE * +PerlIO * nextargv(gv) register GV *gv; { @@ -333,9 +352,9 @@ register GV *gv; int filegid; if (!argvoutgv) - argvoutgv = gv_fetchpv("ARGVOUT",TRUE); + argvoutgv = gv_fetchpv("ARGVOUT",TRUE,SVt_PVIO); if (filemode & (S_ISUID|S_ISGID)) { - fflush(GvIO(argvoutgv)->ifp); /* chmod must follow last write */ + PerlIO_flush(IoIFP(GvIOn(argvoutgv))); /* chmod must follow last write */ #ifdef HAS_FCHMOD (void)fchmod(lastfd,filemode); #else @@ -346,16 +365,16 @@ register GV *gv; while (av_len(GvAV(gv)) >= 0) { STRLEN len; sv = av_shift(GvAV(gv)); + SAVEFREESV(sv); sv_setsv(GvSV(gv),sv); SvSETMAGIC(GvSV(gv)); oldname = SvPVx(GvSV(gv), len); - if (do_open(gv,oldname,len)) { + if (do_open(gv,oldname,len,FALSE,0,0,Nullfp)) { if (inplace) { TAINT_PROPER("inplace open"); if (strEQ(oldname,"-")) { - sv_free(sv); - defoutgv = gv_fetchpv("STDOUT",TRUE); - return GvIO(gv)->ifp; + setdefout(gv_fetchpv("STDOUT",TRUE,SVt_PVIO)); + return IoIFP(GvIOp(gv)); } #ifndef FLEXFILENAMES filedev = statbuf.st_dev; @@ -368,7 +387,6 @@ register GV *gv; warn("Can't do inplace edit: %s is not a regular file", oldname ); do_close(gv,FALSE); - sv_free(sv); continue; } if (*inplace) { @@ -378,13 +396,12 @@ register GV *gv; sv_catpv(sv,inplace); #endif #ifndef FLEXFILENAMES - if (stat(SvPVX(sv),&statbuf) >= 0 + if (Stat(SvPVX(sv),&statbuf) >= 0 && statbuf.st_dev == filedev && statbuf.st_ino == fileino ) { warn("Can't do inplace edit: %s > 14 characters", SvPVX(sv) ); do_close(gv,FALSE); - sv_free(sv); continue; } #endif @@ -392,38 +409,37 @@ register GV *gv; #ifndef DOSISH if (rename(oldname,SvPVX(sv)) < 0) { warn("Can't rename %s to %s: %s, skipping file", - oldname, SvPVX(sv), strerror(errno) ); + oldname, SvPVX(sv), Strerror(errno) ); do_close(gv,FALSE); - sv_free(sv); continue; } #else do_close(gv,FALSE); (void)unlink(SvPVX(sv)); (void)rename(oldname,SvPVX(sv)); - do_open(gv,SvPVX(sv),SvCUR(GvSV(gv))); -#endif /* MSDOS */ + do_open(gv,SvPVX(sv),SvCUR(sv),FALSE,0,0,Nullfp); +#endif /* DOSISH */ #else (void)UNLINK(SvPVX(sv)); if (link(oldname,SvPVX(sv)) < 0) { warn("Can't rename %s to %s: %s, skipping file", - oldname, SvPVX(sv), strerror(errno) ); + oldname, SvPVX(sv), Strerror(errno) ); do_close(gv,FALSE); - sv_free(sv); continue; } (void)UNLINK(oldname); #endif } else { -#ifndef DOSISH +#if !defined(DOSISH) && !defined(AMIGAOS) +# ifndef VMS /* Don't delete; use automatic file versioning */ if (UNLINK(oldname) < 0) { warn("Can't rename %s to %s: %s, skipping file", - oldname, SvPVX(sv), strerror(errno) ); + oldname, SvPVX(sv), Strerror(errno) ); do_close(gv,FALSE); - sv_free(sv); continue; } +# endif #else croak("Can't do inplace edit without backup"); #endif @@ -431,17 +447,16 @@ register GV *gv; sv_setpvn(sv,">",1); sv_catpv(sv,oldname); - errno = 0; /* in case sprintf set errno */ - if (!do_open(argvoutgv,SvPVX(sv),SvCUR(sv))) { + SETERRNO(0,0); /* in case sprintf set errno */ + if (!do_open(argvoutgv,SvPVX(sv),SvCUR(sv),FALSE,0,0,Nullfp)) { warn("Can't do inplace edit on %s: %s", - oldname, strerror(errno) ); + oldname, Strerror(errno) ); do_close(gv,FALSE); - sv_free(sv); continue; } - defoutgv = argvoutgv; - lastfd = fileno(GvIO(argvoutgv)->ifp); - (void)fstat(lastfd,&statbuf); + setdefout(argvoutgv); + lastfd = PerlIO_fileno(IoIFP(GvIOp(argvoutgv))); + (void)Fstat(lastfd,&statbuf); #ifdef HAS_FCHMOD (void)fchmod(lastfd,filemode); #else @@ -457,16 +472,14 @@ register GV *gv; #endif } } - sv_free(sv); - return GvIO(gv)->ifp; + return IoIFP(GvIOp(gv)); } else - fprintf(stderr,"Can't open %s: %s\n",SvPV(sv, na), strerror(errno)); - sv_free(sv); + PerlIO_printf(PerlIO_stderr(), "Can't open %s: %s\n",SvPV(sv, na), Strerror(errno)); } if (inplace) { (void)do_close(argvoutgv,FALSE); - defoutgv = gv_fetchpv("STDOUT",TRUE); + setdefout(gv_fetchpv("STDOUT",TRUE,SVt_PVIO)); } return Nullfp; } @@ -487,29 +500,25 @@ GV *wgv; if (!wgv) goto badexit; - rstio = GvIO(rgv); - wstio = GvIO(wgv); + rstio = GvIOn(rgv); + wstio = GvIOn(wgv); - if (!rstio) - rstio = GvIO(rgv) = newIO(); - else if (rstio->ifp) + if (IoIFP(rstio)) do_close(rgv,FALSE); - if (!wstio) - wstio = GvIO(wgv) = newIO(); - else if (wstio->ifp) + if (IoIFP(wstio)) do_close(wgv,FALSE); if (pipe(fd) < 0) goto badexit; - rstio->ifp = fdopen(fd[0], "r"); - wstio->ofp = fdopen(fd[1], "w"); - wstio->ifp = wstio->ofp; - rstio->type = '<'; - wstio->type = '>'; - if (!rstio->ifp || !wstio->ofp) { - if (rstio->ifp) fclose(rstio->ifp); + IoIFP(rstio) = PerlIO_fdopen(fd[0], "r"); + IoOFP(wstio) = PerlIO_fdopen(fd[1], "w"); + IoIFP(wstio) = IoOFP(wstio); + IoTYPE(rstio) = '<'; + IoTYPE(wstio) = '>'; + if (!IoIFP(rstio) || !IoOFP(wstio)) { + if (IoIFP(rstio)) PerlIO_close(IoIFP(rstio)); else close(fd[0]); - if (wstio->ofp) fclose(wstio->ofp); + if (IoOFP(wstio)) PerlIO_close(IoOFP(wstio)); else close(fd[1]); goto badexit; } @@ -523,51 +532,67 @@ badexit: } #endif +/* explicit renamed to avoid C++ conflict -- kja */ bool -do_close(gv,explicit) +#ifndef CAN_PROTOTYPE +do_close(gv,not_implicit) GV *gv; -bool explicit; +bool not_implicit; +#else +do_close(GV *gv, bool not_implicit) +#endif /* CAN_PROTOTYPE */ { - bool retval = FALSE; - register IO *io; - int status; + bool retval; + IO *io; if (!gv) gv = argvgv; - if (!gv) { - errno = EBADF; + if (!gv || SvTYPE(gv) != SVt_PVGV) { + SETERRNO(EBADF,SS$_IVCHAN); return FALSE; } io = GvIO(gv); if (!io) { /* never opened */ - if (dowarn && explicit) + if (dowarn && not_implicit) warn("Close on unopened file <%s>",GvENAME(gv)); return FALSE; } - if (io->ifp) { - if (io->type == '|') { - status = my_pclose(io->ifp); + retval = io_close(io); + if (not_implicit) { + IoLINES(io) = 0; + IoPAGE(io) = 0; + IoLINES_LEFT(io) = IoPAGE_LEN(io); + } + IoTYPE(io) = ' '; + return retval; +} + +bool +io_close(io) +IO* io; +{ + bool retval = FALSE; + int status; + + if (IoIFP(io)) { + if (IoTYPE(io) == '|') { + status = my_pclose(IoIFP(io)); retval = (status == 0); - statusvalue = (unsigned short)status & 0xffff; + statusvalue = FIXSTATUS(status); } - else if (io->type == '-') + else if (IoTYPE(io) == '-') retval = TRUE; else { - if (io->ofp && io->ofp != io->ifp) { /* a socket */ - retval = (fclose(io->ofp) != EOF); - fclose(io->ifp); /* clear stdio, fd already closed */ + if (IoOFP(io) && IoOFP(io) != IoIFP(io)) { /* a socket */ + retval = (PerlIO_close(IoOFP(io)) != EOF); + PerlIO_close(IoIFP(io)); /* clear stdio, fd already closed */ } else - retval = (fclose(io->ifp) != EOF); + retval = (PerlIO_close(IoIFP(io)) != EOF); } - io->ofp = io->ifp = Nullfp; + IoOFP(io) = IoIFP(io) = Nullfp; } - if (explicit) { - io->lines = 0; - io->page = 0; - io->lines_left = io->page_len; - } - io->type = ' '; + return retval; } @@ -583,23 +608,23 @@ GV *gv; if (!io) return TRUE; - while (io->ifp) { + while (IoIFP(io)) { -#ifdef STDSTDIO /* (the code works without this) */ - if (io->ifp->_cnt > 0) /* cheat a little, since */ - return FALSE; /* this is the most usual case */ -#endif + if (PerlIO_has_cntptr(IoIFP(io))) { /* (the code works without this) */ + if (PerlIO_get_cnt(IoIFP(io)) > 0) /* cheat a little, since */ + return FALSE; /* this is the most usual case */ + } - ch = getc(io->ifp); + ch = PerlIO_getc(IoIFP(io)); if (ch != EOF) { - (void)ungetc(ch, io->ifp); + (void)PerlIO_ungetc(IoIFP(io),ch); return FALSE; } -#ifdef STDSTDIO - if (io->ifp->_cnt < -1) - io->ifp->_cnt = -1; -#endif - if (gv == argvgv) { /* not necessarily a real EOF yet? */ + if (PerlIO_has_cntptr(IoIFP(io)) && PerlIO_canset_cnt(IoIFP(io))) { + if (PerlIO_get_cnt(IoIFP(io)) < -1) + PerlIO_set_cnt(IoIFP(io),-1); + } + if (op->op_flags & OPf_SPECIAL) { /* not necessarily a real EOF yet? */ if (!nextargv(argvgv)) /* get another fp handy */ return TRUE; } @@ -619,20 +644,20 @@ GV *gv; goto phooey; io = GvIO(gv); - if (!io || !io->ifp) + if (!io || !IoIFP(io)) goto phooey; #ifdef ULTRIX_STDIO_BOTCH - if (feof(io->ifp)) - (void)fseek (io->ifp, 0L, 2); /* ultrix 1.2 workaround */ + if (PerlIO_eof(IoIFP(io))) + (void)PerlIO_seek (IoIFP(io), 0L, 2); /* ultrix 1.2 workaround */ #endif - return ftell(io->ifp); + return PerlIO_tell(IoIFP(io)); phooey: if (dowarn) warn("tell() on unopened file"); - errno = EBADF; + SETERRNO(EBADF,RMS$_IFI); return -1L; } @@ -648,108 +673,36 @@ int whence; goto nuts; io = GvIO(gv); - if (!io || !io->ifp) + if (!io || !IoIFP(io)) goto nuts; #ifdef ULTRIX_STDIO_BOTCH - if (feof(io->ifp)) - (void)fseek (io->ifp, 0L, 2); /* ultrix 1.2 workaround */ + if (PerlIO_eof(IoIFP(io))) + (void)PerlIO_seek (IoIFP(io), 0L, 2); /* ultrix 1.2 workaround */ #endif - return fseek(io->ifp, pos, whence) >= 0; + return PerlIO_seek(IoIFP(io), pos, whence) >= 0; nuts: if (dowarn) warn("seek() on unopened file"); - errno = EBADF; + SETERRNO(EBADF,RMS$_IFI); return FALSE; } -I32 -do_ctl(optype,gv,func,argstr) -I32 optype; -GV *gv; -I32 func; -SV *argstr; -{ - register IO *io; - register char *s; - I32 retval; - - if (!gv || !argstr || !(io = GvIO(gv)) || !io->ifp) { - errno = EBADF; /* well, sort of... */ - return -1; - } - - if (SvPOK(argstr) || !SvNIOK(argstr)) { - if (!SvPOK(argstr)) - s = SvPV(argstr, na); - -#ifdef IOCPARM_MASK -#ifndef IOCPARM_LEN -#define IOCPARM_LEN(x) (((x) >> 16) & IOCPARM_MASK) -#endif -#endif -#ifdef IOCPARM_LEN - retval = IOCPARM_LEN(func); /* on BSDish systes we're safe */ -#else - retval = 256; /* otherwise guess at what's safe */ -#endif - if (SvCUR(argstr) < retval) { - Sv_Grow(argstr,retval+1); - SvCUR_set(argstr, retval); - } - - s = SvPVX(argstr); - s[SvCUR(argstr)] = 17; /* a little sanity check here */ - } - else { - retval = SvIV(argstr); -#ifdef DOSISH - s = (char*)(long)retval; /* ouch */ -#else - s = (char*)retval; /* ouch */ -#endif - } - -#ifndef lint - if (optype == OP_IOCTL) - retval = ioctl(fileno(io->ifp), func, s); - else -#ifdef DOSISH - croak("fcntl is not implemented"); -#else -#ifdef HAS_FCNTL - retval = fcntl(fileno(io->ifp), func, s); -#else - croak("fcntl is not implemented"); -#endif -#endif -#else /* lint */ - retval = 0; -#endif /* lint */ - - if (SvPOK(argstr)) { - if (s[SvCUR(argstr)] != 17) - croak("Return value overflowed string"); - s[SvCUR(argstr)] = 0; /* put our null back */ - } - return retval; -} - -#if !defined(HAS_TRUNCATE) && !defined(HAS_CHSIZE) && defined(FFt_FREESP) +#if !defined(HAS_TRUNCATE) && !defined(HAS_CHSIZE) && defined(F_FREESP) /* code courtesy of William Kucharski */ #define HAS_CHSIZE -I32 chsize(fd, length) +I32 my_chsize(fd, length) I32 fd; /* file descriptor */ -off_t length; /* length to set file to */ +Off_t length; /* length to set file to */ { extern long lseek(); struct flock fl; struct stat filebuf; - if (fstat(fd, &filebuf) < 0) + if (Fstat(fd, &filebuf) < 0) return -1; if (filebuf.st_size < length) { @@ -770,118 +723,69 @@ off_t length; /* length to set file to */ fl.l_whence = 0; fl.l_len = 0; fl.l_start = length; - fl.l_type = FFt_WRLCK; /* write lock on file space */ + fl.l_type = F_WRLCK; /* write lock on file space */ /* - * This relies on the UNDOCUMENTED FFt_FREESP argument to + * This relies on the UNDOCUMENTED F_FREESP argument to * fcntl(2), which truncates the file so that it ends at the * position indicated by fl.l_start. * * Will minor miracles never cease? */ - if (fcntl(fd, FFt_FREESP, &fl) < 0) + if (fcntl(fd, F_FREESP, &fl) < 0) return -1; } return 0; } -#endif /* FFt_FREESP */ - -I32 -looks_like_number(sv) -SV *sv; -{ - register char *s; - register char *send; - - if (!SvPOK(sv)) { - STRLEN len; - if (!SvPOKp(sv)) - return TRUE; - s = SvPV(sv, len); - send = s + len; - } - else { - s = SvPVX(sv); - send = s + SvCUR(sv); - } - while (isSPACE(*s)) - s++; - if (s >= send) - return FALSE; - if (*s == '+' || *s == '-') - s++; - while (isDIGIT(*s)) - s++; - if (s == send) - return TRUE; - if (*s == '.') - s++; - else if (s == SvPVX(sv)) - return FALSE; - while (isDIGIT(*s)) - s++; - if (s == send) - return TRUE; - if (*s == 'e' || *s == 'E') { - s++; - if (*s == '+' || *s == '-') - s++; - while (isDIGIT(*s)) - s++; - } - while (isSPACE(*s)) - s++; - if (s >= send) - return TRUE; - return FALSE; -} +#endif /* F_FREESP */ bool do_print(sv,fp) register SV *sv; -FILE *fp; +PerlIO *fp; { register char *tmps; - SV* tmpstr; STRLEN len; /* assuming fp is checked earlier */ if (!sv) return TRUE; if (ofmt) { - if (SvMAGICAL(sv)) + if (SvGMAGICAL(sv)) mg_get(sv); if (SvIOK(sv) && SvIVX(sv) != 0) { - fprintf(fp, ofmt, (double)SvIVX(sv)); - return !ferror(fp); + PerlIO_printf(fp, ofmt, (double)SvIVX(sv)); + return !PerlIO_error(fp); } if ( (SvNOK(sv) && SvNVX(sv) != 0.0) || (looks_like_number(sv) && sv_2nv(sv) != 0.0) ) { - fprintf(fp, ofmt, SvNVX(sv)); - return !ferror(fp); + PerlIO_printf(fp, ofmt, SvNVX(sv)); + return !PerlIO_error(fp); } } switch (SvTYPE(sv)) { case SVt_NULL: + if (dowarn) + warn(warn_uninit); return TRUE; - case SVt_REF: - fprintf(fp, "%s", sv_2pv(sv, &na)); - return !ferror(fp); case SVt_IV: - if (SvMAGICAL(sv)) - mg_get(sv); - fprintf(fp, "%d", SvIVX(sv)); - return !ferror(fp); + if (SvIOK(sv)) { + if (SvGMAGICAL(sv)) + mg_get(sv); + PerlIO_printf(fp, "%ld", (long)SvIVX(sv)); + return !PerlIO_error(fp); + } + /* FALL THROUGH */ default: tmps = SvPV(sv, len); break; } - if (len && (fwrite(tmps,1,len,fp) == 0 || ferror(fp))) + if (len && (PerlIO_write(fp,tmps,len) == 0 || PerlIO_error(fp))) return FALSE; - return TRUE; + return !PerlIO_error(fp); } I32 @@ -890,34 +794,46 @@ dARGS { dSP; IO *io; + GV* tmpgv; - if (op->op_flags & OPf_SPECIAL) { + if (op->op_flags & OPf_REF) { EXTEND(sp,1); - io = GvIO(cGVOP->op_gv); - if (io && io->ifp) { - statgv = cGVOP->op_gv; + tmpgv = cGVOP->op_gv; + do_fstat: + io = GvIO(tmpgv); + if (io && IoIFP(io)) { + statgv = tmpgv; sv_setpv(statname,""); laststype = OP_STAT; - return (laststatval = fstat(fileno(io->ifp), &statcache)); + return (laststatval = Fstat(PerlIO_fileno(IoIFP(io)), &statcache)); } else { - if (cGVOP->op_gv == defgv) + if (tmpgv == defgv) return laststatval; if (dowarn) warn("Stat on unopened file <%s>", - GvENAME(cGVOP->op_gv)); + GvENAME(tmpgv)); statgv = Nullgv; sv_setpv(statname,""); return (laststatval = -1); } } else { - dPOPss; + SV* sv = POPs; PUTBACK; + if (SvTYPE(sv) == SVt_PVGV) { + tmpgv = (GV*)sv; + goto do_fstat; + } + else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) { + tmpgv = (GV*)SvRV(sv); + goto do_fstat; + } + statgv = Nullgv; sv_setpv(statname,SvPV(sv, na)); laststype = OP_STAT; - laststatval = stat(SvPV(sv, na),&statcache); + laststatval = Stat(SvPV(sv, na),&statcache); if (laststatval < 0 && dowarn && strchr(SvPV(sv, na), '\n')) warn(warn_nl, "stat"); return laststatval; @@ -930,7 +846,7 @@ dARGS { dSP; SV *sv; - if (op->op_flags & OPf_SPECIAL) { + if (op->op_flags & OPf_REF) { EXTEND(sp,1); if (cGVOP->op_gv == defgv) { if (laststype != OP_LSTAT) @@ -948,7 +864,7 @@ dARGS #ifdef HAS_LSTAT laststatval = lstat(SvPV(sv, na),&statcache); #else - laststatval = stat(SvPV(sv, na),&statcache); + laststatval = Stat(SvPV(sv, na),&statcache); #endif if (laststatval < 0 && dowarn && strchr(SvPV(sv, na), '\n')) warn(warn_nl, "lstat"); @@ -980,6 +896,8 @@ register SV **sp; execvp(tmps,Argv); else execvp(Argv[0],Argv); + if (dowarn) + warn("Can't exec \"%s\": %s", Argv[0], Strerror(errno)); } do_execfree(); return FALSE; @@ -998,6 +916,8 @@ do_execfree() } } +#ifndef OS2 + bool do_exec(cmd) char *cmd; @@ -1006,6 +926,9 @@ char *cmd; register char *s; char flags[10]; + while (*cmd && isSPACE(*cmd)) + cmd++; + /* save an extra exec if possible */ #ifdef CSH @@ -1037,10 +960,16 @@ char *cmd; /* see if there are shell metacharacters in it */ - /*SUPPRESS 530*/ + if (*cmd == '.' && isSPACE(cmd[1])) + goto doshell; + + if (strnEQ(cmd,"exec",4) && isSPACE(cmd[4])) + goto doshell; + for (s = cmd; *s && isALPHA(*s); s++) ; /* catch VAR=val gizmo */ if (*s == '=') goto doshell; + for (s = cmd; *s; s++) { if (*s != ' ' && !isALPHA(*s) && strchr("$&*(){}[]'\";\\|?<>~`\n",*s)) { if (*s == '\n' && !s[1]) { @@ -1048,12 +977,13 @@ char *cmd; break; } doshell: - execl("/bin/sh","sh","-c",cmd,(char*)0); + execl(sh_path, "sh", "-c", cmd, (char*)0); return FALSE; } } + New(402,Argv, (s - cmd) / 2 + 2, char*); - Cmd = nsavestr(cmd, s-cmd); + Cmd = savepvn(cmd, s-cmd); a = Argv; for (s = Cmd; *s;) { while (*s && isSPACE(*s)) s++; @@ -1070,11 +1000,15 @@ char *cmd; do_execfree(); goto doshell; } + if (dowarn) + warn("Can't exec \"%s\": %s", Argv[0], Strerror(errno)); } do_execfree(); return FALSE; } +#endif /* OS2 */ + I32 apply(type,mark,sp) I32 type; @@ -1089,8 +1023,10 @@ register SV **sp; if (tainting) { while (++mark <= sp) { - if (SvMAGICAL(*mark) && mg_find(*mark, 't')) - tainted = TRUE; + if (SvTAINTED(*mark)) { + TAINT; + break; + } } mark = oldmark; } @@ -1110,9 +1046,9 @@ register SV **sp; case OP_CHOWN: TAINT_PROPER("chown"); if (sp - mark > 2) { - tot = sp - mark; val = SvIVx(*++mark); val2 = SvIVx(*++mark); + tot = sp - mark; while (++mark <= sp) { if (chown(SvPVx(*mark, na),val,val2)) tot--; @@ -1123,6 +1059,8 @@ register SV **sp; #ifdef HAS_KILL case OP_KILL: TAINT_PROPER("kill"); + if (mark == sp) + break; s = SvPVx(*++mark, na); tot = sp - mark; if (isUPPER(*s)) { @@ -1133,6 +1071,36 @@ register SV **sp; } else val = SvIVx(*mark); +#ifdef VMS + /* kill() doesn't do process groups (job trees?) under VMS */ + if (val < 0) val = -val; + if (val == SIGKILL) { +# include + /* Use native sys$delprc() to insure that target process is + * deleted; supervisor-mode images don't pay attention to + * CRTL's emulation of Unix-style signals and kill() + */ + while (++mark <= sp) { + I32 proc = SvIVx(*mark); + register unsigned long int __vmssts; + if (!((__vmssts = sys$delprc(&proc,0)) & 1)) { + tot--; + switch (__vmssts) { + case SS$_NONEXPR: + case SS$_NOSUCHNODE: + SETERRNO(ESRCH,__vmssts); + break; + case SS$_NOPRIV: + SETERRNO(EPERM,__vmssts); + break; + default: + SETERRNO(EVMSERR,__vmssts); + } + } + } + break; + } +#endif if (val < 0) { val = -val; while (++mark <= sp) { @@ -1166,7 +1134,7 @@ register SV **sp; #ifdef HAS_LSTAT if (lstat(s,&statbuf) < 0 || S_ISDIR(statbuf.st_mode)) #else - if (stat(s,&statbuf) < 0 || S_ISDIR(statbuf.st_mode)) + if (Stat(s,&statbuf) < 0 || S_ISDIR(statbuf.st_mode)) #endif tot--; else { @@ -1176,10 +1144,11 @@ register SV **sp; } } break; +#ifdef HAS_UTIME case OP_UTIME: TAINT_PROPER("utime"); if (sp - mark > 2) { -#ifdef I_UTIME +#if defined(I_UTIME) || defined(VMS) struct utimbuf utbuf; #else struct { @@ -1189,8 +1158,13 @@ register SV **sp; #endif Zero(&utbuf, sizeof utbuf, char); +#ifdef BIG_TIME + utbuf.actime = (Time_t)SvNVx(*++mark); /* time accessed */ + utbuf.modtime = (Time_t)SvNVx(*++mark); /* time modified */ +#else utbuf.actime = SvIVx(*++mark); /* time accessed */ utbuf.modtime = SvIVx(*++mark); /* time modified */ +#endif tot = sp - mark; while (++mark <= sp) { if (utime(SvPVx(*mark, na),&utbuf)) @@ -1200,12 +1174,13 @@ register SV **sp; else tot = 0; break; +#endif } return tot; } /* Do the permissions allow some operation? Assumes statcache already set. */ - +#ifndef VMS /* VMS' cando is in vms.c */ I32 cando(bit, effective, statbufp) I32 bit; @@ -1236,7 +1211,7 @@ register struct stat *statbufp; */ return (bit & statbufp->st_mode) ? TRUE : FALSE; -#else /* ! MSDOS */ +#else /* ! DOSISH */ if ((effective ? euid : uid) == 0) { /* root is special */ if (bit == S_IXUSR) { if (statbufp->st_mode & 0111 || S_ISDIR(statbufp->st_mode)) @@ -1257,8 +1232,9 @@ register struct stat *statbufp; else if (statbufp->st_mode & bit >> 6) return TRUE; /* ok as "other" */ return FALSE; -#endif /* ! MSDOS */ +#endif /* ! DOSISH */ } +#endif /* ! VMS */ I32 ingroup(testgid,effective) @@ -1272,7 +1248,7 @@ I32 effective; #define NGROUPS 32 #endif { - GROUPSTYPE gary[NGROUPS]; + Groups_t gary[NGROUPS]; I32 anum; anum = getgroups(NGROUPS,gary); @@ -1298,7 +1274,7 @@ SV **sp; key = (key_t)SvNVx(*++mark); n = (optype == OP_MSGGET) ? 0 : SvIVx(*++mark); flags = SvIVx(*++mark); - errno = 0; + SETERRNO(0,0); switch (optype) { #ifdef HAS_MSG @@ -1315,7 +1291,7 @@ SV **sp; #endif #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM) default: - croak("%s not implemented", op_name[optype]); + croak("%s not implemented", op_desc[optype]); #endif } return -1; /* should never happen */ @@ -1329,7 +1305,8 @@ SV **sp; { SV *astr; char *a; - I32 id, n, cmd, infosize, getinfo, ret; + I32 id, n, cmd, infosize, getinfo; + I32 ret = -1; id = SvIVx(*++mark); n = (optype == OP_SEMCTL) ? SvIVx(*++mark) : 0; @@ -1370,26 +1347,24 @@ SV **sp; #endif #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM) default: - croak("%s not implemented", op_name[optype]); + croak("%s not implemented", op_desc[optype]); #endif } if (infosize) { + STRLEN len; if (getinfo) { - if (SvREADONLY(astr)) - croak("Can't %s to readonly var", op_name[optype]); - SvGROW(astr, infosize+1); - a = SvPV(astr, na); + SvPV_force(astr, len); + a = SvGROW(astr, infosize+1); } else { - STRLEN len; a = SvPV(astr, len); if (len != infosize) croak("Bad arg length for %s, is %d, should be %d", - op_name[optype], len, infosize); + op_desc[optype], len, infosize); } } else @@ -1397,7 +1372,7 @@ SV **sp; I32 i = SvIV(astr); a = (char *)i; /* ouch */ } - errno = 0; + SETERRNO(0,0); switch (optype) { #ifdef HAS_MSG @@ -1419,6 +1394,7 @@ SV **sp; if (getinfo && ret >= 0) { SvCUR_set(astr, infosize); *SvEND(astr) = '\0'; + SvSETMAGIC(astr); } return ret; } @@ -1440,7 +1416,7 @@ SV **sp; mbuf = SvPV(mstr, len); if ((msize = len - sizeof(long)) < 0) croak("Arg too short for msgsnd"); - errno = 0; + SETERRNO(0,0); return msgsnd(id, (struct msgbuf *)mbuf, msize, flags); #else croak("msgsnd not implemented"); @@ -1464,14 +1440,16 @@ SV **sp; msize = SvIVx(*++mark); mtype = (long)SvIVx(*++mark); flags = SvIVx(*++mark); - if (SvREADONLY(mstr)) - croak("Can't msgrcv to readonly var"); - mbuf = SvPV(mstr, len); - if (len < sizeof(long)+msize+1) { - SvGROW(mstr, sizeof(long)+msize+1); - mbuf = SvPV(mstr, len); + if (SvTHINKFIRST(mstr)) { + if (SvREADONLY(mstr)) + croak("Can't msgrcv to readonly var"); + if (SvROK(mstr)) + sv_unref(mstr); } - errno = 0; + SvPV_force(mstr, len); + mbuf = SvGROW(mstr, sizeof(long)+msize+1); + + SETERRNO(0,0); ret = msgrcv(id, (struct msgbuf *)mbuf, msize, mtype, flags); if (ret >= 0) { SvCUR_set(mstr, sizeof(long)+ret); @@ -1499,10 +1477,10 @@ SV **sp; opbuf = SvPV(opstr, opsize); if (opsize < sizeof(struct sembuf) || (opsize % sizeof(struct sembuf)) != 0) { - errno = EINVAL; + SETERRNO(EINVAL,LIB$_INVARG); return -1; } - errno = 0; + SETERRNO(0,0); return semop(id, (struct sembuf *)opbuf, opsize/sizeof(struct sembuf)); #else croak("semop not implemented"); @@ -1521,39 +1499,34 @@ SV **sp; I32 id, mpos, msize; STRLEN len; struct shmid_ds shmds; -#ifndef VOIDSHMAT - extern char *shmat(); -#endif id = SvIVx(*++mark); mstr = *++mark; mpos = SvIVx(*++mark); msize = SvIVx(*++mark); - errno = 0; + SETERRNO(0,0); if (shmctl(id, IPC_STAT, &shmds) == -1) return -1; if (mpos < 0 || msize < 0 || mpos + msize > shmds.shm_segsz) { - errno = EFAULT; /* can't do as caller requested */ + SETERRNO(EFAULT,SS$_ACCVIO); /* can't do as caller requested */ return -1; } - shm = (char*)shmat(id, (char*)NULL, (optype == OP_SHMREAD) ? SHM_RDONLY : 0); + shm = (Shmat_t)shmat(id, (char*)NULL, (optype == OP_SHMREAD) ? SHM_RDONLY : 0); if (shm == (char *)-1) /* I hate System V IPC, I really do */ return -1; - mbuf = SvPV(mstr, len); if (optype == OP_SHMREAD) { - if (SvREADONLY(mstr)) - croak("Can't shmread to readonly var"); - if (len < msize) { - SvGROW(mstr, msize+1); - mbuf = SvPV(mstr, len); - } + SvPV_force(mstr, len); + mbuf = SvGROW(mstr, msize+1); + Copy(shm + mpos, mbuf, msize, char); SvCUR_set(mstr, msize); *SvEND(mstr) = '\0'; + SvSETMAGIC(mstr); } else { I32 n; + mbuf = SvPV(mstr, len); if ((n = len) > msize) n = msize; Copy(mbuf, shm + mpos, n, char);