[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [RFC v3 04/10] linux-user: Implement envlist_appendenv
From: |
Alex Bennée |
Subject: |
Re: [RFC v3 04/10] linux-user: Implement envlist_appendenv |
Date: |
Mon, 03 Jul 2023 17:38:55 +0100 |
User-agent: |
mu4e 1.11.8; emacs 29.0.92 |
Yeqi Fu <fufuyqqqqqq@gmail.com> writes:
> Signed-off-by: Yeqi Fu <fufuyqqqqqq@gmail.com>
> ---
> include/qemu/envlist.h | 1 +
> util/envlist.c | 61 ++++++++++++++++++++++++++++++++++++++++++
> 2 files changed, 62 insertions(+)
>
> diff --git a/include/qemu/envlist.h b/include/qemu/envlist.h
> index 6006dfae44..865eb18e17 100644
> --- a/include/qemu/envlist.h
> +++ b/include/qemu/envlist.h
> @@ -7,6 +7,7 @@ envlist_t *envlist_create(void);
> void envlist_free(envlist_t *);
> int envlist_setenv(envlist_t *, const char *);
> int envlist_unsetenv(envlist_t *, const char *);
> +int envlist_appendenv(envlist_t *, const char *, const char *);
> int envlist_parse_set(envlist_t *, const char *);
> int envlist_parse_unset(envlist_t *, const char *);
> char **envlist_to_environ(const envlist_t *, size_t *);
> diff --git a/util/envlist.c b/util/envlist.c
> index db937c0427..635c9c4fab 100644
> --- a/util/envlist.c
> +++ b/util/envlist.c
> @@ -201,6 +201,67 @@ envlist_unsetenv(envlist_t *envlist, const char *env)
> return (0);
> }
>
> +/*
> + * Appends environment value to envlist. If the environment
> + * variable already exists, the new value is appended to the
> + * existing one.
> + *
> + * Returns 0 in success, errno otherwise.
> + */
> +int
> +envlist_appendenv(envlist_t *envlist, const char *env, const char *separator)
> +{
> + struct envlist_entry *entry = NULL;
> + const char *eq_sign;
> + size_t envname_len;
> +
> + if ((envlist == NULL) || (env == NULL) || (separator == NULL)) {
> + return (EINVAL);
No () around the EINVAL needed here.
> + }
> +
> + /* find out first equals sign in given env */
> + eq_sign = strchr(env, '=');
> + if (eq_sign == NULL) {
> + return (EINVAL);
> + }
> +
> + if (strchr(eq_sign + 1, '=') != NULL) {
> + return (EINVAL);
> + }
> +
> + envname_len = eq_sign - env + 1;
> +
> + /*
> + * If there already exists variable with given name,
> + * we append the new value to the existing one.
> + */
> + for (entry = envlist->el_entries.lh_first; entry != NULL;
> + entry = entry->ev_link.le_next) {
> + if (strncmp(entry->ev_var, env, envname_len) == 0) {
> + break;
> + }
> + }
> +
> + if (entry != NULL) {
> + char *new_env_value = NULL;
> + size_t new_env_len = strlen(entry->ev_var) + strlen(eq_sign)
> + + strlen(separator) + 1;
> + new_env_value = g_malloc(new_env_len);
> + strcpy(new_env_value, entry->ev_var);
> + strcat(new_env_value, separator);
> + strcat(new_env_value, eq_sign + 1);
> + g_free((char *)entry->ev_var);
> + entry->ev_var = new_env_value;
> + } else {
> + envlist->el_count++;
> + entry = g_malloc(sizeof(*entry));
> + entry->ev_var = g_strdup(env);
> + QLIST_INSERT_HEAD(&envlist->el_entries, entry, ev_link);
> + }
> +
> + return (0);
> +}
> +
We really should add something to tests/unit/test-env to check the
various operations work as expected.
> /*
> * Returns given envlist as array of strings (in same form that
> * global variable environ is). Caller must free returned memory
--
Alex Bennée
Virtualisation Tech Lead @ Linaro
- Re: [RFC v3 04/10] linux-user: Implement envlist_appendenv,
Alex Bennée <=