X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=pp_sys.c;h=877d0a084d4dbe5481bc5a6e519c23cc56b76228;hb=11906ba08fed929f780ea3e503eb89c0955fb2d8;hp=b1ec92cf2c8aaf827a4445cc270786663ae66ff4;hpb=7ea3cd407b6ec2a3e424bdfbc486b6e01d6d28bd;p=p5sagit%2Fp5-mst-13.2.git diff --git a/pp_sys.c b/pp_sys.c index b1ec92c..877d0a0 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-2003, 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. @@ -49,6 +49,10 @@ extern "C" int syscall(unsigned long,...); # include #endif +#ifdef NETWARE +NETDB_DEFINE_CONTEXT +#endif + #ifdef HAS_SELECT # ifdef I_SYS_SELECT # include @@ -70,11 +74,17 @@ extern int h_errno; # ifdef I_PWD # include # else +# if !defined(VMS) struct passwd *getpwnam (char *); struct passwd *getpwuid (Uid_t); +# endif # endif # ifdef HAS_GETPWENT +#ifndef getpwent struct passwd *getpwent (void); +#elif defined (VMS) && defined (my_getpwent) + struct passwd *Perl_my_getpwent (void); +#endif # endif #endif @@ -86,7 +96,9 @@ extern int h_errno; struct group *getgrgid (Gid_t); # endif # ifdef HAS_GETGRENT +#ifndef getgrent struct group *getgrent (void); +#endif # endif #endif @@ -98,11 +110,6 @@ extern int h_errno; # endif #endif -/* Put this after #includes because fork and vfork prototypes may conflict. */ -#ifndef HAS_VFORK -# define vfork fork -#endif - #ifdef HAS_CHSIZE # ifdef my_chsize /* Probably #defined to Perl_my_chsize in embed.h */ # undef my_chsize @@ -166,6 +173,8 @@ static char zero_but_true[ZBTLEN + 1] = "0 but true"; # define FD_CLOEXEC 1 /* NeXT needs this */ #endif +#include "reentr.h" + #undef PERL_EFF_ACCESS_R_OK /* EFFective uid/gid ACCESS R_OK */ #undef PERL_EFF_ACCESS_W_OK #undef PERL_EFF_ACCESS_X_OK @@ -272,6 +281,9 @@ S_emulate_eaccess(pTHX_ const char* path, Mode_t mode) #endif #if !defined(PERL_EFF_ACCESS_R_OK) +/* With it or without it: anyway you get a warning: either that + it is unused, or it is declared static and never defined. + */ STATIC int S_emulate_eaccess(pTHX_ const char* path, Mode_t mode) { @@ -283,7 +295,7 @@ S_emulate_eaccess(pTHX_ const char* path, Mode_t mode) PP(pp_backtick) { - djSP; dTARGET; + dSP; dTARGET; PerlIO *fp; STRLEN n_a; char *tmps = POPpx; @@ -311,10 +323,13 @@ PP(pp_backtick) ; } else if (gimme == G_SCALAR) { + SV *oldrs = PL_rs; + PL_rs = &PL_sv_undef; sv_setpv(TARG, ""); /* note that this preserves previous buffer */ while (sv_gets(TARG, fp, SvCUR(TARG)) != Nullch) /*SUPPRESS 530*/ ; + PL_rs = oldrs; XPUSHs(TARG); SvTAINTED_on(TARG); } @@ -385,15 +400,6 @@ PP(pp_glob) return result; } -#if 0 /* XXX never used! */ -PP(pp_indread) -{ - STRLEN n_a; - PL_last_in_gv = gv_fetchpv(SvPVx(GvSV((GV*)(*PL_stack_sp--)), n_a), TRUE,SVt_PVIO); - return do_readline(); -} -#endif - PP(pp_rcatline) { PL_last_in_gv = cGVOP_gv; @@ -402,7 +408,7 @@ PP(pp_rcatline) PP(pp_warn) { - djSP; dMARK; + dSP; dMARK; SV *tmpsv; char *tmps; STRLEN len; @@ -433,11 +439,14 @@ PP(pp_warn) PP(pp_die) { - djSP; dMARK; + dSP; dMARK; char *tmps; SV *tmpsv; STRLEN len; bool multiarg = 0; +#ifdef VMS + VMSISH_HUSHED = VMSISH_HUSHED || (PL_op->op_private & OPpHUSH_VMSISH); +#endif if (SP - MARK != 1) { dTARGET; do_join(TARG, &PL_sv_no, MARK, SP); @@ -448,7 +457,7 @@ PP(pp_die) } else { tmpsv = TOPs; - tmps = (SvROK(tmpsv) && PL_in_eval) ? Nullch : SvPV(tmpsv, len); + tmps = SvROK(tmpsv) ? Nullch : SvPV(tmpsv, len); } if (!tmps || !len) { SV *error = ERRSV; @@ -473,7 +482,7 @@ PP(pp_die) sv_setsv(error,*PL_stack_sp--); } } - DIE(aTHX_ Nullch); + DIE(aTHX_ Nullformat); } else { if (SvPOK(error) && SvCUR(error)) @@ -492,37 +501,28 @@ PP(pp_die) PP(pp_open) { - djSP; dTARGET; + dSP; + dMARK; dORIGMARK; + dTARGET; GV *gv; SV *sv; - SV *name = Nullsv; - I32 have_name = 0; + IO *io; 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)) + if ((io = 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); + if (io && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar))) { + /* Method's args are same as ours ... */ + /* ... except handle is replaced by the object */ + *MARK-- = SvTIED_obj((SV*)io, mg); + PUSHMARK(MARK); PUTBACK; ENTER; call_method("OPEN", G_SCALAR); @@ -531,8 +531,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); @@ -543,8 +552,9 @@ PP(pp_open) PP(pp_close) { - djSP; + dSP; GV *gv; + IO *io; MAGIC *mg; if (MAXARG == 0) @@ -552,9 +562,11 @@ PP(pp_close) else gv = (GV*)POPs; - if ((mg = SvTIED_mg((SV*)gv, 'q'))) { + if (gv && (io = GvIO(gv)) + && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar))) + { PUSHMARK(SP); - XPUSHs(SvTIED_obj((SV*)gv, mg)); + XPUSHs(SvTIED_obj((SV*)io, mg)); PUTBACK; ENTER; call_method("CLOSE", G_SCALAR); @@ -569,8 +581,8 @@ PP(pp_close) PP(pp_pipe_op) { - djSP; #ifdef HAS_PIPE + dSP; GV *rgv; GV *wgv; register IO *rstio; @@ -596,8 +608,9 @@ PP(pp_pipe_op) if (PerlProc_pipe(fd) < 0) goto badexit; - IoIFP(rstio) = PerlIO_fdopen(fd[0], "r"); - IoOFP(wstio) = PerlIO_fdopen(fd[1], "w"); + IoIFP(rstio) = PerlIO_fdopen(fd[0], "r"PIPESOCK_MODE); + IoOFP(wstio) = PerlIO_fdopen(fd[1], "w"PIPESOCK_MODE); + IoOFP(rstio) = IoIFP(rstio); IoIFP(wstio) = IoOFP(wstio); IoTYPE(rstio) = IoTYPE_RDONLY; IoTYPE(wstio) = IoTYPE_WRONLY; @@ -624,7 +637,7 @@ badexit: PP(pp_fileno) { - djSP; dTARGET; + dSP; dTARGET; GV *gv; IO *io; PerlIO *fp; @@ -634,9 +647,11 @@ PP(pp_fileno) RETPUSHUNDEF; gv = (GV*)POPs; - if (gv && (mg = SvTIED_mg((SV*)gv, 'q'))) { + if (gv && (io = GvIO(gv)) + && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar))) + { PUSHMARK(SP); - XPUSHs(SvTIED_obj((SV*)gv, mg)); + XPUSHs(SvTIED_obj((SV*)io, mg)); PUTBACK; ENTER; call_method("FILENO", G_SCALAR); @@ -660,10 +675,10 @@ PP(pp_fileno) PP(pp_umask) { - djSP; dTARGET; + dSP; dTARGET; +#ifdef HAS_UMASK Mode_t anum; -#ifdef HAS_UMASK if (MAXARG < 1) { anum = PerlLIO_umask(0); (void)PerlLIO_umask(anum); @@ -685,14 +700,12 @@ PP(pp_umask) PP(pp_binmode) { - djSP; + dSP; GV *gv; IO *io; PerlIO *fp; MAGIC *mg; SV *discp = Nullsv; - STRLEN len = 0; - char *names = NULL; if (MAXARG < 1) RETPUSHUNDEF; @@ -702,9 +715,11 @@ PP(pp_binmode) gv = (GV*)POPs; - if (gv && (mg = SvTIED_mg((SV*)gv, 'q'))) { + if (gv && (io = GvIO(gv)) + && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar))) + { PUSHMARK(SP); - XPUSHs(SvTIED_obj((SV*)gv, mg)); + XPUSHs(SvTIED_obj((SV*)io, mg)); if (discp) XPUSHs(discp); PUTBACK; @@ -719,23 +734,25 @@ PP(pp_binmode) if (!(io = GvIO(gv)) || !(fp = IoIFP(io))) { if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)) report_evil_fh(gv, io, PL_op->op_type); + SETERRNO(EBADF,RMS_IFI); RETPUSHUNDEF; } - if (discp) { - names = SvPV(discp,len); - } - + PUTBACK; if (PerlIO_binmode(aTHX_ fp,IoTYPE(io),mode_from_discipline(discp), - (discp) ? SvPV_nolen(discp) : Nullch)) + (discp) ? SvPV_nolen(discp) : Nullch)) { + SPAGAIN; RETPUSHYES; - else + } + else { + SPAGAIN; RETPUSHUNDEF; + } } PP(pp_tie) { - djSP; + dSP; dMARK; SV *varsv; HV* stash; @@ -743,25 +760,35 @@ PP(pp_tie) SV *sv; I32 markoff = MARK - PL_stack_base; char *methname; - int how = 'P'; + int how = PERL_MAGIC_tied; U32 items; - STRLEN n_a; varsv = *++MARK; switch(SvTYPE(varsv)) { case SVt_PVHV: methname = "TIEHASH"; + HvEITER((HV *)varsv) = Null(HE *); break; case SVt_PVAV: methname = "TIEARRAY"; break; case SVt_PVGV: +#ifdef GV_UNIQUE_CHECK + if (GvUNIQUE((GV*)varsv)) { + Perl_croak(aTHX_ "Attempt to tie unique GV"); + } +#endif methname = "TIEHANDLE"; - how = 'q'; + how = PERL_MAGIC_tiedscalar; + /* For tied filehandles, we apply tiedscalar magic to the IO + slot of the GP rather than the GV itself. AMS 20010812 */ + if (!GvIOp(varsv)) + GvIOp(varsv) = newIO(); + varsv = (SV *)GvIOp(varsv); break; default: methname = "TIESCALAR"; - how = 'q'; + how = PERL_MAGIC_tiedscalar; break; } items = SP - MARK++; @@ -769,7 +796,7 @@ PP(pp_tie) ENTER; PUSHSTACKi(PERLSI_MAGIC); PUSHMARK(SP); - EXTEND(SP,items); + EXTEND(SP,(I32)items); while (items--) PUSHs(*MARK++); PUTBACK; @@ -781,13 +808,13 @@ PP(pp_tie) */ stash = gv_stashsv(*MARK, FALSE); if (!stash || !(gv = gv_fetchmethod(stash, methname))) { - DIE(aTHX_ "Can't locate object method \"%s\" via package \"%s\"", - methname, SvPV(*MARK,n_a)); + DIE(aTHX_ "Can't locate object method \"%s\" via package \"%"SVf"\"", + methname, *MARK); } ENTER; PUSHSTACKi(PERLSI_MAGIC); PUSHMARK(SP); - EXTEND(SP,items); + EXTEND(SP,(I32)items); while (items--) PUSHs(*MARK++); PUTBACK; @@ -801,11 +828,11 @@ PP(pp_tie) sv_unmagic(varsv, how); /* Croak if a self-tie on an aggregate is attempted. */ if (varsv == SvRV(sv) && - (SvTYPE(sv) == SVt_PVAV || - SvTYPE(sv) == SVt_PVHV)) + (SvTYPE(varsv) == SVt_PVAV || + SvTYPE(varsv) == SVt_PVHV)) Perl_croak(aTHX_ "Self-ties of arrays and hashes are not supported"); - sv_magic(varsv, sv, how, Nullch, 0); + sv_magic(varsv, (SvRV(sv) == varsv ? Nullsv : sv), how, Nullch, 0); } LEAVE; SP = PL_stack_base + markoff; @@ -815,43 +842,53 @@ PP(pp_tie) PP(pp_untie) { - djSP; + dSP; + MAGIC *mg; SV *sv = POPs; - char how = (SvTYPE(sv) == SVt_PVHV || SvTYPE(sv) == SVt_PVAV) ? 'P' : 'q'; + char how = (SvTYPE(sv) == SVt_PVHV || SvTYPE(sv) == SVt_PVAV) + ? PERL_MAGIC_tied : PERL_MAGIC_tiedscalar; + + if (SvTYPE(sv) == SVt_PVGV && !(sv = (SV *)GvIOp(sv))) + RETPUSHYES; - MAGIC * mg ; - if ((mg = SvTIED_mg(sv, how))) { + if ((mg = SvTIED_mg(sv, how))) { SV *obj = SvRV(mg->mg_obj); GV *gv; CV *cv = NULL; - if ((gv = gv_fetchmethod_autoload(SvSTASH(obj), "UNTIE", FALSE)) && - isGV(gv) && (cv = GvCV(gv))) { - PUSHMARK(SP); - XPUSHs(SvTIED_obj((SV*)gv, mg)); - XPUSHs(sv_2mortal(newSViv(SvREFCNT(obj)-1))); - PUTBACK; - ENTER; - call_sv((SV *)cv, G_VOID); - LEAVE; - SPAGAIN; - } - else if (ckWARN(WARN_UNTIE)) { - if (mg && SvREFCNT(obj) > 1) - Perl_warner(aTHX_ WARN_UNTIE, - "untie attempted while %"UVuf" inner references still exist", - (UV)SvREFCNT(obj) - 1 ) ; + if (obj) { + if ((gv = gv_fetchmethod_autoload(SvSTASH(obj), "UNTIE", FALSE)) && + isGV(gv) && (cv = GvCV(gv))) { + PUSHMARK(SP); + XPUSHs(SvTIED_obj((SV*)gv, mg)); + XPUSHs(sv_2mortal(newSViv(SvREFCNT(obj)-1))); + PUTBACK; + ENTER; + call_sv((SV *)cv, G_VOID); + LEAVE; + SPAGAIN; + } + else if (ckWARN(WARN_UNTIE)) { + if (mg && SvREFCNT(obj) > 1) + Perl_warner(aTHX_ packWARN(WARN_UNTIE), + "untie attempted while %"UVuf" inner references still exist", + (UV)SvREFCNT(obj) - 1 ) ; + } } + sv_unmagic(sv, how) ; } - sv_unmagic(sv, how); RETPUSHYES; } PP(pp_tied) { - djSP; - SV *sv = POPs; - char how = (SvTYPE(sv) == SVt_PVHV || SvTYPE(sv) == SVt_PVAV) ? 'P' : 'q'; + dSP; MAGIC *mg; + SV *sv = POPs; + char how = (SvTYPE(sv) == SVt_PVHV || SvTYPE(sv) == SVt_PVAV) + ? PERL_MAGIC_tied : PERL_MAGIC_tiedscalar; + + if (SvTYPE(sv) == SVt_PVGV && !(sv = (SV *)GvIOp(sv))) + RETPUSHUNDEF; if ((mg = SvTIED_mg(sv, how))) { SV *osv = SvTIED_obj(sv, mg); @@ -865,7 +902,7 @@ PP(pp_tied) PP(pp_dbmopen) { - djSP; + dSP; HV *hv; dPOPPOPssrl; HV* stash; @@ -913,8 +950,8 @@ PP(pp_dbmopen) } if (sv_isobject(TOPs)) { - sv_unmagic((SV *) hv, 'P'); - sv_magic((SV*)hv, TOPs, 'P', Nullch, 0); + sv_unmagic((SV *) hv, PERL_MAGIC_tied); + sv_magic((SV*)hv, TOPs, PERL_MAGIC_tied, Nullch, 0); } LEAVE; RETURN; @@ -927,8 +964,8 @@ PP(pp_dbmclose) PP(pp_sselect) { - djSP; dTARGET; #ifdef HAS_SELECT + dSP; dTARGET; register I32 i; register I32 j; register char *s; @@ -964,18 +1001,7 @@ PP(pp_sselect) } /* little endians can use vecs directly */ -#if BYTEORDER == 0x1234 || BYTEORDER == 0x12345678 -# if SELECT_MIN_BITS > 1 - /* If SELECT_MIN_BITS is greater than one we most probably will want - * to align the sizes with SELECT_MIN_BITS/8 because for example - * in many little-endian (Intel, Alpha) systems (Linux, OS/2, Digital - * UNIX, Solaris, NeXT, Darwin) the smallest quantum select() operates - * on (sets/tests/clears bits) is 32 bits. */ - growsize = maxlen + (SELECT_MIN_BITS/8 - (maxlen % (SELECT_MIN_BITS/8))); -# else - growsize = sizeof(fd_set); -# endif -# else +#if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678 # ifdef NFDBITS # ifndef NBBY @@ -986,10 +1012,20 @@ PP(pp_sselect) # else masksize = sizeof(long); /* documented int, everyone seems to use long */ # endif - growsize = maxlen + (masksize - (maxlen % masksize)); Zero(&fd_sets[0], 4, char*); #endif +# if SELECT_MIN_BITS > 1 + /* If SELECT_MIN_BITS is greater than one we most probably will want + * to align the sizes with SELECT_MIN_BITS/8 because for example + * in many little-endian (Intel, Alpha) systems (Linux, OS/2, Digital + * UNIX, Solaris, NeXT, Darwin) the smallest quantum select() operates + * on (sets/tests/clears bits) is 32 bits. */ + growsize = maxlen + (SELECT_MIN_BITS/8 - (maxlen % (SELECT_MIN_BITS/8))); +# else + growsize = sizeof(fd_set); +# endif + sv = SP[4]; if (SvOK(sv)) { value = SvNV(sv); @@ -1032,12 +1068,23 @@ PP(pp_sselect) #endif } +#ifdef PERL_IRIX5_SELECT_TIMEVAL_VOID_CAST + /* Can't make just the (void*) conditional because that would be + * cpp #if within cpp macro, and not all compilers like that. */ + nfound = PerlSock_select( + maxlen * 8, + (Select_fd_set_t) fd_sets[1], + (Select_fd_set_t) fd_sets[2], + (Select_fd_set_t) fd_sets[3], + (void*) tbuf); /* Workaround for compiler bug. */ +#else nfound = PerlSock_select( maxlen * 8, (Select_fd_set_t) fd_sets[1], (Select_fd_set_t) fd_sets[2], (Select_fd_set_t) fd_sets[3], tbuf); +#endif for (i = 1; i <= 3; i++) { if (fd_sets[i]) { sv = SP[i]; @@ -1078,7 +1125,7 @@ Perl_setdefout(pTHX_ GV *gv) PP(pp_select) { - djSP; dTARGET; + dSP; dTARGET; GV *newdefout, *egv; HV *hv; @@ -1112,8 +1159,9 @@ PP(pp_select) PP(pp_getc) { - djSP; dTARGET; + dSP; dTARGET; GV *gv; + IO *io = NULL; MAGIC *mg; if (MAXARG == 0) @@ -1121,10 +1169,12 @@ PP(pp_getc) else gv = (GV*)POPs; - if ((mg = SvTIED_mg((SV*)gv, 'q'))) { + if (gv && (io = GvIO(gv)) + && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar))) + { I32 gimme = GIMME_V; PUSHMARK(SP); - XPUSHs(SvTIED_obj((SV*)gv, mg)); + XPUSHs(SvTIED_obj((SV*)io, mg)); PUTBACK; ENTER; call_method("GETC", gimme); @@ -1134,8 +1184,13 @@ PP(pp_getc) SvSetMagicSV_nosteal(TARG, TOPs); RETURN; } - if (!gv || do_eof(gv)) /* make sure we have fp with something */ + if (!gv || do_eof(gv)) { /* make sure we have fp with something */ + if (ckWARN2(WARN_UNOPENED,WARN_CLOSED) + && (!io || (!IoIFP(io) && IoTYPE(io) != IoTYPE_WRONLY))) + report_evil_fh(gv, io, PL_op->op_type); + SETERRNO(EBADF,RMS_IFI); RETPUSHUNDEF; + } TAINT; sv_setpv(TARG, " "); *SvPVX(TARG) = PerlIO_getc(IoIFP(GvIOp(gv))); /* should never be EOF */ @@ -1163,8 +1218,6 @@ S_doform(pTHX_ CV *cv, GV *gv, OP *retop) { register PERL_CONTEXT *cx; I32 gimme = GIMME_V; - AV* padlist = CvPADLIST(cv); - SV** svp = AvARRAY(padlist); ENTER; SAVETMPS; @@ -1172,8 +1225,7 @@ S_doform(pTHX_ CV *cv, GV *gv, OP *retop) push_return(retop); PUSHBLOCK(cx, CXt_FORMAT, PL_stack_sp); PUSHFORMAT(cx); - SAVEVPTR(PL_curpad); - PL_curpad = AvARRAY((AV*)svp[1]); + PAD_SET_CUR(CvPADLIST(cv), 1); setdefout(gv); /* locally select filehandle so $% et al work */ return CvSTART(cv); @@ -1181,7 +1233,7 @@ S_doform(pTHX_ CV *cv, GV *gv, OP *retop) PP(pp_enterwrite) { - djSP; + dSP; register GV *gv; register IO *io; GV *fgv; @@ -1225,7 +1277,7 @@ PP(pp_enterwrite) PP(pp_leavewrite) { - djSP; + dSP; GV *gv = cxstack[cxstack_ix].blk_sub.gv; register IO *io = GvIOp(gv); PerlIO *ofp = IoOFP(io); @@ -1236,6 +1288,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) { @@ -1320,21 +1374,8 @@ PP(pp_leavewrite) fp = IoOFP(io); if (!fp) { if (ckWARN2(WARN_CLOSED,WARN_IO)) { - if (IoIFP(io)) { - /* integrate with report_evil_fh()? */ - char *name = NULL; - if (isGV(gv)) { - SV* sv = sv_newmortal(); - gv_efullname4(sv, gv, Nullch, FALSE); - name = SvPV_nolen(sv); - } - if (name && *name) - Perl_warner(aTHX_ WARN_IO, - "Filehandle %s opened only for input", name); - else - Perl_warner(aTHX_ WARN_IO, - "Filehandle opened only for input"); - } + if (IoIFP(io)) + report_evil_fh(gv, io, OP_phoney_INPUT_ONLY); else if (ckWARN(WARN_CLOSED)) report_evil_fh(gv, io, PL_op->op_type); } @@ -1343,7 +1384,7 @@ PP(pp_leavewrite) else { if ((IoLINES_LEFT(io) -= FmLINES(PL_formtarget)) < 0) { if (ckWARN(WARN_IO)) - Perl_warner(aTHX_ WARN_IO, "page overflow"); + Perl_warner(aTHX_ packWARN(WARN_IO), "page overflow"); } if (!do_print(PL_formtarget, fp)) PUSHs(&PL_sv_no); @@ -1356,6 +1397,7 @@ PP(pp_leavewrite) PUSHs(&PL_sv_yes); } } + /* bad_ofp: */ PL_formtarget = PL_bodytarget; PUTBACK; return pop_return(); @@ -1363,20 +1405,21 @@ PP(pp_leavewrite) PP(pp_prtf) { - djSP; dMARK; dORIGMARK; + dSP; dMARK; dORIGMARK; GV *gv; IO *io; PerlIO *fp; SV *sv; MAGIC *mg; - STRLEN n_a; if (PL_op->op_flags & OPf_STACKED) gv = (GV*)*++MARK; else gv = PL_defoutgv; - if ((mg = SvTIED_mg((SV*)gv, 'q'))) { + if (gv && (io = GvIO(gv)) + && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar))) + { if (MARK == ORIGMARK) { MEXTEND(SP, 1); ++MARK; @@ -1384,7 +1427,7 @@ PP(pp_prtf) ++SP; } PUSHMARK(MARK - 1); - *MARK = SvTIED_obj((SV*)gv, mg); + *MARK = SvTIED_obj((SV*)io, mg); PUTBACK; ENTER; call_method("PRINTF", G_SCALAR); @@ -1400,29 +1443,17 @@ PP(pp_prtf) if (!(io = GvIO(gv))) { if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)) report_evil_fh(gv, io, PL_op->op_type); - SETERRNO(EBADF,RMS$_IFI); + SETERRNO(EBADF,RMS_IFI); goto just_say_no; } else if (!(fp = IoOFP(io))) { if (ckWARN2(WARN_CLOSED,WARN_IO)) { - /* integrate with report_evil_fh()? */ - if (IoIFP(io)) { - char *name = NULL; - if (isGV(gv)) { - gv_efullname4(sv, gv, Nullch, FALSE); - name = SvPV_nolen(sv); - } - if (name && *name) - Perl_warner(aTHX_ WARN_IO, - "Filehandle %s opened only for input", name); - else - Perl_warner(aTHX_ WARN_IO, - "Filehandle opened only for input"); - } + if (IoIFP(io)) + report_evil_fh(gv, io, OP_phoney_INPUT_ONLY); else if (ckWARN(WARN_CLOSED)) report_evil_fh(gv, io, PL_op->op_type); } - SETERRNO(EBADF,IoIFP(io)?RMS$_FAC:RMS$_IFI); + SETERRNO(EBADF,IoIFP(io)?RMS_FAC:RMS_IFI); goto just_say_no; } else { @@ -1448,7 +1479,7 @@ PP(pp_prtf) PP(pp_sysopen) { - djSP; + dSP; GV *gv; SV *sv; char *tmps; @@ -1478,7 +1509,7 @@ PP(pp_sysopen) PP(pp_sysread) { - djSP; dMARK; dORIGMARK; dTARGET; + dSP; dMARK; dORIGMARK; dTARGET; int offset; GV *gv; IO *io; @@ -1492,15 +1523,19 @@ PP(pp_sysread) int fp_utf8; Size_t got = 0; Size_t wanted; + bool charstart = FALSE; + STRLEN charskip = 0; + STRLEN skip = 0; gv = (GV*)*++MARK; - if ((PL_op->op_type == OP_READ || PL_op->op_type == OP_SYSREAD) && - (mg = SvTIED_mg((SV*)gv, 'q'))) + if ((PL_op->op_type == OP_READ || PL_op->op_type == OP_SYSREAD) + && gv && (io = GvIO(gv)) + && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar))) { SV *sv; PUSHMARK(MARK-1); - *MARK = SvTIED_obj((SV*)gv, mg); + *MARK = SvTIED_obj((SV*)io, mg); ENTER; call_method("READ", G_SCALAR); LEAVE; @@ -1523,9 +1558,13 @@ PP(pp_sysread) else offset = 0; io = GvIO(gv); - if (!io || !IoIFP(io)) + if (!io || !IoIFP(io)) { + if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)) + report_evil_fh(gv, io, PL_op->op_type); + SETERRNO(EBADF,RMS_IFI); goto say_undef; - if ((fp_utf8 = PerlIO_isutf8(IoIFP(io))) && !IN_BYTE) { + } + if ((fp_utf8 = PerlIO_isutf8(IoIFP(io))) && !IN_BYTES) { buffer = SvPVutf8_force(bufsv, blen); /* UTF8 may not have been set if they are all low bytes */ SvUTF8_on(bufsv); @@ -1537,10 +1576,14 @@ PP(pp_sysread) DIE(aTHX_ "Negative length"); wanted = length; + charstart = TRUE; + charskip = 0; + skip = 0; + #ifdef HAS_SOCKET if (PL_op->op_type == OP_RECV) { char namebuf[MAXPATHLEN]; -#if (defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)) || defined(MPE) +#if (defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)) || defined(MPE) || defined(__QNXNTO__) bufsize = sizeof (struct sockaddr_in); #else bufsize = sizeof namebuf; @@ -1549,12 +1592,16 @@ PP(pp_sysread) if (bufsize >= 256) bufsize = 255; #endif - buffer = SvGROW(bufsv, length+1); + buffer = SvGROW(bufsv, (STRLEN)(length+1)); /* 'offset' means 'flags' here */ count = PerlSock_recvfrom(PerlIO_fileno(IoIFP(io)), buffer, length, offset, (struct sockaddr *)namebuf, &bufsize); if (count < 0) RETPUSHUNDEF; +#ifdef EPOC + /* Bogus return without padding */ + bufsize = sizeof (struct sockaddr_in); +#endif SvCUR_set(bufsv, count); *SvEND(bufsv) = '\0'; (void)SvPOK_only(bufsv); @@ -1578,7 +1625,7 @@ PP(pp_sysread) blen = sv_len_utf8(bufsv); } if (offset < 0) { - if (-offset > blen) + if (-offset > (int)blen) DIE(aTHX_ "Offset outside string"); offset += blen; } @@ -1588,7 +1635,7 @@ PP(pp_sysread) } more_bytes: bufsize = SvCUR(bufsv); - buffer = SvGROW(bufsv, length+offset+1); + buffer = SvGROW(bufsv, (STRLEN)(length+offset+1)); if (offset > bufsize) { /* Zero any newly allocated space */ Zero(buffer+bufsize, offset-bufsize, char); } @@ -1628,49 +1675,41 @@ PP(pp_sysread) count = -1; } if (count < 0) { - if ((IoTYPE(io) == IoTYPE_WRONLY || IoIFP(io) == PerlIO_stdout() - || IoIFP(io) == PerlIO_stderr()) && ckWARN(WARN_IO)) - { - /* integrate with report_evil_fh()? */ - char *name = NULL; - if (isGV(gv)) { - SV* sv = sv_newmortal(); - gv_efullname4(sv, gv, Nullch, FALSE); - name = SvPV_nolen(sv); - } - if (name && *name) - Perl_warner(aTHX_ WARN_IO, - "Filehandle %s opened only for output", name); - else - Perl_warner(aTHX_ WARN_IO, - "Filehandle opened only for output"); - } + if ((IoTYPE(io) == IoTYPE_WRONLY) && ckWARN(WARN_IO)) + report_evil_fh(gv, io, OP_phoney_OUTPUT_ONLY); goto say_undef; } SvCUR_set(bufsv, count+(buffer - SvPVX(bufsv))); *SvEND(bufsv) = '\0'; (void)SvPOK_only(bufsv); - if (fp_utf8 && !IN_BYTE) { + if (fp_utf8 && !IN_BYTES) { /* 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) { + if (charstart) { + skip = UTF8SKIP(buffer); + charskip = 0; + } + if (buffer - charskip + skip > bend) { /* partial character - try for rest of it */ length = skip - (bend-buffer); offset = bend - SvPVX(bufsv); + charstart = FALSE; + charskip += count; goto more_bytes; } else { got++; buffer += skip; + charstart = TRUE; + charskip = 0; } } /* 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); + length = wanted - got; offset = bend - SvPVX(bufsv); goto more_bytes; } @@ -1693,7 +1732,7 @@ PP(pp_sysread) PP(pp_syswrite) { - djSP; + dSP; int items = (SP - PL_stack_base) - TOPMARK; if (items == 2) { SV *sv; @@ -1707,7 +1746,7 @@ PP(pp_syswrite) PP(pp_send) { - djSP; dMARK; dORIGMARK; dTARGET; + dSP; dMARK; dORIGMARK; dTARGET; GV *gv; IO *io; SV *bufsv; @@ -1718,11 +1757,14 @@ PP(pp_send) MAGIC *mg; gv = (GV*)*++MARK; - if (PL_op->op_type == OP_SYSWRITE && (mg = SvTIED_mg((SV*)gv, 'q'))) { + if (PL_op->op_type == OP_SYSWRITE + && gv && (io = GvIO(gv)) + && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar))) + { SV *sv; PUSHMARK(MARK-1); - *MARK = SvTIED_obj((SV*)gv, mg); + *MARK = SvTIED_obj((SV*)io, mg); ENTER; call_method("WRITE", G_SCALAR); LEAVE; @@ -1748,6 +1790,7 @@ PP(pp_send) retval = -1; if (ckWARN(WARN_CLOSED)) report_evil_fh(gv, io, PL_op->op_type); + SETERRNO(EBADF,RMS_IFI); goto say_undef; } @@ -1769,10 +1812,10 @@ PP(pp_send) if (MARK < SP) { offset = SvIVx(*++MARK); if (offset < 0) { - if (-offset > blen) + if (-offset > (IV)blen) DIE(aTHX_ "Offset outside string"); offset += blen; - } else if (offset >= blen && blen > 0) + } else if (offset >= (IV)blen && blen > 0) DIE(aTHX_ "Offset outside string"); } else offset = 0; @@ -1817,6 +1860,8 @@ PP(pp_send) if (retval < 0) goto say_undef; SP = ORIGMARK; + if (DO_UTF8(bufsv)) + retval = utf8_length((U8*)buffer, (U8*)buffer + retval); #if Size_t_size > IVSIZE PUSHn(retval); #else @@ -1836,14 +1881,15 @@ PP(pp_recv) PP(pp_eof) { - djSP; + dSP; GV *gv; + IO *io; MAGIC *mg; if (MAXARG == 0) { if (PL_op->op_flags & OPf_SPECIAL) { /* eof() */ IO *io; - gv = PL_last_in_gv = PL_argvgv; + gv = PL_last_in_gv = GvEGV(PL_argvgv); io = GvIO(gv); if (io && !IoIFP(io)) { if ((IoFLAGS(io) & IOf_START) && av_len(GvAVn(gv)) < 0) { @@ -1863,9 +1909,11 @@ PP(pp_eof) else gv = PL_last_in_gv = (GV*)POPs; /* eof(FH) */ - if (gv && (mg = SvTIED_mg((SV*)gv, 'q'))) { + if (gv && (io = GvIO(gv)) + && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar))) + { PUSHMARK(SP); - XPUSHs(SvTIED_obj((SV*)gv, mg)); + XPUSHs(SvTIED_obj((SV*)io, mg)); PUTBACK; ENTER; call_method("EOF", G_SCALAR); @@ -1880,8 +1928,9 @@ PP(pp_eof) PP(pp_tell) { - djSP; dTARGET; + dSP; dTARGET; GV *gv; + IO *io; MAGIC *mg; if (MAXARG == 0) @@ -1889,9 +1938,11 @@ PP(pp_tell) else gv = PL_last_in_gv = (GV*)POPs; - if (gv && (mg = SvTIED_mg((SV*)gv, 'q'))) { + if (gv && (io = GvIO(gv)) + && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar))) + { PUSHMARK(SP); - XPUSHs(SvTIED_obj((SV*)gv, mg)); + XPUSHs(SvTIED_obj((SV*)io, mg)); PUTBACK; ENTER; call_method("TELL", G_SCALAR); @@ -1915,8 +1966,9 @@ PP(pp_seek) PP(pp_sysseek) { - djSP; + dSP; GV *gv; + IO *io; int whence = POPi; #if LSEEKSIZE > IVSIZE Off_t offset = (Off_t)SvNVx(POPs); @@ -1927,9 +1979,11 @@ PP(pp_sysseek) gv = PL_last_in_gv = (GV*)POPs; - if (gv && (mg = SvTIED_mg((SV*)gv, 'q'))) { + if (gv && (io = GvIO(gv)) + && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar))) + { PUSHMARK(SP); - XPUSHs(SvTIED_obj((SV*)gv, mg)); + XPUSHs(SvTIED_obj((SV*)io, mg)); #if LSEEKSIZE > IVSIZE XPUSHs(sv_2mortal(newSVnv((NV) offset))); #else @@ -1966,18 +2020,15 @@ PP(pp_sysseek) PP(pp_truncate) { - djSP; + dSP; /* There seems to be no consensus on the length type of truncate() * and ftruncate(), both off_t and size_t have supporters. In * general one would think that when using large files, off_t is * at least as wide as size_t, so using an off_t should be okay. */ /* XXX Configure probe for the length type of *truncate() needed XXX */ Off_t len; - int result = 1; - GV *tmpgv; - STRLEN n_a; -#if Size_t_size > IVSIZE +#if Off_t_size > IVSIZE len = (Off_t)POPn; #else len = (Off_t)POPi; @@ -1987,60 +2038,80 @@ PP(pp_truncate) /* XXX Configure probe for the signedness of the length type of *truncate() needed? XXX */ SETERRNO(0,0); #if defined(HAS_TRUNCATE) || defined(HAS_CHSIZE) || defined(F_FREESP) - if (PL_op->op_flags & OPf_SPECIAL) { - tmpgv = gv_fetchpv(POPpx, FALSE, SVt_PVIO); - do_ftruncate: - TAINT_PROPER("truncate"); - if (!GvIO(tmpgv) || !IoIFP(GvIOp(tmpgv))) - result = 0; - else { - PerlIO_flush(IoIFP(GvIOp(tmpgv))); + { + STRLEN n_a; + int result = 1; + GV *tmpgv; + IO *io; + + if (PL_op->op_flags & OPf_SPECIAL) { + tmpgv = gv_fetchpv(POPpx, FALSE, SVt_PVIO); + + do_ftruncate_gv: + if (!GvIO(tmpgv)) + result = 0; + else { + PerlIO *fp; + io = GvIOp(tmpgv); + do_ftruncate_io: + TAINT_PROPER("truncate"); + if (!(fp = IoIFP(io))) { + result = 0; + } + else { + PerlIO_flush(fp); #ifdef HAS_TRUNCATE - if (ftruncate(PerlIO_fileno(IoIFP(GvIOn(tmpgv))), len) < 0) + if (ftruncate(PerlIO_fileno(fp), len) < 0) #else - if (my_chsize(PerlIO_fileno(IoIFP(GvIOn(tmpgv))), len) < 0) + if (my_chsize(PerlIO_fileno(fp), len) < 0) #endif - result = 0; - } - } - else { - SV *sv = POPs; - char *name; - STRLEN n_a; - - if (SvTYPE(sv) == SVt_PVGV) { - tmpgv = (GV*)sv; /* *main::FRED for example */ - goto do_ftruncate; - } - else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) { - tmpgv = (GV*) SvRV(sv); /* \*main::FRED for example */ - goto do_ftruncate; + result = 0; + } + } } + else { + SV *sv = POPs; + char *name; + + if (SvTYPE(sv) == SVt_PVGV) { + tmpgv = (GV*)sv; /* *main::FRED for example */ + goto do_ftruncate_gv; + } + else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) { + tmpgv = (GV*) SvRV(sv); /* \*main::FRED for example */ + goto do_ftruncate_gv; + } + else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVIO) { + io = (IO*) SvRV(sv); /* *main::FRED{IO} for example */ + goto do_ftruncate_io; + } - name = SvPV(sv, n_a); - TAINT_PROPER("truncate"); + name = SvPV(sv, n_a); + TAINT_PROPER("truncate"); #ifdef HAS_TRUNCATE - if (truncate(name, len) < 0) - result = 0; + if (truncate(name, len) < 0) + result = 0; #else - { - int tmpfd; - if ((tmpfd = PerlLIO_open(name, O_RDWR)) < 0) - result = 0; - else { - if (my_chsize(tmpfd, len) < 0) + { + int tmpfd; + + if ((tmpfd = PerlLIO_open(name, O_RDWR)) < 0) result = 0; - PerlLIO_close(tmpfd); + else { + if (my_chsize(tmpfd, len) < 0) + result = 0; + PerlLIO_close(tmpfd); + } } - } #endif - } + } - if (result) - RETPUSHYES; - if (!errno) - SETERRNO(EBADF,RMS$_IFI); - RETPUSHUNDEF; + if (result) + RETPUSHYES; + if (!errno) + SETERRNO(EBADF,RMS_IFI); + RETPUSHUNDEF; + } #else DIE(aTHX_ "truncate not implemented"); #endif @@ -2053,9 +2124,9 @@ PP(pp_fcntl) PP(pp_ioctl) { - djSP; dTARGET; + dSP; dTARGET; SV *argsv = POPs; - unsigned int func = U_I(POPn); + unsigned int func = POPu; int optype = PL_op->op_type; char *s; IV retval; @@ -2065,7 +2136,7 @@ PP(pp_ioctl) 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... */ + SETERRNO(EBADF,RMS_IFI); /* well, sort of... */ RETPUSHUNDEF; } @@ -2095,20 +2166,19 @@ PP(pp_ioctl) DIE(aTHX_ "ioctl is not implemented"); #endif else -#ifdef HAS_FCNTL +#ifndef HAS_FCNTL + DIE(aTHX_ "fcntl is not implemented"); +#else #if defined(OS2) && defined(__EMX__) retval = fcntl(PerlIO_fileno(IoIFP(io)), func, (int)s); #else retval = fcntl(PerlIO_fileno(IoIFP(io)), func, s); #endif -#else - DIE(aTHX_ "fcntl is not implemented"); -#endif if (SvPOK(argsv)) { if (s[SvCUR(argsv)] != 17) DIE(aTHX_ "Possible memory corruption: %s overflowed 3rd argument", - PL_op_name[optype]); + OP_NAME(PL_op)); s[SvCUR(argsv)] = 0; /* put our null back */ SvSETMAGIC(argsv); /* Assume it has changed */ } @@ -2121,19 +2191,20 @@ PP(pp_ioctl) else { PUSHp(zero_but_true, ZBTLEN); } +#endif RETURN; } PP(pp_flock) { - djSP; dTARGET; +#ifdef FLOCK + dSP; dTARGET; I32 value; int argtype; GV *gv; IO *io = NULL; PerlIO *fp; -#ifdef FLOCK argtype = POPi; if (MAXARG == 0) gv = PL_last_in_gv; @@ -2153,7 +2224,7 @@ PP(pp_flock) if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)) report_evil_fh(gv, io, PL_op->op_type); value = 0; - SETERRNO(EBADF,RMS$_IFI); + SETERRNO(EBADF,RMS_IFI); } PUSHi(value); RETURN; @@ -2166,8 +2237,8 @@ PP(pp_flock) PP(pp_socket) { - djSP; #ifdef HAS_SOCKET + dSP; GV *gv; register IO *io; int protocol = POPi; @@ -2183,16 +2254,19 @@ PP(pp_socket) report_evil_fh(gv, io, PL_op->op_type); if (IoIFP(io)) do_close(gv, FALSE); - SETERRNO(EBADF,LIB$_INVARG); + SETERRNO(EBADF,LIB_INVARG); RETPUSHUNDEF; } + if (IoIFP(io)) + do_close(gv, FALSE); + TAINT_PROPER("socket"); fd = PerlSock_socket(domain, type, protocol); if (fd < 0) RETPUSHUNDEF; - IoIFP(io) = PerlIO_fdopen(fd, "r"); /* stdio gets confused about sockets */ - IoOFP(io) = PerlIO_fdopen(fd, "w"); + IoIFP(io) = PerlIO_fdopen(fd, "r"PIPESOCK_MODE); /* stdio gets confused about sockets */ + IoOFP(io) = PerlIO_fdopen(fd, "w"PIPESOCK_MODE); IoTYPE(io) = IoTYPE_SOCKET; if (!IoIFP(io) || !IoOFP(io)) { if (IoIFP(io)) PerlIO_close(IoIFP(io)); @@ -2204,6 +2278,10 @@ PP(pp_socket) fcntl(fd, F_SETFD, fd > PL_maxsysfd); /* ensure close-on-exec */ #endif +#ifdef EPOC + setbuf( IoIFP(io), NULL); /* EPOC gets confused about sockets */ +#endif + RETPUSHYES; #else DIE(aTHX_ PL_no_sock_func, "socket"); @@ -2212,8 +2290,8 @@ PP(pp_socket) PP(pp_sockpair) { - djSP; -#ifdef HAS_SOCKETPAIR +#if defined (HAS_SOCKETPAIR) || (defined (HAS_SOCKET) && defined(SOCK_DGRAM) && defined(AF_INET) && defined(PF_INET)) + dSP; GV *gv1; GV *gv2; register IO *io1; @@ -2241,14 +2319,19 @@ PP(pp_sockpair) RETPUSHUNDEF; } + 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) RETPUSHUNDEF; - IoIFP(io1) = PerlIO_fdopen(fd[0], "r"); - IoOFP(io1) = PerlIO_fdopen(fd[0], "w"); + IoIFP(io1) = PerlIO_fdopen(fd[0], "r"PIPESOCK_MODE); + IoOFP(io1) = PerlIO_fdopen(fd[0], "w"PIPESOCK_MODE); IoTYPE(io1) = IoTYPE_SOCKET; - IoIFP(io2) = PerlIO_fdopen(fd[1], "r"); - IoOFP(io2) = PerlIO_fdopen(fd[1], "w"); + IoIFP(io2) = PerlIO_fdopen(fd[1], "r"PIPESOCK_MODE); + IoOFP(io2) = PerlIO_fdopen(fd[1], "w"PIPESOCK_MODE); IoTYPE(io2) = IoTYPE_SOCKET; if (!IoIFP(io1) || !IoOFP(io1) || !IoIFP(io2) || !IoOFP(io2)) { if (IoIFP(io1)) PerlIO_close(IoIFP(io1)); @@ -2272,11 +2355,11 @@ PP(pp_sockpair) PP(pp_bind) { - djSP; #ifdef HAS_SOCKET + dSP; #ifdef MPE /* Requires PRIV mode to bind() to ports < 1024 */ - extern GETPRIVMODE(); - extern GETUSERMODE(); + extern void GETPRIVMODE(); + extern void GETUSERMODE(); #endif SV *addrsv = POPs; char *addr; @@ -2322,7 +2405,7 @@ PP(pp_bind) nuts: if (ckWARN(WARN_CLOSED)) report_evil_fh(gv, io, PL_op->op_type); - SETERRNO(EBADF,SS$_IVCHAN); + SETERRNO(EBADF,SS_IVCHAN); RETPUSHUNDEF; #else DIE(aTHX_ PL_no_sock_func, "bind"); @@ -2331,8 +2414,8 @@ nuts: PP(pp_connect) { - djSP; #ifdef HAS_SOCKET + dSP; SV *addrsv = POPs; char *addr; GV *gv = (GV*)POPs; @@ -2352,7 +2435,7 @@ PP(pp_connect) nuts: if (ckWARN(WARN_CLOSED)) report_evil_fh(gv, io, PL_op->op_type); - SETERRNO(EBADF,SS$_IVCHAN); + SETERRNO(EBADF,SS_IVCHAN); RETPUSHUNDEF; #else DIE(aTHX_ PL_no_sock_func, "connect"); @@ -2361,8 +2444,8 @@ nuts: PP(pp_listen) { - djSP; #ifdef HAS_SOCKET + dSP; int backlog = POPi; GV *gv = (GV*)POPs; register IO *io = gv ? GvIOn(gv) : NULL; @@ -2378,7 +2461,7 @@ PP(pp_listen) nuts: if (ckWARN(WARN_CLOSED)) report_evil_fh(gv, io, PL_op->op_type); - SETERRNO(EBADF,SS$_IVCHAN); + SETERRNO(EBADF,SS_IVCHAN); RETPUSHUNDEF; #else DIE(aTHX_ PL_no_sock_func, "listen"); @@ -2387,8 +2470,8 @@ nuts: PP(pp_accept) { - djSP; dTARGET; #ifdef HAS_SOCKET + dSP; dTARGET; GV *ngv; GV *ggv; register IO *nstio; @@ -2410,14 +2493,13 @@ PP(pp_accept) goto nuts; nstio = GvIOn(ngv); - if (IoIFP(nstio)) - do_close(ngv, FALSE); - fd = PerlSock_accept(PerlIO_fileno(IoIFP(gstio)), (struct sockaddr *)&saddr, &len); if (fd < 0) goto badexit; - IoIFP(nstio) = PerlIO_fdopen(fd, "r"); - IoOFP(nstio) = PerlIO_fdopen(fd, "w"); + if (IoIFP(nstio)) + do_close(ngv, FALSE); + IoIFP(nstio) = PerlIO_fdopen(fd, "r"PIPESOCK_MODE); + IoOFP(nstio) = PerlIO_fdopen(fd, "w"PIPESOCK_MODE); IoTYPE(nstio) = IoTYPE_SOCKET; if (!IoIFP(nstio) || !IoOFP(nstio)) { if (IoIFP(nstio)) PerlIO_close(IoIFP(nstio)); @@ -2430,7 +2512,8 @@ PP(pp_accept) #endif #ifdef EPOC - len = sizeof saddr; /* EPOC somehow truncates info */ + len = sizeof saddr; /* EPOC somehow truncates info */ + setbuf( IoIFP(nstio), NULL); /* EPOC gets confused about sockets */ #endif PUSHp((char *)&saddr, len); @@ -2439,7 +2522,7 @@ PP(pp_accept) nuts: if (ckWARN(WARN_CLOSED)) report_evil_fh(ggv, ggv ? GvIO(ggv) : 0, PL_op->op_type); - SETERRNO(EBADF,SS$_IVCHAN); + SETERRNO(EBADF,SS_IVCHAN); badexit: RETPUSHUNDEF; @@ -2451,8 +2534,8 @@ badexit: PP(pp_shutdown) { - djSP; dTARGET; #ifdef HAS_SOCKET + dSP; dTARGET; int how = POPi; GV *gv = (GV*)POPs; register IO *io = GvIOn(gv); @@ -2466,7 +2549,7 @@ PP(pp_shutdown) nuts: if (ckWARN(WARN_CLOSED)) report_evil_fh(gv, io, PL_op->op_type); - SETERRNO(EBADF,SS$_IVCHAN); + SETERRNO(EBADF,SS_IVCHAN); RETPUSHUNDEF; #else DIE(aTHX_ PL_no_sock_func, "shutdown"); @@ -2484,8 +2567,8 @@ PP(pp_gsockopt) PP(pp_ssockopt) { - djSP; #ifdef HAS_SOCKET + dSP; int optype = PL_op->op_type; SV *sv; int fd; @@ -2545,7 +2628,7 @@ PP(pp_ssockopt) nuts: if (ckWARN(WARN_CLOSED)) report_evil_fh(gv, io, optype); - SETERRNO(EBADF,SS$_IVCHAN); + SETERRNO(EBADF,SS_IVCHAN); nuts2: RETPUSHUNDEF; @@ -2565,8 +2648,8 @@ PP(pp_getsockname) PP(pp_getpeername) { - djSP; #ifdef HAS_SOCKET + dSP; int optype = PL_op->op_type; SV *sv; int fd; @@ -2618,7 +2701,7 @@ PP(pp_getpeername) nuts: if (ckWARN(WARN_CLOSED)) report_evil_fh(gv, io, optype); - SETERRNO(EBADF,SS$_IVCHAN); + SETERRNO(EBADF,SS_IVCHAN); nuts2: RETPUSHUNDEF; @@ -2636,7 +2719,7 @@ PP(pp_lstat) PP(pp_stat) { - djSP; + dSP; GV *gv; I32 gimme; I32 max = 13; @@ -2645,12 +2728,12 @@ PP(pp_stat) if (PL_op->op_flags & OPf_REF) { gv = cGVOP_gv; if (PL_op->op_type == OP_LSTAT) { - if (PL_laststype != OP_LSTAT) - Perl_croak(aTHX_ "The stat preceding lstat() wasn't an lstat"); - if (ckWARN(WARN_IO) && gv != PL_defgv) - Perl_warner(aTHX_ WARN_IO, + if (gv != PL_defgv) { + if (ckWARN(WARN_IO)) + Perl_warner(aTHX_ packWARN(WARN_IO), "lstat() on filehandle %s", GvENAME(gv)); - /* Perl_my_lstat (-l) croak's on filehandle, why warn here? */ + } else if (PL_laststype != OP_LSTAT) + Perl_croak(aTHX_ "The stat preceding lstat() wasn't an lstat"); } do_fstat: @@ -2675,6 +2758,9 @@ PP(pp_stat) } else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) { gv = (GV*)SvRV(sv); + if (PL_op->op_type == OP_LSTAT && ckWARN(WARN_IO)) + Perl_warner(aTHX_ packWARN(WARN_IO), + "lstat() on filehandle %s", GvENAME(gv)); goto do_fstat; } sv_setpv(PL_statname, SvPV(sv,n_a)); @@ -2688,7 +2774,7 @@ PP(pp_stat) PL_laststatval = PerlLIO_stat(SvPV(PL_statname, n_a), &PL_statcache); if (PL_laststatval < 0) { if (ckWARN(WARN_NEWLINE) && strchr(SvPV(PL_statname, n_a), '\n')) - Perl_warner(aTHX_ WARN_NEWLINE, PL_warn_nl, "stat"); + Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "stat"); max = 0; } } @@ -2730,7 +2816,7 @@ PP(pp_stat) PUSHs(sv_2mortal(newSVpvn("", 0))); #endif #if Off_t_size > IVSIZE - PUSHs(sv_2mortal(newSVnv(PL_statcache.st_size))); + PUSHs(sv_2mortal(newSVnv((NV)PL_statcache.st_size))); #else PUSHs(sv_2mortal(newSViv(PL_statcache.st_size))); #endif @@ -2757,7 +2843,7 @@ PP(pp_stat) PP(pp_ftrread) { I32 result; - djSP; + dSP; #if defined(HAS_ACCESS) && defined(R_OK) STRLEN n_a; if ((PL_hints & HINT_FILETEST_ACCESS) && SvPOK(TOPs)) { @@ -2784,7 +2870,7 @@ PP(pp_ftrread) PP(pp_ftrwrite) { I32 result; - djSP; + dSP; #if defined(HAS_ACCESS) && defined(W_OK) STRLEN n_a; if ((PL_hints & HINT_FILETEST_ACCESS) && SvPOK(TOPs)) { @@ -2811,7 +2897,7 @@ PP(pp_ftrwrite) PP(pp_ftrexec) { I32 result; - djSP; + dSP; #if defined(HAS_ACCESS) && defined(X_OK) STRLEN n_a; if ((PL_hints & HINT_FILETEST_ACCESS) && SvPOK(TOPs)) { @@ -2838,7 +2924,7 @@ PP(pp_ftrexec) PP(pp_fteread) { I32 result; - djSP; + dSP; #ifdef PERL_EFF_ACCESS_R_OK STRLEN n_a; if ((PL_hints & HINT_FILETEST_ACCESS) && SvPOK(TOPs)) { @@ -2865,7 +2951,7 @@ PP(pp_fteread) PP(pp_ftewrite) { I32 result; - djSP; + dSP; #ifdef PERL_EFF_ACCESS_W_OK STRLEN n_a; if ((PL_hints & HINT_FILETEST_ACCESS) && SvPOK(TOPs)) { @@ -2892,7 +2978,7 @@ PP(pp_ftewrite) PP(pp_fteexec) { I32 result; - djSP; + dSP; #ifdef PERL_EFF_ACCESS_X_OK STRLEN n_a; if ((PL_hints & HINT_FILETEST_ACCESS) && SvPOK(TOPs)) { @@ -2919,7 +3005,7 @@ PP(pp_fteexec) PP(pp_ftis) { I32 result = my_stat(); - djSP; + dSP; if (result < 0) RETPUSHUNDEF; RETPUSHYES; @@ -2933,7 +3019,7 @@ PP(pp_fteowned) PP(pp_ftrowned) { I32 result = my_stat(); - djSP; + dSP; if (result < 0) RETPUSHUNDEF; if (PL_statcache.st_uid == (PL_op->op_type == OP_FTEOWNED ? @@ -2945,7 +3031,7 @@ PP(pp_ftrowned) PP(pp_ftzero) { I32 result = my_stat(); - djSP; + dSP; if (result < 0) RETPUSHUNDEF; if (PL_statcache.st_size == 0) @@ -2956,7 +3042,7 @@ PP(pp_ftzero) PP(pp_ftsize) { I32 result = my_stat(); - djSP; dTARGET; + dSP; dTARGET; if (result < 0) RETPUSHUNDEF; #if Off_t_size > IVSIZE @@ -2970,7 +3056,7 @@ PP(pp_ftsize) PP(pp_ftmtime) { I32 result = my_stat(); - djSP; dTARGET; + dSP; dTARGET; if (result < 0) RETPUSHUNDEF; PUSHn( (PL_basetime - PL_statcache.st_mtime) / 86400.0 ); @@ -2980,7 +3066,7 @@ PP(pp_ftmtime) PP(pp_ftatime) { I32 result = my_stat(); - djSP; dTARGET; + dSP; dTARGET; if (result < 0) RETPUSHUNDEF; PUSHn( (PL_basetime - PL_statcache.st_atime) / 86400.0 ); @@ -2990,7 +3076,7 @@ PP(pp_ftatime) PP(pp_ftctime) { I32 result = my_stat(); - djSP; dTARGET; + dSP; dTARGET; if (result < 0) RETPUSHUNDEF; PUSHn( (PL_basetime - PL_statcache.st_ctime) / 86400.0 ); @@ -3000,7 +3086,7 @@ PP(pp_ftctime) PP(pp_ftsock) { I32 result = my_stat(); - djSP; + dSP; if (result < 0) RETPUSHUNDEF; if (S_ISSOCK(PL_statcache.st_mode)) @@ -3011,7 +3097,7 @@ PP(pp_ftsock) PP(pp_ftchr) { I32 result = my_stat(); - djSP; + dSP; if (result < 0) RETPUSHUNDEF; if (S_ISCHR(PL_statcache.st_mode)) @@ -3022,7 +3108,7 @@ PP(pp_ftchr) PP(pp_ftblk) { I32 result = my_stat(); - djSP; + dSP; if (result < 0) RETPUSHUNDEF; if (S_ISBLK(PL_statcache.st_mode)) @@ -3033,7 +3119,7 @@ PP(pp_ftblk) PP(pp_ftfile) { I32 result = my_stat(); - djSP; + dSP; if (result < 0) RETPUSHUNDEF; if (S_ISREG(PL_statcache.st_mode)) @@ -3044,7 +3130,7 @@ PP(pp_ftfile) PP(pp_ftdir) { I32 result = my_stat(); - djSP; + dSP; if (result < 0) RETPUSHUNDEF; if (S_ISDIR(PL_statcache.st_mode)) @@ -3055,7 +3141,7 @@ PP(pp_ftdir) PP(pp_ftpipe) { I32 result = my_stat(); - djSP; + dSP; if (result < 0) RETPUSHUNDEF; if (S_ISFIFO(PL_statcache.st_mode)) @@ -3066,7 +3152,7 @@ PP(pp_ftpipe) PP(pp_ftlink) { I32 result = my_lstat(); - djSP; + dSP; if (result < 0) RETPUSHUNDEF; if (S_ISLNK(PL_statcache.st_mode)) @@ -3076,7 +3162,7 @@ PP(pp_ftlink) PP(pp_ftsuid) { - djSP; + dSP; #ifdef S_ISUID I32 result = my_stat(); SPAGAIN; @@ -3090,7 +3176,7 @@ PP(pp_ftsuid) PP(pp_ftsgid) { - djSP; + dSP; #ifdef S_ISGID I32 result = my_stat(); SPAGAIN; @@ -3104,7 +3190,7 @@ PP(pp_ftsgid) PP(pp_ftsvtx) { - djSP; + dSP; #ifdef S_ISVTX I32 result = my_stat(); SPAGAIN; @@ -3118,7 +3204,7 @@ PP(pp_ftsvtx) PP(pp_fttty) { - djSP; + dSP; int fd; GV *gv; char *tmps = Nullch; @@ -3154,7 +3240,7 @@ PP(pp_fttty) PP(pp_fttext) { - djSP; + dSP; I32 i; I32 len; I32 odd = 0; @@ -3197,11 +3283,12 @@ PP(pp_fttext) PL_laststatval = PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache); if (PL_laststatval < 0) RETPUSHUNDEF; - if (S_ISDIR(PL_statcache.st_mode)) /* handle NFS glitch */ + if (S_ISDIR(PL_statcache.st_mode)) { /* handle NFS glitch */ if (PL_op->op_type == OP_FTTEXT) RETPUSHNO; else RETPUSHYES; + } if (PerlIO_get_cnt(IoIFP(io)) <= 0) { i = PerlIO_getc(IoIFP(io)); if (i != EOF) @@ -3220,7 +3307,7 @@ PP(pp_fttext) gv = cGVOP_gv; report_evil_fh(gv, GvIO(gv), PL_op->op_type); } - SETERRNO(EBADF,RMS$_IFI); + SETERRNO(EBADF,RMS_IFI); RETPUSHUNDEF; } } @@ -3229,10 +3316,11 @@ PP(pp_fttext) really_filename: PL_statgv = Nullgv; PL_laststatval = -1; + PL_laststype = OP_STAT; sv_setpv(PL_statname, SvPV(sv, n_a)); if (!(fp = PerlIO_open(SvPVX(PL_statname), "r"))) { if (ckWARN(WARN_NEWLINE) && strchr(SvPV(sv, n_a), '\n')) - Perl_warner(aTHX_ WARN_NEWLINE, PL_warn_nl, "open"); + Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "open"); RETPUSHUNDEF; } PL_laststatval = PerlLIO_fstat(PerlIO_fileno(fp), &PL_statcache); @@ -3271,16 +3359,16 @@ PP(pp_fttext) #else else if (*s & 128) { #ifdef USE_LOCALE - if ((PL_op->op_private & OPpLOCALE) && isALPHA_LC(*s)) + if (IN_LOCALE_RUNTIME && isALPHA_LC(*s)) 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 */ @@ -3314,32 +3402,35 @@ PP(pp_ftbinary) PP(pp_chdir) { - djSP; dTARGET; + dSP; dTARGET; char *tmps; SV **svp; STRLEN n_a; - if (MAXARG < 1) - tmps = Nullch; + if( MAXARG == 1 ) + tmps = POPpx; else - tmps = POPpx; - if (!tmps || !*tmps) { - svp = hv_fetch(GvHVn(PL_envgv), "HOME", 4, FALSE); - if (svp) - tmps = SvPV(*svp, n_a); - } - if (!tmps || !*tmps) { - svp = hv_fetch(GvHVn(PL_envgv), "LOGDIR", 6, FALSE); - if (svp) - tmps = SvPV(*svp, n_a); - } + tmps = 0; + + if( !tmps || !*tmps ) { + if ( (svp = hv_fetch(GvHVn(PL_envgv), "HOME", 4, FALSE)) + || (svp = hv_fetch(GvHVn(PL_envgv), "LOGDIR", 6, FALSE)) #ifdef VMS - if (!tmps || !*tmps) { - svp = hv_fetch(GvHVn(PL_envgv), "SYS$LOGIN", 9, FALSE); - if (svp) - tmps = SvPV(*svp, n_a); - } + || (svp = hv_fetch(GvHVn(PL_envgv), "SYS$LOGIN", 9, FALSE)) #endif + ) + { + if( MAXARG == 1 ) + deprecate("chdir('') or chdir(undef) as chdir()"); + tmps = SvPV(*svp, n_a); + } + else { + PUSHi(0); + TAINT_PROPER("chdir"); + RETURN; + } + } + TAINT_PROPER("chdir"); PUSHi( PerlDir_chdir(tmps) >= 0 ); #ifdef VMS @@ -3352,25 +3443,24 @@ PP(pp_chdir) PP(pp_chown) { - djSP; dMARK; dTARGET; - I32 value; #ifdef HAS_CHOWN - value = (I32)apply(PL_op->op_type, MARK, SP); + dSP; dMARK; dTARGET; + I32 value = (I32)apply(PL_op->op_type, MARK, SP); + SP = MARK; PUSHi(value); RETURN; #else - DIE(aTHX_ PL_no_func, "Unsupported function chown"); + DIE(aTHX_ PL_no_func, "chown"); #endif } PP(pp_chroot) { - djSP; dTARGET; - char *tmps; #ifdef HAS_CHROOT + dSP; dTARGET; STRLEN n_a; - tmps = POPpx; + char *tmps = POPpx; TAINT_PROPER("chroot"); PUSHi( chroot(tmps) >= 0 ); RETURN; @@ -3381,7 +3471,7 @@ PP(pp_chroot) PP(pp_unlink) { - djSP; dMARK; dTARGET; + dSP; dMARK; dTARGET; I32 value; value = (I32)apply(PL_op->op_type, MARK, SP); SP = MARK; @@ -3391,7 +3481,7 @@ PP(pp_unlink) PP(pp_chmod) { - djSP; dMARK; dTARGET; + dSP; dMARK; dTARGET; I32 value; value = (I32)apply(PL_op->op_type, MARK, SP); SP = MARK; @@ -3401,7 +3491,7 @@ PP(pp_chmod) PP(pp_utime) { - djSP; dMARK; dTARGET; + dSP; dMARK; dTARGET; I32 value; value = (I32)apply(PL_op->op_type, MARK, SP); SP = MARK; @@ -3411,7 +3501,7 @@ PP(pp_utime) PP(pp_rename) { - djSP; dTARGET; + dSP; dTARGET; int anum; STRLEN n_a; @@ -3438,23 +3528,23 @@ PP(pp_rename) PP(pp_link) { - djSP; dTARGET; #ifdef HAS_LINK + dSP; dTARGET; STRLEN n_a; char *tmps2 = POPpx; char *tmps = SvPV(TOPs, n_a); TAINT_PROPER("link"); SETi( PerlLIO_link(tmps, tmps2) >= 0 ); + RETURN; #else - DIE(aTHX_ PL_no_func, "Unsupported function link"); + DIE(aTHX_ PL_no_func, "link"); #endif - RETURN; } PP(pp_symlink) { - djSP; dTARGET; #ifdef HAS_SYMLINK + dSP; dTARGET; STRLEN n_a; char *tmps2 = POPpx; char *tmps = SvPV(TOPs, n_a); @@ -3468,8 +3558,9 @@ PP(pp_symlink) PP(pp_readlink) { - djSP; dTARGET; + dSP; #ifdef HAS_SYMLINK + dTARGET; char *tmps; char buf[MAXPATHLEN]; int len; @@ -3479,7 +3570,7 @@ PP(pp_readlink) TAINT; #endif tmps = POPpx; - len = readlink(tmps, buf, sizeof buf); + len = readlink(tmps, buf, sizeof(buf) - 1); EXTEND(SP, 1); if (len < 0) RETPUSHUNDEF; @@ -3545,21 +3636,21 @@ S_dooneliner(pTHX_ char *cmd, char *filename) #define EACCES EPERM #endif if (instr(s, "cannot make")) - SETERRNO(EEXIST,RMS$_FEX); + SETERRNO(EEXIST,RMS_FEX); else if (instr(s, "existing file")) - SETERRNO(EEXIST,RMS$_FEX); + SETERRNO(EEXIST,RMS_FEX); else if (instr(s, "ile exists")) - SETERRNO(EEXIST,RMS$_FEX); + SETERRNO(EEXIST,RMS_FEX); else if (instr(s, "non-exist")) - SETERRNO(ENOENT,RMS$_FNF); + SETERRNO(ENOENT,RMS_FNF); else if (instr(s, "does not exist")) - SETERRNO(ENOENT,RMS$_FNF); + SETERRNO(ENOENT,RMS_FNF); else if (instr(s, "not empty")) - SETERRNO(EBUSY,SS$_DEVOFFLINE); + SETERRNO(EBUSY,SS_DEVOFFLINE); else if (instr(s, "cannot access")) - SETERRNO(EACCES,RMS$_PRV); + SETERRNO(EACCES,RMS_PRV); else - SETERRNO(EPERM,RMS$_PRV); + SETERRNO(EPERM,RMS_PRV); return 0; } else { /* some mkdirs return no failure indication */ @@ -3569,7 +3660,7 @@ S_dooneliner(pTHX_ char *cmd, char *filename) if (anum) SETERRNO(0,0); else - SETERRNO(EACCES,RMS$_PRV); /* a guess */ + SETERRNO(EACCES,RMS_PRV); /* a guess */ } return anum; } @@ -3578,22 +3669,43 @@ S_dooneliner(pTHX_ char *cmd, char *filename) } #endif +/* This macro removes trailing slashes from a directory name. + * 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 */ + +#define TRIMSLASHES(tmps,len,copy) (tmps) = SvPV(TOPs, (len)); \ + if ((len) > 1 && (tmps)[(len)-1] == '/') { \ + do { \ + (len)--; \ + } while ((len) > 1 && (tmps)[(len)-1] == '/'); \ + (tmps) = savepvn((tmps), (len)); \ + (copy) = TRUE; \ + } + PP(pp_mkdir) { - djSP; dTARGET; + dSP; dTARGET; int mode; #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); + TRIMSLASHES(tmps,len,copy); TAINT_PROPER("mkdir"); #ifdef HAS_MKDIR @@ -3604,22 +3716,27 @@ PP(pp_mkdir) PerlLIO_umask(oldumask); PerlLIO_chmod(tmps, (mode & ~oldumask) & 0777); #endif + if (copy) + Safefree(tmps); RETURN; } PP(pp_rmdir) { - djSP; dTARGET; + dSP; dTARGET; + STRLEN len; char *tmps; - STRLEN n_a; + bool copy = FALSE; - tmps = POPpx; + TRIMSLASHES(tmps,len,copy); TAINT_PROPER("rmdir"); #ifdef HAS_RMDIR - XPUSHi( PerlDir_rmdir(tmps) >= 0 ); + SETi( PerlDir_rmdir(tmps) >= 0 ); #else - XPUSHi( dooneliner("rmdir", tmps) ); + SETi( dooneliner("rmdir", tmps) ); #endif + if (copy) + Safefree(tmps); RETURN; } @@ -3627,8 +3744,8 @@ PP(pp_rmdir) PP(pp_open_dir) { - djSP; #if defined(Direntry_t) && defined(HAS_READDIR) + dSP; STRLEN n_a; char *dirname = POPpx; GV *gv = (GV*)POPs; @@ -3645,7 +3762,7 @@ PP(pp_open_dir) RETPUSHYES; nope: if (!errno) - SETERRNO(EBADF,RMS$_DIR); + SETERRNO(EBADF,RMS_DIR); RETPUSHUNDEF; #else DIE(aTHX_ PL_no_dir_func, "opendir"); @@ -3654,9 +3771,9 @@ nope: PP(pp_readdir) { - djSP; #if defined(Direntry_t) && defined(HAS_READDIR) -#ifndef I_DIRENT + dSP; +#if !defined(I_DIRENT) && !defined(VMS) Direntry_t *readdir (DIR *); #endif register Direntry_t *dp; @@ -3700,7 +3817,7 @@ PP(pp_readdir) nope: if (!errno) - SETERRNO(EBADF,RMS$_ISI); + SETERRNO(EBADF,RMS_ISI); if (GIMME == G_ARRAY) RETURN; else @@ -3712,8 +3829,8 @@ nope: PP(pp_telldir) { - djSP; dTARGET; #if defined(HAS_TELLDIR) || defined(telldir) + dSP; dTARGET; /* XXX does _anyone_ need this? --AD 2/20/1998 */ /* XXX netbsd still seemed to. XXX HAS_TELLDIR_PROTO is new style, NEED_TELLDIR_PROTO is old style. @@ -3731,7 +3848,7 @@ PP(pp_telldir) RETURN; nope: if (!errno) - SETERRNO(EBADF,RMS$_ISI); + SETERRNO(EBADF,RMS_ISI); RETPUSHUNDEF; #else DIE(aTHX_ PL_no_dir_func, "telldir"); @@ -3740,8 +3857,8 @@ nope: PP(pp_seekdir) { - djSP; #if defined(HAS_SEEKDIR) || defined(seekdir) + dSP; long along = POPl; GV *gv = (GV*)POPs; register IO *io = GvIOn(gv); @@ -3754,7 +3871,7 @@ PP(pp_seekdir) RETPUSHYES; nope: if (!errno) - SETERRNO(EBADF,RMS$_ISI); + SETERRNO(EBADF,RMS_ISI); RETPUSHUNDEF; #else DIE(aTHX_ PL_no_dir_func, "seekdir"); @@ -3763,8 +3880,8 @@ nope: PP(pp_rewinddir) { - djSP; #if defined(HAS_REWINDDIR) || defined(rewinddir) + dSP; GV *gv = (GV*)POPs; register IO *io = GvIOn(gv); @@ -3775,7 +3892,7 @@ PP(pp_rewinddir) RETPUSHYES; nope: if (!errno) - SETERRNO(EBADF,RMS$_ISI); + SETERRNO(EBADF,RMS_ISI); RETPUSHUNDEF; #else DIE(aTHX_ PL_no_dir_func, "rewinddir"); @@ -3784,8 +3901,8 @@ nope: PP(pp_closedir) { - djSP; #if defined(Direntry_t) && defined(HAS_READDIR) + dSP; GV *gv = (GV*)POPs; register IO *io = GvIOn(gv); @@ -3805,7 +3922,7 @@ PP(pp_closedir) RETPUSHYES; nope: if (!errno) - SETERRNO(EBADF,RMS$_IFI); + SETERRNO(EBADF,RMS_IFI); RETPUSHUNDEF; #else DIE(aTHX_ PL_no_dir_func, "closedir"); @@ -3817,29 +3934,32 @@ nope: PP(pp_fork) { #ifdef HAS_FORK - djSP; dTARGET; + dSP; dTARGET; Pid_t childpid; GV *tmpgv; EXTEND(SP, 1); PERL_FLUSHALL_FOR_CHILD; - childpid = fork(); + childpid = PerlProc_fork(); if (childpid < 0) RETSETUNDEF; if (!childpid) { -#ifdef SOCKS_64BIT_BUG - Perl_do_s64_init_buffer(); -#endif /*SUPPRESS 560*/ - if ((tmpgv = gv_fetchpv("$", TRUE, SVt_PV))) + if ((tmpgv = gv_fetchpv("$", TRUE, SVt_PV))) { + SvREADONLY_off(GvSV(tmpgv)); sv_setiv(GvSV(tmpgv), (IV)PerlProc_getpid()); + SvREADONLY_on(GvSV(tmpgv)); + } +#ifdef THREADS_HAVE_PIDS + PL_ppid = (IV)getppid(); +#endif hv_clear(PL_pidstatus); /* no kids, so don't wait for 'em */ } PUSHi(childpid); RETURN; #else # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS) - djSP; dTARGET; + dSP; dTARGET; Pid_t childpid; EXTEND(SP, 1); @@ -3850,7 +3970,7 @@ PP(pp_fork) PUSHi(childpid); RETURN; # else - DIE(aTHX_ PL_no_func, "Unsupported function fork"); + DIE(aTHX_ PL_no_func, "fork"); # endif #endif } @@ -3858,11 +3978,18 @@ PP(pp_fork) PP(pp_wait) { #if (!defined(DOSISH) || defined(OS2) || defined(WIN32)) && !defined(MACOS_TRADITIONAL) - djSP; dTARGET; + dSP; dTARGET; Pid_t childpid; int argflags; - childpid = wait4pid(-1, &argflags, 0); + if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG) + childpid = wait4pid(-1, &argflags, 0); + else { + while ((childpid = wait4pid(-1, &argflags, 0)) == -1 && + errno == EINTR) { + PERL_ASYNC_CHECK(); + } + } # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS) /* 0 and -1 are both error returns (the former applies to WNOHANG case) */ STATUS_NATIVE_SET((childpid && childpid != -1) ? argflags : -1); @@ -3872,21 +3999,28 @@ PP(pp_wait) XPUSHi(childpid); RETURN; #else - DIE(aTHX_ PL_no_func, "Unsupported function wait"); + DIE(aTHX_ PL_no_func, "wait"); #endif } PP(pp_waitpid) { #if (!defined(DOSISH) || defined(OS2) || defined(WIN32)) && !defined(MACOS_TRADITIONAL) - djSP; dTARGET; + dSP; dTARGET; Pid_t childpid; int optype; int argflags; optype = POPi; childpid = TOPi; - childpid = wait4pid(childpid, &argflags, optype); + if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG) + childpid = wait4pid(childpid, &argflags, optype); + else { + while ((childpid = wait4pid(childpid, &argflags, optype)) == -1 && + errno == EINTR) { + PERL_ASYNC_CHECK(); + } + } # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS) /* 0 and -1 are both error returns (the former applies to WNOHANG case) */ STATUS_NATIVE_SET((childpid && childpid != -1) ? argflags : -1); @@ -3896,111 +4030,127 @@ PP(pp_waitpid) SETi(childpid); RETURN; #else - DIE(aTHX_ PL_no_func, "Unsupported function waitpid"); + DIE(aTHX_ PL_no_func, "waitpid"); #endif } PP(pp_system) { - djSP; dMARK; dORIGMARK; dTARGET; + dSP; dMARK; dORIGMARK; dTARGET; I32 value; - Pid_t childpid; - int result; - int status; - Sigsave_t ihand,qhand; /* place to save signals during system() */ STRLEN n_a; + int result; I32 did_pipes = 0; - int pp[2]; - if (SP - MARK == 1) { - if (PL_tainting) { - char *junk = SvPV(TOPs, n_a); - TAINT_ENV(); - TAINT_PROPER("system"); + if (PL_tainting) { + TAINT_ENV(); + while (++MARK <= SP) { + (void)SvPV_nolen(*MARK); /* stringify for taint check */ + if (PL_tainted) + break; } + MARK = ORIGMARK; + TAINT_PROPER("system"); } PERL_FLUSHALL_FOR_CHILD; -#if (defined(HAS_FORK) || defined(AMIGAOS)) && !defined(VMS) && !defined(OS2) && !defined(__CYGWIN__) || defined(PERL_MICRO) - if (PerlProc_pipe(pp) >= 0) - did_pipes = 1; - while ((childpid = vfork()) == -1) { - if (errno != EAGAIN) { - value = -1; - SP = ORIGMARK; - PUSHi(value); - if (did_pipes) { - PerlLIO_close(pp[0]); - PerlLIO_close(pp[1]); +#if (defined(HAS_FORK) || defined(AMIGAOS)) && !defined(VMS) && !defined(OS2) || defined(PERL_MICRO) + { + Pid_t childpid; + int pp[2]; + + if (PerlProc_pipe(pp) >= 0) + did_pipes = 1; + while ((childpid = PerlProc_fork()) == -1) { + if (errno != EAGAIN) { + value = -1; + SP = ORIGMARK; + PUSHi(value); + if (did_pipes) { + PerlLIO_close(pp[0]); + PerlLIO_close(pp[1]); + } + RETURN; } - RETURN; + sleep(5); } - sleep(5); - } - if (childpid > 0) { - if (did_pipes) - PerlLIO_close(pp[1]); + if (childpid > 0) { + Sigsave_t ihand,qhand; /* place to save signals during system() */ + int status; + + if (did_pipes) + PerlLIO_close(pp[1]); #ifndef PERL_MICRO - rsignal_save(SIGINT, SIG_IGN, &ihand); - rsignal_save(SIGQUIT, SIG_IGN, &qhand); + rsignal_save(SIGINT, SIG_IGN, &ihand); + rsignal_save(SIGQUIT, SIG_IGN, &qhand); #endif - do { - result = wait4pid(childpid, &status, 0); - } while (result == -1 && errno == EINTR); + do { + result = wait4pid(childpid, &status, 0); + } while (result == -1 && errno == EINTR); #ifndef PERL_MICRO - (void)rsignal_restore(SIGINT, &ihand); - (void)rsignal_restore(SIGQUIT, &qhand); + (void)rsignal_restore(SIGINT, &ihand); + (void)rsignal_restore(SIGQUIT, &qhand); #endif - STATUS_NATIVE_SET(result == -1 ? -1 : status); - do_execfree(); /* free any memory child malloced on vfork */ - SP = ORIGMARK; - if (did_pipes) { - int errkid; - int n = 0, n1; - - while (n < sizeof(int)) { - n1 = PerlLIO_read(pp[0], - (void*)(((char*)&errkid)+n), - (sizeof(int)) - n); - if (n1 <= 0) - break; - n += n1; - } - PerlLIO_close(pp[0]); - if (n) { /* Error */ - if (n != sizeof(int)) - DIE(aTHX_ "panic: kid popen errno read"); - errno = errkid; /* Propagate errno from kid */ - STATUS_CURRENT = -1; + STATUS_NATIVE_SET(result == -1 ? -1 : status); + do_execfree(); /* free any memory child malloced on fork */ + SP = ORIGMARK; + if (did_pipes) { + int errkid; + int n = 0, n1; + + while (n < sizeof(int)) { + n1 = PerlLIO_read(pp[0], + (void*)(((char*)&errkid)+n), + (sizeof(int)) - n); + if (n1 <= 0) + break; + n += n1; + } + PerlLIO_close(pp[0]); + if (n) { /* Error */ + if (n != sizeof(int)) + DIE(aTHX_ "panic: kid popen errno read"); + errno = errkid; /* Propagate errno from kid */ + STATUS_CURRENT = -1; + } } + PUSHi(STATUS_CURRENT); + RETURN; } - PUSHi(STATUS_CURRENT); - RETURN; - } - if (did_pipes) { - PerlLIO_close(pp[0]); + if (did_pipes) { + PerlLIO_close(pp[0]); #if defined(HAS_FCNTL) && defined(F_SETFD) - fcntl(pp[1], F_SETFD, FD_CLOEXEC); + fcntl(pp[1], F_SETFD, FD_CLOEXEC); #endif + } + if (PL_op->op_flags & OPf_STACKED) { + SV *really = *++MARK; + value = (I32)do_aexec5(really, MARK, SP, pp[1], did_pipes); + } + else if (SP - MARK != 1) + value = (I32)do_aexec5(Nullsv, MARK, SP, pp[1], did_pipes); + else { + value = (I32)do_exec3(SvPVx(sv_mortalcopy(*SP), n_a), pp[1], did_pipes); + } + PerlProc__exit(-1); } - if (PL_op->op_flags & OPf_STACKED) { - SV *really = *++MARK; - value = (I32)do_aexec5(really, MARK, SP, pp[1], did_pipes); - } - else if (SP - MARK != 1) - value = (I32)do_aexec5(Nullsv, MARK, SP, pp[1], did_pipes); - else { - value = (I32)do_exec3(SvPVx(sv_mortalcopy(*SP), n_a), pp[1], did_pipes); - } - PerlProc__exit(-1); #else /* ! FORK or VMS or OS/2 */ PL_statusvalue = 0; result = 0; if (PL_op->op_flags & OPf_STACKED) { SV *really = *++MARK; +# ifdef WIN32 + value = (I32)do_aspawn(really, MARK, SP); +# else value = (I32)do_aspawn(really, (void **)MARK, (void **)SP); +# endif } - else if (SP - MARK != 1) + else if (SP - MARK != 1) { +# ifdef WIN32 + value = (I32)do_aspawn(Nullsv, MARK, SP); +# else value = (I32)do_aspawn(Nullsv, (void **)MARK, (void **)SP); +# endif + } else { value = (I32)do_spawn(SvPVx(sv_mortalcopy(*SP), n_a)); } @@ -4016,10 +4166,20 @@ PP(pp_system) PP(pp_exec) { - djSP; dMARK; dORIGMARK; dTARGET; + dSP; dMARK; dORIGMARK; dTARGET; I32 value; STRLEN n_a; + if (PL_tainting) { + TAINT_ENV(); + while (++MARK <= SP) { + (void)SvPV_nolen(*MARK); /* stringify for taint check */ + if (PL_tainted) + break; + } + MARK = ORIGMARK; + TAINT_PROPER("exec"); + } PERL_FLUSHALL_FOR_CHILD; if (PL_op->op_flags & OPf_STACKED) { SV *really = *++MARK; @@ -4039,11 +4199,6 @@ PP(pp_exec) # endif #endif else { - if (PL_tainting) { - char *junk = SvPV(*SP, n_a); - TAINT_ENV(); - TAINT_PROPER("exec"); - } #ifdef VMS value = (I32)vms_do_exec(SvPVx(sv_mortalcopy(*SP), n_a)); #else @@ -4056,11 +4211,6 @@ PP(pp_exec) #endif } -#if !defined(HAS_FORK) && defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS) - if (value >= 0) - my_exit(value); -#endif - SP = ORIGMARK; PUSHi(value); RETURN; @@ -4068,23 +4218,27 @@ PP(pp_exec) PP(pp_kill) { - djSP; dMARK; dTARGET; - I32 value; #ifdef HAS_KILL + dSP; dMARK; dTARGET; + I32 value; value = (I32)apply(PL_op->op_type, MARK, SP); SP = MARK; PUSHi(value); RETURN; #else - DIE(aTHX_ PL_no_func, "Unsupported function kill"); + DIE(aTHX_ PL_no_func, "kill"); #endif } PP(pp_getppid) { #ifdef HAS_GETPPID - djSP; dTARGET; + dSP; dTARGET; +# ifdef THREADS_HAVE_PIDS + XPUSHi( PL_ppid ); +# else XPUSHi( getppid() ); +# endif RETURN; #else DIE(aTHX_ PL_no_func, "getppid"); @@ -4094,7 +4248,7 @@ PP(pp_getppid) PP(pp_getpgrp) { #ifdef HAS_GETPGRP - djSP; dTARGET; + dSP; dTARGET; Pid_t pid; Pid_t pgrp; @@ -4119,7 +4273,7 @@ PP(pp_getpgrp) PP(pp_setpgrp) { #ifdef HAS_SETPGRP - djSP; dTARGET; + dSP; dTARGET; Pid_t pgrp; Pid_t pid; if (MAXARG < 2) { @@ -4150,12 +4304,10 @@ PP(pp_setpgrp) PP(pp_getpriority) { - djSP; dTARGET; - int which; - int who; #ifdef HAS_GETPRIORITY - who = POPi; - which = TOPi; + dSP; dTARGET; + int who = POPi; + int which = TOPi; SETi( getpriority(which, who) ); RETURN; #else @@ -4165,14 +4317,11 @@ PP(pp_getpriority) PP(pp_setpriority) { - djSP; dTARGET; - int which; - int who; - int niceval; #ifdef HAS_SETPRIORITY - niceval = POPi; - who = POPi; - which = TOPi; + dSP; dTARGET; + int niceval = POPi; + int who = POPi; + int which = TOPi; TAINT_PROPER("setpriority"); SETi( setpriority(which, who, niceval) >= 0 ); RETURN; @@ -4185,7 +4334,7 @@ PP(pp_setpriority) PP(pp_time) { - djSP; dTARGET; + dSP; dTARGET; #ifdef BIG_TIME XPUSHn( time(Null(Time_t*)) ); #else @@ -4194,31 +4343,11 @@ PP(pp_time) RETURN; } -/* XXX The POSIX name is CLK_TCK; it is to be preferred - to HZ. Probably. For now, assume that if the system - defines HZ, it does so correctly. (Will this break - on VMS?) - Probably we ought to use _sysconf(_SC_CLK_TCK), if - it's supported. --AD 9/96. -*/ - -#ifndef HZ -# ifdef CLK_TCK -# define HZ CLK_TCK -# else -# define HZ 60 -# endif -#endif - PP(pp_tms) { - djSP; - -#ifndef HAS_TIMES - DIE(aTHX_ "times not implemented"); -#else +#ifdef HAS_TIMES + dSP; EXTEND(SP, 4); - #ifndef VMS (void)PerlProc_times(&PL_timesbuf); #else @@ -4227,13 +4356,15 @@ PP(pp_tms) /* is returned. */ #endif - PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_utime)/HZ))); + PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_utime)/(NV)PL_clocktick))); if (GIMME == G_ARRAY) { - PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_stime)/HZ))); - PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_cutime)/HZ))); - PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_cstime)/HZ))); + PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_stime)/(NV)PL_clocktick))); + PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_cutime)/(NV)PL_clocktick))); + PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_cstime)/(NV)PL_clocktick))); } RETURN; +#else + DIE(aTHX_ "times not implemented"); #endif /* HAS_TIMES */ } @@ -4244,7 +4375,7 @@ PP(pp_localtime) PP(pp_gmtime) { - djSP; + dSP; Time_t when; struct tm *tmbuf; static char *dayname[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}; @@ -4265,10 +4396,10 @@ PP(pp_gmtime) else tmbuf = gmtime(&when); - EXTEND(SP, 9); - EXTEND_MORTAL(9); if (GIMME != G_ARRAY) { SV *tsv; + EXTEND(SP, 1); + EXTEND_MORTAL(1); if (!tmbuf) RETPUSHUNDEF; tsv = Perl_newSVpvf(aTHX_ "%s %s %2d %02d:%02d:%02d %d", @@ -4282,7 +4413,9 @@ PP(pp_gmtime) PUSHs(sv_2mortal(tsv)); } else if (tmbuf) { - PUSHs(sv_2mortal(newSViv(tmbuf->tm_sec))); + EXTEND(SP, 9); + EXTEND_MORTAL(9); + PUSHs(sv_2mortal(newSViv(tmbuf->tm_sec))); PUSHs(sv_2mortal(newSViv(tmbuf->tm_min))); PUSHs(sv_2mortal(newSViv(tmbuf->tm_hour))); PUSHs(sv_2mortal(newSViv(tmbuf->tm_mday))); @@ -4297,9 +4430,9 @@ PP(pp_gmtime) PP(pp_alarm) { - djSP; dTARGET; - int anum; #ifdef HAS_ALARM + dSP; dTARGET; + int anum; anum = POPi; anum = alarm((unsigned int)anum); EXTEND(SP, 1); @@ -4308,13 +4441,13 @@ PP(pp_alarm) PUSHi(anum); RETURN; #else - DIE(aTHX_ PL_no_func, "Unsupported function alarm"); + DIE(aTHX_ PL_no_func, "alarm"); #endif } PP(pp_sleep) { - djSP; dTARGET; + dSP; dTARGET; I32 duration; Time_t lasttime; Time_t when; @@ -4351,7 +4484,7 @@ PP(pp_shmread) PP(pp_shmwrite) { #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM) - djSP; dMARK; dTARGET; + dSP; dMARK; dTARGET; I32 value = (I32)(do_shmio(PL_op->op_type, MARK, SP) >= 0); SP = MARK; PUSHi(value); @@ -4376,7 +4509,7 @@ PP(pp_msgctl) PP(pp_msgsnd) { #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM) - djSP; dMARK; dTARGET; + dSP; dMARK; dTARGET; I32 value = (I32)(do_msgsnd(MARK, SP) >= 0); SP = MARK; PUSHi(value); @@ -4389,7 +4522,7 @@ PP(pp_msgsnd) PP(pp_msgrcv) { #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM) - djSP; dMARK; dTARGET; + dSP; dMARK; dTARGET; I32 value = (I32)(do_msgrcv(MARK, SP) >= 0); SP = MARK; PUSHi(value); @@ -4404,7 +4537,7 @@ PP(pp_msgrcv) PP(pp_semget) { #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM) - djSP; dMARK; dTARGET; + dSP; dMARK; dTARGET; int anum = do_ipcget(PL_op->op_type, MARK, SP); SP = MARK; if (anum == -1) @@ -4419,7 +4552,7 @@ PP(pp_semget) PP(pp_semctl) { #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM) - djSP; dMARK; dTARGET; + dSP; dMARK; dTARGET; int anum = do_ipcctl(PL_op->op_type, MARK, SP); SP = MARK; if (anum == -1) @@ -4439,7 +4572,7 @@ PP(pp_semctl) PP(pp_semop) { #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM) - djSP; dMARK; dTARGET; + dSP; dMARK; dTARGET; I32 value = (I32)(do_semop(MARK, SP) >= 0); SP = MARK; PUSHi(value); @@ -4471,33 +4604,35 @@ PP(pp_ghbyaddr) PP(pp_ghostent) { - djSP; #if defined(HAS_GETHOSTBYNAME) || defined(HAS_GETHOSTBYADDR) || defined(HAS_GETHOSTENT) + dSP; I32 which = PL_op->op_type; register char **elem; register SV *sv; #ifndef HAS_GETHOST_PROTOS /* XXX Do we need individual probes? */ - struct hostent *PerlSock_gethostbyaddr(Netdb_host_t, Netdb_hlen_t, int); - struct hostent *PerlSock_gethostbyname(Netdb_name_t); - struct hostent *PerlSock_gethostent(void); + struct hostent *gethostbyaddr(Netdb_host_t, Netdb_hlen_t, int); + struct hostent *gethostbyname(Netdb_name_t); + struct hostent *gethostent(void); #endif struct hostent *hent; unsigned long len; STRLEN n_a; EXTEND(SP, 10); - if (which == OP_GHBYNAME) + if (which == OP_GHBYNAME) { #ifdef HAS_GETHOSTBYNAME - hent = PerlSock_gethostbyname(POPpx); + char* name = POPpbytex; + hent = PerlSock_gethostbyname(name); #else DIE(aTHX_ PL_no_sock_func, "gethostbyname"); #endif + } else if (which == OP_GHBYADDR) { #ifdef HAS_GETHOSTBYADDR int addrtype = POPi; SV *addrsv = POPs; STRLEN addrlen; - Netdb_host_t addr = (Netdb_host_t) SvPV(addrsv, addrlen); + Netdb_host_t addr = (Netdb_host_t) SvPVbyte(addrsv, addrlen); hent = PerlSock_gethostbyaddr(addr, (Netdb_hlen_t) addrlen, addrtype); #else @@ -4512,8 +4647,14 @@ PP(pp_ghostent) #endif #ifdef HOST_NOT_FOUND - if (!hent) - STATUS_NATIVE_SET(h_errno); + if (!hent) { +#ifdef USE_REENTRANT_API +# ifdef USE_GETHOSTENT_ERRNO + h_errno = PL_reentrant_buffer->_gethostent_errno; +# endif +#endif + STATUS_NATIVE_SET(h_errno); + } #endif if (GIMME != G_ARRAY) { @@ -4580,29 +4721,31 @@ PP(pp_gnbyaddr) PP(pp_gnetent) { - djSP; #if defined(HAS_GETNETBYNAME) || defined(HAS_GETNETBYADDR) || defined(HAS_GETNETENT) + dSP; I32 which = PL_op->op_type; register char **elem; register SV *sv; #ifndef HAS_GETNET_PROTOS /* XXX Do we need individual probes? */ - struct netent *PerlSock_getnetbyaddr(Netdb_net_t, int); - struct netent *PerlSock_getnetbyname(Netdb_name_t); - struct netent *PerlSock_getnetent(void); + struct netent *getnetbyaddr(Netdb_net_t, int); + struct netent *getnetbyname(Netdb_name_t); + struct netent *getnetent(void); #endif struct netent *nent; STRLEN n_a; - if (which == OP_GNBYNAME) + if (which == OP_GNBYNAME){ #ifdef HAS_GETNETBYNAME - nent = PerlSock_getnetbyname(POPpx); + char *name = POPpbytex; + nent = PerlSock_getnetbyname(name); #else DIE(aTHX_ PL_no_sock_func, "getnetbyname"); #endif + } else if (which == OP_GNBYADDR) { #ifdef HAS_GETNETBYADDR int addrtype = POPi; - Netdb_net_t addr = (Netdb_net_t) U_L(POPn); + Netdb_net_t addr = (Netdb_net_t) (U32)POPu; nent = PerlSock_getnetbyaddr(addr, addrtype); #else DIE(aTHX_ PL_no_sock_func, "getnetbyaddr"); @@ -4615,6 +4758,17 @@ PP(pp_gnetent) DIE(aTHX_ PL_no_sock_func, "getnetent"); #endif +#ifdef HOST_NOT_FOUND + if (!nent) { +#ifdef USE_REENTRANT_API +# ifdef USE_GETNETENT_ERRNO + h_errno = PL_reentrant_buffer->_getnetent_errno; +# endif +#endif + STATUS_NATIVE_SET(h_errno); + } +#endif + EXTEND(SP, 4); if (GIMME != G_ARRAY) { PUSHs(sv = sv_newmortal()); @@ -4668,31 +4822,35 @@ PP(pp_gpbynumber) PP(pp_gprotoent) { - djSP; #if defined(HAS_GETPROTOBYNAME) || defined(HAS_GETPROTOBYNUMBER) || defined(HAS_GETPROTOENT) + dSP; I32 which = PL_op->op_type; register char **elem; register SV *sv; #ifndef HAS_GETPROTO_PROTOS /* XXX Do we need individual probes? */ - struct protoent *PerlSock_getprotobyname(Netdb_name_t); - struct protoent *PerlSock_getprotobynumber(int); - struct protoent *PerlSock_getprotoent(void); + struct protoent *getprotobyname(Netdb_name_t); + struct protoent *getprotobynumber(int); + struct protoent *getprotoent(void); #endif struct protoent *pent; STRLEN n_a; - if (which == OP_GPBYNAME) + if (which == OP_GPBYNAME) { #ifdef HAS_GETPROTOBYNAME - pent = PerlSock_getprotobyname(POPpx); + char* name = POPpbytex; + pent = PerlSock_getprotobyname(name); #else DIE(aTHX_ PL_no_sock_func, "getprotobyname"); #endif - else if (which == OP_GPBYNUMBER) + } + else if (which == OP_GPBYNUMBER) { #ifdef HAS_GETPROTOBYNUMBER - pent = PerlSock_getprotobynumber(POPi); + int number = POPi; + pent = PerlSock_getprotobynumber(number); #else - DIE(aTHX_ PL_no_sock_func, "getprotobynumber"); + DIE(aTHX_ PL_no_sock_func, "getprotobynumber"); #endif + } else #ifdef HAS_GETPROTOENT pent = PerlSock_getprotoent(); @@ -4751,23 +4909,23 @@ PP(pp_gsbyport) PP(pp_gservent) { - djSP; #if defined(HAS_GETSERVBYNAME) || defined(HAS_GETSERVBYPORT) || defined(HAS_GETSERVENT) + dSP; I32 which = PL_op->op_type; register char **elem; register SV *sv; #ifndef HAS_GETSERV_PROTOS /* XXX Do we need individual probes? */ - struct servent *PerlSock_getservbyname(Netdb_name_t, Netdb_name_t); - struct servent *PerlSock_getservbyport(int, Netdb_name_t); - struct servent *PerlSock_getservent(void); + struct servent *getservbyname(Netdb_name_t, Netdb_name_t); + struct servent *getservbyport(int, Netdb_name_t); + struct servent *getservent(void); #endif struct servent *sent; STRLEN n_a; if (which == OP_GSBYNAME) { #ifdef HAS_GETSERVBYNAME - char *proto = POPpx; - char *name = POPpx; + char *proto = POPpbytex; + char *name = POPpbytex; if (proto && !*proto) proto = Nullch; @@ -4779,8 +4937,11 @@ PP(pp_gservent) } else if (which == OP_GSBYPORT) { #ifdef HAS_GETSERVBYPORT - char *proto = POPpx; - unsigned short port = POPu; + char *proto = POPpbytex; + unsigned short port = (unsigned short)POPu; + + if (proto && !*proto) + proto = Nullch; #ifdef HAS_HTONS port = PerlSock_htons(port); @@ -4841,8 +5002,8 @@ PP(pp_gservent) PP(pp_shostent) { - djSP; #ifdef HAS_SETHOSTENT + dSP; PerlSock_sethostent(TOPi); RETSETYES; #else @@ -4852,8 +5013,8 @@ PP(pp_shostent) PP(pp_snetent) { - djSP; #ifdef HAS_SETNETENT + dSP; PerlSock_setnetent(TOPi); RETSETYES; #else @@ -4863,8 +5024,8 @@ PP(pp_snetent) PP(pp_sprotoent) { - djSP; #ifdef HAS_SETPROTOENT + dSP; PerlSock_setprotoent(TOPi); RETSETYES; #else @@ -4874,8 +5035,8 @@ PP(pp_sprotoent) PP(pp_sservent) { - djSP; #ifdef HAS_SETSERVENT + dSP; PerlSock_setservent(TOPi); RETSETYES; #else @@ -4885,8 +5046,8 @@ PP(pp_sservent) PP(pp_ehostent) { - djSP; #ifdef HAS_ENDHOSTENT + dSP; PerlSock_endhostent(); EXTEND(SP,1); RETPUSHYES; @@ -4897,8 +5058,8 @@ PP(pp_ehostent) PP(pp_enetent) { - djSP; #ifdef HAS_ENDNETENT + dSP; PerlSock_endnetent(); EXTEND(SP,1); RETPUSHYES; @@ -4909,8 +5070,8 @@ PP(pp_enetent) PP(pp_eprotoent) { - djSP; #ifdef HAS_ENDPROTOENT + dSP; PerlSock_endprotoent(); EXTEND(SP,1); RETPUSHYES; @@ -4921,8 +5082,8 @@ PP(pp_eprotoent) PP(pp_eservent) { - djSP; #ifdef HAS_ENDSERVENT + dSP; PerlSock_endservent(); EXTEND(SP,1); RETPUSHYES; @@ -4951,8 +5112,8 @@ PP(pp_gpwuid) PP(pp_gpwent) { - djSP; #ifdef HAS_PASSWD + dSP; I32 which = PL_op->op_type; register SV *sv; STRLEN n_a; @@ -5013,14 +5174,23 @@ PP(pp_gpwent) switch (which) { case OP_GPWNAM: - pwent = getpwnam(POPpx); - break; + { + char* name = POPpbytex; + pwent = getpwnam(name); + } + break; case OP_GPWUID: - pwent = getpwuid((Uid_t)POPi); + { + Uid_t uid = POPi; + pwent = getpwuid(uid); + } break; case OP_GPWENT: # ifdef HAS_GETPWENT pwent = getpwent(); +#ifdef POSIX_BC /* In some cases pw_passwd has invalid addresses */ + if (pwent) pwent = getpwnam(pwent->pw_name); +#endif # else DIE(aTHX_ PL_no_func, "getpwent"); # endif @@ -5165,8 +5335,8 @@ PP(pp_gpwent) PP(pp_spwent) { - djSP; #if defined(HAS_PASSWD) && defined(HAS_SETPWENT) + dSP; setpwent(); RETPUSHYES; #else @@ -5176,8 +5346,8 @@ PP(pp_spwent) PP(pp_epwent) { - djSP; #if defined(HAS_PASSWD) && defined(HAS_ENDPWENT) + dSP; endpwent(); RETPUSHYES; #else @@ -5205,18 +5375,22 @@ PP(pp_ggrgid) PP(pp_ggrent) { - djSP; #ifdef HAS_GROUP + dSP; I32 which = PL_op->op_type; register char **elem; register SV *sv; struct group *grent; STRLEN n_a; - if (which == OP_GGRNAM) - grent = (struct group *)getgrnam(POPpx); - else if (which == OP_GGRGID) - grent = (struct group *)getgrgid(POPi); + if (which == OP_GGRNAM) { + char* name = POPpbytex; + grent = (struct group *)getgrnam(name); + } + else if (which == OP_GGRGID) { + Gid_t gid = POPi; + grent = (struct group *)getgrgid(gid); + } else #ifdef HAS_GETGRENT grent = (struct group *)getgrent(); @@ -5248,12 +5422,22 @@ PP(pp_ggrent) PUSHs(sv = sv_mortalcopy(&PL_sv_no)); sv_setiv(sv, (IV)grent->gr_gid); +#if !(defined(_CRAYMPP) && defined(USE_REENTRANT_API)) PUSHs(sv = sv_mortalcopy(&PL_sv_no)); + /* In UNICOS/mk (_CRAYMPP) the multithreading + * versions (getgrnam_r, getgrgid_r) + * seem to return an illegal pointer + * as the group members list, gr_mem. + * getgrent() doesn't even have a _r version + * but the gr_mem is poisonous anyway. + * So yes, you cannot get the list of group + * members if building multithreaded in UNICOS/mk. */ for (elem = grent->gr_mem; elem && *elem; elem++) { sv_catpv(sv, *elem); if (elem[1]) sv_catpvn(sv, " ", 1); } +#endif } RETURN; @@ -5264,8 +5448,8 @@ PP(pp_ggrent) PP(pp_sgrent) { - djSP; #if defined(HAS_GROUP) && defined(HAS_SETGRENT) + dSP; setgrent(); RETPUSHYES; #else @@ -5275,8 +5459,8 @@ PP(pp_sgrent) PP(pp_egrent) { - djSP; #if defined(HAS_GROUP) && defined(HAS_ENDGRENT) + dSP; endgrent(); RETPUSHYES; #else @@ -5286,8 +5470,8 @@ PP(pp_egrent) PP(pp_getlogin) { - djSP; dTARGET; #ifdef HAS_GETLOGIN + dSP; dTARGET; char *tmps; EXTEND(SP, 1); if (!(tmps = PerlProc_getlogin())) @@ -5304,7 +5488,7 @@ PP(pp_getlogin) PP(pp_syscall) { #ifdef HAS_SYSCALL - djSP; dMARK; dORIGMARK; dTARGET; + dSP; dMARK; dORIGMARK; dTARGET; register I32 items = SP - MARK; unsigned long a[20]; register I32 i = 0;