Oops. Forgot to add this file.
[p5sagit/p5-mst-13.2.git] / mg.c
diff --git a/mg.c b/mg.c
index aa7ef7d..c277576 100644 (file)
--- a/mg.c
+++ b/mg.c
@@ -86,11 +86,10 @@ S_save_magic(pTHX_ I32 mgs_ix, SV *sv)
     dVAR;
     MGS* mgs;
     assert(SvMAGICAL(sv));
-#ifdef PERL_OLD_COPY_ON_WRITE
-    /* Turning READONLY off for a copy-on-write scalar is a bad idea.  */
+    /* Turning READONLY off for a copy-on-write scalar (including shared
+       hash keys) is a bad idea.  */
     if (SvIsCOW(sv))
       sv_force_normal_flags(sv, 0);
-#endif
 
     SAVEDESTRUCTOR_X(S_restore_magic, INT2PTR(void*, (IV)mgs_ix));
 
@@ -409,7 +408,7 @@ Perl_mg_localize(pTHX_ SV *sv, SV *nsv)
     dVAR;
     MAGIC *mg;
     for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
-       const MGVTBL* const vtbl = mg->mg_virtual;
+       MGVTBL* const vtbl = mg->mg_virtual;
        switch (mg->mg_type) {
        /* value magic types: don't copy */
        case PERL_MAGIC_bm:
@@ -642,10 +641,14 @@ Perl_magic_len(pTHX_ SV *sv, MAGIC *mg)
 }
 
 #define SvRTRIM(sv) STMT_START { \
-    STRLEN len = SvCUR(sv); \
-    while (len > 0 && isSPACE(SvPVX(sv)[len-1])) \
-       --len; \
-    SvCUR_set(sv, len); \
+    if (SvPOK(sv)) { \
+        STRLEN len = SvCUR(sv); \
+        char * const p = SvPVX(sv); \
+       while (len > 0 && isSPACE(p[len-1])) \
+          --len; \
+       SvCUR_set(sv, len); \
+       p[len] = '\0'; \
+    } \
 } STMT_END
 
 int
@@ -677,15 +680,14 @@ Perl_magic_get(pTHX_ SV *sv, MAGIC *mg)
        break;
     case '\005':  /* ^E */
         if (nextchar == '\0') {
-#ifdef MACOS_TRADITIONAL
+#if defined(MACOS_TRADITIONAL)
             {
                  char msg[256];
 
                  sv_setnv(sv,(double)gMacPerl_OSErr);
                  sv_setpv(sv, gMacPerl_OSErr ? GetSysErrText(gMacPerl_OSErr, msg) : "");
             }
-#else
-#ifdef VMS
+#elif defined(VMS)
             {
 #                include <descrip.h>
 #                include <starlet.h>
@@ -697,8 +699,7 @@ Perl_magic_get(pTHX_ SV *sv, MAGIC *mg)
                  else
                       sv_setpvn(sv,"",0);
             }
-#else
-#ifdef OS2
+#elif defined(OS2)
             if (!(_emx_env & 0x200)) { /* Under DOS */
                  sv_setnv(sv, (NV)errno);
                  sv_setpv(sv, errno ? Strerror(errno) : "");
@@ -711,8 +712,7 @@ Perl_magic_get(pTHX_ SV *sv, MAGIC *mg)
                  sv_setnv(sv, (NV)Perl_rc);
                  sv_setpv(sv, os2error(Perl_rc));
             }
-#else
-#ifdef WIN32
+#elif defined(WIN32)
             {
                  DWORD dwErr = GetLastError();
                  sv_setnv(sv, (NV)dwErr);
@@ -731,9 +731,6 @@ Perl_magic_get(pTHX_ SV *sv, MAGIC *mg)
                 errno = saveerrno;
             }
 #endif
-#endif
-#endif
-#endif
             SvRTRIM(sv);
             SvNOK_on(sv);      /* what a wonderful hack! */
         }
@@ -816,7 +813,7 @@ Perl_magic_get(pTHX_ SV *sv, MAGIC *mg)
                 * it could have been extended by warnings::register */
                SV **bits_all;
                HV * const bits=get_hv("warnings::Bits", FALSE);
