X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=doio.c;h=61c21b5c1ca0d07989f1b41b1ab106fd959a1da9;hb=58a50f6288bdd18f3f469352f83c5e7ade50ee7a;hp=b25bb9c30f876f245c25d4b500702aea87ac7596;hpb=3028581bb6e49415e90ca9e7935ef77e075f56d6;p=p5sagit%2Fp5-mst-13.2.git diff --git a/doio.c b/doio.c index b25bb9c..61c21b5 100644 --- a/doio.c +++ b/doio.c @@ -34,18 +34,24 @@ #endif #ifdef I_UTIME -# ifdef _MSC_VER +# if defined(_MSC_VER) || defined(__MINGW32__) # include # else # include # endif #endif + #ifdef I_FCNTL #include #endif #ifdef I_SYS_FILE #include #endif +#ifdef O_EXCL +# define OPEN_EXCL O_EXCL +#else +# define OPEN_EXCL 0 +#endif #if !defined(NSIG) || defined(M_UNIX) || defined(M_XENIX) #include @@ -86,6 +92,7 @@ do_open(GV *gv, register char *name, I32 len, int as_raw, int rawmode, int rawpe PerlIO *fp; int fd; int result; + bool was_fdopen = FALSE; forkprocess = 1; /* assume true if no fork */ @@ -164,8 +171,11 @@ do_open(GV *gv, register char *name, I32 len, int as_raw, int rawmode, int rawpe if (strNE(name,"-")) TAINT_ENV(); TAINT_PROPER("piped open"); - if (dowarn && name[strlen(name)-1] == '|') - warn("Can't do bidirectional pipe"); + if (name[strlen(name)-1] == '|') { + name[strlen(name)-1] = '\0' ; + if (dowarn) + warn("Can't do bidirectional pipe"); + } fp = PerlProc_popen(name,"w"); writing = 1; } @@ -215,6 +225,8 @@ do_open(GV *gv, register char *name, I32 len, int as_raw, int rawmode, int rawpe } if (dodup) fd = PerlLIO_dup(fd); + else + was_fdopen = TRUE; if (!(fp = PerlIO_fdopen(fd,mode))) { if (dodup) PerlLIO_close(fd); @@ -324,7 +336,8 @@ do_open(GV *gv, register char *name, I32 len, int as_raw, int rawmode, int rawpe sv = *av_fetch(fdpid,fd,TRUE); (void)SvUPGRADE(sv, SVt_IV); SvIVX(sv) = pid; - PerlIO_close(fp); + if (!was_fdopen) + PerlIO_close(fp); } fp = saveifp; @@ -381,16 +394,16 @@ nextargv(register GV *gv) filemode = 0; while (av_len(GvAV(gv)) >= 0) { dTHR; - STRLEN len; + STRLEN oldlen; 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,FALSE,0,0,Nullfp)) { + oldname = SvPVx(GvSV(gv), oldlen); + if (do_open(gv,oldname,oldlen,inplace!=0,0,0,Nullfp)) { if (inplace) { TAINT_PROPER("inplace open"); - if (strEQ(oldname,"-")) { + if (oldlen == 1 && *oldname == '-') { setdefout(gv_fetchpv("STDOUT",TRUE,SVt_PVIO)); return IoIFP(GvIOp(gv)); } @@ -439,7 +452,7 @@ nextargv(register GV *gv) do_close(gv,FALSE); (void)PerlLIO_unlink(SvPVX(sv)); (void)PerlLIO_rename(oldname,SvPVX(sv)); - do_open(gv,SvPVX(sv),SvCUR(sv),FALSE,0,0,Nullfp); + do_open(gv,SvPVX(sv),SvCUR(sv),inplace!=0,0,0,Nullfp); #endif /* DOSISH */ #else (void)UNLINK(SvPVX(sv)); @@ -456,8 +469,8 @@ nextargv(register GV *gv) #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) ); + warn("Can't remove %s: %s, skipping file", + oldname, Strerror(errno) ); do_close(gv,FALSE); continue; } @@ -467,10 +480,11 @@ nextargv(register GV *gv) #endif } - sv_setpvn(sv,">",1); - sv_catpv(sv,oldname); + sv_setpvn(sv,">",!inplace); + sv_catpvn(sv,oldname,oldlen); SETERRNO(0,0); /* in case sprintf set errno */ - if (!do_open(argvoutgv,SvPVX(sv),SvCUR(sv),FALSE,0,0,Nullfp)) { + if (!do_open(argvoutgv,SvPVX(sv),SvCUR(sv),inplace!=0, + O_WRONLY|O_CREAT|OPEN_EXCL,0666,Nullfp)) { warn("Can't do inplace edit on %s: %s", oldname, Strerror(errno) ); do_close(gv,FALSE); @@ -492,7 +506,7 @@ nextargv(register GV *gv) (void)fchown(lastfd,fileuid,filegid); #else #ifdef HAS_CHOWN - (void)chown(oldname,fileuid,filegid); + (void)PerlLIO_chown(oldname,fileuid,filegid); #endif #endif } @@ -557,13 +571,7 @@ badexit: /* explicit renamed to avoid C++ conflict -- kja */ bool -#ifndef CAN_PROTOTYPE -do_close(gv,not_implicit) -GV *gv; -bool not_implicit; -#else do_close(GV *gv, bool not_implicit) -#endif /* CAN_PROTOTYPE */ { bool retval; IO *io; @@ -578,6 +586,7 @@ do_close(GV *gv, bool not_implicit) if (!io) { /* never opened */ if (dowarn && not_implicit) warn("Close on unopened file <%s>",GvENAME(gv)); + SETERRNO(EBADF,SS$_IVCHAN); return FALSE; } retval = io_close(io); @@ -614,6 +623,9 @@ io_close(IO *io) } IoOFP(io) = IoIFP(io) = Nullfp; } + else { + SETERRNO(EBADF,SS$_IVCHAN); + } return retval; } @@ -708,6 +720,46 @@ do_sysseek(GV *gv, long int pos, int whence) return -1L; } +int +do_binmode(PerlIO *fp, int iotype, int flag) +{ + if (flag != TRUE) + croak("panic: unsetting binmode"); /* Not implemented yet */ +#ifdef DOSISH +#ifdef atarist + if (!PerlIO_flush(fp) && (fp->_flag |= _IOBIN)) + return 1; + else + return 0; +#else + if (PerlLIO_setmode(PerlIO_fileno(fp), OP_BINARY) != -1) { +#if defined(WIN32) && defined(__BORLANDC__) + /* The translation mode of the stream is maintained independent + * of the translation mode of the fd in the Borland RTL (heavy + * digging through their runtime sources reveal). User has to + * set the mode explicitly for the stream (though they don't + * document this anywhere). GSAR 97-5-24 + */ + PerlIO_seek(fp,0L,0); + ((FILE*)fp)->flags |= _F_BIN; +#endif + return 1; + } + else + return 0; +#endif +#else +#if defined(USEMYBINMODE) + if (my_binmode(fp,iotype) != NULL) + return 1; + else + return 0; +#else + return 1; +#endif +#endif +} + #if !defined(HAS_TRUNCATE) && !defined(HAS_CHSIZE) && defined(F_FREESP) /* code courtesy of William Kucharski */ #define HAS_CHSIZE @@ -811,7 +863,7 @@ my_stat(ARGSproto) GV* tmpgv; if (op->op_flags & OPf_REF) { - EXTEND(sp,1); + EXTEND(SP,1); tmpgv = cGVOP->op_gv; do_fstat: io = GvIO(tmpgv); @@ -834,6 +886,7 @@ my_stat(ARGSproto) } else { SV* sv = POPs; + char *s; PUTBACK; if (SvTYPE(sv) == SVt_PVGV) { tmpgv = (GV*)sv; @@ -844,11 +897,12 @@ my_stat(ARGSproto) goto do_fstat; } + s = SvPV(sv, na); statgv = Nullgv; - sv_setpv(statname,SvPV(sv, na)); + sv_setpv(statname, s); laststype = OP_STAT; - laststatval = PerlLIO_stat(SvPV(sv, na),&statcache); - if (laststatval < 0 && dowarn && strchr(SvPV(sv, na), '\n')) + laststatval = PerlLIO_stat(s, &statcache); + if (laststatval < 0 && dowarn && strchr(s, '\n')) warn(warn_nl, "stat"); return laststatval; } @@ -860,7 +914,7 @@ my_lstat(ARGSproto) djSP; SV *sv; if (op->op_flags & OPf_REF) { - EXTEND(sp,1); + EXTEND(SP,1); if (cGVOP->op_gv == defgv) { if (laststype != OP_LSTAT) croak("The stat preceding -l _ wasn't an lstat"); @@ -1026,9 +1080,16 @@ apply(I32 type, register SV **mark, register SV **sp) register I32 val; register I32 val2; register I32 tot = 0; + char *what; char *s; SV **oldmark = mark; +#define APPLY_TAINT_PROPER() \ + STMT_START { \ + if (tainting && tainted) { goto taint_proper_label; } \ + } STMT_END + + /* This is a first heuristic; it doesn't catch tainting magic. */ if (tainting) { while (++mark <= sp) { if (SvTAINTED(*mark)) { @@ -1040,25 +1101,33 @@ apply(I32 type, register SV **mark, register SV **sp) } switch (type) { case OP_CHMOD: - TAINT_PROPER("chmod"); + what = "chmod"; + APPLY_TAINT_PROPER(); if (++mark <= sp) { - tot = sp - mark; val = SvIVx(*mark); + APPLY_TAINT_PROPER(); + tot = sp - mark; while (++mark <= sp) { - if (PerlLIO_chmod(SvPVx(*mark, na),val)) + char *name = SvPVx(*mark, na); + APPLY_TAINT_PROPER(); + if (PerlLIO_chmod(name, val)) tot--; } } break; #ifdef HAS_CHOWN case OP_CHOWN: - TAINT_PROPER("chown"); + what = "chown"; + APPLY_TAINT_PROPER(); if (sp - mark > 2) { val = SvIVx(*++mark); val2 = SvIVx(*++mark); + APPLY_TAINT_PROPER(); tot = sp - mark; while (++mark <= sp) { - if (chown(SvPVx(*mark, na),val,val2)) + char *name = SvPVx(*mark, na); + APPLY_TAINT_PROPER(); + if (PerlLIO_chown(name, val, val2)) tot--; } } @@ -1066,11 +1135,11 @@ apply(I32 type, register SV **mark, register SV **sp) #endif #ifdef HAS_KILL case OP_KILL: - TAINT_PROPER("kill"); + what = "kill"; + APPLY_TAINT_PROPER(); if (mark == sp) break; s = SvPVx(*++mark, na); - tot = sp - mark; if (isUPPER(*s)) { if (*s == 'S' && s[1] == 'I' && s[2] == 'G') s += 3; @@ -1079,6 +1148,8 @@ apply(I32 type, register SV **mark, register SV **sp) } else val = SvIVx(*mark); + APPLY_TAINT_PROPER(); + tot = sp - mark; #ifdef VMS /* kill() doesn't do process groups (job trees?) under VMS */ if (val < 0) val = -val; @@ -1091,6 +1162,7 @@ apply(I32 type, register SV **mark, register SV **sp) while (++mark <= sp) { I32 proc = SvIVx(*mark); register unsigned long int __vmssts; + APPLY_TAINT_PROPER(); if (!((__vmssts = sys$delprc(&proc,0)) & 1)) { tot--; switch (__vmssts) { @@ -1113,6 +1185,7 @@ apply(I32 type, register SV **mark, register SV **sp) val = -val; while (++mark <= sp) { I32 proc = SvIVx(*mark); + APPLY_TAINT_PROPER(); #ifdef HAS_KILLPG if (PerlProc_killpg(proc,val)) /* BSD */ #else @@ -1123,17 +1196,21 @@ apply(I32 type, register SV **mark, register SV **sp) } else { while (++mark <= sp) { - if (PerlProc_kill(SvIVx(*mark),val)) + I32 proc = SvIVx(*mark); + APPLY_TAINT_PROPER(); + if (PerlProc_kill(proc, val)) tot--; } } break; #endif case OP_UNLINK: - TAINT_PROPER("unlink"); + what = "unlink"; + APPLY_TAINT_PROPER(); tot = sp - mark; while (++mark <= sp) { s = SvPVx(*mark, na); + APPLY_TAINT_PROPER(); if (euid || unsafe) { if (UNLINK(s)) tot--; @@ -1154,7 +1231,8 @@ apply(I32 type, register SV **mark, register SV **sp) break; #ifdef HAS_UTIME case OP_UTIME: - TAINT_PROPER("utime"); + what = "utime"; + APPLY_TAINT_PROPER(); if (sp - mark > 2) { #if defined(I_UTIME) || defined(VMS) struct utimbuf utbuf; @@ -1173,9 +1251,12 @@ apply(I32 type, register SV **mark, register SV **sp) utbuf.actime = SvIVx(*++mark); /* time accessed */ utbuf.modtime = SvIVx(*++mark); /* time modified */ #endif + APPLY_TAINT_PROPER(); tot = sp - mark; while (++mark <= sp) { - if (PerlLIO_utime(SvPVx(*mark, na),&utbuf)) + char *name = SvPVx(*mark, na); + APPLY_TAINT_PROPER(); + if (PerlLIO_utime(name, &utbuf)) tot--; } } @@ -1185,6 +1266,12 @@ apply(I32 type, register SV **mark, register SV **sp) #endif } return tot; + + taint_proper_label: + TAINT_PROPER(what); + return 0; /* this should never happen */ + +#undef APPLY_TAINT_PROPER } /* Do the permissions allow some operation? Assumes statcache already set. */ @@ -1298,6 +1385,21 @@ do_ipcget(I32 optype, SV **mark, SV **sp) return -1; /* should never happen */ } +#if defined(__sun) && defined(__svr4__) /* XXX Need metaconfig test */ +/* Solaris manpage says that it uses (like linux) + int semctl (int semid, int semnum, int cmd, union semun arg) + but the system include files do not define union semun !!!! + Note: Linux/glibc *does* declare union semun in + but, unlike the older Linux libc and Solaris, it has an extra + struct seminfo * on the end. +*/ +union semun { + int val; + struct semid_ds *buf; + ushort *array; +}; +#endif + I32 do_ipcctl(I32 optype, SV **mark, SV **sp) { @@ -1306,8 +1408,25 @@ do_ipcctl(I32 optype, SV **mark, SV **sp) char *a; I32 id, n, cmd, infosize, getinfo; I32 ret = -1; -#ifdef __linux__ /* XXX Need metaconfig test */ - union semun unsemds; +/* XXX REALLY need metaconfig test */ +/* linux and Solaris2 use: + int semctl (int semid, int semnum, int cmd, union semun arg) + instead of: + int semctl (int semid, int semnum, int cmd, struct semid_ds *arg); + Solaris and Linux (pre-glibc) use + union semun { + int val; + struct semid_ds *buf; + ushort *array; + }; + but Solaris doesn't declare it in a header file (we declared it + explicitly earlier). Linux/glibc declares a *different* union semun + so we just refer to "union semun" here. + +*/ +#if defined(__linux__) || (defined(__sun__) && defined(__svr4__)) +# define SEMCTL_SEMUN + union semun unsemds, semun; #endif id = SvIVx(*++mark); @@ -1338,16 +1457,7 @@ do_ipcctl(I32 optype, SV **mark, SV **sp) else if (cmd == GETALL || cmd == SETALL) { struct semid_ds semds; -#ifdef __linux__ /* XXX Need metaconfig test */ -/* linux (and Solaris2?) uses : - int semctl (int semid, int semnum, int cmd, union semun arg) - union semun { - int val; - struct semid_ds *buf; - ushort *array; - }; -*/ - union semun semun; +#ifdef SEMCTL_SEMUN semun.buf = &semds; if (semctl(id, 0, IPC_STAT, semun) == -1) #else @@ -1398,7 +1508,8 @@ do_ipcctl(I32 optype, SV **mark, SV **sp) #endif #ifdef HAS_SEM case OP_SEMCTL: -#ifdef __linux__ /* XXX Need metaconfig test */ +#ifdef SEMCTL_SEMUN + /* XXX Need metaconfig test */ unsemds.buf = (struct semid_ds *)a; ret = semctl(id, n, cmd, unsemds); #else