This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Encoding neutral unpack
[perl5.git] / util.c
diff --git a/util.c b/util.c
index 52319d3..fc99463 100644 (file)
--- a/util.c
+++ b/util.c
@@ -1,7 +1,7 @@
 /*    util.c
  *
  *    Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999,
- *    2000, 2001, 2002, 2003, 2004, by Larry Wall and others
+ *    2000, 2001, 2002, 2003, 2004, 2005, 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.
@@ -758,10 +758,18 @@ char *
 Perl_savepv(pTHX_ const char *pv)
 {
     register char *newaddr;
+#ifdef PERL_MALLOC_WRAP
+    STRLEN pvlen;
+#endif
     if (!pv)
        return Nullch;
 
+#ifdef PERL_MALLOC_WRAP
+    pvlen = strlen(pv)+1;
+    New(902,newaddr,pvlen,char);
+#else
     New(902,newaddr,strlen(pv)+1,char);
+#endif
     return strcpy(newaddr,pv);
 }
 
@@ -819,6 +827,26 @@ Perl_savesharedpv(pTHX_ const char *pv)
     return strcpy(newaddr,pv);
 }
 
+/*
+=for apidoc savesvpv
+
+A version of C<savepv()>/C<savepvn()> which gets the string to duplicate from
+the passed in SV using C<SvPV()>
+
+=cut
+*/
+
+char *
+Perl_savesvpv(pTHX_ SV *sv)
+{
+    STRLEN len;
+    const char *pv = SvPV(sv, len);
+    register char *newaddr;
+
+    ++len;
+    New(903,newaddr,len,char);
+    return CopyD(pv,newaddr,len,char);
+}
 
 
 /* the SV for Perl_form() and mess() is not kept in an arena */
@@ -1372,6 +1400,7 @@ Perl_my_setenv(pTHX_ char *nam, char *val)
 #endif
   {
 #ifndef PERL_USE_SAFE_PUTENV
+    if (!PL_use_safe_putenv) {
     /* most putenv()s leak, so we manipulate environ directly */
     register I32 i=setenv_getix(nam);          /* where does it go? */
     int nlen, vlen;
@@ -1412,8 +1441,8 @@ Perl_my_setenv(pTHX_ char *nam, char *val)
     environ[i] = (char*)safesysmalloc((nlen+vlen+2) * sizeof(char));
     /* all that work just for this */
     my_setenv_format(environ[i], nam, nlen, val, vlen);
-
-#else   /* PERL_USE_SAFE_PUTENV */
+    } else {
+# endif
 #   if defined(__CYGWIN__) || defined( EPOC)
     setenv(nam, val, 1);
 #   else
@@ -1428,7 +1457,9 @@ Perl_my_setenv(pTHX_ char *nam, char *val)
     my_setenv_format(new_env, nam, nlen, val, vlen);
     (void)putenv(new_env);
 #   endif /* __CYGWIN__ */
-#endif  /* PERL_USE_SAFE_PUTENV */
+#ifndef PERL_USE_SAFE_PUTENV
+    }
+#endif
   }
 }
 
@@ -2042,7 +2073,7 @@ Perl_my_popen(pTHX_ char *cmd, char *mode)
     register I32 This, that;
     register Pid_t pid;
     SV *sv;
-    I32 doexec = strNE(cmd,"-");
+    I32 doexec = !(*cmd == '-' && cmd[1] == '\0');
     I32 did_pipes = 0;
     int pp[2];
 
@@ -2536,7 +2567,7 @@ Perl_wait4pid(pTHX_ Pid_t pid, int *statusp, int flags)
     {
        SV *sv;
        SV** svp;
-       char spid[TYPE_CHARS(int)];
+       char spid[TYPE_CHARS(IV)];
 
        if (pid > 0) {
            sprintf(spid, "%"IVdf, (IV)pid);
@@ -2552,9 +2583,6 @@ Perl_wait4pid(pTHX_ Pid_t pid, int *statusp, int flags)
 
            hv_iterinit(PL_pidstatus);
            if ((entry = hv_iternext(PL_pidstatus))) {
-               SV *sv;
-               char spid[TYPE_CHARS(int)];
-
                pid = atoi(hv_iterkey(entry,(I32*)statusp));
                sv = hv_iterval(PL_pidstatus,entry);
                *statusp = SvIVX(sv);
@@ -2603,7 +2631,7 @@ void
 Perl_pidgone(pTHX_ Pid_t pid, int status)
 {
     register SV *sv;
-    char spid[TYPE_CHARS(int)];
+    char spid[TYPE_CHARS(IV)];
 
     sprintf(spid, "%"IVdf, (IV)pid);
     sv = *hv_fetch(PL_pidstatus,spid,strlen(spid),TRUE);
@@ -3976,8 +4004,7 @@ Perl_upg_version(pTHX_ SV *ver)
 #endif
     else /* must be a string or something like a string */
     {
-       STRLEN n_a;
-       version = savepv(SvPV(ver,n_a));
+       version = savesvpv(ver);
     }
     (void)scan_version(version, ver, qv);
     Safefree(version);