Minor cleanups
authorGurusamy Sarathy <gsar@engin.umich.edu>
Fri, 4 Apr 1997 00:56:57 +0000 (19:56 -0500)
committerChip Salzenberg <chip@atlantic.net>
Wed, 2 Apr 1997 22:03:25 +0000 (10:03 +1200)
This patch straightens out some code that used to be crooked,
avoids redundant SV allocs/frees, and makes a macro more
consistent.  Nothing critical here.

The changes in ENV_FETCH code are untested, so vox VMS populi
invited.

p5p-msgid: 199704040056.TAA22253@aatma.engin.umich.edu

mg.c
mg.h
perl.c

diff --git a/mg.c b/mg.c
index 5b25c40..b4b8e3e 100644 (file)
--- a/mg.c
+++ b/mg.c
@@ -581,27 +581,20 @@ MAGIC* mg;
 {
     register char *s;
     char *ptr;
-    STRLEN len;
+    STRLEN len, klen;
     I32 i;
 
     s = SvPV(sv,len);
-    ptr = MgPV(mg);
+    ptr = MgPV(mg,klen);
     my_setenv(ptr, s);
 
 #ifdef DYNAMIC_ENV_FETCH
      /* We just undefd an environment var.  Is a replacement */
      /* waiting in the wings? */
     if (!len) {
-       HE *envhe;
-       SV *keysv;
-       if (mg->mg_len == HEf_SVKEY)
-           keysv = (SV *)mg->mg_ptr;
-       else
-           keysv = newSVpv(mg->mg_ptr, mg->mg_len);
-       if ((envhe = hv_fetch_ent(GvHVn(envgv), keysv, FALSE, 0)))
-           s = SvPV(HeVAL(envhe), len);
-       if (mg->mg_len != HEf_SVKEY)
-           SvREFCNT_dec(keysv);
+       SV **valp;
+       if ((valp = hv_fetch(GvHVn(envgv), ptr, klen, FALSE)))
+           s = SvPV(*valp, len);
     }
 #endif
 
@@ -611,7 +604,7 @@ MAGIC* mg;
     if (tainting) {
        MgTAINTEDDIR_off(mg);
 #ifdef VMS
-       if (s && strnEQ(ptr, "DCL$PATH", 8)) {
+       if (s && klen == 8 && strEQ(ptr, "DCL$PATH")) {
            char pathbuf[256], eltbuf[256], *cp, *elt = s;
            struct stat sbuf;
            int i = 0, j = 0;
@@ -636,7 +629,7 @@ MAGIC* mg;
            } while (my_trnlnm(s, pathbuf, i++) && (elt = pathbuf));
        }
 #endif /* VMS */
-       if (s && strEQ(ptr,"PATH")) {
+       if (s && klen == 4 && strEQ(ptr,"PATH")) {
            char *strend = s + len;
 
            while (s < strend) {
@@ -661,7 +654,7 @@ magic_clearenv(sv,mg)
 SV* sv;
 MAGIC* mg;
 {
-    my_setenv(MgPV(mg),Nullch);
+    my_setenv(MgPV(mg,na),Nullch);
     return 0;
 }
 
@@ -672,7 +665,7 @@ MAGIC* mg;
 {
     I32 i;
     /* Are we fetching a signal entry? */
-    i = whichsig(MgPV(mg));
+    i = whichsig(MgPV(mg,na));
     if (i) {
        if(psig_ptr[i])
            sv_setsv(sv,psig_ptr[i]);
@@ -697,7 +690,7 @@ MAGIC* mg;
 {
     I32 i;
     /* Are we clearing a signal entry? */
-    i = whichsig(MgPV(mg));
+    i = whichsig(MgPV(mg,na));
     if (i) {
        if(psig_ptr[i]) {
            SvREFCNT_dec(psig_ptr[i]);
@@ -720,7 +713,7 @@ MAGIC* mg;
     I32 i;
     SV** svp;
 
-    s = MgPV(mg);
+    s = MgPV(mg,na);
     if (*s == '_') {
        if (strEQ(s,"__DIE__"))
            svp = &diehook;
@@ -958,7 +951,7 @@ MAGIC* mg;
     gv = DBline;
     i = SvTRUE(sv);
     svp = av_fetch(GvAV(gv),
-                    atoi(MgPV(mg)), FALSE);
+                    atoi(MgPV(mg,na)), FALSE);
     if (svp && SvIOKp(*svp) && (o = (OP*)SvSTASH(*svp)))
        o->op_private = i;
     else
diff --git a/mg.h b/mg.h
index c40a866..c464746 100644 (file)
--- a/mg.h
+++ b/mg.h
@@ -36,6 +36,6 @@ struct magic {
 #define MgTAINTEDDIR_on(mg)    (mg->mg_flags |= MGf_TAINTEDDIR)
 #define MgTAINTEDDIR_off(mg)   (mg->mg_flags &= ~MGf_TAINTEDDIR)
 
-#define MgPV(mg)                ((mg)->mg_len == HEf_SVKEY) ?   \
-                                SvPV((SV*)((mg)->mg_ptr),na) :   \
-                                (mg)->mg_ptr
+#define MgPV(mg,lp)            (((lp = (mg)->mg_len) == HEf_SVKEY) ?   \
+                                SvPV((SV*)((mg)->mg_ptr),lp) :         \
+                                (mg)->mg_ptr)
diff --git a/perl.c b/perl.c
index 9f06f13..2b53a81 100644 (file)
--- a/perl.c
+++ b/perl.c
@@ -2278,7 +2278,7 @@ register char **env;
        HV *hv;
        GvMULTI_on(envgv);
        hv = GvHVn(envgv);
-       hv_clear(hv);
+       hv_magic(hv, envgv, 'E');
 #ifndef VMS  /* VMS doesn't have environ array */
        /* Note that if the supplied env parameter is actually a copy
           of the global environ then it may now point to free'd memory
@@ -2287,16 +2287,13 @@ register char **env;
        */
        if (!env)
            env = environ;
-       if (env != environ) {
+       if (env != environ)
            environ[0] = Nullch;
-           hv_magic(hv, envgv, 'E');
-       }
        for (; *env; env++) {
            if (!(s = strchr(*env,'=')))
                continue;
            *s++ = '\0';
            sv = newSVpv(s--,0);
-           sv_magic(sv, sv, 'e', *env, s - *env);
            (void)hv_store(hv, *env, s - *env, sv, 0);
            *s = '=';
        }
@@ -2304,7 +2301,6 @@ register char **env;
 #ifdef DYNAMIC_ENV_FETCH
        HvNAME(hv) = savepv(ENV_HV_NAME);
 #endif
-       hv_magic(hv, envgv, 'E');
     }
     TAINT_NOT;
     if (tmpgv = gv_fetchpv("$",TRUE, SVt_PV))