[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[RFC v3 04/10] linux-user: Implement envlist_appendenv
From: |
Yeqi Fu |
Subject: |
[RFC v3 04/10] linux-user: Implement envlist_appendenv |
Date: |
Mon, 26 Jun 2023 05:27:01 +0800 |
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);
+ }
+
+ /* 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);
+}
+
/*
* Returns given envlist as array of strings (in same form that
* global variable environ is). Caller must free returned memory
--
2.34.1
- [RFC v3 00/10] Native Library Calls, Yeqi Fu, 2023/06/25
- [RFC v3 01/10] docs: Add specification for native library calls, Yeqi Fu, 2023/06/25
- [RFC v3 02/10] build: Add configure options for native calls, Yeqi Fu, 2023/06/25
- [RFC v3 03/10] build: Implement libnative library and configure options, Yeqi Fu, 2023/06/25
- [RFC v3 04/10] linux-user: Implement envlist_appendenv,
Yeqi Fu <=
- [RFC v3 05/10] linux-user: Implement native-bypass option support, Yeqi Fu, 2023/06/25
- [RFC v3 06/10] accel/tcg: Add support for native library calls, Yeqi Fu, 2023/06/25
- [RFC v3 07/10] target/i386: Add support for native library calls, Yeqi Fu, 2023/06/25
- [RFC v3 08/10] target/mips: Add support for native library calls, Yeqi Fu, 2023/06/25
- [RFC v3 09/10] target/arm: Add support for native library calls, Yeqi Fu, 2023/06/25
- [RFC v3 10/10] tests/tcg/multiarch: Add nativecalls.c test, Yeqi Fu, 2023/06/25