struct ul_env_list {
- char *env;
+ char *name;
+ char *value;
+
struct ul_env_list *next;
};
/*
- * Saves @name env.variable to @ls, returns pointer to the new head of the list.
+ * Saves to the list and returns a pointer to the new head of the list.
*/
-static struct ul_env_list *env_list_add(struct ul_env_list *ls0, const char *str)
+static struct ul_env_list *env_list_add( struct ul_env_list *ls0,
+ const char *name, size_t namesz,
+ const char *value, size_t valsz)
{
struct ul_env_list *ls;
- char *p;
- size_t sz = 0;
- if (!str || !*str)
+ ls = calloc(1, sizeof(struct ul_env_list) + namesz + valsz + 2);
+ if (!ls)
return ls0;
- sz = strlen(str) + 1;
- p = malloc(sizeof(struct ul_env_list) + sz);
- if (!p)
- return ls0;
+ ls->name = ((char *) ls) + sizeof(struct ul_env_list);
+ ls->value = ls->name + namesz + 1;
- ls = (struct ul_env_list *) p;
- p += sizeof(struct ul_env_list);
- memcpy(p, str, sz);
- ls->env = p;
+ memcpy(ls->name, name, namesz);
+ memcpy(ls->value, value, valsz);
ls->next = ls0;
return ls;
}
+
+/*
+ * Saves the @str (with the name=value string) to the @ls and returns a pointer
+ * to the new head of the list.
+ */
+static struct ul_env_list *env_list_add_from_string(struct ul_env_list *ls,
+ const char *str)
+{
+ size_t namesz = 0, valsz = 0;
+ const char *val;
+
+ if (!str || !*str)
+ return ls;
+
+ val = strchr(str, '=');
+ if (!val)
+ return NULL;
+ namesz = val - str;
+
+ val++;
+ valsz = strlen(val);
+
+ return env_list_add(ls, str, namesz, val, valsz);
+}
+
+/*
+ * Saves the @name and @value to @ls0 and returns a pointer to the new head of
+ * the list.
+ */
+struct ul_env_list *env_list_add_variable(
+ struct ul_env_list *ls,
+ const char *name, const char *value)
+{
+ if (!name || !*name)
+ return ls;
+
+ return env_list_add(ls, name, strlen(name), value, value ? strlen(value) : 0);
+}
+
/*
* Use env_from_fd() to read environment from @fd.
*
p = buf;
while (rc > 0) {
- ls = env_list_add(ls, p);
+ ls = env_list_add_from_string(ls, p);
p += strlen(p) + 1;
rc -= strlen(p) + 1;
}
/*
* Use setenv() for all stuff in @ls.
- *
- * It would be possible to use putenv(), but we want to keep @ls free()-able.
*/
int env_list_setenv(struct ul_env_list *ls)
{
int rc = 0;
while (ls && rc == 0) {
- if (ls->env) {
- char *val = strchr(ls->env, '=');
- if (!val)
- continue;
- *val = '\0';
- rc = setenv(ls->env, val + 1, 0);
- *val = '=';
- }
+ if (ls->name && ls->value)
+ rc = setenv(ls->name, ls->value, 0);
ls = ls->next;
}
return rc;
for (bad = forbid; *bad; bad++) {
if (strncmp(*cur, *bad, strlen(*bad)) == 0) {
if (org)
- *org = env_list_add(*org, *cur);
+ *org = env_list_add_from_string(*org, *cur);
last = remove_entry(envp, cur - envp, last);
cur--;
break;
if (!strchr(*cur, '/'))
continue; /* OK */
if (org)
- *org = env_list_add(*org, *cur);
+ *org = env_list_add_from_string(*org, *cur);
last = remove_entry(envp, cur - envp, last);
cur--;
break;
int retval = EXIT_SUCCESS;
struct ul_env_list *removed = NULL;
+ /* define env. */
for (bad = forbid; *bad; bad++) {
strcpy(copy, *bad);
p = strchr(copy, '=');
if (p)
*p = '\0';
- setenv(copy, copy, 1);
+ setenv(copy, "abc", 1);
}
/* removed */