This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
pp_sys.c: some Coverity findings: NULL guards for io pointers
[perl5.git] / pp_sys.c
index c6eb020..1db57b0 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
+ *    2000, 2001, 2002, 2003, 2004, 2005, 2006, 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.
@@ -307,8 +307,8 @@ S_emulate_eaccess(pTHX_ const char* path, Mode_t mode)
 STATIC int
 S_emulate_eaccess(pTHX_ const char* path, Mode_t mode)
 {
-    (void)path;
-    (void)mode;
+    PERL_UNUSED_ARG(path);
+    PERL_UNUSED_ARG(mode);
     Perl_croak(aTHX_ "switching effective uid is not implemented");
     /*NOTREACHED*/
     return -1;
@@ -317,7 +317,7 @@ S_emulate_eaccess(pTHX_ const char* path, Mode_t mode)
 
 PP(pp_backtick)
 {
-    dSP; dTARGET;
+    dVAR; dSP; dTARGET;
     PerlIO *fp;
     const char * const tmps = POPpconstx;
     const I32 gimme = GIMME_V;
@@ -330,10 +330,7 @@ PP(pp_backtick)
        mode = "rt";
     fp = PerlProc_popen(tmps, mode);
     if (fp) {
-        const char *type = NULL;
-       if (PL_curcop->cop_io) {
-           type = SvPV_nolen_const(PL_curcop->cop_io);
-       }
+        const char * const type = PL_curcop->cop_io ? SvPV_nolen_const(PL_curcop->cop_io) : NULL;
        if (type && *type)
            PerlIO_apply_layers(aTHX_ fp,mode,type);
 
@@ -347,7 +344,7 @@ PP(pp_backtick)
            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)
                ;
            LEAVE;
            XPUSHs(TARG);
@@ -355,8 +352,8 @@ PP(pp_backtick)
        }
        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;
                }
@@ -406,7 +403,7 @@ PP(pp_glob)
     PL_last_in_gv = (GV*)*PL_stack_sp--;
 
     SAVESPTR(PL_rs);           /* This is not permanent, either. */
-    PL_rs = sv_2mortal(newSVpvn("\000", 1));
+    PL_rs = sv_2mortal(newSVpvs("\000"));
 #ifndef DOSISH
 #ifndef CSH
     *SvPVX(PL_rs) = '\n';
@@ -420,22 +417,27 @@ 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;
-    if (SP - MARK != 1) {
+    if (SP - MARK > 1) {
        dTARGET;
        do_join(TARG, &PL_sv_no, MARK, SP);
        tmpsv = TARG;
        SP = MARK + 1;
     }
+    else if (SP == MARK) {
+       tmpsv = &PL_sv_no;
+       EXTEND(SP, 1);
+    }
     else {
        tmpsv = TOPs;
     }
@@ -444,12 +446,12 @@ PP(pp_warn)
        SV * const error = ERRSV;
        SvUPGRADE(error, SVt_PV);
        if (SvPOK(error) && SvCUR(error))
-           sv_catpv(error, "\t...caught");
+           sv_catpvs(error, "\t...caught");
        tmpsv = error;
        tmps = SvPV_const(tmpsv, len);
     }
     if (!tmps || !len)
-       tmpsv = sv_2mortal(newSVpvn("Warning: something's wrong", 26));
+       tmpsv = sv_2mortal(newSVpvs("Warning: something's wrong"));
 
     Perl_warn(aTHX_ "%"SVf, tmpsv);
     RETSETYES;
@@ -457,7 +459,7 @@ PP(pp_warn)
 
 PP(pp_die)
 {
-    dSP; dMARK;
+    dVAR; dSP; dMARK;
     const char *tmps;
     SV *tmpsv;
     STRLEN len;
@@ -475,20 +477,20 @@ PP(pp_die)
     }
     else {
        tmpsv = TOPs;
-        tmps = SvROK(tmpsv) ? Nullch : SvPV_const(tmpsv, len);
+        tmps = SvROK(tmpsv) ? NULL : SvPV_const(tmpsv, len);
     }
     if (!tmps || !len) {
-       SV *error = ERRSV;
+       SV * const error = ERRSV;
        SvUPGRADE(error, SVt_PV);
        if (multiarg ? SvROK(error) : SvROK(tmpsv)) {
            if (!multiarg)
                SvSetSV(error,tmpsv);
            else if (sv_isobject(error)) {
-               HV *stash = SvSTASH(SvRV(error));
-               GV *gv = gv_fetchmethod(stash, "PROPAGATE");
+               HV * const stash = SvSTASH(SvRV(error));
+               GV * const gv = gv_fetchmethod(stash, "PROPAGATE");
                if (gv) {
-                   SV *file = sv_2mortal(newSVpv(CopFILE(PL_curcop),0));
-                   SV *line = sv_2mortal(newSVuv(CopLINE(PL_curcop)));
+                   SV * const file = sv_2mortal(newSVpv(CopFILE(PL_curcop),0));
+                   SV * const line = sv_2mortal(newSVuv(CopLINE(PL_curcop)));
                    EXTEND(SP, 3);
                    PUSHMARK(SP);
                    PUSHs(error);
@@ -500,20 +502,20 @@ PP(pp_die)
                    sv_setsv(error,*PL_stack_sp--);
                }
            }
-           DIE(aTHX_ Nullch);
+           DIE(aTHX_ NULL);
        }
        else {
            if (SvPOK(error) && SvCUR(error))
-               sv_catpv(error, "\t...propagated");
+               sv_catpvs(error, "\t...propagated");
            tmpsv = error;
            if (SvOK(tmpsv))
                tmps = SvPV_const(tmpsv, len);
            else
-               tmps = Nullch;
+               tmps = NULL;
        }
     }
     if (!tmps || !len)
-       tmpsv = sv_2mortal(newSVpvn("Died", 4));
+       tmpsv = sv_2mortal(newSVpvs("Died"));
 
     DIE(aTHX_ "%"SVf, tmpsv);
 }
@@ -562,7 +564,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 );
@@ -600,6 +602,7 @@ PP(pp_close)
 PP(pp_pipe_op)
 {
 #ifdef HAS_PIPE
+    dVAR;
     dSP;
     register IO *rstio;
     register IO *wstio;
@@ -691,6 +694,7 @@ PP(pp_fileno)
 
 PP(pp_umask)
 {
+    dVAR;
     dSP;
 #ifdef HAS_UMASK
     dTARGET;
@@ -722,7 +726,7 @@ PP(pp_binmode)
     IO *io;
     PerlIO *fp;
     MAGIC *mg;
-    SV *discp = Nullsv;
+    SV *discp = NULL;
 
     if (MAXARG < 1)
        RETPUSHUNDEF;
@@ -757,11 +761,11 @@ PP(pp_binmode)
 
     PUTBACK;
     if (PerlIO_binmode(aTHX_ fp,IoTYPE(io),mode_from_discipline(discp),
-                       (discp) ? SvPV_nolen_const(discp) : Nullch)) {
+                       (discp) ? SvPV_nolen_const(discp) : NULL)) {
        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)) {
+                       (discp) ? SvPV_nolen_const(discp) : NULL)) {
                SPAGAIN;
                RETPUSHUNDEF;
             }
