X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=pp_sys.c;h=283dbc186803720db552be67160e1be92e39d82b;hb=eba8ff18d7b4f163fccc1997c51233b02fd29e80;hp=9e6d0652487073c271ee6c111a3bdbc601bef667;hpb=ac27b0f573239284c298fcf96fb6c966551ef207;p=p5sagit%2Fp5-mst-13.2.git diff --git a/pp_sys.c b/pp_sys.c index 9e6d065..283dbc1 100644 --- a/pp_sys.c +++ b/pp_sys.c @@ -1,6 +1,6 @@ /* pp_sys.c * - * Copyright (c) 1991-2000, Larry Wall + * Copyright (c) 1991-2001, 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. @@ -35,11 +35,6 @@ # include #endif -/* XXX If this causes problems, set i_unistd=undef in the hint file. */ -#ifdef I_UNISTD -# include -#endif - #ifdef HAS_SYSCALL #ifdef __cplusplus extern "C" int syscall(unsigned long,...); @@ -306,9 +301,6 @@ PP(pp_backtick) if (PL_curcop->cop_io) { type = SvPV_nolen(PL_curcop->cop_io); } - else if (O_BINARY != O_TEXT) { - type = ":crlf"; - } if (type && *type) PerlIO_apply_layers(aTHX_ fp,mode,type); @@ -456,7 +448,7 @@ PP(pp_die) } else { tmpsv = TOPs; - tmps = SvROK(tmpsv) ? Nullch : SvPV(tmpsv, len); + tmps = (SvROK(tmpsv) && PL_in_eval) ? Nullch : SvPV(tmpsv, len); } if (!tmps || !len) { SV *error = ERRSV; @@ -500,7 +492,9 @@ PP(pp_die) PP(pp_open) { - djSP; dTARGET; + djSP; + dMARK; dORIGMARK; + dTARGET; GV *gv; SV *sv; SV *name = Nullsv; @@ -508,29 +502,19 @@ PP(pp_open) char *tmps; STRLEN len; MAGIC *mg; + bool ok; - if (MAXARG > 2) { - name = POPs; - have_name = 1; - } - if (MAXARG > 1) - sv = POPs; - if (!isGV(TOPs)) - DIE(aTHX_ PL_no_usym, "filehandle"); - if (MAXARG <= 1) - sv = GvSV(TOPs); - gv = (GV*)POPs; + gv = (GV *)*++MARK; if (!isGV(gv)) DIE(aTHX_ PL_no_usym, "filehandle"); if (GvIOp(gv)) IoFLAGS(GvIOp(gv)) &= ~IOf_UNTAINT; if ((mg = SvTIED_mg((SV*)gv, 'q'))) { - PUSHMARK(SP); - XPUSHs(SvTIED_obj((SV*)gv, mg)); - XPUSHs(sv); - if (have_name) - XPUSHs(name); + /* Method's args are same as ours ... */ + /* ... except handle is replaced by the object */ + *MARK-- = SvTIED_obj((SV*)gv, mg); + PUSHMARK(MARK); PUTBACK; ENTER; call_method("OPEN", G_SCALAR); @@ -539,8 +523,17 @@ PP(pp_open) RETURN; } + if (MARK < SP) { + sv = *++MARK; + } + else { + sv = GvSV(gv); + } + tmps = SvPV(sv, len); - if (do_open9(gv, tmps, len, FALSE, O_RDONLY, 0, Nullfp, name, have_name)) + ok = do_openn(gv, tmps, len, FALSE, O_RDONLY, 0, Nullfp, MARK+1, (SP-MARK)); + SP = ORIGMARK; + if (ok) PUSHi( (I32)PL_forkprocess ); else if (PL_forkprocess == 0) /* we are a new child */ PUSHi(0); @@ -653,8 +646,15 @@ PP(pp_fileno) RETURN; } - if (!gv || !(io = GvIO(gv)) || !(fp = IoIFP(io))) + if (!gv || !(io = GvIO(gv)) || !(fp = IoIFP(io))) { + /* Can't do this because people seem to do things like + defined(fileno($foo)) to check whether $foo is a valid fh. + if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)) + report_evil_fh(gv, io, PL_op->op_type); + */ RETPUSHUNDEF; + } + PUSHi(PerlIO_fileno(fp)); RETURN; } @@ -692,11 +692,14 @@ PP(pp_binmode) PerlIO *fp; MAGIC *mg; SV *discp = Nullsv; + STRLEN len = 0; + char *names = NULL; if (MAXARG < 1) RETPUSHUNDEF; - if (MAXARG > 1) + if (MAXARG > 1) { discp = POPs; + } gv = (GV*)POPs; @@ -714,10 +717,18 @@ PP(pp_binmode) } EXTEND(SP, 1); - if (!(io = GvIO(gv)) || !(fp = IoIFP(io))) - RETPUSHUNDEF; + if (!(io = GvIO(gv)) || !(fp = IoIFP(io))) { + if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)) + report_evil_fh(gv, io, PL_op->op_type); + RETPUSHUNDEF; + } + + if (discp) { + names = SvPV(discp,len); + } - if (do_binmode(fp,IoTYPE(io),mode_from_discipline(discp))) + if (PerlIO_binmode(aTHX_ fp,IoTYPE(io),mode_from_discipline(discp), + (discp) ? SvPV_nolen(discp) : Nullch)) RETPUSHYES; else RETPUSHUNDEF; @@ -1059,7 +1070,6 @@ PP(pp_sselect) void Perl_setdefout(pTHX_ GV *gv) { - dTHR; if (gv) (void)SvREFCNT_inc(gv); if (PL_defoutgv) @@ -1130,6 +1140,16 @@ PP(pp_getc) TAINT; sv_setpv(TARG, " "); *SvPVX(TARG) = PerlIO_getc(IoIFP(GvIOp(gv))); /* should never be EOF */ + if (PerlIO_isutf8(IoIFP(GvIOp(gv)))) { + /* Find out how many bytes the char needs */ + Size_t len = UTF8SKIP(SvPVX(TARG)); + if (len > 1) { + SvGROW(TARG,len+1); + len = PerlIO_read(IoIFP(GvIOp(gv)),SvPVX(TARG)+1,len-1); + SvCUR_set(TARG,1+len); + } + SvUTF8_on(TARG); + } PUSHTARG; RETURN; } @@ -1142,7 +1162,6 @@ PP(pp_read) STATIC OP * S_doform(pTHX_ CV *cv, GV *gv, OP *retop) { - dTHR; register PERL_CONTEXT *cx; I32 gimme = GIMME_V; AV* padlist = CvPADLIST(cv); @@ -1218,6 +1237,8 @@ PP(pp_leavewrite) DEBUG_f(PerlIO_printf(Perl_debug_log, "left=%ld, todo=%ld\n", (long)IoLINES_LEFT(io), (long)FmLINES(PL_formtarget))); + if (!io || !ofp) + goto forget_top; if (IoLINES_LEFT(io) < FmLINES(PL_formtarget) && PL_formtarget != PL_toptarget) { @@ -1257,13 +1278,16 @@ PP(pp_leavewrite) s++; } if (s) { - PerlIO_write(ofp, SvPVX(PL_formtarget), s - SvPVX(PL_formtarget)); + STRLEN save = SvCUR(PL_formtarget); + SvCUR_set(PL_formtarget, s - SvPVX(PL_formtarget)); + do_print(PL_formtarget, ofp); + SvCUR_set(PL_formtarget, save); sv_chop(PL_formtarget, s); FmLINES(PL_formtarget) -= IoLINES_LEFT(io); } } if (IoLINES_LEFT(io) >= 0 && IoPAGE(io) > 0) - PerlIO_write(ofp, SvPVX(PL_formfeed), SvCUR(PL_formfeed)); + do_print(PL_formfeed, ofp); IoLINES_LEFT(io) = IoPAGE_LEN(io); IoPAGE(io)++; PL_formtarget = PL_toptarget; @@ -1324,8 +1348,7 @@ PP(pp_leavewrite) if (ckWARN(WARN_IO)) Perl_warner(aTHX_ WARN_IO, "page overflow"); } - if (!PerlIO_write(ofp, SvPVX(PL_formtarget), SvCUR(PL_formtarget)) || - PerlIO_error(fp)) + if (!do_print(PL_formtarget, fp)) PUSHs(&PL_sv_no); else { FmLINES(PL_formtarget) = 0; @@ -1336,6 +1359,7 @@ PP(pp_leavewrite) PUSHs(&PL_sv_yes); } } +bad_ofp: PL_formtarget = PL_bodytarget; PUTBACK; return pop_return(); @@ -1378,7 +1402,6 @@ PP(pp_prtf) sv = NEWSV(0,0); if (!(io = GvIO(gv))) { - dTHR; if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)) report_evil_fh(gv, io, PL_op->op_type); SETERRNO(EBADF,RMS$_IFI); @@ -1465,10 +1488,14 @@ PP(pp_sysread) IO *io; char *buffer; SSize_t length; + SSize_t count; Sock_size_t bufsize; SV *bufsv; STRLEN blen; MAGIC *mg; + int fp_utf8; + Size_t got = 0; + Size_t wanted; gv = (GV*)*++MARK; if ((PL_op->op_type == OP_READ || PL_op->op_type == OP_SYSREAD) && @@ -1493,10 +1520,7 @@ PP(pp_sysread) bufsv = *++MARK; if (! SvOK(bufsv)) sv_setpvn(bufsv, "", 0); - buffer = SvPV_force(bufsv, blen); length = SvIVx(*++MARK); - if (length < 0) - DIE(aTHX_ "Negative length"); SETERRNO(0,0); if (MARK < SP) offset = SvIVx(*++MARK); @@ -1505,6 +1529,18 @@ PP(pp_sysread) io = GvIO(gv); if (!io || !IoIFP(io)) goto say_undef; + if ((fp_utf8 = PerlIO_isutf8(IoIFP(io))) && !IN_BYTE) { + buffer = SvPVutf8_force(bufsv, blen); + /* UTF8 may not have been set if they are all low bytes */ + SvUTF8_on(bufsv); + } + else { + buffer = SvPV_force(bufsv, blen); + } + if (length < 0) + DIE(aTHX_ "Negative length"); + wanted = length; + #ifdef HAS_SOCKET if (PL_op->op_type == OP_RECV) { char namebuf[MAXPATHLEN]; @@ -1517,19 +1553,17 @@ PP(pp_sysread) if (bufsize >= 256) bufsize = 255; #endif -#ifdef OS2 /* At least Warp3+IAK: only the first byte of bufsize set */ - if (bufsize >= 256) - bufsize = 255; -#endif buffer = SvGROW(bufsv, length+1); /* 'offset' means 'flags' here */ - length = PerlSock_recvfrom(PerlIO_fileno(IoIFP(io)), buffer, length, offset, + count = PerlSock_recvfrom(PerlIO_fileno(IoIFP(io)), buffer, length, offset, (struct sockaddr *)namebuf, &bufsize); - if (length < 0) + if (count < 0) RETPUSHUNDEF; - SvCUR_set(bufsv, length); + SvCUR_set(bufsv, count); *SvEND(bufsv) = '\0'; (void)SvPOK_only(bufsv); + if (fp_utf8) + SvUTF8_on(bufsv); SvSETMAGIC(bufsv); /* This should not be marked tainted if the fp is marked clean */ if (!(IoFLAGS(io) & IOf_UNTAINT)) @@ -1543,27 +1577,38 @@ PP(pp_sysread) if (PL_op->op_type == OP_RECV) DIE(aTHX_ PL_no_sock_func, "recv"); #endif + if (DO_UTF8(bufsv)) { + /* offset adjust in characters not bytes */ + blen = sv_len_utf8(bufsv); + } if (offset < 0) { if (-offset > blen) DIE(aTHX_ "Offset outside string"); offset += blen; } + if (DO_UTF8(bufsv)) { + /* convert offset-as-chars to offset-as-bytes */ + offset = utf8_hop((U8 *)buffer,offset) - (U8 *) buffer; + } + more_bytes: bufsize = SvCUR(bufsv); - buffer = SvGROW(bufsv, length+offset+1); + buffer = SvGROW(bufsv, length+offset+1); if (offset > bufsize) { /* Zero any newly allocated space */ Zero(buffer+bufsize, offset-bufsize, char); } + buffer = buffer + offset; + if (PL_op->op_type == OP_SYSREAD) { #ifdef PERL_SOCK_SYSREAD_IS_RECV if (IoTYPE(io) == IoTYPE_SOCKET) { - length = PerlSock_recv(PerlIO_fileno(IoIFP(io)), - buffer+offset, length, 0); + count = PerlSock_recv(PerlIO_fileno(IoIFP(io)), + buffer, length, 0); } else #endif { - length = PerlLIO_read(PerlIO_fileno(IoIFP(io)), - buffer+offset, length); + count = PerlLIO_read(PerlIO_fileno(IoIFP(io)), + buffer, length); } } else @@ -1575,18 +1620,18 @@ PP(pp_sysread) #else bufsize = sizeof namebuf; #endif - length = PerlSock_recvfrom(PerlIO_fileno(IoIFP(io)), buffer+offset, length, 0, + count = PerlSock_recvfrom(PerlIO_fileno(IoIFP(io)), buffer, length, 0, (struct sockaddr *)namebuf, &bufsize); } else #endif { - length = PerlIO_read(IoIFP(io), buffer+offset, length); - /* fread() returns 0 on both error and EOF */ - if (length == 0 && PerlIO_error(IoIFP(io))) - length = -1; + count = PerlIO_read(IoIFP(io), buffer, length); + /* PerlIO_read() - like fread() returns 0 on both error and EOF */ + if (count == 0 && PerlIO_error(IoIFP(io))) + count = -1; } - if (length < 0) { + if (count < 0) { if ((IoTYPE(io) == IoTYPE_WRONLY || IoIFP(io) == PerlIO_stdout() || IoIFP(io) == PerlIO_stderr()) && ckWARN(WARN_IO)) { @@ -1606,15 +1651,43 @@ PP(pp_sysread) } goto say_undef; } - SvCUR_set(bufsv, length+offset); + SvCUR_set(bufsv, count+(buffer - SvPVX(bufsv))); *SvEND(bufsv) = '\0'; (void)SvPOK_only(bufsv); + if (fp_utf8 && !IN_BYTE) { + /* Look at utf8 we got back and count the characters */ + char *bend = buffer + count; + while (buffer < bend) { + STRLEN skip = UTF8SKIP(buffer); + if (buffer+skip > bend) { + /* partial character - try for rest of it */ + length = skip - (bend-buffer); + offset = bend - SvPVX(bufsv); + goto more_bytes; + } + else { + got++; + buffer += skip; + } + } + /* If we have not 'got' the number of _characters_ we 'wanted' get some more + provided amount read (count) was what was requested (length) + */ + if (got < wanted && count == length) { + length = (wanted-got); + offset = bend - SvPVX(bufsv); + goto more_bytes; + } + /* return value is character count */ + count = got; + SvUTF8_on(bufsv); + } SvSETMAGIC(bufsv); /* This should not be marked tainted if the fp is marked clean */ if (!(IoFLAGS(io) & IOf_UNTAINT)) SvTAINTED_on(bufsv); SP = ORIGMARK; - PUSHi(length); + PUSHi(count); RETURN; say_undef: @@ -1645,7 +1718,6 @@ PP(pp_send) char *buffer; Size_t length; SSize_t retval; - IV offset; STRLEN blen; MAGIC *mg; @@ -1667,7 +1739,6 @@ PP(pp_send) if (!gv) goto say_undef; bufsv = *++MARK; - buffer = SvPV(bufsv, blen); #if Size_t_size > IVSIZE length = (Size_t)SvNVx(*++MARK); #else @@ -1681,8 +1752,24 @@ PP(pp_send) retval = -1; if (ckWARN(WARN_CLOSED)) report_evil_fh(gv, io, PL_op->op_type); + goto say_undef; } - else if (PL_op->op_type == OP_SYSWRITE) { + + if (PerlIO_isutf8(IoIFP(io))) { + buffer = SvPVutf8(bufsv, blen); + } + else { + if (DO_UTF8(bufsv)) + sv_utf8_downgrade(bufsv, FALSE); + buffer = SvPV(bufsv, blen); + } + + if (PL_op->op_type == OP_SYSWRITE) { + IV offset; + if (DO_UTF8(bufsv)) { + /* length and offset are in chars */ + blen = sv_len_utf8(bufsv); + } if (MARK < SP) { offset = SvIVx(*++MARK); if (offset < 0) { @@ -1695,17 +1782,24 @@ PP(pp_send) offset = 0; if (length > blen - offset) length = blen - offset; + if (DO_UTF8(bufsv)) { + buffer = (char*)utf8_hop((U8 *)buffer, offset); + length = utf8_hop((U8 *)buffer, length) - (U8 *)buffer; + } + else { + buffer = buffer+offset; + } #ifdef PERL_SOCK_SYSWRITE_IS_SEND if (IoTYPE(io) == IoTYPE_SOCKET) { retval = PerlSock_send(PerlIO_fileno(IoIFP(io)), - buffer+offset, length, 0); + buffer, length, 0); } else #endif { /* See the note at doio.c:do_print about filesize limits. --jhi */ retval = PerlLIO_write(PerlIO_fileno(IoIFP(io)), - buffer+offset, length); + buffer, length); } } #ifdef HAS_SOCKET @@ -1713,12 +1807,13 @@ PP(pp_send) char *sockbuf; STRLEN mlen; sockbuf = SvPVx(*++MARK, mlen); + /* length is really flags */ retval = PerlSock_sendto(PerlIO_fileno(IoIFP(io)), buffer, blen, length, (struct sockaddr *)sockbuf, mlen); } else + /* length is really flags */ retval = PerlSock_send(PerlIO_fileno(IoIFP(io)), buffer, blen, length); - #else else DIE(aTHX_ PL_no_sock_func, "send"); @@ -1969,9 +2064,11 @@ PP(pp_ioctl) char *s; IV retval; GV *gv = (GV*)POPs; - IO *io = GvIOn(gv); + IO *io = gv ? GvIOn(gv) : 0; if (!io || !argsv || !IoIFP(io)) { + if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)) + report_evil_fh(gv, io, PL_op->op_type); SETERRNO(EBADF,RMS$_IFI); /* well, sort of... */ RETPUSHUNDEF; } @@ -2083,16 +2180,17 @@ PP(pp_socket) int fd; gv = (GV*)POPs; + io = gv ? GvIOn(gv) : NULL; - if (!gv) { + if (!gv || !io) { + if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)) + report_evil_fh(gv, io, PL_op->op_type); + if (IoIFP(io)) + do_close(gv, FALSE); SETERRNO(EBADF,LIB$_INVARG); RETPUSHUNDEF; } - io = GvIOn(gv); - if (IoIFP(io)) - do_close(gv, FALSE); - TAINT_PROPER("socket"); fd = PerlSock_socket(domain, type, protocol); if (fd < 0) @@ -2131,15 +2229,21 @@ PP(pp_sockpair) gv2 = (GV*)POPs; gv1 = (GV*)POPs; - if (!gv1 || !gv2) + io1 = gv1 ? GvIOn(gv1) : NULL; + io2 = gv2 ? GvIOn(gv2) : NULL; + if (!gv1 || !gv2 || !io1 || !io2) { + if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)) { + if (!gv1 || !io1) + report_evil_fh(gv1, io1, PL_op->op_type); + if (!gv2 || !io2) + report_evil_fh(gv1, io2, PL_op->op_type); + } + if (IoIFP(io1)) + do_close(gv1, FALSE); + if (IoIFP(io2)) + do_close(gv2, FALSE); RETPUSHUNDEF; - - io1 = GvIOn(gv1); - io2 = GvIOn(gv2); - if (IoIFP(io1)) - do_close(gv1, FALSE); - if (IoIFP(io2)) - do_close(gv2, FALSE); + } TAINT_PROPER("socketpair"); if (PerlSock_socketpair(domain, type, protocol, fd) < 0) @@ -2265,9 +2369,9 @@ PP(pp_listen) #ifdef HAS_SOCKET int backlog = POPi; GV *gv = (GV*)POPs; - register IO *io = GvIOn(gv); + register IO *io = gv ? GvIOn(gv) : NULL; - if (!io || !IoIFP(io)) + if (!gv || !io || !IoIFP(io)) goto nuts; if (PerlSock_listen(PerlIO_fileno(IoIFP(io)), backlog) >= 0) @@ -2562,7 +2666,6 @@ PP(pp_stat) ? PerlLIO_fstat(PerlIO_fileno(IoIFP(GvIOn(gv))), &PL_statcache) : -1); } if (PL_laststatval < 0) { - dTHR; if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)) report_evil_fh(gv, GvIO(gv), PL_op->op_type); max = 0; @@ -3117,7 +3220,6 @@ PP(pp_fttext) len = 512; } else { - dTHR; if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)) { gv = cGVOP_gv; report_evil_fh(gv, GvIO(gv), PL_op->op_type); @@ -3142,7 +3244,7 @@ PP(pp_fttext) (void)PerlIO_close(fp); RETPUSHUNDEF; } - do_binmode(fp, '<', O_BINARY); + PerlIO_binmode(aTHX_ fp, '<', O_BINARY, Nullch); len = PerlIO_read(fp, tbuf, sizeof(tbuf)); (void)PerlIO_close(fp); if (len <= 0) { @@ -3177,12 +3279,12 @@ PP(pp_fttext) continue; #endif /* utf8 characters don't count as odd */ - if (*s & 0x40) { + if (UTF8_IS_START(*s)) { int ulen = UTF8SKIP(s); if (ulen < len - i) { int j; for (j = 1; j < ulen; j++) { - if ((s[j] & 0xc0) != 0x80) + if (!UTF8_IS_CONTINUATION(s[j])) goto not_utf8; } --ulen; /* loop does extra increment */ @@ -3487,15 +3589,31 @@ PP(pp_mkdir) #ifndef HAS_MKDIR int oldumask; #endif - STRLEN n_a; + STRLEN len; char *tmps; + bool copy = FALSE; if (MAXARG > 1) mode = POPi; else mode = 0777; - tmps = SvPV(TOPs, n_a); + tmps = SvPV(TOPs, len); + /* Different operating and file systems take differently to + * trailing slashes. According to POSIX 1003.1 1996 Edition + * any number of trailing slashes should be allowed. + * Thusly we snip them away so that even non-conforming + * systems are happy. */ + /* We should probably do this "filtering" for all + * the functions that expect (potentially) directory names: + * -d, chdir(), chmod(), chown(), chroot(), fcntl()?, + * (mkdir()), opendir(), rename(), rmdir(), stat(). --jhi */ + if (len > 1 && tmps[len-1] == '/') { + while (tmps[len] == '/' && len > 1) + len--; + tmps = savepvn(tmps, len); + copy = TRUE; + } TAINT_PROPER("mkdir"); #ifdef HAS_MKDIR @@ -3506,6 +3624,8 @@ PP(pp_mkdir) PerlLIO_umask(oldumask); PerlLIO_chmod(tmps, (mode & ~oldumask) & 0777); #endif + if (copy) + Safefree(tmps); RETURN; }