aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/progman.c82
1 files changed, 57 insertions, 25 deletions
diff --git a/src/progman.c b/src/progman.c
index fdb829a..43aff48 100644
--- a/src/progman.c
+++ b/src/progman.c
@@ -659,22 +659,44 @@ add_env (const char *name, const char *value)
-static char *
-find_env (const char *name, int val)
+/* Find variable NAME in environment ENV.
+ On success, store the index of the ENV slot in *IDX,
+ the offset of the value (position right past '=') in *VALOFF, and
+ return 0 (IDX and/or VALOFF can be NULL, if that info is not needed).
+ Return -1 if NAME was not found. */
+static int
+find_env_pos (char **env, char *name, size_t *idx, size_t *valoff)
{
- if (environ)
- {
- int nlen = strcspn (name, "+=");
- int i;
+ size_t nlen = strcspn (name, "+=");
+ size_t i;
- for (i = 0; environ[i]; i++)
+ for (i = 0; env[i]; i++)
+ {
+ size_t elen = strcspn (env[i], "=");
+ if (elen == nlen && memcmp (name, env[i], nlen) == 0)
{
- size_t elen = strcspn (environ[i], "=");
- if (elen == nlen && memcmp (name, environ[i], nlen) == 0)
- return val ? environ[i] + elen + 1 : environ[i];
+ if (idx)
+ *idx = i;
+ if (valoff)
+ *valoff = elen + 1;
+ return 0;
}
}
- return NULL;
+ return -1;
+}
+
+/* Find variable NAME in environment ENV.
+ On success, return pointer to the variable assignment (if VAL is 0),
+ or to the value (if VAL is 1).
+ Return NULL if NAME is not present in ENV. */
+static char *
+find_env_ptr (char **env, char *name, int val)
+{
+ size_t i, j;
+ if (find_env_pos (env, name, &i, &j))
+ return NULL;
+ return val ? env[i] + j : env[i];
}
+/* Return 1 if ENV contains a matching unset statement for variable NAME. */
static int
-locate_unset (char **env, const char *name)
+var_is_unset (char **env, const char *name)
{
@@ -710,3 +732,3 @@ env_concat (const char *name, size_t namelen, const char *a, const char *b)
strcpy (res + namelen + 1, a);
- strcat (res, b);
+ strcat (res + namelen + 1, b);
}
@@ -739,3 +761,3 @@ environ_setup (char **hint)
char **new_env;
- size_t count, i, n;
+ size_t count, i, j, n;
@@ -768,3 +790,3 @@ environ_setup (char **hint)
{
- if (!locate_unset (hint, old_env[i]))
+ if (!var_is_unset (hint, old_env[i]))
new_env[n++] = old_env[i];
@@ -781,2 +803,8 @@ environ_setup (char **hint)
}
+
+ /* Find the slot for the variable. Use next available
+ slot if there's no such variable in new_env */
+ if (find_env_pos (new_env, hint[i], &j, NULL))
+ j = n;
+
if ((p = strchr (hint[i], '=')))
@@ -786,16 +814,20 @@ environ_setup (char **hint)
if (p[-1] == '+')
- new_env[n++] = env_concat (hint[i], p - hint[i] - 1,
- find_env(hint[i], 1), p + 1);
+ new_env[j] = env_concat (hint[i], p - hint[i] - 1,
+ find_env_ptr (environ, hint[i], 1),
+ p + 1);
else if (p[1] == '+')
- new_env[n++] = env_concat (hint[i], p - hint[i],
- p + 2, find_env(hint[i], 1));
+ new_env[j] = env_concat (hint[i], p - hint[i],
+ p + 2,
+ find_env_ptr (environ, hint[i], 1));
else
- new_env[n++] = hint[i];
+ new_env[j] = hint[i];
}
+ else if ((p = find_env_ptr (environ, hint[i], 0)))
+ new_env[j] = p;
else
- {
- p = find_env (hint[i], 0);
- if (p)
- new_env[n++] = p;
- }
+ continue;
+
+ /* Adjust environment size */
+ if (j == n)
+ ++n;
}

Return to:

Send suggestions and report system problems to the System administrator.