This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Extend PUSHFORMAT() to take a second parameter to set retop, to save
[perl5.git] / pp_sys.c
index 6ff9acd..b8fa1a9 100644 (file)
--- a/pp_sys.c
+++ b/pp_sys.c
@@ -1,7 +1,7 @@
 /*    pp_sys.c
  *
- *    Copyright (C) 1995, 1996, 1997, 1998, 1999,
- *    2000, 2001, 2002, 2003, 2004, 2005, by Larry Wall and others
+ *    Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
+ *    2004, 2005, 2006, 2007 by Larry Wall and others
  *
  *    You may distribute under the terms of either the GNU General Public
  *    License or the Artistic License, as specified in the README file.
@@ -297,27 +297,12 @@ S_emulate_eaccess(pTHX_ const char* path, Mode_t mode)
 
     return res;
 }
-#   define PERL_EFF_ACCESS(p,f) (emulate_eaccess((p), (f)))
-#endif
-
-#if !defined(PERL_EFF_ACCESS)
-/* 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)
-{
-    (void)path;
-    (void)mode;
-    Perl_croak(aTHX_ "switching effective uid is not implemented");
-    /*NOTREACHED*/
-    return -1;
-}
+#   define PERL_EFF_ACCESS(p,f) (S_emulate_eaccess(aTHX_ (p), (f)))
 #endif
 
 PP(pp_backtick)
 {
-    dSP; dTARGET;
+    dVAR; dSP; dTARGET;
     PerlIO *fp;
     const char * const tmps = POPpconstx;
     const I32 gimme = GIMME_V;
@@ -330,34 +315,34 @@ PP(pp_backtick)
        mode = "rt";
     fp = PerlProc_popen(tmps, mode);
     if (fp) {
-        const char * const type = PL_curcop->cop_io ? SvPV_nolen_const(PL_curcop->cop_io) : NULL;
+        const char * const type = Perl_PerlIO_context_layers(aTHX_ NULL);
        if (type && *type)
            PerlIO_apply_layers(aTHX_ fp,mode,type);
 
        if (gimme == G_VOID) {
            char tmpbuf[256];
            while (PerlIO_read(fp, tmpbuf, sizeof tmpbuf) > 0)
-               ;
+               NOOP;
        }
        else if (gimme == G_SCALAR) {
            ENTER;
            SAVESPTR(PL_rs);
            PL_rs = &PL_sv_undef;
            sv_setpvn(TARG, "", 0);     /* note that this preserves previous buffer */
-           while (sv_gets(TARG, fp, SvCUR(TARG)) != Nullch)
-               ;
+           while (sv_gets(TARG, fp, SvCUR(TARG)) != NULL)
+               NOOP;
            LEAVE;
            XPUSHs(TARG);
            SvTAINTED_on(TARG);
        }
        else {
            for (;;) {
-               SV * const sv = NEWSV(56, 79);
-               if (sv_gets(sv, fp, 0) == Nullch) {
+               SV * const sv = newSV(79);
+               if (sv_gets(sv, fp, 0) == NULL) {
                    SvREFCNT_dec(sv);
                    break;
                }
-               XPUSHs(sv_2mortal(sv));
+               mXPUSHs(sv);
                if (SvLEN(sv) - SvCUR(sv) > 20) {
                    SvPV_shrink_to_cur(sv);
                }
@@ -403,7 +388,7 @@ PP(pp_glob)
     PL_last_in_gv = (GV*)*PL_stack_sp--;
 
     SAVESPTR(PL_rs);           /* This is not permanent, either. */
-    PL_rs = sv_2mortal(newSVpvs("\000"));
+    PL_rs = newSVpvs_flags("\000", SVs_TEMP);
 #ifndef DOSISH
 #ifndef CSH
     *SvPVX(PL_rs) = '\n';
@@ -417,13 +402,14 @@ PP(pp_glob)
 
 PP(pp_rcatline)
 {
+    dVAR;
     PL_last_in_gv = cGVOP_gv;
     return do_readline();
 }
 
 PP(pp_warn)
 {
-    dSP; dMARK;
+    dVAR; dSP; dMARK;
     SV *tmpsv;
     const char *tmps;
     STRLEN len;
@@ -436,6 +422,7 @@ PP(pp_warn)
     else if (SP == MARK) {
        tmpsv = &PL_sv_no;
        EXTEND(SP, 1);
+       SP = MARK + 1;
     }
     else {
        tmpsv = TOPs;
@@ -450,15 +437,15 @@ PP(pp_warn)
        tmps = SvPV_const(tmpsv, len);
     }
     if (!tmps || !len)
-       tmpsv = sv_2mortal(newSVpvs("Warning: something's wrong"));
+       tmpsv = newSVpvs_flags("Warning: something's wrong", SVs_TEMP);
 
-    Perl_warn(aTHX_ "%"SVf, tmpsv);
+    Perl_warn(aTHX_ "%"SVf, SVfARG(tmpsv));
     RETSETYES;
 }
 
 PP(pp_die)
 {
-    dSP; dMARK;
+    dVAR; dSP; dMARK;
     const char *tmps;
     SV *tmpsv;
     STRLEN len;
@@ -476,7 +463,7 @@ PP(pp_die)
     }
     else {
        tmpsv = TOPs;
-        tmps = SvROK(tmpsv) ? Nullch : SvPV_const(tmpsv, len);
+        tmps = SvROK(tmpsv) ? (const char *)NULL : SvPV_const(tmpsv, len);
     }
     if (!tmps || !len) {
        SV * const error = ERRSV;
@@ -501,7 +488,7 @@ PP(pp_die)
                    sv_setsv(error,*PL_stack_sp--);
                }
            }
-           DIE(aTHX_ Nullch);
+           DIE(aTHX_ NULL);
        }
        else {
            if (SvPOK(error) && SvCUR(error))
@@ -510,13 +497,13 @@ PP(pp_die)
            if (SvOK(tmpsv))
                tmps = SvPV_const(tmpsv, len);
            else
-               tmps = Nullch;
+               tmps = NULL;
        }
     }
     if (!tmps || !len)
-       tmpsv = sv_2mortal(newSVpvs("Died"));
+       tmpsv = newSVpvs_flags("Died", SVs_TEMP);
 
-    DIE(aTHX_ "%"SVf, tmpsv);
+    DIE(aTHX_ "%"SVf, SVfARG(tmpsv));
 }
 
 /* I/O. */
@@ -536,11 +523,16 @@ PP(pp_open)
 
     if (!isGV(gv))
        DIE(aTHX_ PL_no_usym, "filehandle");
-    if ((io = GvIOp(gv)))
+
+    if ((io = GvIOp(gv))) {
+       MAGIC *mg;
        IoFLAGS(GvIOp(gv)) &= ~IOf_UNTAINT;
 
-    if (io) {
-       MAGIC * const mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar);
+       if (IoDIRP(io) && ckWARN2(WARN_IO, WARN_DEPRECATED))
+           Perl_warner(aTHX_ packWARN2(WARN_IO, WARN_DEPRECATED),
+                   "Opening dirhandle %s also as a file", GvENAME(gv));
+
+       mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar);
        if (mg) {
            /* Method's args are same as ours ... */
            /* ... except handle is replaced by the object */
@@ -563,7 +555,7 @@ PP(pp_open)
     }
 
     tmps = SvPV_const(sv, len);
-    ok = do_openn(gv, tmps, len, FALSE, O_RDONLY, 0, Nullfp, MARK+1, (SP-MARK));
+    ok = do_openn(gv, tmps, len, FALSE, O_RDONLY, 0, NULL, MARK+1, (SP-MARK));
     SP = ORIGMARK;
     if (ok)
        PUSHi( (I32)PL_forkprocess );
@@ -577,21 +569,23 @@ PP(pp_open)
 PP(pp_close)
 {
     dVAR; dSP;
-    IO *io;
-    MAGIC *mg;
     GV * const gv = (MAXARG == 0) ? PL_defoutgv : (GV*)POPs;
 
-    if (gv && (io = GvIO(gv))
-       && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
-    {
-       PUSHMARK(SP);
-       XPUSHs(SvTIED_obj((SV*)io, mg));
-       PUTBACK;
-       ENTER;
-       call_method("CLOSE", G_SCALAR);
-       LEAVE;
-       SPAGAIN;
-       RETURN;
+    if (gv) {
+       IO * const io = GvIO(gv);
+       if (io) {
+           MAGIC * const mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar);
+           if (mg) {
+               PUSHMARK(SP);
+               XPUSHs(SvTIED_obj((SV*)io, mg));
+               PUTBACK;
+               ENTER;
+               call_method("CLOSE", G_SCALAR);
+               LEAVE;
+               SPAGAIN;
+               RETURN;
+           }
+       }
     }
     EXTEND(SP, 1);
     PUSHs(boolSV(do_close(gv, TRUE)));
@@ -601,6 +595,7 @@ PP(pp_close)
 PP(pp_pipe_op)
 {
 #ifdef HAS_PIPE
+    dVAR;
     dSP;
     register IO *rstio;
     register IO *wstio;
@@ -633,10 +628,14 @@ PP(pp_pipe_op)
     IoTYPE(wstio) = IoTYPE_WRONLY;
 
     if (!IoIFP(rstio) || !IoOFP(wstio)) {
-       if (IoIFP(rstio)) PerlIO_close(IoIFP(rstio));
-       else PerlLIO_close(fd[0]);
-       if (IoOFP(wstio)) PerlIO_close(IoOFP(wstio));
-       else PerlLIO_close(fd[1]);
+       if (IoIFP(rstio))
+           PerlIO_close(IoIFP(rstio));
+       else
+           PerlLIO_close(fd[0]);
+       if (IoOFP(wstio))
+           PerlIO_close(IoOFP(wstio));
+       else
+           PerlLIO_close(fd[1]);
        goto badexit;
     }
 #if defined(HAS_FCNTL) && defined(F_SETFD)
@@ -692,14 +691,19 @@ PP(pp_fileno)
 
 PP(pp_umask)
 {
+    dVAR;
     dSP;
 #ifdef HAS_UMASK
     dTARGET;
     Mode_t anum;
 
     if (MAXARG < 1) {
-       anum = PerlLIO_umask(0);
-       (void)PerlLIO_umask(anum);
+       anum = PerlLIO_umask(022);
+       /* setting it to 022 between the two calls to umask avoids
+        * to have a window where the umask is set to 0 -- meaning
+        * that another thread could create world-writeable files. */
+       if (anum != 022)
+           (void)PerlLIO_umask(anum);
     }
     else
        anum = PerlLIO_umask(POPi);
@@ -722,8 +726,7 @@ PP(pp_binmode)
     GV *gv;
     IO *io;
     PerlIO *fp;
-    MAGIC *mg;
-    SV *discp = Nullsv;
+    SV *discp = NULL;
 
     if (MAXARG < 1)
        RETPUSHUNDEF;
@@ -733,19 +736,20 @@ PP(pp_binmode)
 
     gv = (GV*)POPs;
 
-    if (gv && (io = GvIO(gv))
-       && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
-    {
-       PUSHMARK(SP);
-       XPUSHs(SvTIED_obj((SV*)io, mg));
-       if (discp)
-           XPUSHs(discp);
-       PUTBACK;
-       ENTER;
-       call_method("BINMODE", G_SCALAR);
-       LEAVE;
-       SPAGAIN;
-       RETURN;
+    if (gv && (io = GvIO(gv))) {
+       MAGIC * const mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar);
+       if (mg) {
+           PUSHMARK(SP);
+           XPUSHs(SvTIED_obj((SV*)io, mg));
+           if (discp)
+               XPUSHs(discp);
+           PUTBACK;
+           ENTER;
+           call_method("BINMODE", G_SCALAR);
+           LEAVE;
+           SPAGAIN;
+           RETURN;
+       }
     }
 
     EXTEND(SP, 1);
@@ -757,22 +761,23 @@ PP(pp_binmode)
     }
 
     PUTBACK;
-    if (PerlIO_binmode(aTHX_ fp,IoTYPE(io),mode_from_discipline(discp),
-                       (discp) ? SvPV_nolen_const(discp) : Nullch)) {
-       if (IoOFP(io) && IoOFP(io) != IoIFP(io)) {
-            if (!PerlIO_binmode(aTHX_ IoOFP(io),IoTYPE(io),
-                       mode_from_discipline(discp),
-                       (discp) ? SvPV_nolen_const(discp) : Nullch)) {
-               SPAGAIN;
-               RETPUSHUNDEF;
-            }
+    {
+       const int mode = mode_from_discipline(discp);
+       const char *const d = (discp ? SvPV_nolen_const(discp) : NULL);
+       if (PerlIO_binmode(aTHX_ fp, IoTYPE(io), mode, d)) {
+           if (IoOFP(io) && IoOFP(io) != IoIFP(io)) {
+               if (!PerlIO_binmode(aTHX_ IoOFP(io), IoTYPE(io), mode, d)) {
+                   SPAGAIN;
+                   RETPUSHUNDEF;
+               }
+           }
+           SPAGAIN;
+           RETPUSHYES;
+       }
+       else {
+           SPAGAIN;
+           RETPUSHUNDEF;
        }
-       SPAGAIN;
-       RETPUSHYES;
-    }
-    else {
-       SPAGAIN;
-       RETPUSHUNDEF;
     }
 }
 
@@ -830,10 +835,10 @@ PP(pp_tie)
        /* Not clear why we don't call call_method here too.
         * perhaps to get different error message ?
         */
-       stash = gv_stashsv(*MARK, FALSE);
+       stash = gv_stashsv(*MARK, 0);
        if (!stash || !(gv = gv_fetchmethod(stash, methname))) {
            DIE(aTHX_ "Can't locate object method \"%s\" via package \"%"SVf"\"",
-                methname, *MARK);
+                methname, SVfARG(*MARK));
        }
        ENTER;
        PUSHSTACKi(PERLSI_MAGIC);
@@ -856,7 +861,7 @@ PP(pp_tie)
             SvTYPE(varsv) == SVt_PVHV))
            Perl_croak(aTHX_
                       "Self-ties of arrays and hashes are not supported");
-       sv_magic(varsv, (SvRV(sv) == varsv ? Nullsv : sv), how, Nullch, 0);
+       sv_magic(varsv, (SvRV(sv) == varsv ? NULL : sv), how, NULL, 0);
     }
     LEAVE;
     SP = PL_stack_base + markoff;
@@ -883,7 +888,7 @@ PP(pp_untie)
            if (gv && isGV(gv) && (cv = GvCV(gv))) {
               PUSHMARK(SP);
               XPUSHs(SvTIED_obj((SV*)gv, mg));
-              XPUSHs(sv_2mortal(newSViv(SvREFCNT(obj)-1)));
+              mXPUSHi(SvREFCNT(obj) - 1);
               PUTBACK;
               ENTER;
               call_sv((SV *)cv, G_VOID);
@@ -903,6 +908,7 @@ PP(pp_untie)
 
 PP(pp_tied)
 {
+    dVAR;
     dSP;
     const MAGIC *mg;
     SV *sv = POPs;
@@ -930,10 +936,8 @@ PP(pp_dbmopen)
     GV *gv;
 
     HV * const hv = (HV*)POPs;
-    SV * const sv = sv_mortalcopy(&PL_sv_no);
-
-    sv_setpv(sv, "AnyDBM_File");
-    stash = gv_stashsv(sv, FALSE);
+    SV * const sv = newSVpvs_flags("AnyDBM_File", SVs_TEMP);
+    stash = gv_stashsv(sv, 0);
     if (!stash || !(gv = gv_fetchmethod(stash, "TIEHASH"))) {
        PUTBACK;
        require_pv("AnyDBM_File.pm");
@@ -949,9 +953,9 @@ PP(pp_dbmopen)
     PUSHs(sv);
     PUSHs(left);
     if (SvIV(right))
-       PUSHs(sv_2mortal(newSVuv(O_RDWR|O_CREAT)));
+       mPUSHu(O_RDWR|O_CREAT);
     else
-       PUSHs(sv_2mortal(newSVuv(O_RDWR)));
+       mPUSHu(O_RDWR);
     PUSHs(right);
     PUTBACK;
     call_sv((SV*)GvCV(gv), G_SCALAR);
@@ -962,7 +966,7 @@ PP(pp_dbmopen)
        PUSHMARK(SP);
        PUSHs(sv);
        PUSHs(left);
-       PUSHs(sv_2mortal(newSVuv(O_RDONLY)));
+       mPUSHu(O_RDONLY);
        PUSHs(right);
        PUTBACK;
        call_sv((SV*)GvCV(gv), G_SCALAR);
@@ -971,7 +975,7 @@ PP(pp_dbmopen)
 
     if (sv_isobject(TOPs)) {
        sv_unmagic((SV *) hv, PERL_MAGIC_tied);
-       sv_magic((SV*)hv, TOPs, PERL_MAGIC_tied, Nullch, 0);
+       sv_magic((SV*)hv, TOPs, PERL_MAGIC_tied, NULL, 0);
     }
     LEAVE;
     RETURN;
@@ -980,7 +984,7 @@ PP(pp_dbmopen)
 PP(pp_sselect)
 {
 #ifdef HAS_SELECT
-    dSP; dTARGET;
+    dVAR; dSP; dTARGET;
     register I32 i;
     register I32 j;
     register char *s;
@@ -1066,7 +1070,7 @@ PP(pp_sselect)
        timebuf.tv_usec = (long)(value * 1000000.0);
     }
     else
-       tbuf = Null(struct timeval*);
+       tbuf = NULL;
 
     for (i = 1; i <= 3; i++) {
        sv = SP[i];
@@ -1133,8 +1137,7 @@ PP(pp_sselect)
     if (GIMME == G_ARRAY && tbuf) {
        value = (NV)(timebuf.tv_sec) +
                (NV)(timebuf.tv_usec) / 1000000.0;
-       PUSHs(sv = sv_mortalcopy(&PL_sv_no));
-       sv_setnv(sv, value);
+       mPUSHn(value);
     }
     RETURN;
 #else
@@ -1145,8 +1148,8 @@ PP(pp_sselect)
 void
 Perl_setdefout(pTHX_ GV *gv)
 {
-    if (gv)
-       (void)SvREFCNT_inc(gv);
+    dVAR;
+    SvREFCNT_inc_simple_void(gv);
     if (PL_defoutgv)
        SvREFCNT_dec(PL_defoutgv);
     PL_defoutgv = gv;
@@ -1154,9 +1157,9 @@ Perl_setdefout(pTHX_ GV *gv)
 
 PP(pp_select)
 {
-    dSP; dTARGET;
+    dVAR; dSP; dTARGET;
     HV *hv;
-    GV * const newdefout = (PL_op->op_private > 0) ? ((GV *) POPs) : (GV *) NULL;
+    GV * const newdefout = (PL_op->op_private > 0) ? ((GV *) POPs) : NULL;
     GV * egv = GvEGV(PL_defoutgv);
 
     if (!egv)
@@ -1167,11 +1170,11 @@ PP(pp_select)
     else {
        GV * const * const gvp = (GV**)hv_fetch(hv, GvNAME(egv), GvNAMELEN(egv), FALSE);
        if (gvp && *gvp == egv) {
-           gv_efullname4(TARG, PL_defoutgv, Nullch, TRUE);
+           gv_efullname4(TARG, PL_defoutgv, NULL, TRUE);
            XPUSHTARG;
        }
        else {
-           XPUSHs(sv_2mortal(newRV((SV*)egv)));
+           mXPUSHs(newRV((SV*)egv));
        }
     }
 
@@ -1188,23 +1191,23 @@ PP(pp_getc)
 {
     dVAR; dSP; dTARGET;
     IO *io = NULL;
-    MAGIC *mg;
     GV * const gv = (MAXARG==0) ? PL_stdingv : (GV*)POPs;
 
-    if (gv && (io = GvIO(gv))
-       && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
-    {
-       const I32 gimme = GIMME_V;
-       PUSHMARK(SP);
-       XPUSHs(SvTIED_obj((SV*)io, mg));
-       PUTBACK;
-       ENTER;
-       call_method("GETC", gimme);
-       LEAVE;
-       SPAGAIN;
-       if (gimme == G_SCALAR)
-           SvSetMagicSV_nosteal(TARG, TOPs);
-       RETURN;
+    if (gv && (io = GvIO(gv))) {
+       MAGIC * const mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar);
+       if (mg) {
+           const I32 gimme = GIMME_V;
+           PUSHMARK(SP);
+           XPUSHs(SvTIED_obj((SV*)io, mg));
+           PUTBACK;
+           ENTER;
+           call_method("GETC", gimme);
+           LEAVE;
+           SPAGAIN;
+           if (gimme == G_SCALAR)
+               SvSetMagicSV_nosteal(TARG, TOPs);
+           RETURN;
+       }
     }
     if (!gv || do_eof(gv)) { /* make sure we have fp with something */
        if ((!io || (!IoIFP(io) && IoTYPE(io) != IoTYPE_WRONLY))
@@ -1241,8 +1244,7 @@ S_doform(pTHX_ CV *cv, GV *gv, OP *retop)
     SAVETMPS;
 
     PUSHBLOCK(cx, CXt_FORMAT, PL_stack_sp);
-    PUSHFORMAT(cx);
-    cx->blk_sub.retop = retop;
+    PUSHFORMAT(cx, retop);
     SAVECOMPPAD();
     PAD_SET_CUR_NOSAVE(CvPADLIST(cv), 1);
 
@@ -1252,11 +1254,13 @@ S_doform(pTHX_ CV *cv, GV *gv, OP *retop)
 
 PP(pp_enterwrite)
 {
+    dVAR;
     dSP;
     register GV *gv;
     register IO *io;
     GV *fgv;
     CV *cv;
+    SV * tmpsv = NULL;
 
     if (MAXARG == 0)
        gv = PL_defoutgv;
@@ -1275,16 +1279,19 @@ PP(pp_enterwrite)
     else
        fgv = gv;
 
+    if (!fgv)
+       goto not_a_format_reference;
+
     cv = GvFORM(fgv);
     if (!cv) {
-       if (fgv) {
-           SV * const tmpsv = sv_newmortal();
-           const char *name;
-           gv_efullname4(tmpsv, fgv, Nullch, FALSE);
-           name = SvPV_nolen_const(tmpsv);
-           if (name && *name)
-               DIE(aTHX_ "Undefined format \"%s\" called", name);
-       }
+       const char *name;
+       tmpsv = sv_newmortal();
+       gv_efullname4(tmpsv, fgv, NULL, FALSE);
+       name = SvPV_nolen_const(tmpsv);
+       if (name && *name)
+           DIE(aTHX_ "Undefined format \"%s\" called", name);
+
+       not_a_format_reference:
        DIE(aTHX_ "Not a format reference");
     }
     if (CvCLONE(cv))
@@ -1299,16 +1306,18 @@ PP(pp_leavewrite)
     dVAR; dSP;
     GV * const gv = cxstack[cxstack_ix].blk_sub.gv;
     register IO * const io = GvIOp(gv);
-    PerlIO * const ofp = IoOFP(io);
+    PerlIO *ofp;
     PerlIO *fp;
     SV **newsp;
     I32 gimme;
     register PERL_CONTEXT *cx;
 
+    if (!io || !(ofp = IoOFP(io)))
+        goto forget_top;
+
     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)
     {
@@ -1324,10 +1333,10 @@ PP(pp_leavewrite)
                topname = sv_2mortal(Perl_newSVpvf(aTHX_ "%s_TOP", GvNAME(gv)));
                topgv = gv_fetchsv(topname, 0, SVt_PVFM);
                if ((topgv && GvFORM(topgv)) ||
-                 !gv_fetchpv("top", 0, SVt_PVFM))
+                 !gv_fetchpvs("top", GV_NOTQUAL, SVt_PVFM))
                    IoTOP_NAME(io) = savesvpv(topname);
                else
-                   IoTOP_NAME(io) = savepvn("top", 3);
+                   IoTOP_NAME(io) = savepvs("top");
            }
            topgv = gv_fetchpv(IoTOP_NAME(io), 0, SVt_PVFM);
            if (!topgv || !GvFORM(topgv)) {
@@ -1369,18 +1378,16 @@ PP(pp_leavewrite)
        if (!cv) {
            SV * const sv = sv_newmortal();
            const char *name;
-           gv_efullname4(sv, fgv, Nullch, FALSE);
+           gv_efullname4(sv, fgv, NULL, FALSE);
            name = SvPV_nolen_const(sv);
            if (name && *name)
-               DIE(aTHX_ "Undefined top format \"%s\" called",name);
+               DIE(aTHX_ "Undefined top format \"%s\" called", name);
+           else
+               DIE(aTHX_ "Undefined top format called");
        }
-       /* why no:
-       else
-           DIE(aTHX_ "Undefined top format called");
-       ?*/
-       if (CvCLONE(cv))
+       if (cv && CvCLONE(cv))
            cv = (CV*)sv_2mortal((SV*)cv_clone(cv));
-       return doform(cv,gv,PL_op);
+       return doform(cv, gv, PL_op);
     }
 
   forget_top:
@@ -1428,33 +1435,33 @@ PP(pp_prtf)
     IO *io;
     PerlIO *fp;
     SV *sv;
-    MAGIC *mg;
 
     GV * const gv = (PL_op->op_flags & OPf_STACKED) ? (GV*)*++MARK : PL_defoutgv;
 
-    if (gv && (io = GvIO(gv))
-       && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
-    {
-       if (MARK == ORIGMARK) {
-           MEXTEND(SP, 1);
-           ++MARK;
-           Move(MARK, MARK + 1, (SP - MARK) + 1, SV*);
-           ++SP;
+    if (gv && (io = GvIO(gv))) {
+       MAGIC * const mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar);
+       if (mg) {
+           if (MARK == ORIGMARK) {
+               MEXTEND(SP, 1);
+               ++MARK;
+               Move(MARK, MARK + 1, (SP - MARK) + 1, SV*);
+               ++SP;
+           }
+           PUSHMARK(MARK - 1);
+           *MARK = SvTIED_obj((SV*)io, mg);
+           PUTBACK;
+           ENTER;
+           call_method("PRINTF", G_SCALAR);
+           LEAVE;
+           SPAGAIN;
+           MARK = ORIGMARK + 1;
+           *MARK = *SP;
+           SP = MARK;
+           RETURN;
        }
-       PUSHMARK(MARK - 1);
-       *MARK = SvTIED_obj((SV*)io, mg);
-       PUTBACK;
-       ENTER;
-       call_method("PRINTF", G_SCALAR);
-       LEAVE;
-       SPAGAIN;
-       MARK = ORIGMARK + 1;
-       *MARK = *SP;
-       SP = MARK;
-       RETURN;
     }
 
-    sv = NEWSV(0,0);
+    sv = newSV(0);
     if (!(io = GvIO(gv))) {
        if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
            report_evil_fh(gv, io, PL_op->op_type);
@@ -1472,6 +1479,8 @@ PP(pp_prtf)
        goto just_say_no;
     }
     else {
+       if (SvTAINTED(MARK[1]))
+           TAINT_PROPER("printf");
        do_sprintf(sv, SP - MARK, MARK + 1);
        if (!do_print(sv, fp))
            goto just_say_no;
@@ -1494,6 +1503,7 @@ PP(pp_prtf)
 
 PP(pp_sysopen)
 {
+    dVAR;
     dSP;
     const int perm = (MAXARG > 3) ? POPi : 0666;
     const int mode = POPi;
@@ -1504,7 +1514,7 @@ PP(pp_sysopen)
     /* Need TIEHANDLE method ? */
     const char * const tmps = SvPV_const(sv, len);
     /* FIXME? do_open should do const  */
-    if (do_open(gv, tmps, len, TRUE, mode, perm, Nullfp)) {
+    if (do_open(gv, tmps, len, TRUE, mode, perm, NULL)) {
        IoLINES(GvIOp(gv)) = 0;
        PUSHs(&PL_sv_yes);
     }
@@ -1605,7 +1615,7 @@ PP(pp_sysread)
        buffer = SvGROW(bufsv, (STRLEN)(length+1));
        /* 'offset' means 'flags' here */
        count = PerlSock_recvfrom(PerlIO_fileno(IoIFP(io)), buffer, length, offset,
-                         (struct sockaddr *)namebuf, &bufsize);
+                                 (struct sockaddr *)namebuf, &bufsize);
        if (count < 0)
            RETPUSHUNDEF;
 #ifdef EPOC
@@ -1773,102 +1783,163 @@ PP(pp_send)
     IO *io;
     SV *bufsv;
     const char *buffer;
-    Size_t length = 0;
     SSize_t retval;
     STRLEN blen;
-    MAGIC *mg;
+    STRLEN orig_blen_bytes;
     const int op_type = PL_op->op_type;
+    bool doing_utf8;
+    U8 *tmpbuf = NULL;
     
     GV *const gv = (GV*)*++MARK;
     if (PL_op->op_type == OP_SYSWRITE
-       && gv && (io = GvIO(gv))
-       && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
-    {
-       SV *sv;
+       && gv && (io = GvIO(gv))) {
+       MAGIC * const mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar);
+       if (mg) {
+           SV *sv;
+
+           if (MARK == SP - 1) {
+               EXTEND(SP, 1000);
+               sv = sv_2mortal(newSViv(sv_len(*SP)));
+               PUSHs(sv);
+               PUTBACK;
+           }
 
-       if (MARK == SP - 1) {
-           EXTEND(SP, 1000);
-           sv = sv_2mortal(newSViv(sv_len(*SP)));
+           PUSHMARK(ORIGMARK);
+           *(ORIGMARK+1) = SvTIED_obj((SV*)io, mg);
+           ENTER;
+           call_method("WRITE", G_SCALAR);
+           LEAVE;
+           SPAGAIN;
+           sv = POPs;
+           SP = ORIGMARK;
            PUSHs(sv);
-           PUTBACK;
+           RETURN;
        }
-       
-       PUSHMARK(ORIGMARK);
-       *(ORIGMARK+1) = SvTIED_obj((SV*)io, mg);
-       ENTER;
-       call_method("WRITE", G_SCALAR);
-       LEAVE;
-       SPAGAIN;
-       sv = POPs;
-       SP = ORIGMARK;
-       PUSHs(sv);
-       RETURN;
     }
     if (!gv)
        goto say_undef;
 
     bufsv = *++MARK;
 
-    if (op_type == OP_SYSWRITE) {
-       if (MARK >= SP) {
-           length = (Size_t) sv_len(bufsv);
-       } else {
-#if Size_t_size > IVSIZE
-           length = (Size_t)SvNVx(*++MARK);
-#else
-           length = (Size_t)SvIVx(*++MARK);
-#endif
-           if ((SSize_t)length < 0)
-               DIE(aTHX_ "Negative length");
-       }
-    }
     SETERRNO(0,0);
     io = GvIO(gv);
-    if (!io || !IoIFP(io)) {
+    if (!io || !IoIFP(io) || IoTYPE(io) == IoTYPE_RDONLY) {
        retval = -1;
-       if (ckWARN(WARN_CLOSED))
-           report_evil_fh(gv, io, PL_op->op_type);
+       if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)) {
+           if (io && IoIFP(io))
+               report_evil_fh(gv, io, OP_phoney_INPUT_ONLY);
+           else
+               report_evil_fh(gv, io, PL_op->op_type);
+       }
        SETERRNO(EBADF,RMS_IFI);
        goto say_undef;
     }
 
+    /* Do this first to trigger any overloading.  */
+    buffer = SvPV_const(bufsv, blen);
+    orig_blen_bytes = blen;
+    doing_utf8 = DO_UTF8(bufsv);
+
     if (PerlIO_isutf8(IoIFP(io))) {
        if (!SvUTF8(bufsv)) {
-           bufsv = sv_2mortal(newSVsv(bufsv));
-           buffer = sv_2pvutf8(bufsv, &blen);
-       } else
-           buffer = SvPV_const(bufsv, blen);
+           /* We don't modify the original scalar.  */
+           tmpbuf = bytes_to_utf8((const U8*) buffer, &blen);
+           buffer = (char *) tmpbuf;
+           doing_utf8 = TRUE;
+       }
     }
-    else {
-        if (DO_UTF8(bufsv)) {
-             /* Not modifying source SV, so making a temporary copy. */
-             bufsv = sv_2mortal(newSVsv(bufsv));
-             sv_utf8_downgrade(bufsv, FALSE);
-        }
-        buffer = SvPV_const(bufsv, blen);
+    else if (doing_utf8) {
+       STRLEN tmplen = blen;
+       U8 * const result = bytes_from_utf8((const U8*) buffer, &tmplen, &doing_utf8);
+       if (!doing_utf8) {
+           tmpbuf = result;
+           buffer = (char *) tmpbuf;
+           blen = tmplen;
+       }
+       else {
+           assert((char *)result == buffer);
+           Perl_croak(aTHX_ "Wide character in %s", OP_DESC(PL_op));
+       }
     }
 
     if (op_type == OP_SYSWRITE) {
+       Size_t length = 0; /* This length is in characters.  */
+       STRLEN blen_chars;
        IV offset;
-       if (DO_UTF8(bufsv)) {
-           /* length and offset are in chars */
-           blen   = sv_len_utf8(bufsv);
+
+       if (doing_utf8) {
+           if (tmpbuf) {
+               /* The SV is bytes, and we've had to upgrade it.  */
+               blen_chars = orig_blen_bytes;
+           } else {
+               /* The SV really is UTF-8.  */
+               if (SvGMAGICAL(bufsv) || SvAMAGIC(bufsv)) {
+                   /* Don't call sv_len_utf8 again because it will call magic
+                      or overloading a second time, and we might get back a
+                      different result.  */
+                   blen_chars = utf8_length((U8*)buffer, (U8*)buffer + blen);
+               } else {
+                   /* It's safe, and it may well be cached.  */
+                   blen_chars = sv_len_utf8(bufsv);
+               }
+           }
+       } else {
+           blen_chars = blen;
+       }
+
+       if (MARK >= SP) {
+           length = blen_chars;
+       } else {
+#if Size_t_size > IVSIZE
+           length = (Size_t)SvNVx(*++MARK);
+#else
+           length = (Size_t)SvIVx(*++MARK);
+#endif
+           if ((SSize_t)length < 0) {
+               Safefree(tmpbuf);
+               DIE(aTHX_ "Negative length");
+           }
        }
+
        if (MARK < SP) {
            offset = SvIVx(*++MARK);
            if (offset < 0) {
-               if (-offset > (IV)blen)
+               if (-offset > (IV)blen_chars) {
+                   Safefree(tmpbuf);
                    DIE(aTHX_ "Offset outside string");
-               offset += blen;
-           } else if (offset >= (IV)blen && blen > 0)
+               }
+               offset += blen_chars;
+           } else if (offset >= (IV)blen_chars && blen_chars > 0) {
+               Safefree(tmpbuf);
                DIE(aTHX_ "Offset outside string");
+           }
        } else
            offset = 0;
-       if (length > blen - offset)
-           length = blen - offset;
-       if (DO_UTF8(bufsv)) {
-           buffer = (const char*)utf8_hop((const U8 *)buffer, offset);
-           length = utf8_hop((U8 *)buffer, length) - (U8 *)buffer;
+       if (length > blen_chars - offset)
+           length = blen_chars - offset;
+       if (doing_utf8) {
+           /* Here we convert length from characters to bytes.  */
+           if (tmpbuf || SvGMAGICAL(bufsv) || SvAMAGIC(bufsv)) {
+               /* Either we had to convert the SV, or the SV is magical, or
+                  the SV has overloading, in which case we can't or mustn't
+                  or mustn't call it again.  */
+
+               buffer = (const char*)utf8_hop((const U8 *)buffer, offset);
+               length = utf8_hop((U8 *)buffer, length) - (U8 *)buffer;
+           } else {
+               /* It's a real UTF-8 SV, and it's not going to change under
+                  us.  Take advantage of any cache.  */
+               I32 start = offset;
+               I32 len_I32 = length;
+
+               /* Convert the start and end character positions to bytes.
+                  Remember that the second argument to sv_pos_u2b is relative
+                  to the first.  */
+               sv_pos_u2b(bufsv, &start, &len_I32);
+
+               buffer += start;
+               length = len_I32;
+           }
        }
        else {
            buffer = buffer+offset;
@@ -1904,11 +1975,14 @@ PP(pp_send)
     else
        DIE(aTHX_ PL_no_sock_func, "send");
 #endif
+
     if (retval < 0)
        goto say_undef;
     SP = ORIGMARK;
-    if (DO_UTF8(bufsv))
+    if (doing_utf8)
         retval = utf8_length((U8*)buffer, (U8*)buffer + retval);
+
+    Safefree(tmpbuf);
 #if Size_t_size > IVSIZE
     PUSHn(retval);
 #else
@@ -1917,6 +1991,7 @@ PP(pp_send)
     RETURN;
 
   say_undef:
+    Safefree(tmpbuf);
     SP = ORIGMARK;
     RETPUSHUNDEF;
 }
@@ -1925,8 +2000,6 @@ PP(pp_eof)
 {
     dVAR; dSP;
     GV *gv;
-    IO *io;
-    MAGIC *mg;
 
     if (MAXARG == 0) {
        if (PL_op->op_flags & OPf_SPECIAL) {    /* eof() */
@@ -1937,8 +2010,13 @@ PP(pp_eof)
                if ((IoFLAGS(io) & IOf_START) && av_len(GvAVn(gv)) < 0) {
                    IoLINES(io) = 0;
                    IoFLAGS(io) &= ~IOf_START;
-                   do_open(gv, "-", 1, FALSE, O_RDONLY, 0, Nullfp);
-                   sv_setpvn(GvSV(gv), "-", 1);
+                   do_open(gv, "-", 1, FALSE, O_RDONLY, 0, NULL);
+                   if ( GvSV(gv) ) {
+                       sv_setpvn(GvSV(gv), "-", 1);
+                   }
+                   else {
+                       GvSV(gv) = newSVpvn("-", 1);
+                   }
                    SvSETMAGIC(GvSV(gv));
                }
                else if (!nextargv(gv))
@@ -1951,17 +2029,19 @@ PP(pp_eof)
     else
        gv = PL_last_in_gv = (GV*)POPs;         /* eof(FH) */
 
-    if (gv && (io = GvIO(gv))
-       && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
-    {
-       PUSHMARK(SP);
-       XPUSHs(SvTIED_obj((SV*)io, mg));
-       PUTBACK;
-       ENTER;
-       call_method("EOF", G_SCALAR);
-       LEAVE;
-       SPAGAIN;
-       RETURN;
+    if (gv) {
+       IO * const io = GvIO(gv);
+       MAGIC * mg;
+       if (io && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar))) {
+           PUSHMARK(SP);
+           XPUSHs(SvTIED_obj((SV*)io, mg));
+           PUTBACK;
+           ENTER;
+           call_method("EOF", G_SCALAR);
+           LEAVE;
+           SPAGAIN;
+           RETURN;
+       }
     }
 
     PUSHs(boolSV(!gv || do_eof(gv)));
@@ -1973,23 +2053,23 @@ PP(pp_tell)
     dVAR; dSP; dTARGET;
     GV *gv;
     IO *io;
-    MAGIC *mg;
 
     if (MAXARG != 0)
        PL_last_in_gv = (GV*)POPs;
     gv = PL_last_in_gv;
 
-    if (gv && (io = GvIO(gv))
-       && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
-    {
-       PUSHMARK(SP);
-       XPUSHs(SvTIED_obj((SV*)io, mg));
-       PUTBACK;
-       ENTER;
-       call_method("TELL", G_SCALAR);
-       LEAVE;
-       SPAGAIN;
-       RETURN;
+    if (gv && (io = GvIO(gv))) {
+       MAGIC * const mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar);
+       if (mg) {
+           PUSHMARK(SP);
+           XPUSHs(SvTIED_obj((SV*)io, mg));
+           PUTBACK;
+           ENTER;
+           call_method("TELL", G_SCALAR);
+           LEAVE;
+           SPAGAIN;
+           RETURN;
+       }
     }
 
 #if LSEEKSIZE > IVSIZE
@@ -2003,34 +2083,34 @@ PP(pp_tell)
 PP(pp_sysseek)
 {
     dVAR; dSP;
-    IO *io;
     const int whence = POPi;
 #if LSEEKSIZE > IVSIZE
     const Off_t offset = (Off_t)SvNVx(POPs);
 #else
     const Off_t offset = (Off_t)SvIVx(POPs);
 #endif
-    MAGIC *mg;
 
     GV * const gv = PL_last_in_gv = (GV*)POPs;
+    IO *io;
 
-    if (gv && (io = GvIO(gv))
-       && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
-    {
-       PUSHMARK(SP);
-       XPUSHs(SvTIED_obj((SV*)io, mg));
+    if (gv && (io = GvIO(gv))) {
+       MAGIC * const mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar);
+       if (mg) {
+           PUSHMARK(SP);
+           XPUSHs(SvTIED_obj((SV*)io, mg));
 #if LSEEKSIZE > IVSIZE
-       XPUSHs(sv_2mortal(newSVnv((NV) offset)));
+           mXPUSHn((NV) offset);
 #else
-       XPUSHs(sv_2mortal(newSViv(offset)));
+           mXPUSHi(offset);
 #endif
-       XPUSHs(sv_2mortal(newSViv(whence)));
-       PUTBACK;
-       ENTER;
-       call_method("SEEK", G_SCALAR);
-       LEAVE;
-       SPAGAIN;
-       RETURN;
+           mXPUSHi(whence);
+           PUTBACK;
+           ENTER;
+           call_method("SEEK", G_SCALAR);
+           LEAVE;
+           SPAGAIN;
+           RETURN;
+       }
     }
 
     if (PL_op->op_type == OP_SEEK)
@@ -2047,7 +2127,7 @@ PP(pp_sysseek)
                 newSViv(sought)
 #endif
                 : newSVpvn(zero_but_true, ZBTLEN);
-            PUSHs(sv_2mortal(sv));
+            mPUSHs(sv);
         }
     }
     RETURN;
@@ -2055,6 +2135,7 @@ PP(pp_sysseek)
 
 PP(pp_truncate)
 {
+    dVAR;
     dSP;
     /* There seems to be no consensus on the length type of truncate()
      * and ftruncate(), both off_t and size_t have supporters. In
@@ -2149,12 +2230,12 @@ PP(pp_truncate)
 
 PP(pp_ioctl)
 {
-    dSP; dTARGET;
+    dVAR; dSP; dTARGET;
     SV * const argsv = POPs;
     const unsigned int func = POPu;
     const int optype = PL_op->op_type;
     GV * const gv = (GV*)POPs;
-    IO * const io = gv ? GvIOn(gv) : Null(IO*);
+    IO * const io = gv ? GvIOn(gv) : NULL;
     char *s;
     IV retval;
 
@@ -2225,7 +2306,7 @@ PP(pp_ioctl)
 PP(pp_flock)
 {
 #ifdef FLOCK
-    dSP; dTARGET;
+    dVAR; dSP; dTARGET;
     I32 value;
     IO *io = NULL;
     PerlIO *fp;
@@ -2235,7 +2316,7 @@ PP(pp_flock)
     if (gv && (io = GvIO(gv)))
        fp = IoIFP(io);
     else {
-       fp = Nullfp;
+       fp = NULL;
        io = NULL;
     }
     /* XXX Looks to me like io is always NULL at this point */
@@ -2261,7 +2342,7 @@ PP(pp_flock)
 PP(pp_socket)
 {
 #ifdef HAS_SOCKET
-    dSP;
+    dVAR; dSP;
     const int protocol = POPi;
     const int type = POPi;
     const int domain = POPi;
@@ -2272,7 +2353,7 @@ PP(pp_socket)
     if (!gv || !io) {
        if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
            report_evil_fh(gv, io, PL_op->op_type);
-       if (IoIFP(io))
+       if (io && IoIFP(io))
            do_close(gv, FALSE);
        SETERRNO(EBADF,LIB_INVARG);
        RETPUSHUNDEF;
@@ -2311,7 +2392,7 @@ PP(pp_socket)
 PP(pp_sockpair)
 {
 #if defined (HAS_SOCKETPAIR) || (defined (HAS_SOCKET) && defined(SOCK_DGRAM) && defined(AF_INET) && defined(PF_INET))
-    dSP;
+    dVAR; dSP;
     const int protocol = POPi;
     const int type = POPi;
     const int domain = POPi;
@@ -2328,9 +2409,9 @@ PP(pp_sockpair)
            if (!gv2 || !io2)
                report_evil_fh(gv1, io2, PL_op->op_type);
        }
-       if (IoIFP(io1))
+       if (io1 && IoIFP(io1))
            do_close(gv1, FALSE);
-       if (IoIFP(io2))
+       if (io2 && IoIFP(io2))
            do_close(gv2, FALSE);
        RETPUSHUNDEF;
     }
@@ -2372,49 +2453,20 @@ PP(pp_sockpair)
 PP(pp_bind)
 {
 #ifdef HAS_SOCKET
-    dSP;
-#ifdef MPE /* Requires PRIV mode to bind() to ports < 1024 */
-    extern void GETPRIVMODE();
-    extern void GETUSERMODE();
-#endif
+    dVAR; dSP;
     SV * const addrsv = POPs;
     /* OK, so on what platform does bind modify addr?  */
     const char *addr;
     GV * const gv = (GV*)POPs;
     register IO * const io = GvIOn(gv);
     STRLEN len;
-    int bind_ok = 0;
-#ifdef MPE
-    int mpeprivmode = 0;
-#endif
 
     if (!io || !IoIFP(io))
        goto nuts;
 
     addr = SvPV_const(addrsv, len);
     TAINT_PROPER("bind");
-#ifdef MPE /* Deal with MPE bind() peculiarities */
-    if (((struct sockaddr *)addr)->sa_family == AF_INET) {
-        /* The address *MUST* stupidly be zero. */
-        ((struct sockaddr_in *)addr)->sin_addr.s_addr = INADDR_ANY;
-        /* PRIV mode is required to bind() to ports < 1024. */
-        if (((struct sockaddr_in *)addr)->sin_port < 1024 &&
-            ((struct sockaddr_in *)addr)->sin_port > 0) {
-            GETPRIVMODE(); /* If this fails, we are aborted by MPE/iX. */
-           mpeprivmode = 1;
-       }
-    }
-#endif /* MPE */
-    if (PerlSock_bind(PerlIO_fileno(IoIFP(io)),
-                     (struct sockaddr *)addr, len) >= 0)
-       bind_ok = 1;
-
-#ifdef MPE /* Switch back to USER mode */
-    if (mpeprivmode)
-       GETUSERMODE();
-#endif /* MPE */
-
-    if (bind_ok)
+    if (PerlSock_bind(PerlIO_fileno(IoIFP(io)), (struct sockaddr *)addr, len) >= 0)
        RETPUSHYES;
     else
        RETPUSHUNDEF;
@@ -2432,7 +2484,7 @@ nuts:
 PP(pp_connect)
 {
 #ifdef HAS_SOCKET
-    dSP;
+    dVAR; dSP;
     SV * const addrsv = POPs;
     GV * const gv = (GV*)POPs;
     register IO * const io = GvIOn(gv);
@@ -2462,7 +2514,7 @@ nuts:
 PP(pp_listen)
 {
 #ifdef HAS_SOCKET
-    dSP;
+    dVAR; dSP;
     const int backlog = POPi;
     GV * const gv = (GV*)POPs;
     register IO * const io = gv ? GvIOn(gv) : NULL;
@@ -2488,7 +2540,7 @@ nuts:
 PP(pp_accept)
 {
 #ifdef HAS_SOCKET
-    dSP; dTARGET;
+    dVAR; dSP; dTARGET;
     register IO *nstio;
     register IO *gstio;
     char namebuf[MAXPATHLEN];
@@ -2512,6 +2564,17 @@ PP(pp_accept)
 
     nstio = GvIOn(ngv);
     fd = PerlSock_accept(PerlIO_fileno(IoIFP(gstio)), (struct sockaddr *) namebuf, &len);
+#if defined(OEMVS)
+    if (len == 0) {
+       /* Some platforms indicate zero length when an AF_UNIX client is
+        * not bound. Simulate a non-zero-length sockaddr structure in
+        * this case. */
+       namebuf[0] = 0;        /* sun_len */
+       namebuf[1] = AF_UNIX;  /* sun_family */
+       len = 2;
+    }
+#endif
+
     if (fd < 0)
        goto badexit;
     if (IoIFP(nstio))
@@ -2556,7 +2619,7 @@ badexit:
 PP(pp_shutdown)
 {
 #ifdef HAS_SOCKET
-    dSP; dTARGET;
+    dVAR; dSP; dTARGET;
     const int how = POPi;
     GV * const gv = (GV*)POPs;
     register IO * const io = GvIOn(gv);
@@ -2580,9 +2643,9 @@ nuts:
 PP(pp_ssockopt)
 {
 #ifdef HAS_SOCKET
-    dSP;
+    dVAR; dSP;
     const int optype = PL_op->op_type;
-    SV * const sv = (optype == OP_GSOCKOPT) ? sv_2mortal(NEWSV(22, 257)) : POPs;
+    SV * const sv = (optype == OP_GSOCKOPT) ? sv_2mortal(newSV(257)) : POPs;
     const unsigned int optname = (unsigned int) POPi;
     const unsigned int lvl = (unsigned int) POPi;
     GV * const gv = (GV*)POPs;
@@ -2657,7 +2720,7 @@ nuts2:
 PP(pp_getpeername)
 {
 #ifdef HAS_SOCKET
-    dSP;
+    dVAR; dSP;
     const int optype = PL_op->op_type;
     GV * const gv = (GV*)POPs;
     register IO * const io = GvIOn(gv);
@@ -2668,7 +2731,7 @@ PP(pp_getpeername)
     if (!io || !IoIFP(io))
        goto nuts;
 
-    sv = sv_2mortal(NEWSV(22, 257));
+    sv = sv_2mortal(newSV(257));
     (void)SvPOK_only(sv);
     len = 256;
     SvCUR_set(sv, len);
@@ -2722,8 +2785,10 @@ nuts2:
 
 PP(pp_stat)
 {
+    dVAR;
     dSP;
-    GV *gv;
+    GV *gv = NULL;
+    IO *io;
     I32 gimme;
     I32 max = 13;
 
@@ -2734,7 +2799,7 @@ PP(pp_stat)
            do_fstat_warning_check:
                if (ckWARN(WARN_IO))
                    Perl_warner(aTHX_ packWARN(WARN_IO),
-                       "lstat() on filehandle %s", GvENAME(gv));
+                       "lstat() on filehandle %s", gv ? GvENAME(gv) : "");
            } else if (PL_laststype != OP_LSTAT)
                Perl_croak(aTHX_ "The stat preceding lstat() wasn't an lstat");
        }
@@ -2744,9 +2809,23 @@ PP(pp_stat)
            PL_laststype = OP_STAT;
            PL_statgv = gv;
            sv_setpvn(PL_statname, "", 0);
-           PL_laststatval = (GvIO(gv) && IoIFP(GvIOp(gv))
-               ? PerlLIO_fstat(PerlIO_fileno(IoIFP(GvIOn(gv))), &PL_statcache) : -1);
-       }
+            if(gv) {
+                io = GvIO(gv);
+                do_fstat_have_io:
+                if (io) {
+                    if (IoIFP(io)) {
+                        PL_laststatval = 
+                            PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache);   
+                    } else if (IoDIRP(io)) {
+                        PL_laststatval =
+                            PerlLIO_fstat(my_dirfd(IoDIRP(io)), &PL_statcache);
+                    } else {
+                        PL_laststatval = -1;
+                    }
+               }
+            }
+        }
+
        if (PL_laststatval < 0) {
            if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
                report_evil_fh(gv, GvIO(gv), PL_op->op_type);
@@ -2758,15 +2837,20 @@ PP(pp_stat)
        if (SvTYPE(sv) == SVt_PVGV) {
            gv = (GV*)sv;
            goto do_fstat;
-       }
-       else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
-           gv = (GV*)SvRV(sv);
-           if (PL_op->op_type == OP_LSTAT)
-               goto do_fstat_warning_check;
-           goto do_fstat;
-       }
+       } else if(SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
+            gv = (GV*)SvRV(sv);
+            if (PL_op->op_type == OP_LSTAT)
+                goto do_fstat_warning_check;
+            goto do_fstat;
+        } else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVIO) { 
+            io = (IO*)SvRV(sv);
+            if (PL_op->op_type == OP_LSTAT)
+                goto do_fstat_warning_check;
+            goto do_fstat_have_io; 
+        }
+        
        sv_setpv(PL_statname, SvPV_nolen_const(sv));
-       PL_statgv = Nullgv;
+       PL_statgv = NULL;
        PL_laststype = PL_op->op_type;
        if (PL_op->op_type == OP_LSTAT)
            PL_laststatval = PerlLIO_lstat(SvPV_nolen_const(PL_statname), &PL_statcache);
@@ -2788,53 +2872,53 @@ PP(pp_stat)
     if (max) {
        EXTEND(SP, max);
        EXTEND_MORTAL(max);
-       PUSHs(sv_2mortal(newSViv(PL_statcache.st_dev)));
-       PUSHs(sv_2mortal(newSViv(PL_statcache.st_ino)));
-       PUSHs(sv_2mortal(newSVuv(PL_statcache.st_mode)));
-       PUSHs(sv_2mortal(newSVuv(PL_statcache.st_nlink)));
+       mPUSHi(PL_statcache.st_dev);
+       mPUSHi(PL_statcache.st_ino);
+       mPUSHu(PL_statcache.st_mode);
+       mPUSHu(PL_statcache.st_nlink);
 #if Uid_t_size > IVSIZE
-       PUSHs(sv_2mortal(newSVnv(PL_statcache.st_uid)));
+       mPUSHn(PL_statcache.st_uid);
 #else
 #   if Uid_t_sign <= 0
-       PUSHs(sv_2mortal(newSViv(PL_statcache.st_uid)));
+       mPUSHi(PL_statcache.st_uid);
 #   else
-       PUSHs(sv_2mortal(newSVuv(PL_statcache.st_uid)));
+       mPUSHu(PL_statcache.st_uid);
 #   endif
 #endif
 #if Gid_t_size > IVSIZE
-       PUSHs(sv_2mortal(newSVnv(PL_statcache.st_gid)));
+       mPUSHn(PL_statcache.st_gid);
 #else
 #   if Gid_t_sign <= 0
-       PUSHs(sv_2mortal(newSViv(PL_statcache.st_gid)));
+       mPUSHi(PL_statcache.st_gid);
 #   else
-       PUSHs(sv_2mortal(newSVuv(PL_statcache.st_gid)));
+       mPUSHu(PL_statcache.st_gid);
 #   endif
 #endif
 #ifdef USE_STAT_RDEV
-       PUSHs(sv_2mortal(newSViv(PL_statcache.st_rdev)));
+       mPUSHi(PL_statcache.st_rdev);
 #else
-       PUSHs(sv_2mortal(newSVpvs("")));
+       PUSHs(newSVpvs_flags("", SVs_TEMP));
 #endif
 #if Off_t_size > IVSIZE
-       PUSHs(sv_2mortal(newSVnv((NV)PL_statcache.st_size)));
+       mPUSHn(PL_statcache.st_size);
 #else
-       PUSHs(sv_2mortal(newSViv(PL_statcache.st_size)));
+       mPUSHi(PL_statcache.st_size);
 #endif
 #ifdef BIG_TIME
-       PUSHs(sv_2mortal(newSVnv(PL_statcache.st_atime)));
-       PUSHs(sv_2mortal(newSVnv(PL_statcache.st_mtime)));
-       PUSHs(sv_2mortal(newSVnv(PL_statcache.st_ctime)));
+       mPUSHn(PL_statcache.st_atime);
+       mPUSHn(PL_statcache.st_mtime);
+       mPUSHn(PL_statcache.st_ctime);
 #else
-       PUSHs(sv_2mortal(newSViv(PL_statcache.st_atime)));
-       PUSHs(sv_2mortal(newSViv(PL_statcache.st_mtime)));
-       PUSHs(sv_2mortal(newSViv(PL_statcache.st_ctime)));
+       mPUSHi(PL_statcache.st_atime);
+       mPUSHi(PL_statcache.st_mtime);
+       mPUSHi(PL_statcache.st_ctime);
 #endif
 #ifdef USE_STAT_BLOCKS
-       PUSHs(sv_2mortal(newSVuv(PL_statcache.st_blksize)));
-       PUSHs(sv_2mortal(newSVuv(PL_statcache.st_blocks)));
+       mPUSHu(PL_statcache.st_blksize);
+       mPUSHu(PL_statcache.st_blocks);
 #else
-       PUSHs(sv_2mortal(newSVpvs("")));
-       PUSHs(sv_2mortal(newSVpvs("")));
+       PUSHs(newSVpvs_flags("", SVs_TEMP));
+       PUSHs(newSVpvs_flags("", SVs_TEMP));
 #endif
     }
     RETURN;
@@ -2851,6 +2935,7 @@ PP(pp_stat)
 
 PP(pp_ftrread)
 {
+    dVAR;
     I32 result;
     /* Not const, because things tweak this below. Not bool, because there's
        no guarantee that OPp_FT_ACCESS is <= CHAR_MAX  */
@@ -2913,10 +2998,9 @@ PP(pp_ftrread)
        effective = TRUE;
        break;
 
-
     case OP_FTEEXEC:
 #ifdef PERL_EFF_ACCESS
-       access_mode = W_OK;
+       access_mode = X_OK;
 #else
        use_access = 0;
 #endif
@@ -2927,7 +3011,7 @@ PP(pp_ftrread)
 
     if (use_access) {
 #if defined(HAS_ACCESS) || defined (PERL_EFF_ACCESS)
-       const char *const name = POPpx;
+       const char *name = POPpx;
        if (effective) {
 #  ifdef PERL_EFF_ACCESS
            result = PERL_EFF_ACCESS(name, access_mode);
@@ -2962,6 +3046,7 @@ PP(pp_ftrread)
 
 PP(pp_ftis)
 {
+    dVAR;
     I32 result;
     const int op_type = PL_op->op_type;
     dSP;
@@ -3000,6 +3085,7 @@ PP(pp_ftis)
 
 PP(pp_ftrowned)
 {
+    dVAR;
     I32 result;
     dSP;
 
@@ -3084,6 +3170,7 @@ PP(pp_ftrowned)
 
 PP(pp_ftlink)
 {
+    dVAR;
     I32 result = my_lstat();
     dSP;
     if (result < 0)
@@ -3095,10 +3182,11 @@ PP(pp_ftlink)
 
 PP(pp_fttty)
 {
+    dVAR;
     dSP;
     int fd;
     GV *gv;
-    SV *tmpsv = Nullsv;
+    SV *tmpsv = NULL;
 
     STACKED_FTEST_CHECK;
 
@@ -3137,6 +3225,7 @@ PP(pp_fttty)
 
 PP(pp_fttext)
 {
+    dVAR;
     dSP;
     I32 i;
     I32 len;
@@ -3157,7 +3246,7 @@ PP(pp_fttext)
     else if (SvROK(TOPs) && isGV(SvRV(TOPs)))
        gv = (GV*)SvRV(POPs);
     else
-       gv = Nullgv;
+       gv = NULL;
 
     if (gv) {
        EXTEND(SP, 1);
@@ -3212,7 +3301,7 @@ PP(pp_fttext)
     else {
        sv = POPs;
       really_filename:
-       PL_statgv = Nullgv;
+       PL_statgv = NULL;
        PL_laststype = OP_STAT;
        sv_setpv(PL_statname, SvPV_nolen_const(sv));
        if (!(fp = PerlIO_open(SvPVX_const(PL_statname), "r"))) {
@@ -3226,7 +3315,7 @@ PP(pp_fttext)
            (void)PerlIO_close(fp);
            RETPUSHUNDEF;
        }
-       PerlIO_binmode(aTHX_ fp, '<', O_BINARY, Nullch);
+       PerlIO_binmode(aTHX_ fp, '<', O_BINARY, NULL);
        len = PerlIO_read(fp, tbuf, sizeof(tbuf));
        (void)PerlIO_close(fp);
        if (len <= 0) {
@@ -3242,7 +3331,7 @@ PP(pp_fttext)
 
 #if defined(DOSISH) || defined(USEMYBINMODE)
     /* ignore trailing ^Z on short files */
-    if (len && len < sizeof(tbuf) && tbuf[len-1] == 26)
+    if (len && len < (I32)sizeof(tbuf) && tbuf[len-1] == 26)
        --len;
 #endif
 
@@ -3295,20 +3384,23 @@ PP(pp_fttext)
 
 PP(pp_chdir)
 {
-    dSP; dTARGET;
+    dVAR; dSP; dTARGET;
     const char *tmps = NULL;
     GV *gv = NULL;
 
     if( MAXARG == 1 ) {
        SV * const sv = POPs;
-        if (SvTYPE(sv) == SVt_PVGV) {
+       if (PL_op->op_flags & OPf_SPECIAL) {
+           gv = gv_fetchsv(sv, 0, SVt_PVIO);
+       }
+        else if (SvTYPE(sv) == SVt_PVGV) {
            gv = (GV*)sv;
         }
        else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
             gv = (GV*)SvRV(sv);
         }
         else {
-           tmps = SvPVx_nolen_const(sv);
+           tmps = SvPV_nolen_const(sv);
        }
     }
 
@@ -3316,10 +3408,10 @@ PP(pp_chdir)
        HV * const table = GvHVn(PL_envgv);
        SV **svp;
 
-        if (    (svp = hv_fetch(table, "HOME", 4, FALSE))
-             || (svp = hv_fetch(table, "LOGDIR", 6, FALSE))
+        if (    (svp = hv_fetchs(table, "HOME", FALSE))
+             || (svp = hv_fetchs(table, "LOGDIR", FALSE))
 #ifdef VMS
-             || (svp = hv_fetch(table, "SYS$LOGIN", 9, FALSE))
+             || (svp = hv_fetchs(table, "SYS$LOGIN", FALSE))
 #endif
            )
         {
@@ -3339,21 +3431,22 @@ PP(pp_chdir)
 #ifdef HAS_FCHDIR
        IO* const io = GvIO(gv);
        if (io) {
-           if (IoIFP(io)) {
-               PUSHi(fchdir(PerlIO_fileno(IoIFP(io))) >= 0);
-           }
-           else if (IoDIRP(io)) {
-#ifdef HAS_DIRFD
-               PUSHi(fchdir(dirfd(IoDIRP(io))) >= 0);
-#else
-               DIE(aTHX_ PL_no_func, "dirfd");
-#endif
+           if (IoDIRP(io)) {
+               PUSHi(fchdir(my_dirfd(IoDIRP(io))) >= 0);
+           } else if (IoIFP(io)) {
+                PUSHi(fchdir(PerlIO_fileno(IoIFP(io))) >= 0);
            }
            else {
+               if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
+                   report_evil_fh(gv, io, PL_op->op_type);
+               SETERRNO(EBADF, RMS_IFI);
                PUSHi(0);
            }
         }
        else {
+           if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
+               report_evil_fh(gv, io, PL_op->op_type);
+           SETERRNO(EBADF,RMS_IFI);
            PUSHi(0);
        }
 #else
@@ -3372,7 +3465,7 @@ PP(pp_chdir)
 
 PP(pp_chown)
 {
-    dSP; dMARK; dTARGET;
+    dVAR; dSP; dMARK; dTARGET;
     const I32 value = (I32)apply(PL_op->op_type, MARK, SP);
 
     SP = MARK;
@@ -3383,7 +3476,7 @@ PP(pp_chown)
 PP(pp_chroot)
 {
 #ifdef HAS_CHROOT
-    dSP; dTARGET;
+    dVAR; dSP; dTARGET;
     char * const tmps = POPpx;
     TAINT_PROPER("chroot");
     PUSHi( chroot(tmps) >= 0 );
@@ -3395,7 +3488,7 @@ PP(pp_chroot)
 
 PP(pp_rename)
 {
-    dSP; dTARGET;
+    dVAR; dSP; dTARGET;
     int anum;
     const char * const tmps2 = POPpconstx;
     const char * const tmps = SvPV_nolen_const(TOPs);
@@ -3421,7 +3514,7 @@ PP(pp_rename)
 #if defined(HAS_LINK) || defined(HAS_SYMLINK)
 PP(pp_link)
 {
-    dSP; dTARGET;
+    dVAR; dSP; dTARGET;
     const int op_type = PL_op->op_type;
     int result;
 
@@ -3469,6 +3562,7 @@ PP(pp_link)
 
 PP(pp_readlink)
 {
+    dVAR;
     dSP;
 #ifdef HAS_SYMLINK
     dTARGET;
@@ -3501,15 +3595,17 @@ S_dooneliner(pTHX_ const char *cmd, const char *filename)
     char *s;
     PerlIO *myfp;
     int anum = 1;
+    Size_t size = strlen(cmd) + (strlen(filename) * 2) + 10;
 
-    Newx(cmdline, strlen(cmd) + (strlen(filename) * 2) + 10, char);
-    strcpy(cmdline, cmd);
-    strcat(cmdline, " ");
+    Newx(cmdline, size, char);
+    my_strlcpy(cmdline, cmd, size);
+    my_strlcat(cmdline, " ", size);
     for (s = cmdline + strlen(cmdline); *filename; ) {
        *s++ = '\\';
        *s++ = *filename++;
     }
-    strcpy(s, " 2>&1");
+    if (s - cmdline < size)
+       my_strlcpy(s, " 2>&1", size - (s - cmdline));
     myfp = PerlProc_popen(cmdline, "r");
     Safefree(cmdline);
 
@@ -3518,7 +3614,7 @@ S_dooneliner(pTHX_ const char *cmd, const char *filename)
        /* Need to save/restore 'PL_rs' ?? */
        s = sv_gets(tmpsv, myfp, 0);
        (void)PerlProc_pclose(myfp);
-       if (s != Nullch) {
+       if (s != NULL) {
            int e;
            for (e = 1;
 #ifdef HAS_SYS_ERRLIST
@@ -3527,7 +3623,7 @@ S_dooneliner(pTHX_ const char *cmd, const char *filename)
                 ; e++)
            {
                /* you don't see this */
-               char *errmsg =
+               const char * const errmsg =
 #ifdef HAS_SYS_ERRLIST
                    sys_errlist[e]
 #else
@@ -3601,7 +3697,7 @@ S_dooneliner(pTHX_ const char *cmd, const char *filename)
 
 PP(pp_mkdir)
 {
-    dSP; dTARGET;
+    dVAR; dSP; dTARGET;
     STRLEN len;
     const char *tmps;
     bool copy = FALSE;
@@ -3628,7 +3724,7 @@ PP(pp_mkdir)
 
 PP(pp_rmdir)
 {
-    dSP; dTARGET;
+    dVAR; dSP; dTARGET;
     STRLEN len;
     const char *tmps;
     bool copy = FALSE;
@@ -3650,7 +3746,7 @@ PP(pp_rmdir)
 PP(pp_open_dir)
 {
 #if defined(Direntry_t) && defined(HAS_READDIR)
-    dSP;
+    dVAR; dSP;
     const char * const dirname = POPpconstx;
     GV * const gv = (GV*)POPs;
     register IO * const io = GvIOn(gv);
@@ -3658,6 +3754,9 @@ PP(pp_open_dir)
     if (!io)
        goto nope;
 
+    if ((IoIFP(io) || IoOFP(io)) && ckWARN2(WARN_IO, WARN_DEPRECATED))
+       Perl_warner(aTHX_ packWARN2(WARN_IO, WARN_DEPRECATED),
+               "Opening filehandle %s also as a directory", GvENAME(gv));
     if (IoDIRP(io))
        PerlDir_close(IoDIRP(io));
     if (!(IoDIRP(io) = PerlDir_open(dirname)))
@@ -3681,6 +3780,7 @@ PP(pp_readdir)
 #if !defined(I_DIRENT) && !defined(VMS)
     Direntry_t *readdir (DIR *);
 #endif
+    dVAR;
     dSP;
 
     SV *sv;
@@ -3710,9 +3810,8 @@ PP(pp_readdir)
         if (!(IoFLAGS(io) & IOf_UNTAINT))
             SvTAINTED_on(sv);
 #endif
-        XPUSHs(sv_2mortal(sv));
-    }
-    while (gimme == G_ARRAY);
+        mXPUSHs(sv);
+    } while (gimme == G_ARRAY);
 
     if (!dp && gimme != G_ARRAY)
         goto nope;
@@ -3765,7 +3864,7 @@ nope:
 PP(pp_seekdir)
 {
 #if defined(HAS_SEEKDIR) || defined(seekdir)
-    dSP;
+    dVAR; dSP;
     const long along = POPl;
     GV * const gv = (GV*)POPs;
     register IO * const io = GvIOn(gv);
@@ -3792,7 +3891,7 @@ nope:
 PP(pp_rewinddir)
 {
 #if defined(HAS_REWINDDIR) || defined(rewinddir)
-    dSP;
+    dVAR; dSP;
     GV * const gv = (GV*)POPs;
     register IO * const io = GvIOn(gv);
 
@@ -3817,7 +3916,7 @@ nope:
 PP(pp_closedir)
 {
 #if defined(Direntry_t) && defined(HAS_READDIR)
-    dSP;
+    dVAR; dSP;
     GV * const gv = (GV*)POPs;
     register IO * const io = GvIOn(gv);
 
@@ -3853,7 +3952,7 @@ nope:
 PP(pp_fork)
 {
 #ifdef HAS_FORK
-    dSP; dTARGET;
+    dVAR; dSP; dTARGET;
     Pid_t childpid;
 
     EXTEND(SP, 1);
@@ -3862,7 +3961,7 @@ PP(pp_fork)
     if (childpid < 0)
        RETSETUNDEF;
     if (!childpid) {
-       GV * const tmpgv = gv_fetchpv("$", GV_ADD, SVt_PV);
+       GV * const tmpgv = gv_fetchpvs("$", GV_ADD|GV_NOTQUAL, SVt_PV);
        if (tmpgv) {
             SvREADONLY_off(GvSV(tmpgv));
            sv_setiv(GvSV(tmpgv), (IV)PerlProc_getpid());
@@ -3897,8 +3996,8 @@ PP(pp_fork)
 
 PP(pp_wait)
 {
-#if (!defined(DOSISH) || defined(OS2) || defined(WIN32)) && !defined(MACOS_TRADITIONAL)
-    dSP; dTARGET;
+#if (!defined(DOSISH) || defined(OS2) || defined(WIN32)) && !defined(MACOS_TRADITIONAL) && !defined(__LIBCATAMOUNT__)
+    dVAR; dSP; dTARGET;
     Pid_t childpid;
     int argflags;
 
@@ -3925,8 +4024,8 @@ PP(pp_wait)
 
 PP(pp_waitpid)
 {
-#if (!defined(DOSISH) || defined(OS2) || defined(WIN32)) && !defined(MACOS_TRADITIONAL)
-    dSP; dTARGET;
+#if (!defined(DOSISH) || defined(OS2) || defined(WIN32)) && !defined(MACOS_TRADITIONAL) && !defined(__LIBCATAMOUNT__)
+    dVAR; dSP; dTARGET;
     const int optype = POPi;
     const Pid_t pid = TOPi;
     Pid_t result;
@@ -3955,7 +4054,12 @@ PP(pp_waitpid)
 
 PP(pp_system)
 {
-    dSP; dMARK; dORIGMARK; dTARGET;
+    dVAR; dSP; dMARK; dORIGMARK; dTARGET;
+#if defined(__LIBCATAMOUNT__)
+    PL_statusvalue = -1;
+    SP = ORIGMARK;
+    XPUSHi(-1);
+#else
     I32 value;
     int result;
 
@@ -4013,7 +4117,8 @@ PP(pp_system)
            SP = ORIGMARK;
            if (did_pipes) {
                int errkid;
-               int n = 0, n1;
+               unsigned n = 0;
+               SSize_t n1;
 
                while (n < sizeof(int)) {
                    n1 = PerlLIO_read(pp[0],
@@ -4045,7 +4150,7 @@ PP(pp_system)
            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);
+           value = (I32)do_aexec5(NULL, MARK, SP, pp[1], did_pipes);
        else {
            value = (I32)do_exec3(SvPVx_nolen(sv_mortalcopy(*SP)), pp[1], did_pipes);
        }
@@ -4064,9 +4169,9 @@ PP(pp_system)
     }
     else if (SP - MARK != 1) {
 #  if defined(WIN32) || defined(OS2) || defined(__SYMBIAN32__)
-       value = (I32)do_aspawn(Nullsv, MARK, SP);
+       value = (I32)do_aspawn(NULL, MARK, SP);
 #  else
-       value = (I32)do_aspawn(Nullsv, (void **)MARK, (void **)SP);
+       value = (I32)do_aspawn(NULL, (void **)MARK, (void **)SP);
 #  endif
     }
     else {
@@ -4078,13 +4183,14 @@ PP(pp_system)
     do_execfree();
     SP = ORIGMARK;
     XPUSHi(result ? value : STATUS_CURRENT);
-#endif /* !FORK or VMS */
+#endif /* !FORK or VMS or OS/2 */
+#endif
     RETURN;
 }
 
 PP(pp_exec)
 {
-    dSP; dMARK; dORIGMARK; dTARGET;
+    dVAR; dSP; dMARK; dORIGMARK; dTARGET;
     I32 value;
 
     if (PL_tainting) {
@@ -4104,15 +4210,15 @@ PP(pp_exec)
     }
     else if (SP - MARK != 1)
 #ifdef VMS
-       value = (I32)vms_do_aexec(Nullsv, MARK, SP);
+       value = (I32)vms_do_aexec(NULL, MARK, SP);
 #else
 #  ifdef __OPEN_VM
        {
-          (void ) do_aspawn(Nullsv, MARK, SP);
+          (void ) do_aspawn(NULL, MARK, SP);
           value = 0;
        }
 #  else
-       value = (I32)do_aexec(Nullsv, MARK, SP);
+       value = (I32)do_aexec(NULL, MARK, SP);
 #  endif
 #endif
     else {
@@ -4136,7 +4242,7 @@ PP(pp_exec)
 PP(pp_getppid)
 {
 #ifdef HAS_GETPPID
-    dSP; dTARGET;
+    dVAR; dSP; dTARGET;
 #   ifdef THREADS_HAVE_PIDS
     if (PL_ppid != 1 && getppid() == 1)
        /* maybe the parent process has died. Refresh ppid cache */
@@ -4154,7 +4260,7 @@ PP(pp_getppid)
 PP(pp_getpgrp)
 {
 #ifdef HAS_GETPGRP
-    dSP; dTARGET;
+    dVAR; dSP; dTARGET;
     Pid_t pgrp;
     const Pid_t pid = (MAXARG < 1) ? 0 : SvIVx(POPs);
 
@@ -4175,7 +4281,7 @@ PP(pp_getpgrp)
 PP(pp_setpgrp)
 {
 #ifdef HAS_SETPGRP
-    dSP; dTARGET;
+    dVAR; dSP; dTARGET;
     Pid_t pgrp;
     Pid_t pid;
     if (MAXARG < 2) {
@@ -4207,7 +4313,7 @@ PP(pp_setpgrp)
 PP(pp_getpriority)
 {
 #ifdef HAS_GETPRIORITY
-    dSP; dTARGET;
+    dVAR; dSP; dTARGET;
     const int who = POPi;
     const int which = TOPi;
     SETi( getpriority(which, who) );
@@ -4220,7 +4326,7 @@ PP(pp_getpriority)
 PP(pp_setpriority)
 {
 #ifdef HAS_SETPRIORITY
-    dSP; dTARGET;
+    dVAR; dSP; dTARGET;
     const int niceval = POPi;
     const int who = POPi;
     const int which = TOPi;
@@ -4236,11 +4342,11 @@ PP(pp_setpriority)
 
 PP(pp_time)
 {
-    dSP; dTARGET;
+    dVAR; dSP; dTARGET;
 #ifdef BIG_TIME
-    XPUSHn( time(Null(Time_t*)) );
+    XPUSHn( time(NULL) );
 #else
-    XPUSHi( time(Null(Time_t*)) );
+    XPUSHi( time(NULL) );
 #endif
     RETURN;
 }
@@ -4248,6 +4354,7 @@ PP(pp_time)
 PP(pp_tms)
 {
 #ifdef HAS_TIMES
+    dVAR;
     dSP;
     EXTEND(SP, 4);
 #ifndef VMS
@@ -4258,22 +4365,22 @@ PP(pp_tms)
                                                    /* is returned.                   */
 #endif
 
-    PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_utime)/(NV)PL_clocktick)));
+    mPUSHn(((NV)PL_timesbuf.tms_utime)/(NV)PL_clocktick);
     if (GIMME == G_ARRAY) {
-       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)));
+       mPUSHn(((NV)PL_timesbuf.tms_stime)/(NV)PL_clocktick);
+       mPUSHn(((NV)PL_timesbuf.tms_cutime)/(NV)PL_clocktick);
+       mPUSHn(((NV)PL_timesbuf.tms_cstime)/(NV)PL_clocktick);
     }
     RETURN;
 #else
 #   ifdef PERL_MICRO
     dSP;
-    PUSHs(sv_2mortal(newSVnv((NV)0.0)));
+    mPUSHn(0.0);
     EXTEND(SP, 4);
     if (GIMME == G_ARRAY) {
-        PUSHs(sv_2mortal(newSVnv((NV)0.0)));
-        PUSHs(sv_2mortal(newSVnv((NV)0.0)));
-        PUSHs(sv_2mortal(newSVnv((NV)0.0)));
+        mPUSHn(0.0);
+        mPUSHn(0.0);
+        mPUSHn(0.0);
     }
     RETURN;
 #   else
@@ -4324,6 +4431,7 @@ static struct tm *S_my_localtime (pTHX_ Time_t *tp)
 
 PP(pp_gmtime)
 {
+    dVAR;
     dSP;
     Time_t when;
     const struct tm *tmbuf;
@@ -4365,20 +4473,20 @@ PP(pp_gmtime)
                            tmbuf->tm_min,
                            tmbuf->tm_sec,
                            tmbuf->tm_year + 1900);
-       PUSHs(sv_2mortal(tsv));
+       mPUSHs(tsv);
     }
     else if (tmbuf) {
         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)));
-       PUSHs(sv_2mortal(newSViv(tmbuf->tm_mon)));
-       PUSHs(sv_2mortal(newSViv(tmbuf->tm_year)));
-       PUSHs(sv_2mortal(newSViv(tmbuf->tm_wday)));
-       PUSHs(sv_2mortal(newSViv(tmbuf->tm_yday)));
-       PUSHs(sv_2mortal(newSViv(tmbuf->tm_isdst)));
+        mPUSHi(tmbuf->tm_sec);
+       mPUSHi(tmbuf->tm_min);
+       mPUSHi(tmbuf->tm_hour);
+       mPUSHi(tmbuf->tm_mday);
+       mPUSHi(tmbuf->tm_mon);
+       mPUSHi(tmbuf->tm_year);
+       mPUSHi(tmbuf->tm_wday);
+       mPUSHi(tmbuf->tm_yday);
+       mPUSHi(tmbuf->tm_isdst);
     }
     RETURN;
 }
@@ -4386,7 +4494,7 @@ PP(pp_gmtime)
 PP(pp_alarm)
 {
 #ifdef HAS_ALARM
-    dSP; dTARGET;
+    dVAR; dSP; dTARGET;
     int anum;
     anum = POPi;
     anum = alarm((unsigned int)anum);
@@ -4402,7 +4510,7 @@ PP(pp_alarm)
 
 PP(pp_sleep)
 {
-    dSP; dTARGET;
+    dVAR; dSP; dTARGET;
     I32 duration;
     Time_t lasttime;
     Time_t when;
@@ -4425,7 +4533,7 @@ PP(pp_sleep)
 PP(pp_shmwrite)
 {
 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
-    dSP; dMARK; dTARGET;
+    dVAR; dSP; dMARK; dTARGET;
     const int op_type = PL_op->op_type;
     I32 value;
 
@@ -4457,7 +4565,7 @@ PP(pp_shmwrite)
 PP(pp_semget)
 {
 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
-    dSP; dMARK; dTARGET;
+    dVAR; dSP; dMARK; dTARGET;
     const int anum = do_ipcget(PL_op->op_type, MARK, SP);
     SP = MARK;
     if (anum == -1)
@@ -4472,7 +4580,7 @@ PP(pp_semget)
 PP(pp_semctl)
 {
 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
-    dSP; dMARK; dTARGET;
+    dVAR; dSP; dMARK; dTARGET;
     const int anum = do_ipcctl(PL_op->op_type, MARK, SP);
     SP = MARK;
     if (anum == -1)
@@ -4489,12 +4597,33 @@ PP(pp_semctl)
 #endif
 }
 
+/* I can't const this further without getting warnings about the types of
+   various arrays passed in from structures.  */
+static SV *
+S_space_join_names_mortal(pTHX_ char *const *array)
+{
+    SV *target;
+
+    if (array && *array) {
+       target = newSVpvs_flags("", SVs_TEMP);
+       while (1) {
+           sv_catpv(target, *array);
+           if (!*++array)
+               break;
+           sv_catpvs(target, " ");
+       }
+    } else {
+       target = sv_mortalcopy(&PL_sv_no);
+    }
+    return target;
+}
+
 /* Get system info. */
 
 PP(pp_ghostent)
 {
 #if defined(HAS_GETHOSTBYNAME) || defined(HAS_GETHOSTBYADDR) || defined(HAS_GETHOSTENT)
-    dSP;
+    dVAR; dSP;
     I32 which = PL_op->op_type;
     register char **elem;
     register SV *sv;
@@ -4520,7 +4649,7 @@ PP(pp_ghostent)
        const int addrtype = POPi;
        SV * const addrsv = POPs;
        STRLEN addrlen;
-       Netdb_host_t addr = (Netdb_host_t) SvPVbyte(addrsv, addrlen);
+       const char *addr = (char *)SvPVbyte(addrsv, addrlen);
 
        hent = PerlSock_gethostbyaddr(addr, (Netdb_hlen_t) addrlen, addrtype);
 #else
@@ -4559,28 +4688,20 @@ PP(pp_ghostent)
     }
 
     if (hent) {
-       PUSHs(sv = sv_mortalcopy(&PL_sv_no));
-       sv_setpv(sv, (char*)hent->h_name);
-       PUSHs(sv = sv_mortalcopy(&PL_sv_no));
-       for (elem = hent->h_aliases; elem && *elem; elem++) {
-           sv_catpv(sv, *elem);
-           if (elem[1])
-               sv_catpvs(sv, " ");
-       }
-       PUSHs(sv = sv_mortalcopy(&PL_sv_no));
-       sv_setiv(sv, (IV)hent->h_addrtype);
-       PUSHs(sv = sv_mortalcopy(&PL_sv_no));
+       mPUSHs(newSVpv((char*)hent->h_name, 0));
+       PUSHs(space_join_names_mortal(hent->h_aliases));
+       mPUSHi(hent->h_addrtype);
        len = hent->h_length;
-       sv_setiv(sv, (IV)len);
+       mPUSHi(len);
 #ifdef h_addr
        for (elem = hent->h_addr_list; elem && *elem; elem++) {
-           XPUSHs(sv = sv_mortalcopy(&PL_sv_no));
-           sv_setpvn(sv, *elem, len);
+           mXPUSHp(*elem, len);
        }
 #else
-       PUSHs(sv = sv_mortalcopy(&PL_sv_no));
        if (hent->h_addr)
-           sv_setpvn(sv, hent->h_addr, len);
+           mPUSHp(hent->h_addr, len);
+       else
+           PUSHs(sv_mortalcopy(&PL_sv_no));
 #endif /* h_addr */
     }
     RETURN;
@@ -4592,9 +4713,8 @@ PP(pp_ghostent)
 PP(pp_gnetent)
 {
 #if defined(HAS_GETNETBYNAME) || defined(HAS_GETNETBYADDR) || defined(HAS_GETNETENT)
-    dSP;
+    dVAR; 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 *getnetbyaddr(Netdb_net_t, int);
@@ -4651,18 +4771,10 @@ PP(pp_gnetent)
     }
 
     if (nent) {
-       PUSHs(sv = sv_mortalcopy(&PL_sv_no));
-       sv_setpv(sv, nent->n_name);
-       PUSHs(sv = sv_mortalcopy(&PL_sv_no));
-       for (elem = nent->n_aliases; elem && *elem; elem++) {
-           sv_catpv(sv, *elem);
-           if (elem[1])
-               sv_catpvs(sv, " ");
-       }
-       PUSHs(sv = sv_mortalcopy(&PL_sv_no));
-       sv_setiv(sv, (IV)nent->n_addrtype);
-       PUSHs(sv = sv_mortalcopy(&PL_sv_no));
-       sv_setiv(sv, (IV)nent->n_net);
+       mPUSHs(newSVpv(nent->n_name, 0));
+       PUSHs(space_join_names_mortal(nent->n_aliases));
+       mPUSHi(nent->n_addrtype);
+       mPUSHi(nent->n_net);
     }
 
     RETURN;
@@ -4674,9 +4786,8 @@ PP(pp_gnetent)
 PP(pp_gprotoent)
 {
 #if defined(HAS_GETPROTOBYNAME) || defined(HAS_GETPROTOBYNUMBER) || defined(HAS_GETPROTOENT)
-    dSP;
+    dVAR; 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 *getprotobyname(Netdb_name_t);
@@ -4721,16 +4832,9 @@ PP(pp_gprotoent)
     }
 
     if (pent) {
-       PUSHs(sv = sv_mortalcopy(&PL_sv_no));
-       sv_setpv(sv, pent->p_name);
-       PUSHs(sv = sv_mortalcopy(&PL_sv_no));
-       for (elem = pent->p_aliases; elem && *elem; elem++) {
-           sv_catpv(sv, *elem);
-           if (elem[1])
-               sv_catpvs(sv, " ");
-       }
-       PUSHs(sv = sv_mortalcopy(&PL_sv_no));
-       sv_setiv(sv, (IV)pent->p_proto);
+       mPUSHs(newSVpv(pent->p_name, 0));
+       PUSHs(space_join_names_mortal(pent->p_aliases));
+       mPUSHi(pent->p_proto);
     }
 
     RETURN;
@@ -4742,9 +4846,8 @@ PP(pp_gprotoent)
 PP(pp_gservent)
 {
 #if defined(HAS_GETSERVBYNAME) || defined(HAS_GETSERVBYPORT) || defined(HAS_GETSERVENT)
-    dSP;
+    dVAR; 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 *getservbyname(Netdb_name_t, Netdb_name_t);
@@ -4757,7 +4860,7 @@ PP(pp_gservent)
 #ifdef HAS_GETSERVBYNAME
        const char * const proto = POPpbytex;
        const char * const name = POPpbytex;
-       sent = PerlSock_getservbyname(name, (proto && !*proto) ? Nullch : proto);
+       sent = PerlSock_getservbyname(name, (proto && !*proto) ? NULL : proto);
 #else
        DIE(aTHX_ PL_no_sock_func, "getservbyname");
 #endif
@@ -4769,7 +4872,7 @@ PP(pp_gservent)
 #ifdef HAS_HTONS
        port = PerlSock_htons(port);
 #endif
-       sent = PerlSock_getservbyport(port, (proto && !*proto) ? Nullch : proto);
+       sent = PerlSock_getservbyport(port, (proto && !*proto) ? NULL : proto);
 #else
        DIE(aTHX_ PL_no_sock_func, "getservbyport");
 #endif
@@ -4799,22 +4902,14 @@ PP(pp_gservent)
     }
 
     if (sent) {
-       PUSHs(sv = sv_mortalcopy(&PL_sv_no));
-       sv_setpv(sv, sent->s_name);
-       PUSHs(sv = sv_mortalcopy(&PL_sv_no));
-       for (elem = sent->s_aliases; elem && *elem; elem++) {
-           sv_catpv(sv, *elem);
-           if (elem[1])
-               sv_catpvs(sv, " ");
-       }
-       PUSHs(sv = sv_mortalcopy(&PL_sv_no));
+       mPUSHs(newSVpv(sent->s_name, 0));
+       PUSHs(space_join_names_mortal(sent->s_aliases));
 #ifdef HAS_NTOHS
-       sv_setiv(sv, (IV)PerlSock_ntohs(sent->s_port));
+       mPUSHi(PerlSock_ntohs(sent->s_port));
 #else
-       sv_setiv(sv, (IV)(sent->s_port));
+       mPUSHi(sent->s_port);
 #endif
-       PUSHs(sv = sv_mortalcopy(&PL_sv_no));
-       sv_setpv(sv, sent->s_proto);
+       mPUSHs(newSVpv(sent->s_proto, 0));
     }
 
     RETURN;
@@ -4826,7 +4921,7 @@ PP(pp_gservent)
 PP(pp_shostent)
 {
 #ifdef HAS_SETHOSTENT
-    dSP;
+    dVAR; dSP;
     PerlSock_sethostent(TOPi);
     RETSETYES;
 #else
@@ -4837,7 +4932,7 @@ PP(pp_shostent)
 PP(pp_snetent)
 {
 #ifdef HAS_SETNETENT
-    dSP;
+    dVAR; dSP;
     PerlSock_setnetent(TOPi);
     RETSETYES;
 #else
@@ -4848,7 +4943,7 @@ PP(pp_snetent)
 PP(pp_sprotoent)
 {
 #ifdef HAS_SETPROTOENT
-    dSP;
+    dVAR; dSP;
     PerlSock_setprotoent(TOPi);
     RETSETYES;
 #else
@@ -4859,7 +4954,7 @@ PP(pp_sprotoent)
 PP(pp_sservent)
 {
 #ifdef HAS_SETSERVENT
-    dSP;
+    dVAR; dSP;
     PerlSock_setservent(TOPi);
     RETSETYES;
 #else
@@ -4870,7 +4965,7 @@ PP(pp_sservent)
 PP(pp_ehostent)
 {
 #ifdef HAS_ENDHOSTENT
-    dSP;
+    dVAR; dSP;
     PerlSock_endhostent();
     EXTEND(SP,1);
     RETPUSHYES;
@@ -4882,7 +4977,7 @@ PP(pp_ehostent)
 PP(pp_enetent)
 {
 #ifdef HAS_ENDNETENT
-    dSP;
+    dVAR; dSP;
     PerlSock_endnetent();
     EXTEND(SP,1);
     RETPUSHYES;
@@ -4894,7 +4989,7 @@ PP(pp_enetent)
 PP(pp_eprotoent)
 {
 #ifdef HAS_ENDPROTOENT
-    dSP;
+    dVAR; dSP;
     PerlSock_endprotoent();
     EXTEND(SP,1);
     RETPUSHYES;
@@ -4906,7 +5001,7 @@ PP(pp_eprotoent)
 PP(pp_eservent)
 {
 #ifdef HAS_ENDSERVENT
-    dSP;
+    dVAR; dSP;
     PerlSock_endservent();
     EXTEND(SP,1);
     RETPUSHYES;
@@ -4918,7 +5013,7 @@ PP(pp_eservent)
 PP(pp_gpwent)
 {
 #ifdef HAS_PASSWD
-    dSP;
+    dVAR; dSP;
     I32 which = PL_op->op_type;
     register SV *sv;
     struct passwd *pwent  = NULL;
@@ -5030,11 +5125,10 @@ PP(pp_gpwent)
     }
 
     if (pwent) {
-       PUSHs(sv = sv_mortalcopy(&PL_sv_no));
-       sv_setpv(sv, pwent->pw_name);
+       mPUSHs(newSVpv(pwent->pw_name, 0));
 
-       PUSHs(sv = sv_mortalcopy(&PL_sv_no));
-       SvPOK_off(sv);
+       sv = newSViv(0);
+       mPUSHs(sv);
        /* If we have getspnam(), we try to dig up the shadow
         * password.  If we are underprivileged, the shadow
         * interface will set the errno to EACCES or similar,
@@ -5077,70 +5171,70 @@ PP(pp_gpwent)
        SvTAINTED_on(sv);
 #   endif
 
-       PUSHs(sv = sv_mortalcopy(&PL_sv_no));
 #   if Uid_t_sign <= 0
-       sv_setiv(sv, (IV)pwent->pw_uid);
+       mPUSHi(pwent->pw_uid);
 #   else
-       sv_setuv(sv, (UV)pwent->pw_uid);
+       mPUSHu(pwent->pw_uid);
 #   endif
 
-       PUSHs(sv = sv_mortalcopy(&PL_sv_no));
 #   if Uid_t_sign <= 0
-       sv_setiv(sv, (IV)pwent->pw_gid);
+       mPUSHi(pwent->pw_gid);
 #   else
-       sv_setuv(sv, (UV)pwent->pw_gid);
+       mPUSHu(pwent->pw_gid);
 #   endif
        /* pw_change, pw_quota, and pw_age are mutually exclusive--
         * because of the poor interface of the Perl getpw*(),
         * not because there's some standard/convention saying so.
         * A better interface would have been to return a hash,
         * but we are accursed by our history, alas. --jhi.  */
-       PUSHs(sv = sv_mortalcopy(&PL_sv_no));
 #   ifdef PWCHANGE
-       sv_setiv(sv, (IV)pwent->pw_change);
+       mPUSHi(pwent->pw_change);
 #   else
 #       ifdef PWQUOTA
-       sv_setiv(sv, (IV)pwent->pw_quota);
+       mPUSHi(pwent->pw_quota);
 #       else
 #           ifdef PWAGE
-       sv_setpv(sv, pwent->pw_age);
+       mPUSHs(newSVpv(pwent->pw_age, 0));
+#          else
+       /* I think that you can never get this compiled, but just in case.  */
+       PUSHs(sv_mortalcopy(&PL_sv_no));
 #           endif
 #       endif
 #   endif
 
        /* pw_class and pw_comment are mutually exclusive--.
         * see the above note for pw_change, pw_quota, and pw_age. */
-       PUSHs(sv = sv_mortalcopy(&PL_sv_no));
 #   ifdef PWCLASS
-       sv_setpv(sv, pwent->pw_class);
+       mPUSHs(newSVpv(pwent->pw_class, 0));
 #   else
 #       ifdef PWCOMMENT
-       sv_setpv(sv, pwent->pw_comment);
+       mPUSHs(newSVpv(pwent->pw_comment, 0));
+#      else
+       /* I think that you can never get this compiled, but just in case.  */
+       PUSHs(sv_mortalcopy(&PL_sv_no));
 #       endif
 #   endif
 
-       PUSHs(sv = sv_mortalcopy(&PL_sv_no));
 #   ifdef PWGECOS
-       sv_setpv(sv, pwent->pw_gecos);
+       PUSHs(sv = sv_2mortal(newSVpv(pwent->pw_gecos, 0)));
+#   else
+       PUSHs(sv = sv_mortalcopy(&PL_sv_no));
 #   endif
 #   ifndef INCOMPLETE_TAINTS
        /* pw_gecos is tainted because user himself can diddle with it. */
        SvTAINTED_on(sv);
 #   endif
 
-       PUSHs(sv = sv_mortalcopy(&PL_sv_no));
-       sv_setpv(sv, pwent->pw_dir);
+       mPUSHs(newSVpv(pwent->pw_dir, 0));
 
-       PUSHs(sv = sv_mortalcopy(&PL_sv_no));
-       sv_setpv(sv, pwent->pw_shell);
+       PUSHs(sv = sv_2mortal(newSVpv(pwent->pw_shell, 0)));
 #   ifndef INCOMPLETE_TAINTS
        /* pw_shell is tainted because user himself can diddle with it. */
        SvTAINTED_on(sv);
 #   endif
 
 #   ifdef PWEXPIRE
-       PUSHs(sv = sv_mortalcopy(&PL_sv_no));
-       sv_setiv(sv, (IV)pwent->pw_expire);
+       mPUSHi(pwent->pw_expire);
 #   endif
     }
     RETURN;
@@ -5152,7 +5246,7 @@ PP(pp_gpwent)
 PP(pp_spwent)
 {
 #if defined(HAS_PASSWD) && defined(HAS_SETPWENT)
-    dSP;
+    dVAR; dSP;
     setpwent();
     RETPUSHYES;
 #else
@@ -5163,7 +5257,7 @@ PP(pp_spwent)
 PP(pp_epwent)
 {
 #if defined(HAS_PASSWD) && defined(HAS_ENDPWENT)
-    dSP;
+    dVAR; dSP;
     endpwent();
     RETPUSHYES;
 #else
@@ -5174,19 +5268,17 @@ PP(pp_epwent)
 PP(pp_ggrent)
 {
 #ifdef HAS_GROUP
-    dSP;
-    I32 which = PL_op->op_type;
-    register char **elem;
-    register SV *sv;
-    struct group *grent;
+    dVAR; dSP;
+    const I32 which = PL_op->op_type;
+    const struct group *grent;
 
     if (which == OP_GGRNAM) {
        const char* const name = POPpbytex;
-       grent = (struct group *)getgrnam(name);
+       grent = (const struct group *)getgrnam(name);
     }
     else if (which == OP_GGRGID) {
        const Gid_t gid = POPi;
-       grent = (struct group *)getgrgid(gid);
+       grent = (const struct group *)getgrgid(gid);
     }
     else
 #ifdef HAS_GETGRENT
@@ -5197,7 +5289,9 @@ PP(pp_ggrent)
 
     EXTEND(SP, 4);
     if (GIMME != G_ARRAY) {
-       PUSHs(sv = sv_newmortal());
+       SV * const sv = sv_newmortal();
+
+       PUSHs(sv);
        if (grent) {
            if (which == OP_GGRNAM)
                sv_setiv(sv, (IV)grent->gr_gid);
@@ -5208,19 +5302,17 @@ PP(pp_ggrent)
     }
 
     if (grent) {
-       PUSHs(sv = sv_mortalcopy(&PL_sv_no));
-       sv_setpv(sv, grent->gr_name);
+       mPUSHs(newSVpv(grent->gr_name, 0));
 
-       PUSHs(sv = sv_mortalcopy(&PL_sv_no));
 #ifdef GRPASSWD
-       sv_setpv(sv, grent->gr_passwd);
+       mPUSHs(newSVpv(grent->gr_passwd, 0));
+#else
+       PUSHs(sv_mortalcopy(&PL_sv_no));
 #endif
 
-       PUSHs(sv = sv_mortalcopy(&PL_sv_no));
-       sv_setiv(sv, (IV)grent->gr_gid);
+       mPUSHi(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
@@ -5229,11 +5321,7 @@ PP(pp_ggrent)
         * 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_catpvs(sv, " ");
-       }
+       PUSHs(space_join_names_mortal(grent->gr_mem));
 #endif
     }
 
@@ -5246,7 +5334,7 @@ PP(pp_ggrent)
 PP(pp_sgrent)
 {
 #if defined(HAS_GROUP) && defined(HAS_SETGRENT)
-    dSP;
+    dVAR; dSP;
     setgrent();
     RETPUSHYES;
 #else
@@ -5257,7 +5345,7 @@ PP(pp_sgrent)
 PP(pp_egrent)
 {
 #if defined(HAS_GROUP) && defined(HAS_ENDGRENT)
-    dSP;
+    dVAR; dSP;
     endgrent();
     RETPUSHYES;
 #else
@@ -5268,7 +5356,7 @@ PP(pp_egrent)
 PP(pp_getlogin)
 {
 #ifdef HAS_GETLOGIN
-    dSP; dTARGET;
+    dVAR; dSP; dTARGET;
     char *tmps;
     EXTEND(SP, 1);
     if (!(tmps = PerlProc_getlogin()))
@@ -5285,7 +5373,7 @@ PP(pp_getlogin)
 PP(pp_syscall)
 {
 #ifdef HAS_SYSCALL
-    dSP; dMARK; dORIGMARK; dTARGET;
+    dVAR; dSP; dMARK; dORIGMARK; dTARGET;
     register I32 items = SP - MARK;
     unsigned long a[20];
     register I32 i = 0;