@@ -855,7 +859,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;
@@ -876,9 +880,9 @@ PP(pp_untie)
 
     if ((mg = SvTIED_mg(sv, how))) {
        SV * const obj = SvRV(SvTIED_obj(sv, mg));
-       CV *cv = NULL;
         if (obj) {
            GV * const gv = gv_fetchmethod_autoload(SvSTASH(obj), "UNTIE", FALSE);
+           CV *cv;
            if (gv && isGV(gv) && (cv = GvCV(gv))) {
               PUSHMARK(SP);
               XPUSHs(SvTIED_obj((SV*)gv, mg));
@@ -902,6 +906,7 @@ PP(pp_untie)
 
 PP(pp_tied)
 {
+    dVAR;
     dSP;
     const MAGIC *mg;
     SV *sv = POPs;
@@ -929,9 +934,7 @@ PP(pp_dbmopen)
     GV *gv;
 
     HV * const hv = (HV*)POPs;
-    SV * const sv = sv_mortalcopy(&PL_sv_no);
-
-    sv_setpv(sv, "AnyDBM_File");
+    SV * const sv = sv_2mortal(newSVpvs("AnyDBM_File"));
     stash = gv_stashsv(sv, FALSE);
     if (!stash || !(gv = gv_fetchmethod(stash, "TIEHASH"))) {
        PUTBACK;
@@ -970,7 +973,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;
@@ -979,7 +982,7 @@ PP(pp_dbmopen)
 PP(pp_sselect)
 {
 #ifdef HAS_SELECT
-    dSP; dTARGET;
+    dVAR; dSP; dTARGET;
     register I32 i;
     register I32 j;
     register char *s;
@@ -1065,7 +1068,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];
@@ -1132,8 +1135,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);
+       PUSHs(sv_2mortal(newSVnv(value)));
     }
     RETURN;
 #else
@@ -1144,8 +1146,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;
@@ -1153,13 +1155,11 @@ Perl_setdefout(pTHX_ GV *gv)
 
 PP(pp_select)
 {
-    dSP; dTARGET;
-    GV *egv;
+    dVAR; dSP; dTARGET;
     HV *hv;
-
     GV * const newdefout = (PL_op->op_private > 0) ? ((GV *) POPs) : (GV *) NULL;
+    GV * egv = GvEGV(PL_defoutgv);
 
-    egv = GvEGV(PL_defoutgv);
     if (!egv)
        egv = PL_defoutgv;
     hv = GvSTASH(egv);
@@ -1168,7 +1168,7 @@ 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 {
@@ -1253,6 +1253,7 @@ S_doform(pTHX_ CV *cv, GV *gv, OP *retop)
 
 PP(pp_enterwrite)
 {
+    dVAR;
     dSP;
     register GV *gv;
     register IO *io;
@@ -1281,7 +1282,7 @@ PP(pp_enterwrite)
        if (fgv) {
            SV * const tmpsv = sv_newmortal();
            const char *name;
-           gv_efullname4(tmpsv, fgv, Nullch, FALSE);
+           gv_efullname4(tmpsv, fgv, NULL, FALSE);
            name = SvPV_nolen_const(tmpsv);
            if (name && *name)
                DIE(aTHX_ "Undefined format \"%s\" called", name);
@@ -1323,14 +1324,14 @@ PP(pp_leavewrite)
                if (!IoFMT_NAME(io))
                    IoFMT_NAME(io) = savepv(GvNAME(gv));
                topname = sv_2mortal(Perl_newSVpvf(aTHX_ "%s_TOP", GvNAME(gv)));
-               topgv = gv_fetchsv(topname, FALSE, SVt_PVFM);
+               topgv = gv_fetchsv(topname, 0, SVt_PVFM);
                if ((topgv && GvFORM(topgv)) ||
-                 !gv_fetchpv("top",FALSE,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),FALSE, SVt_PVFM);
+           topgv = gv_fetchpv(IoTOP_NAME(io), 0, SVt_PVFM);
            if (!topgv || !GvFORM(topgv)) {
                IoLINES_LEFT(io) = IoPAGE_LEN(io);
                goto forget_top;
@@ -1370,7 +1371,7 @@ 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);
@@ -1455,7 +1456,7 @@ PP(pp_prtf)
        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);
@@ -1495,6 +1496,7 @@ PP(pp_prtf)
 
 PP(pp_sysopen)
 {
+    dVAR;
     dSP;
     const int perm = (MAXARG > 3) ? POPi : 0666;
     const int mode = POPi;
@@ -1505,7 +1507,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);
     }
@@ -1771,7 +1773,6 @@ PP(pp_sysread)
 PP(pp_send)
 {
     dVAR; dSP; dMARK; dORIGMARK; dTARGET;
-    GV *gv;
     IO *io;
     SV *bufsv;
     const char *buffer;
@@ -1781,7 +1782,7 @@ PP(pp_send)
     MAGIC *mg;
     const int op_type = PL_op->op_type;
     
-    gv = (GV*)*++MARK;
+    GV *const gv = (GV*)*++MARK;
     if (PL_op->op_type == OP_SYSWRITE
        && gv && (io = GvIO(gv))
        && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
@@ -1927,8 +1928,6 @@ PP(pp_eof)
 {
     dVAR; dSP;
     GV *gv;
-    IO *io;
-    MAGIC *mg;
 
     if (MAXARG == 0) {
        if (PL_op->op_flags & OPf_SPECIAL) {    /* eof() */
@@ -1939,7 +1938,7 @@ 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);
+                   do_open(gv, "-", 1, FALSE, O_RDONLY, 0, NULL);
                    sv_setpvn(GvSV(gv), "-", 1);
                    SvSETMAGIC(GvSV(gv));
                }
@@ -1953,17 +1952,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)));
@@ -2005,17 +2006,16 @@ PP(pp_tell)
 PP(pp_sysseek)
 {
     dVAR; dSP;
-    GV *gv;
     IO *io;
     const int whence = POPi;
 #if LSEEKSIZE > IVSIZE
-    Off_t offset = (Off_t)SvNVx(POPs);
+    const Off_t offset = (Off_t)SvNVx(POPs);
 #else
-    Off_t offset = (Off_t)SvIVx(POPs);
+    const Off_t offset = (Off_t)SvIVx(POPs);
 #endif
     MAGIC *mg;
 
-    gv = PL_last_in_gv = (GV*)POPs;
+    GV * const gv = PL_last_in_gv = (GV*)POPs;
 
     if (gv && (io = GvIO(gv))
        && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
@@ -2039,11 +2039,11 @@ PP(pp_sysseek)
     if (PL_op->op_type == OP_SEEK)
        PUSHs(boolSV(do_seek(gv, offset, whence)));
     else {
-       Off_t sought = do_sysseek(gv, offset, whence);
+       const Off_t sought = do_sysseek(gv, offset, whence);
         if (sought < 0)
             PUSHs(&PL_sv_undef);
         else {
-            SV* sv = sought ?
+            SV* const sv = sought ?
 #if LSEEKSIZE > IVSIZE
                 newSVnv((NV)sought)
 #else
@@ -2058,6 +2058,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
@@ -2081,7 +2082,7 @@ PP(pp_truncate)
        IO *io;
 
        if (PL_op->op_flags & OPf_SPECIAL) {
-           tmpgv = gv_fetchsv(POPs, FALSE, SVt_PVIO);
+           tmpgv = gv_fetchsv(POPs, 0, SVt_PVIO);
 
        do_ftruncate_gv:
            if (!GvIO(tmpgv))
@@ -2106,7 +2107,7 @@ PP(pp_truncate)
            }
        }
        else {
-           SV *sv = POPs;
+           SV * const sv = POPs;
            const char *name;
 
            if (SvTYPE(sv) == SVt_PVGV) {
@@ -2129,9 +2130,9 @@ PP(pp_truncate)
                result = 0;
 #else
            {
-               int tmpfd;
+               const int tmpfd = PerlLIO_open(name, O_RDWR);
 
-               if ((tmpfd = PerlLIO_open(name, O_RDWR)) < 0)
+               if (tmpfd < 0)
                    result = 0;
                else {
                    if (my_chsize(tmpfd, len) < 0)
@@ -2152,14 +2153,14 @@ PP(pp_truncate)
 
 PP(pp_ioctl)
 {
-    dSP; dTARGET;
-    SV *argsv = POPs;
+    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;
     char *s;
     IV retval;
-    GV *gv = (GV*)POPs;
-    IO *io = gv ? GvIOn(gv) : 0;
 
     if (!io || !argsv || !IoIFP(io)) {
        if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
@@ -2185,7 +2186,7 @@ PP(pp_ioctl)
        s = INT2PTR(char*,retval);              /* ouch */
     }
 
-    TAINT_PROPER(optype == OP_IOCTL ? "ioctl" : "fcntl");
+    TAINT_PROPER(PL_op_desc[optype]);
 
     if (optype == OP_IOCTL)
 #ifdef HAS_IOCTL
@@ -2228,24 +2229,20 @@ PP(pp_ioctl)
 PP(pp_flock)
 {
 #ifdef FLOCK
-    dSP; dTARGET;
+    dVAR; dSP; dTARGET;
     I32 value;
-    int argtype;
-    GV *gv;
     IO *io = NULL;
     PerlIO *fp;
+    const int argtype = POPi;
+    GV * const gv = (MAXARG == 0) ? PL_last_in_gv : (GV*)POPs;
 
-    argtype = POPi;
-    if (MAXARG == 0)
-       gv = PL_last_in_gv;
-    else
-       gv = (GV*)POPs;
     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 */
     if (fp) {
        (void)PerlIO_flush(fp);
        value = (I32)(PerlLIO_flock(PerlIO_fileno(fp), argtype) >= 0);
@@ -2268,21 +2265,18 @@ PP(pp_flock)
 PP(pp_socket)
 {
 #ifdef HAS_SOCKET
-    dSP;
-    GV *gv;
-    register IO *io;
-    int protocol = POPi;
-    int type = POPi;
-    int domain = POPi;
+    dVAR; dSP;
+    const int protocol = POPi;
+    const int type = POPi;
+    const int domain = POPi;
+    GV * const gv = (GV*)POPs;
+    register IO * const io = gv ? GvIOn(gv) : NULL;
     int fd;
 
-    gv = (GV*)POPs;
-    io = gv ? GvIOn(gv) : NULL;
-
     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;
@@ -2321,20 +2315,16 @@ PP(pp_socket)
 PP(pp_sockpair)
 {
 #if defined (HAS_SOCKETPAIR) || (defined (HAS_SOCKET) && defined(SOCK_DGRAM) && defined(AF_INET) && defined(PF_INET))
-    dSP;
-    GV *gv1;
-    GV *gv2;
-    register IO *io1;
-    register IO *io2;
-    int protocol = POPi;
-    int type = POPi;
-    int domain = POPi;
+    dVAR; dSP;
+    const int protocol = POPi;
+    const int type = POPi;
+    const int domain = POPi;
+    GV * const gv2 = (GV*)POPs;
+    GV * const gv1 = (GV*)POPs;
+    register IO * const io1 = gv1 ? GvIOn(gv1) : NULL;
+    register IO * const io2 = gv2 ? GvIOn(gv2) : NULL;
     int fd[2];
 
-    gv2 = (GV*)POPs;
-    gv1 = (GV*)POPs;
-    io1 = gv1 ? GvIOn(gv1) : NULL;
-    io2 = gv2 ? GvIOn(gv2) : NULL;
     if (!gv1 || !gv2 || !io1 || !io2) {
        if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)) {
            if (!gv1 || !io1)
@@ -2342,9 +2332,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;
     }
@@ -2386,47 +2376,24 @@ 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
-    SV *addrsv = POPs;
+    dVAR; dSP;
+    SV * const addrsv = POPs;
     /* OK, so on what platform does bind modify addr?  */
     const char *addr;
-    GV *gv = (GV*)POPs;
-    register IO *io = GvIOn(gv);
+    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)
        RETPUSHYES;
@@ -2446,11 +2413,11 @@ nuts:
 PP(pp_connect)
 {
 #ifdef HAS_SOCKET
-    dSP;
-    SV *addrsv = POPs;
+    dVAR; dSP;
+    SV * const addrsv = POPs;
+    GV * const gv = (GV*)POPs;
+    register IO * const io = GvIOn(gv);
     const char *addr;
-    GV *gv = (GV*)POPs;
-    register IO *io = GvIOn(gv);
     STRLEN len;
 
     if (!io || !IoIFP(io))
@@ -2476,10 +2443,10 @@ nuts:
 PP(pp_listen)
 {
 #ifdef HAS_SOCKET
-    dSP;
-    int backlog = POPi;
-    GV *gv = (GV*)POPs;
-    register IO *io = gv ? GvIOn(gv) : NULL;
+    dVAR; dSP;
+    const int backlog = POPi;
+    GV * const gv = (GV*)POPs;
+    register IO * const io = gv ? GvIOn(gv) : NULL;
 
     if (!gv || !io || !IoIFP(io))
        goto nuts;
@@ -2502,9 +2469,7 @@ nuts:
 PP(pp_accept)
 {
 #ifdef HAS_SOCKET
-    dSP; dTARGET;
-    GV *ngv;
-    GV *ggv;
+    dVAR; dSP; dTARGET;
     register IO *nstio;
     register IO *gstio;
     char namebuf[MAXPATHLEN];
@@ -2513,11 +2478,10 @@ PP(pp_accept)
 #else
     Sock_size_t len = sizeof namebuf;
 #endif
+    GV * const ggv = (GV*)POPs;
+    GV * const ngv = (GV*)POPs;
     int fd;
 
-    ggv = (GV*)POPs;
-    ngv = (GV*)POPs;
-
     if (!ngv)
        goto badexit;
     if (!ggv)
@@ -2573,10 +2537,10 @@ badexit:
 PP(pp_shutdown)
 {
 #ifdef HAS_SOCKET
-    dSP; dTARGET;
-    int how = POPi;
-    GV *gv = (GV*)POPs;
-    register IO *io = GvIOn(gv);
+    dVAR; dSP; dTARGET;
+    const int how = POPi;
+    GV * const gv = (GV*)POPs;
+    register IO * const io = GvIOn(gv);
 
     if (!io || !IoIFP(io))
        goto nuts;
@@ -2597,25 +2561,16 @@ nuts:
 PP(pp_ssockopt)
 {
 #ifdef HAS_SOCKET
-    dSP;
-    int optype = PL_op->op_type;
-    SV *sv;
+    dVAR; dSP;
+    const int optype = PL_op->op_type;
+    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;
+    register IO * const io = GvIOn(gv);
     int fd;
-    unsigned int optname;
-    unsigned int lvl;
-    GV *gv;
-    register IO *io;
     Sock_size_t len;
 
-    if (optype == OP_GSOCKOPT)
-       sv = sv_2mortal(NEWSV(22, 257));
-    else
-       sv = POPs;
-    optname = (unsigned int) POPi;
-    lvl = (unsigned int) POPi;
-
-    gv = (GV*)POPs;
-    io = GvIOn(gv);
     if (!io || !IoIFP(io))
        goto nuts;
 
@@ -2683,18 +2638,18 @@ nuts2:
 PP(pp_getpeername)
 {
 #ifdef HAS_SOCKET
-    dSP;
-    int optype = PL_op->op_type;
+    dVAR; dSP;
+    const int optype = PL_op->op_type;
+    GV * const gv = (GV*)POPs;
+    register IO * const io = GvIOn(gv);
+    Sock_size_t len;
     SV *sv;
     int fd;
-    GV *gv = (GV*)POPs;
-    register IO *io = GvIOn(gv);
-    Sock_size_t len;
 
     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);
@@ -2748,6 +2703,7 @@ nuts2:
 
 PP(pp_stat)
 {
+    dVAR;
     dSP;
     GV *gv;
     I32 gimme;
@@ -2780,7 +2736,7 @@ PP(pp_stat)
        }
     }
     else {
-       SV* sv = POPs;
+       SV* const sv = POPs;
        if (SvTYPE(sv) == SVt_PVGV) {
            gv = (GV*)sv;
            goto do_fstat;
@@ -2792,7 +2748,7 @@ PP(pp_stat)
            goto do_fstat;
        }
        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);
@@ -2839,7 +2795,7 @@ PP(pp_stat)
 #ifdef USE_STAT_RDEV
        PUSHs(sv_2mortal(newSViv(PL_statcache.st_rdev)));
 #else
-       PUSHs(sv_2mortal(newSVpvn("", 0)));
+       PUSHs(sv_2mortal(newSVpvs("")));
 #endif
 #if Off_t_size > IVSIZE
        PUSHs(sv_2mortal(newSVnv((NV)PL_statcache.st_size)));
@@ -2859,8 +2815,8 @@ PP(pp_stat)
        PUSHs(sv_2mortal(newSVuv(PL_statcache.st_blksize)));
        PUSHs(sv_2mortal(newSVuv(PL_statcache.st_blocks)));
 #else
-       PUSHs(sv_2mortal(newSVpvn("", 0)));
-       PUSHs(sv_2mortal(newSVpvn("", 0)));
+       PUSHs(sv_2mortal(newSVpvs("")));
+       PUSHs(sv_2mortal(newSVpvs("")));
 #endif
     }
     RETURN;
@@ -2877,6 +2833,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  */
@@ -2988,6 +2945,7 @@ PP(pp_ftrread)
 
 PP(pp_ftis)
 {
+    dVAR;
     I32 result;
     const int op_type = PL_op->op_type;
     dSP;
@@ -3026,6 +2984,7 @@ PP(pp_ftis)
 
 PP(pp_ftrowned)
 {
+    dVAR;
     I32 result;
     dSP;
 
@@ -3110,6 +3069,7 @@ PP(pp_ftrowned)
 
 PP(pp_ftlink)
 {
+    dVAR;
     I32 result = my_lstat();
     dSP;
     if (result < 0)
@@ -3121,10 +3081,11 @@ PP(pp_ftlink)
 
 PP(pp_fttty)
 {
+    dVAR;
     dSP;
     int fd;
     GV *gv;
-    SV *tmpsv = Nullsv;
+    SV *tmpsv = NULL;
 
     STACKED_FTEST_CHECK;
 
@@ -3135,7 +3096,7 @@ PP(pp_fttty)
     else if (SvROK(TOPs) && isGV(SvRV(TOPs)))
        gv = (GV*)SvRV(POPs);
     else
-       gv = gv_fetchsv(tmpsv = POPs, FALSE, SVt_PVIO);
+       gv = gv_fetchsv(tmpsv = POPs, 0, SVt_PVIO);
 
     if (GvIO(gv) && IoIFP(GvIOp(gv)))
        fd = PerlIO_fileno(IoIFP(GvIOp(gv)));
@@ -3163,6 +3124,7 @@ PP(pp_fttty)
 
 PP(pp_fttext)
 {
+    dVAR;
     dSP;
     I32 i;
     I32 len;
@@ -3183,7 +3145,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);
@@ -3238,7 +3200,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"))) {
@@ -3252,7 +3214,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) {
@@ -3321,13 +3283,16 @@ PP(pp_fttext)
 
 PP(pp_chdir)
 {
-    dSP; dTARGET;
-    const char *tmps = 0;
+    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) {
@@ -3342,10 +3307,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
            )
         {
@@ -3376,10 +3341,16 @@ PP(pp_chdir)
 #endif
            }
            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
@@ -3398,19 +3369,19 @@ 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;
-    PUSHi(value);
+    XPUSHi(value);
     RETURN;
 }
 
 PP(pp_chroot)
 {
 #ifdef HAS_CHROOT
-    dSP; dTARGET;
-    char *tmps = POPpx;
+    dVAR; dSP; dTARGET;
+    char * const tmps = POPpx;
     TAINT_PROPER("chroot");
     PUSHi( chroot(tmps) >= 0 );
     RETURN;
@@ -3421,10 +3392,10 @@ PP(pp_chroot)
 
 PP(pp_rename)
 {
-    dSP; dTARGET;
+    dVAR; dSP; dTARGET;
     int anum;
-    const char *tmps2 = POPpconstx;
-    const char *tmps = SvPV_nolen_const(TOPs);
+    const char * const tmps2 = POPpconstx;
+    const char * const tmps = SvPV_nolen_const(TOPs);
     TAINT_PROPER("rename");
 #ifdef HAS_RENAME
     anum = PerlLIO_rename(tmps, tmps2);
@@ -3447,7 +3418,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;
 
@@ -3461,8 +3432,8 @@ PP(pp_link)
 #  endif
 
     {
-       const char *tmps2 = POPpconstx;
-       const char *tmps = SvPV_nolen_const(TOPs);
+       const char * const tmps2 = POPpconstx;
+       const char * const tmps = SvPV_nolen_const(TOPs);
        TAINT_PROPER(PL_op_desc[op_type]);
        result =
 #  if defined(HAS_LINK)
@@ -3495,6 +3466,7 @@ PP(pp_link)
 
 PP(pp_readlink)
 {
+    dVAR;
     dSP;
 #ifdef HAS_SYMLINK
     dTARGET;
@@ -3540,11 +3512,11 @@ S_dooneliner(pTHX_ const char *cmd, const char *filename)
     Safefree(cmdline);
 
     if (myfp) {
-       SV *tmpsv = sv_newmortal();
+       SV * const tmpsv = sv_newmortal();
        /* 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
@@ -3553,7 +3525,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
@@ -3627,19 +3599,11 @@ S_dooneliner(pTHX_ const char *cmd, const char *filename)
 
 PP(pp_mkdir)
 {
-    dSP; dTARGET;
-    int mode;
-#ifndef HAS_MKDIR
-    int oldumask;
-#endif
+    dVAR; dSP; dTARGET;
     STRLEN len;
     const char *tmps;
     bool copy = FALSE;
-
-    if (MAXARG > 1)
-       mode = POPi;
-    else
-       mode = 0777;
+    const int mode = (MAXARG > 1) ? POPi : 0777;
 
     TRIMSLASHES(tmps,len,copy);
 
@@ -3647,10 +3611,13 @@ PP(pp_mkdir)
 #ifdef HAS_MKDIR
     SETi( PerlDir_mkdir(tmps, mode) >= 0 );
 #else
+    {
+    int oldumask;
     SETi( dooneliner("mkdir", tmps) );
     oldumask = PerlLIO_umask(0);
     PerlLIO_umask(oldumask);
     PerlLIO_chmod(tmps, (mode & ~oldumask) & 0777);
+    }
 #endif
     if (copy)
        Safefree(tmps);
@@ -3659,7 +3626,7 @@ PP(pp_mkdir)
 
 PP(pp_rmdir)
 {
-    dSP; dTARGET;
+    dVAR; dSP; dTARGET;
     STRLEN len;
     const char *tmps;
     bool copy = FALSE;
@@ -3681,10 +3648,10 @@ PP(pp_rmdir)
 PP(pp_open_dir)
 {
 #if defined(Direntry_t) && defined(HAS_READDIR)
-    dSP;
-    const char *dirname = POPpconstx;
-    GV *gv = (GV*)POPs;
-    register IO *io = GvIOn(gv);
+    dVAR; dSP;
+    const char * const dirname = POPpconstx;
+    GV * const gv = (GV*)POPs;
+    register IO * const io = GvIOn(gv);
 
     if (!io)
        goto nope;
@@ -3712,16 +3679,22 @@ PP(pp_readdir)
 #if !defined(I_DIRENT) && !defined(VMS)
     Direntry_t *readdir (DIR *);
 #endif
+    dVAR;
     dSP;
 
     SV *sv;
     const I32 gimme = GIMME;
-    GV *gv = (GV *)POPs;
-    register Direntry_t *dp;
-    register IO *io = GvIOn(gv);
+    GV * const gv = (GV *)POPs;
+    register const Direntry_t *dp;
+    register IO * const io = GvIOn(gv);
 
-    if (!io || !IoDIRP(io))
-       goto nope;
+    if (!io || !IoDIRP(io)) {
+        if(ckWARN(WARN_IO)) {
+            Perl_warner(aTHX_ packWARN(WARN_IO),
+                "readdir() attempted on invalid dirhandle %s", GvENAME(gv));
+        }
+        goto nope;
+    }
 
     do {
         dp = (Direntry_t *)PerlDir_read(IoDIRP(io));
@@ -3766,11 +3739,16 @@ PP(pp_telldir)
 # if !defined(HAS_TELLDIR_PROTO) || defined(NEED_TELLDIR_PROTO)
     long telldir (DIR *);
 # endif
-    GV *gv = (GV*)POPs;
-    register IO *io = GvIOn(gv);
+    GV * const gv = (GV*)POPs;
+    register IO * const io = GvIOn(gv);
 
-    if (!io || !IoDIRP(io))
-       goto nope;
+    if (!io || !IoDIRP(io)) {
+        if(ckWARN(WARN_IO)) {
+            Perl_warner(aTHX_ packWARN(WARN_IO),
+               "telldir() attempted on invalid dirhandle %s", GvENAME(gv));
+        }
+        goto nope;
+    }
 
     PUSHi( PerlDir_tell(IoDIRP(io)) );
     RETURN;
@@ -3786,14 +3764,18 @@ nope:
 PP(pp_seekdir)
 {
 #if defined(HAS_SEEKDIR) || defined(seekdir)
-    dSP;
-    long along = POPl;
-    GV *gv = (GV*)POPs;
-    register IO *io = GvIOn(gv);
-
-    if (!io || !IoDIRP(io))
-       goto nope;
-
+    dVAR; dSP;
+    const long along = POPl;
+    GV * const gv = (GV*)POPs;
+    register IO * const io = GvIOn(gv);
+
+    if (!io || !IoDIRP(io)) {
+       if(ckWARN(WARN_IO)) {
+           Perl_warner(aTHX_ packWARN(WARN_IO),
+                "seekdir() attempted on invalid dirhandle %s", GvENAME(gv));
+        }
+        goto nope;
+    }
     (void)PerlDir_seek(IoDIRP(io), along);
 
     RETPUSHYES;
@@ -3809,13 +3791,17 @@ nope:
 PP(pp_rewinddir)
 {
 #if defined(HAS_REWINDDIR) || defined(rewinddir)
-    dSP;
-    GV *gv = (GV*)POPs;
-    register IO *io = GvIOn(gv);
+    dVAR; dSP;
+    GV * const gv = (GV*)POPs;
+    register IO * const io = GvIOn(gv);
 
-    if (!io || !IoDIRP(io))
+    if (!io || !IoDIRP(io)) {
+       if(ckWARN(WARN_IO)) {
+           Perl_warner(aTHX_ packWARN(WARN_IO),
+               "rewinddir() attempted on invalid dirhandle %s", GvENAME(gv));
+       }
        goto nope;
-
+    }
     (void)PerlDir_rewind(IoDIRP(io));
     RETPUSHYES;
 nope:
@@ -3830,13 +3816,17 @@ nope:
 PP(pp_closedir)
 {
 #if defined(Direntry_t) && defined(HAS_READDIR)
-    dSP;
-    GV *gv = (GV*)POPs;
-    register IO *io = GvIOn(gv);
-
-    if (!io || !IoDIRP(io))
-       goto nope;
+    dVAR; dSP;
+    GV * const gv = (GV*)POPs;
+    register IO * const io = GvIOn(gv);
 
+    if (!io || !IoDIRP(io)) {
+       if(ckWARN(WARN_IO)) {
+           Perl_warner(aTHX_ packWARN(WARN_IO),
+                "closedir() attempted on invalid dirhandle %s", GvENAME(gv));
+        }
+        goto nope;
+    }
 #ifdef VOID_CLOSEDIR
     PerlDir_close(IoDIRP(io));
 #else
@@ -3862,9 +3852,8 @@ nope:
 PP(pp_fork)
 {
 #ifdef HAS_FORK
-    dSP; dTARGET;
+    dVAR; dSP; dTARGET;
     Pid_t childpid;
-    GV *tmpgv;
 
     EXTEND(SP, 1);
     PERL_FLUSHALL_FOR_CHILD;
@@ -3872,7 +3861,8 @@ PP(pp_fork)
     if (childpid < 0)
        RETSETUNDEF;
     if (!childpid) {
-       if ((tmpgv = gv_fetchpv("$", TRUE, 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());
             SvREADONLY_on(GvSV(tmpgv));
@@ -3907,7 +3897,7 @@ PP(pp_fork)
 PP(pp_wait)
 {
 #if (!defined(DOSISH) || defined(OS2) || defined(WIN32)) && !defined(MACOS_TRADITIONAL)
-    dSP; dTARGET;
+    dVAR; dSP; dTARGET;
     Pid_t childpid;
     int argflags;
 
@@ -3935,14 +3925,12 @@ PP(pp_wait)
 PP(pp_waitpid)
 {
 #if (!defined(DOSISH) || defined(OS2) || defined(WIN32)) && !defined(MACOS_TRADITIONAL)
-    dSP; dTARGET;
-    Pid_t pid;
+    dVAR; dSP; dTARGET;
+    const int optype = POPi;
+    const Pid_t pid = TOPi;
     Pid_t result;
-    int optype;
     int argflags;
 
-    optype = POPi;
-    pid = TOPi;
     if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG)
         result = wait4pid(pid, &argflags, optype);
     else {
@@ -3966,7 +3954,7 @@ PP(pp_waitpid)
 
 PP(pp_system)
 {
-    dSP; dMARK; dORIGMARK; dTARGET;
+    dVAR; dSP; dMARK; dORIGMARK; dTARGET;
     I32 value;
     int result;
 
@@ -3993,7 +3981,7 @@ PP(pp_system)
            if (errno != EAGAIN) {
                value = -1;
                SP = ORIGMARK;
-               PUSHi(value);
+               XPUSHi(value);
                if (did_pipes) {
                    PerlLIO_close(pp[0]);
                    PerlLIO_close(pp[1]);
@@ -4042,7 +4030,7 @@ PP(pp_system)
                    STATUS_NATIVE_CHILD_SET(-1);
                }
            }
-           PUSHi(STATUS_CURRENT);
+           XPUSHi(STATUS_CURRENT);
            RETURN;
        }
        if (did_pipes) {
@@ -4052,11 +4040,11 @@ PP(pp_system)
 #endif
        }
        if (PL_op->op_flags & OPf_STACKED) {
-           SV *really = *++MARK;
+           SV * const 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);
+           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);
        }
@@ -4066,7 +4054,7 @@ PP(pp_system)
     PL_statusvalue = 0;
     result = 0;
     if (PL_op->op_flags & OPf_STACKED) {
-       SV *really = *++MARK;
+       SV * const really = *++MARK;
 #  if defined(WIN32) || defined(OS2) || defined(__SYMBIAN32__)
        value = (I32)do_aspawn(really, MARK, SP);
 #  else
@@ -4075,9 +4063,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 {
@@ -4088,14 +4076,14 @@ PP(pp_system)
     STATUS_NATIVE_CHILD_SET(value);
     do_execfree();
     SP = ORIGMARK;
-    PUSHi(result ? value : STATUS_CURRENT);
+    XPUSHi(result ? value : STATUS_CURRENT);
 #endif /* !FORK or VMS */
     RETURN;
 }
 
 PP(pp_exec)
 {
-    dSP; dMARK; dORIGMARK; dTARGET;
+    dVAR; dSP; dMARK; dORIGMARK; dTARGET;
     I32 value;
 
     if (PL_tainting) {
@@ -4110,20 +4098,20 @@ PP(pp_exec)
     }
     PERL_FLUSHALL_FOR_CHILD;
     if (PL_op->op_flags & OPf_STACKED) {
-       SV *really = *++MARK;
+       SV * const really = *++MARK;
        value = (I32)do_aexec(really, MARK, SP);
     }
     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 {
@@ -4140,14 +4128,14 @@ PP(pp_exec)
     }
 
     SP = ORIGMARK;
-    PUSHi(value);
+    XPUSHi(value);
     RETURN;
 }
 
 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 */
@@ -4165,14 +4153,10 @@ PP(pp_getppid)
 PP(pp_getpgrp)
 {
 #ifdef HAS_GETPGRP
-    dSP; dTARGET;
-    Pid_t pid;
+    dVAR; dSP; dTARGET;
     Pid_t pgrp;
+    const Pid_t pid = (MAXARG < 1) ? 0 : SvIVx(POPs);
 
-    if (MAXARG < 1)
-       pid = 0;
-    else
-       pid = SvIVx(POPs);
 #ifdef BSD_GETPGRP
     pgrp = (I32)BSD_GETPGRP(pid);
 #else
@@ -4190,7 +4174,7 @@ PP(pp_getpgrp)
 PP(pp_setpgrp)
 {
 #ifdef HAS_SETPGRP
-    dSP; dTARGET;
+    dVAR; dSP; dTARGET;
     Pid_t pgrp;
     Pid_t pid;
     if (MAXARG < 2) {
@@ -4222,9 +4206,9 @@ PP(pp_setpgrp)
 PP(pp_getpriority)
 {
 #ifdef HAS_GETPRIORITY
-    dSP; dTARGET;
-    int who = POPi;
-    int which = TOPi;
+    dVAR; dSP; dTARGET;
+    const int who = POPi;
+    const int which = TOPi;
     SETi( getpriority(which, who) );
     RETURN;
 #else
@@ -4235,10 +4219,10 @@ PP(pp_getpriority)
 PP(pp_setpriority)
 {
 #ifdef HAS_SETPRIORITY
-    dSP; dTARGET;
-    int niceval = POPi;
-    int who = POPi;
-    int which = TOPi;
+    dVAR; dSP; dTARGET;
+    const int niceval = POPi;
+    const int who = POPi;
+    const int which = TOPi;
     TAINT_PROPER("setpriority");
     SETi( setpriority(which, who, niceval) >= 0 );
     RETURN;
@@ -4251,11 +4235,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;
 }
@@ -4263,6 +4247,7 @@ PP(pp_time)
 PP(pp_tms)
 {
 #ifdef HAS_TIMES
+    dVAR;
     dSP;
     EXTEND(SP, 4);
 #ifndef VMS
@@ -4339,6 +4324,7 @@ static struct tm *S_my_localtime (pTHX_ Time_t *tp)
 
 PP(pp_gmtime)
 {
+    dVAR;
     dSP;
     Time_t when;
     const struct tm *tmbuf;
@@ -4401,7 +4387,7 @@ PP(pp_gmtime)
 PP(pp_alarm)
 {
 #ifdef HAS_ALARM
-    dSP; dTARGET;
+    dVAR; dSP; dTARGET;
     int anum;
     anum = POPi;
     anum = alarm((unsigned int)anum);
@@ -4417,7 +4403,7 @@ PP(pp_alarm)
 
 PP(pp_sleep)
 {
-    dSP; dTARGET;
+    dVAR; dSP; dTARGET;
     I32 duration;
     Time_t lasttime;
     Time_t when;
@@ -4440,7 +4426,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;
 
@@ -4472,8 +4458,8 @@ PP(pp_shmwrite)
 PP(pp_semget)
 {
 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
-    dSP; dMARK; dTARGET;
-    int anum = do_ipcget(PL_op->op_type, MARK, SP);
+    dVAR; dSP; dMARK; dTARGET;
+    const int anum = do_ipcget(PL_op->op_type, MARK, SP);
     SP = MARK;
     if (anum == -1)
        RETPUSHUNDEF;
@@ -4487,8 +4473,8 @@ PP(pp_semget)
 PP(pp_semctl)
 {
 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
-    dSP; dMARK; dTARGET;
-    int anum = do_ipcctl(PL_op->op_type, MARK, SP);
+    dVAR; dSP; dMARK; dTARGET;
+    const int anum = do_ipcctl(PL_op->op_type, MARK, SP);
     SP = MARK;
     if (anum == -1)
        RETSETUNDEF;
@@ -4504,12 +4490,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 = sv_2mortal(newSVpvs(""));
+       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;
@@ -4524,7 +4531,7 @@ PP(pp_ghostent)
     EXTEND(SP, 10);
     if (which == OP_GHBYNAME) {
 #ifdef HAS_GETHOSTBYNAME
-        char* name = POPpbytex;
+       const char* const name = POPpbytex;
        hent = PerlSock_gethostbyname(name);
 #else
        DIE(aTHX_ PL_no_sock_func, "gethostbyname");
@@ -4532,8 +4539,8 @@ PP(pp_ghostent)
     }
     else if (which == OP_GHBYADDR) {
 #ifdef HAS_GETHOSTBYADDR
-       int addrtype = POPi;
-       SV *addrsv = POPs;
+       const int addrtype = POPi;
+       SV * const addrsv = POPs;
        STRLEN addrlen;
        Netdb_host_t addr = (Netdb_host_t) SvPVbyte(addrsv, addrlen);
 
@@ -4574,28 +4581,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_catpvn(sv, " ", 1);
-       }
-       PUSHs(sv = sv_mortalcopy(&PL_sv_no));
-       sv_setiv(sv, (IV)hent->h_addrtype);
-       PUSHs(sv = sv_mortalcopy(&PL_sv_no));
+       PUSHs(sv_2mortal(newSVpv((char*)hent->h_name, 0)));
+       PUSHs(S_space_join_names_mortal(aTHX_ hent->h_aliases));
+       PUSHs(sv_2mortal(newSViv((IV)hent->h_addrtype)));
        len = hent->h_length;
-       sv_setiv(sv, (IV)len);
+       PUSHs(sv_2mortal(newSViv((IV)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);
+           XPUSHs(sv_2mortal(newSVpvn(*elem, len)));
        }
 #else
-       PUSHs(sv = sv_mortalcopy(&PL_sv_no));
        if (hent->h_addr)
-           sv_setpvn(sv, hent->h_addr, len);
+           PUSHs(newSVpvn(hent->h_addr, len));
+       else
+           PUSHs(sv_mortalcopy(&PL_sv_no));
 #endif /* h_addr */
     }
     RETURN;
@@ -4607,9 +4606,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);
@@ -4620,7 +4618,7 @@ PP(pp_gnetent)
 
     if (which == OP_GNBYNAME){
 #ifdef HAS_GETNETBYNAME
-        char *name = POPpbytex;
+       const char * const name = POPpbytex;
        nent = PerlSock_getnetbyname(name);
 #else
         DIE(aTHX_ PL_no_sock_func, "getnetbyname");
@@ -4628,8 +4626,8 @@ PP(pp_gnetent)
     }
     else if (which == OP_GNBYADDR) {
 #ifdef HAS_GETNETBYADDR
-       int addrtype = POPi;
-       Netdb_net_t addr = (Netdb_net_t) (U32)POPu;
+       const int addrtype = POPi;
+       const Netdb_net_t addr = (Netdb_net_t) (U32)POPu;
        nent = PerlSock_getnetbyaddr(addr, addrtype);
 #else
        DIE(aTHX_ PL_no_sock_func, "getnetbyaddr");
@@ -4666,18 +4664,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_catpvn(sv, " ", 1);
-       }
-       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);
+       PUSHs(sv_2mortal(newSVpv(nent->n_name, 0)));
+       PUSHs(S_space_join_names_mortal(aTHX_ nent->n_aliases));
+       PUSHs(sv_2mortal(newSViv((IV)nent->n_addrtype)));
+       PUSHs(sv_2mortal(newSViv((IV)nent->n_net)));
     }
 
     RETURN;
@@ -4689,9 +4679,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);
@@ -4702,7 +4691,7 @@ PP(pp_gprotoent)
 
     if (which == OP_GPBYNAME) {
 #ifdef HAS_GETPROTOBYNAME
-        char* name = POPpbytex;
+       const char* const name = POPpbytex;
        pent = PerlSock_getprotobyname(name);
 #else
        DIE(aTHX_ PL_no_sock_func, "getprotobyname");
@@ -4710,7 +4699,7 @@ PP(pp_gprotoent)
     }
     else if (which == OP_GPBYNUMBER) {
 #ifdef HAS_GETPROTOBYNUMBER
-        int number = POPi;
+       const int number = POPi;
        pent = PerlSock_getprotobynumber(number);
 #else
        DIE(aTHX_ PL_no_sock_func, "getprotobynumber");
@@ -4736,16 +4725,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_catpvn(sv, " ", 1);
-       }
-       PUSHs(sv = sv_mortalcopy(&PL_sv_no));
-       sv_setiv(sv, (IV)pent->p_proto);
+       PUSHs(sv_2mortal(newSVpv(pent->p_name, 0)));
+       PUSHs(S_space_join_names_mortal(aTHX_ pent->p_aliases));
+       PUSHs(sv_2mortal(newSViv((IV)pent->p_proto)));
     }
 
     RETURN;
@@ -4757,9 +4739,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);
@@ -4770,29 +4751,21 @@ PP(pp_gservent)
 
     if (which == OP_GSBYNAME) {
 #ifdef HAS_GETSERVBYNAME
-       char *proto = POPpbytex;
-       char *name = POPpbytex;
-
-       if (proto && !*proto)
-           proto = Nullch;
-
-       sent = PerlSock_getservbyname(name, proto);
+       const char * const proto = POPpbytex;
+       const char * const name = POPpbytex;
+       sent = PerlSock_getservbyname(name, (proto && !*proto) ? NULL : proto);
 #else
        DIE(aTHX_ PL_no_sock_func, "getservbyname");
 #endif
     }
     else if (which == OP_GSBYPORT) {
 #ifdef HAS_GETSERVBYPORT
-       char *proto = POPpbytex;
+       const char * const proto = POPpbytex;
        unsigned short port = (unsigned short)POPu;
-
-       if (proto && !*proto)
-           proto = Nullch;
-
 #ifdef HAS_HTONS
        port = PerlSock_htons(port);
 #endif
-       sent = PerlSock_getservbyport(port, proto);
+       sent = PerlSock_getservbyport(port, (proto && !*proto) ? NULL : proto);
 #else
        DIE(aTHX_ PL_no_sock_func, "getservbyport");
 #endif
@@ -4822,22 +4795,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_catpvn(sv, " ", 1);
-       }
-       PUSHs(sv = sv_mortalcopy(&PL_sv_no));
+       PUSHs(sv_2mortal(newSVpv(sent->s_name, 0)));
+       PUSHs(S_space_join_names_mortal(aTHX_ sent->s_aliases));
 #ifdef HAS_NTOHS
-       sv_setiv(sv, (IV)PerlSock_ntohs(sent->s_port));
+       PUSHs(sv_2mortal(newSViv((IV)PerlSock_ntohs(sent->s_port))));
 #else
-       sv_setiv(sv, (IV)(sent->s_port));
+       PUSHs(sv_2mortal(newSViv((IV)(sent->s_port))));
 #endif
-       PUSHs(sv = sv_mortalcopy(&PL_sv_no));
-       sv_setpv(sv, sent->s_proto);
+       PUSHs(sv_2mortal(newSVpv(sent->s_proto, 0)));
     }
 
     RETURN;
@@ -4849,7 +4814,7 @@ PP(pp_gservent)
 PP(pp_shostent)
 {
 #ifdef HAS_SETHOSTENT
-    dSP;
+    dVAR; dSP;
     PerlSock_sethostent(TOPi);
     RETSETYES;
 #else
@@ -4860,7 +4825,7 @@ PP(pp_shostent)
 PP(pp_snetent)
 {
 #ifdef HAS_SETNETENT
-    dSP;
+    dVAR; dSP;
     PerlSock_setnetent(TOPi);
     RETSETYES;
 #else
@@ -4871,7 +4836,7 @@ PP(pp_snetent)
 PP(pp_sprotoent)
 {
 #ifdef HAS_SETPROTOENT
-    dSP;
+    dVAR; dSP;
     PerlSock_setprotoent(TOPi);
     RETSETYES;
 #else
@@ -4882,7 +4847,7 @@ PP(pp_sprotoent)
 PP(pp_sservent)
 {
 #ifdef HAS_SETSERVENT
-    dSP;
+    dVAR; dSP;
     PerlSock_setservent(TOPi);
     RETSETYES;
 #else
@@ -4893,7 +4858,7 @@ PP(pp_sservent)
 PP(pp_ehostent)
 {
 #ifdef HAS_ENDHOSTENT
-    dSP;
+    dVAR; dSP;
     PerlSock_endhostent();
     EXTEND(SP,1);
     RETPUSHYES;
@@ -4905,7 +4870,7 @@ PP(pp_ehostent)
 PP(pp_enetent)
 {
 #ifdef HAS_ENDNETENT
-    dSP;
+    dVAR; dSP;
     PerlSock_endnetent();
     EXTEND(SP,1);
     RETPUSHYES;
@@ -4917,7 +4882,7 @@ PP(pp_enetent)
 PP(pp_eprotoent)
 {
 #ifdef HAS_ENDPROTOENT
-    dSP;
+    dVAR; dSP;
     PerlSock_endprotoent();
     EXTEND(SP,1);
     RETPUSHYES;
@@ -4929,7 +4894,7 @@ PP(pp_eprotoent)
 PP(pp_eservent)
 {
 #ifdef HAS_ENDSERVENT
-    dSP;
+    dVAR; dSP;
     PerlSock_endservent();
     EXTEND(SP,1);
     RETPUSHYES;
@@ -4941,7 +4906,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;
@@ -5014,7 +4979,7 @@ PP(pp_gpwent)
     switch (which) {
     case OP_GPWNAM:
       {
-       char* name = POPpbytex;
+       const char* const name = POPpbytex;
        pwent  = getpwnam(name);
       }
       break;
@@ -5053,11 +5018,9 @@ PP(pp_gpwent)
     }
 
     if (pwent) {
-       PUSHs(sv = sv_mortalcopy(&PL_sv_no));
-       sv_setpv(sv, pwent->pw_name);
+       PUSHs(sv_2mortal(newSVpv(pwent->pw_name, 0)));
 
-       PUSHs(sv = sv_mortalcopy(&PL_sv_no));
-       SvPOK_off(sv);
+       PUSHs(sv = sv_2mortal(newSViv(0)));
        /* 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,
@@ -5078,14 +5041,12 @@ PP(pp_gpwent)
         * has a different API than the Solaris/IRIX one. */
 #   if defined(HAS_GETSPNAM) && !defined(_AIX)
        {
-           struct spwd *spwent;
-           int saverrno; /* Save and restore errno so that
+           const int saverrno = errno;
+           const struct spwd * const spwent = getspnam(pwent->pw_name);
+                         /* Save and restore errno so that
                           * underprivileged attempts seem
                           * to have never made the unsccessful
                           * attempt to retrieve the shadow password. */
-
-           saverrno = errno;
-           spwent = getspnam(pwent->pw_name);
            errno = saverrno;
            if (spwent && spwent->sp_pwdp)
                sv_setpv(sv, spwent->sp_pwdp);
@@ -5102,70 +5063,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);
+       PUSHs(sv_2mortal(newSViv((IV)pwent->pw_uid)));
 #   else
-       sv_setuv(sv, (UV)pwent->pw_uid);
+       PUSHs(sv_2mortal(newSVuv((UV)pwent->pw_uid)));
 #   endif
 
-       PUSHs(sv = sv_mortalcopy(&PL_sv_no));
 #   if Uid_t_sign <= 0
-       sv_setiv(sv, (IV)pwent->pw_gid);
+       PUSHs(sv_2mortal(newSViv((IV)pwent->pw_gid)));
 #   else
-       sv_setuv(sv, (UV)pwent->pw_gid);
+       PUSHs(sv_2mortal(newSVuv((UV)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);
+       PUSHs(sv_2mortal(newSViv((IV)pwent->pw_change)));
 #   else
 #       ifdef PWQUOTA
-       sv_setiv(sv, (IV)pwent->pw_quota);
+       PUSHs(sv_2mortal(newSViv((IV)pwent->pw_quota)));
 #       else
 #           ifdef PWAGE
-       sv_setpv(sv, pwent->pw_age);
+       PUSHs(sv_2mortal(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);
+       PUSHs(sv_2mortal(newSVpv(pwent->pw_class, 0)));
 #   else
 #       ifdef PWCOMMENT
-       sv_setpv(sv, pwent->pw_comment);
+       PUSHs(sv_2mortal(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_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);
+       PUSHs(sv_2mortal(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);
+       PUSHs(sv_2mortal(newSViv((IV)pwent->pw_expire)));
 #   endif
     }
     RETURN;
@@ -5177,7 +5138,7 @@ PP(pp_gpwent)
 PP(pp_spwent)
 {
 #if defined(HAS_PASSWD) && defined(HAS_SETPWENT)
-    dSP;
+    dVAR; dSP;
     setpwent();
     RETPUSHYES;
 #else
@@ -5188,7 +5149,7 @@ PP(pp_spwent)
 PP(pp_epwent)
 {
 #if defined(HAS_PASSWD) && defined(HAS_ENDPWENT)
-    dSP;
+    dVAR; dSP;
     endpwent();
     RETPUSHYES;
 #else
@@ -5199,19 +5160,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) {
-        char* name = POPpbytex;
-       grent = (struct group *)getgrnam(name);
+       const char* const name = POPpbytex;
+       grent = (const struct group *)getgrnam(name);
     }
     else if (which == OP_GGRGID) {
-        Gid_t gid = POPi;
-       grent = (struct group *)getgrgid(gid);
+       const Gid_t gid = POPi;
+       grent = (const struct group *)getgrgid(gid);
     }
     else
 #ifdef HAS_GETGRENT
@@ -5222,7 +5181,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);
@@ -5233,19 +5194,17 @@ PP(pp_ggrent)
     }
 
     if (grent) {
-       PUSHs(sv = sv_mortalcopy(&PL_sv_no));
-       sv_setpv(sv, grent->gr_name);
+       PUSHs(sv_2mortal(newSVpv(grent->gr_name, 0)));
 
-       PUSHs(sv = sv_mortalcopy(&PL_sv_no));
 #ifdef GRPASSWD
-       sv_setpv(sv, grent->gr_passwd);
+       PUSHs(sv_2mortal(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);
+       PUSHs(sv_2mortal(newSViv((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
@@ -5254,11 +5213,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_catpvn(sv, " ", 1);
-       }
+       PUSHs(S_space_join_names_mortal(aTHX_ grent->gr_mem));
 #endif
     }
 
@@ -5271,7 +5226,7 @@ PP(pp_ggrent)
 PP(pp_sgrent)
 {
 #if defined(HAS_GROUP) && defined(HAS_SETGRENT)
-    dSP;
+    dVAR; dSP;
     setgrent();
     RETPUSHYES;
 #else
@@ -5282,7 +5237,7 @@ PP(pp_sgrent)
 PP(pp_egrent)
 {
 #if defined(HAS_GROUP) && defined(HAS_ENDGRENT)
-    dSP;
+    dVAR; dSP;
     endgrent();
     RETPUSHYES;
 #else
@@ -5293,7 +5248,7 @@ PP(pp_egrent)
 PP(pp_getlogin)
 {
 #ifdef HAS_GETLOGIN
-    dSP; dTARGET;
+    dVAR; dSP; dTARGET;
     char *tmps;
     EXTEND(SP, 1);
     if (!(tmps = PerlProc_getlogin()))
@@ -5310,7 +5265,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;
@@ -5470,11 +5425,10 @@ static int
 lockf_emulate_flock(int fd, int operation)
 {
     int i;
-    int save_errno;
+    const int save_errno = errno;
     Off_t pos;
 
     /* flock locks entire file so for lockf we need to do the same     */
-    save_errno = errno;
     pos = PerlLIO_lseek(fd, (Off_t)0, SEEK_CUR);    /* get pos to restore later */
     if (pos > 0)       /* is seekable and needs to be repositioned     */
        if (PerlLIO_lseek(fd, (Off_t)0, SEEK_SET) < 0)