diff options
author | Doug MacEachern <dougm@covalent.net> | 2001-11-09 10:13:18 -0800 |
---|---|---|
committer | Abhijit Menon-Sen <ams@wiw.org> | 2001-11-10 03:23:16 +0000 |
commit | e658793210bbe632a5e80a876acfcd0984c46b87 (patch) | |
tree | 6c4d8e35e15182146198f483ba21d8d815c9e266 /util.c | |
parent | 8fe5b9ff3e6e41dabf838b17a3dafbf2b798ab07 (diff) | |
download | perl-e658793210bbe632a5e80a876acfcd0984c46b87.tar.gz |
[patch] my_setenv speedup + fixes
Message-Id: <Pine.LNX.4.21.0111091808500.22397-100000@localhost>
p4raw-id: //depot/perl@12926
Diffstat (limited to 'util.c')
-rw-r--r-- | util.c | 43 |
1 files changed, 32 insertions, 11 deletions
@@ -1496,6 +1496,16 @@ Perl_vwarner(pTHX_ U32 err, const char* pat, va_list* args) } } +/* since we've already done strlen() for both nam and val + * we can use that info to make things faster than + * sprintf(s, "%s=%s", nam, val) + */ +#define my_setenv_format(s, nam, nlen, val, vlen) \ + Copy(nam, s, nlen, char); \ + *(s+nlen) = '='; \ + Copy(val, s+(nlen+1), vlen, char); \ + *(s+(nlen+1+vlen)) = '\0' + #ifdef USE_ENVIRON_ARRAY /* VMS' and EPOC's my_setenv() is in vms.c and epoc.c */ #if !defined(WIN32) && !defined(NETWARE) @@ -1505,6 +1515,7 @@ Perl_my_setenv(pTHX_ char *nam, char *val) #ifndef PERL_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; if (environ == PL_origenviron) { /* need we copy environment? */ I32 j; @@ -1515,8 +1526,9 @@ Perl_my_setenv(pTHX_ char *nam, char *val) for (max = i; environ[max]; max++) ; tmpenv = (char**)safesysmalloc((max+2) * sizeof(char*)); for (j=0; j<max; j++) { /* copy environment */ - tmpenv[j] = (char*)safesysmalloc((strlen(environ[j])+1)*sizeof(char)); - strcpy(tmpenv[j], environ[j]); + int len = strlen(environ[j]); + tmpenv[j] = (char*)safesysmalloc((len+1)*sizeof(char)); + Copy(environ[j], tmpenv[j], len+1, char); } tmpenv[max] = Nullch; environ = tmpenv; /* tell exec where it is now */ @@ -1535,18 +1547,26 @@ Perl_my_setenv(pTHX_ char *nam, char *val) } else safesysfree(environ[i]); - environ[i] = (char*)safesysmalloc((strlen(nam)+strlen(val)+2) * sizeof(char)); + nlen = strlen(nam); + vlen = strlen(val); - (void)sprintf(environ[i],"%s=%s",nam,val);/* all that work just for this */ + 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 */ # if defined(__CYGWIN__) setenv(nam, val, 1); # else char *new_env; - - new_env = (char*)safesysmalloc((strlen(nam) + strlen(val) + 2) * sizeof(char)); - (void)sprintf(new_env,"%s=%s",nam,val);/* all that work just for this */ + int nlen = strlen(nam), vlen; + if (!val) { + val = ""; + } + vlen = strlen(val); + new_env = (char*)safesysmalloc((nlen + vlen + 2) * sizeof(char)); + /* all that work just for this */ + my_setenv_format(new_env, nam, nlen, val, vlen); (void)putenv(new_env); # endif /* __CYGWIN__ */ #endif /* PERL_USE_SAFE_PUTENV */ @@ -1558,13 +1578,14 @@ void Perl_my_setenv(pTHX_ char *nam,char *val) { register char *envstr; - STRLEN len = strlen(nam) + 3; + int nlen = strlen(nam), vlen; + if (!val) { val = ""; } - len += strlen(val); - New(904, envstr, len, char); - (void)sprintf(envstr,"%s=%s",nam,val); + vlen = strlen(val); + New(904, envstr, nlen+vlen+2, char); + my_setenv_format(envstr, nam, nlen, val, vlen); (void)PerlEnv_putenv(envstr); Safefree(envstr); } |