-               if (bits && (bits_all=hv_fetch(bits, "all", 3, FALSE))) {
+               if (bits && (bits_all=hv_fetchs(bits, "all", FALSE))) {
                    sv_setsv(sv, *bits_all);
                }
                else {
@@ -1002,29 +999,22 @@ Perl_magic_get(pTHX_ SV *sv, MAGIC *mg)
        break;
     case '(':
        sv_setiv(sv, (IV)PL_gid);
-#ifdef HAS_GETGROUPS
-       Perl_sv_setpvf(aTHX_ sv, "%"Gid_t_f, (long unsigned int)PL_gid);
-#endif
        goto add_groups;
     case ')':
        sv_setiv(sv, (IV)PL_egid);
-#ifdef HAS_GETGROUPS
-       Perl_sv_setpvf(aTHX_ sv, "%"Gid_t_f, (long unsigned int)PL_egid);
-#endif
       add_groups:
 #ifdef HAS_GETGROUPS
        {
            Groups_t *gary = NULL;
-           I32 num_groups = getgroups(0, gary);
+           I32 i, num_groups = getgroups(0, gary);
             Newx(gary, num_groups, Groups_t);
             num_groups = getgroups(num_groups, gary);
-           while (--num_groups >= 0)
-               Perl_sv_catpvf(aTHX_ sv, " %"Gid_t_f,
-                    (long unsigned int)gary[num_groups]);
+           for (i = 0; i < num_groups; i++)
+               Perl_sv_catpvf(aTHX_ sv, " %"IVdf, (IV)gary[i]);
             Safefree(gary);
        }
-#endif
        (void)SvIOK_on(sv);     /* what a wonderful hack! */
+#endif
        break;
 #ifndef MACOS_TRADITIONAL
     case '0':
@@ -1257,7 +1247,7 @@ Perl_magic_clearsig(pTHX_ SV *sv, MAGIC *mg)
                PL_psig_name[i]=0;
            }
            if(PL_psig_ptr[i]) {
-                SV *to_dec=PL_psig_ptr[i];
+               SV * const to_dec=PL_psig_ptr[i];
                PL_psig_ptr[i]=0;
                LEAVE;
                SvREFCNT_dec(to_dec);
@@ -1964,12 +1954,11 @@ Perl_magic_getvec(pTHX_ SV *sv, MAGIC *mg)
     SV * const lsv = LvTARG(sv);
     PERL_UNUSED_ARG(mg);
 
-    if (!lsv) {
+    if (lsv)
+       sv_setuv(sv, do_vecget(lsv, LvTARGOFF(sv), LvTARGLEN(sv)));
+    else
        SvOK_off(sv);
-       return 0;
-    }
 
-    sv_setuv(sv, do_vecget(lsv, LvTARGOFF(sv), LvTARGLEN(sv)));
     return 0;
 }
 
@@ -2540,7 +2529,7 @@ Perl_magic_set(pTHX_ SV *sv, MAGIC *mg)
        /* The BSDs don't show the argv[] in ps(1) output, they
         * show a string from the process struct and provide
         * the setproctitle() routine to manipulate that. */
-       {
+       if (PL_origalen != 1) {
            s = SvPV_const(sv, len);
 #   if __FreeBSD_version > 410001
            /* The leading "-" removes the "perl: " prefix,
@@ -2561,35 +2550,37 @@ Perl_magic_set(pTHX_ SV *sv, MAGIC *mg)
        }
 #endif
 #if defined(__hpux) && defined(PSTAT_SETCMD)
-       {
+       if (PL_origalen != 1) {
             union pstun un;
             s = SvPV_const(sv, len);
             un.pst_command = (char *)s;
             pstat(PSTAT_SETCMD, un, len, 0, 0);
        }
 #endif
-       /* PL_origalen is set in perl_parse(). */
-       s = SvPV_force(sv,len);
-       if (len >= (STRLEN)PL_origalen-1) {
-           /* Longer than original, will be truncated. We assume that
-             * PL_origalen bytes are available. */
-           Copy(s, PL_origargv[0], PL_origalen-1, char);
+       if (PL_origalen > 1) {
+           /* PL_origalen is set in perl_parse(). */
+           s = SvPV_force(sv,len);
+           if (len >= (STRLEN)PL_origalen-1) {
+               /* Longer than original, will be truncated. We assume that
+                * PL_origalen bytes are available. */
+               Copy(s, PL_origargv[0], PL_origalen-1, char);
+           }
+           else {
+               /* Shorter than original, will be padded. */
+               Copy(s, PL_origargv[0], len, char);
+               PL_origargv[0][len] = 0;
+               memset(PL_origargv[0] + len + 1,
+                      /* Is the space counterintuitive?  Yes.
+                       * (You were expecting \0?)  
+                       * Does it work?  Seems to.  (In Linux 2.4.20 at least.)
+                       * --jhi */
+                      (int)' ',
+                      PL_origalen - len - 1);
+           }
+           PL_origargv[0][PL_origalen-1] = 0;
+           for (i = 1; i < PL_origargc; i++)
+               PL_origargv[i] = 0;
        }
-       else {
-           /* Shorter than original, will be padded. */
-           Copy(s, PL_origargv[0], len, char);
-           PL_origargv[0][len] = 0;
-           memset(PL_origargv[0] + len + 1,
-                  /* Is the space counterintuitive?  Yes.
-                   * (You were expecting \0?)  
-                   * Does it work?  Seems to.  (In Linux 2.4.20 at least.)
-                   * --jhi */
-                  (int)' ',
-                  PL_origalen - len - 1);
-       }
-       PL_origargv[0][PL_origalen-1] = 0;
-       for (i = 1; i < PL_origargc; i++)
-           PL_origargv[i] = 0;
        UNLOCK_DOLLARZERO_MUTEX;
        break;
 #endif