e: cleanup env var setting

Return early if there is nothing to append/prepend.
Only add ':' if there are two elements to join.

SVN revision: 77295
This commit is contained in:
Sebastian Dransfeld 2012-10-02 09:26:30 +00:00
parent d6e1350427
commit 5969b88a6a
1 changed files with 22 additions and 17 deletions

View File

@ -199,15 +199,18 @@ copy_args(char **dst, char **src, size_t count)
static void
_env_path_prepend(const char *env, const char *path)
{
char *p, *p2, *s;
char *p, *s;
const char *p2;
int len = 0, len2 = 0;
if (!path) return;
p = getenv(env);
if (p) len = strlen(p);
p2 = (char *)path;
if (p2) len2 = strlen(p2);
if (p && p2)
p2 = path;
len2 = strlen(p2);
if (p)
{
len = strlen(p);
// path already there at the start. dont prepend. :)
if ((!strcmp(p, p2)) ||
((len > len2) &&
@ -219,12 +222,12 @@ _env_path_prepend(const char *env, const char *path)
if (s)
{
s[0] = 0;
if (p2)
strcat(s, p2);
if (p)
{
strcat(s, p2);
strcat(s, ":");
strcat(s, p);
}
if (p) strcat(s, p);
env_set(env, s);
free(s);
}
@ -233,16 +236,18 @@ _env_path_prepend(const char *env, const char *path)
static void
_env_path_append(const char *env, const char *path)
{
char *p, *p2, *s;
char *p, *s;
const char *p2;
int len = 0, len2 = 0;
if (!path) return;
p = getenv(env);
if (!p) return;
len = strlen(p);
p2 = (char *)path;
if (p2) len2 = strlen(p2);
if (p && p2)
p2 = path;
len2 = strlen(p2);
if (p)
{
len = strlen(p);
// path already there at the end. dont append. :)
if ((!strcmp(p, p2)) ||
((len > len2) &&
@ -254,12 +259,12 @@ _env_path_append(const char *env, const char *path)
if (s)
{
s[0] = 0;
if (p) strcat(s, p);
if (p2)
if (p)
{
strcat(s, p);
strcat(s, ":");
strcat(s, p2);
}
strcat(s, p2);
env_set(env, s);
free(s);
}