[Libguestfs] [PATCH nbdkit 6/9] common/utils: Add a copy_environ utility function.

Richard W.M. Jones
Wed, 15 Apr 2020 16:16:47 -0000

This allows you to copy an environ, while optionally adding extra
(key, value) pairs to it.
---
 common/utils/Makefile.am |   2 +
 common/utils/utils.h     |   1 +
 common/utils/environ.c   | 116 +++++++++++++++++++++++++++++++++++++++
 3 files changed, 119 insertions(+)

diff --git a/common/utils/Makefile.am b/common/utils/Makefile.am
index 9a78339d..41761c79 100644
--- a/common/utils/Makefile.am
+++ b/common/utils/Makefile.am
@@ -37,12 +37,14 @@ libutils_la_SOURCES = \
 	cleanup.c \
 	cleanup-nbdkit.c \
 	cleanup.h \
+	environ.c \
 	quote.c \
 	utils.c \
 	utils.h \
 	$(NULL)
 libutils_la_CPPFLAGS = \
 	-I$(top_srcdir)/include \
+	-I$(top_srcdir)/common/include \
 	$(NULL)
 libutils_la_CFLAGS = $(WARNINGS_CFLAGS)

diff --git a/common/utils/utils.h b/common/utils/utils.h
index ebd5f66b..5c121ccb 100644
--- a/common/utils/utils.h
+++ b/common/utils/utils.h
@@ -38,5 +38,6 @@ extern void uri_quote (const char *str, FILE *fp);
 extern int exit_status_to_nbd_error (int status, const char *cmd);
 extern int set_cloexec (int fd);
 extern int set_nonblock (int fd);
+extern char **copy_environ (char **env, ...);

 #endif /* NBDKIT_UTILS_H */
diff --git a/common/utils/environ.c b/common/utils/environ.c
new file mode 100644
index 00000000..a43eeb71
--- /dev/null
+++ b/common/utils/environ.c
@@ -0,0 +1,116 @@
+/* nbdkit
+ * Copyright (C) 2018-2020 Red Hat Inc.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * * Neither the name of Red Hat nor the names of its contributors may be
+ * used to endorse or promote products derived from this software without
+ * specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY RED HAT AND CONTRIBUTORS ''AS
IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
+ * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RED HAT OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+ * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <config.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdarg.h>
+#include <string.h>
+
+#include <nbdkit-plugin.h>
+
+#include "utils.h"
+#include "vector.h"
+
+DEFINE_VECTOR_TYPE(environ_t, char *);
+
+/* Copy an environ.  Also this allows you to add (key, value) pairs to
+ * the environ through the varargs NULL-terminated list.  Returns NULL
+ * if the copy or allocation failed.
+ */
+char **
+copy_environ (char **env, ...)
+{
+  environ_t ret = empty_vector;
+  size_t i, len;
+  char *s;
+  va_list argp;
+  const char *key, *value;
+
+  /* Copy the existing keys into the new vector. */
+  for (i = 0; env[i] != NULL; ++i) {
+    s = strdup (env[i]);
+    if (s == NULL) {
+      nbdkit_error ("strdup: %m");
+      goto error;
+    }
+    if (environ_t_append (&ret, s) == -1) {
+      nbdkit_error ("realloc: %m");
+      goto error;
+    }
+  }
+
+  /* Add the new keys. */
+  va_start (argp, env);
+  while ((key = va_arg (argp, const char *)) != NULL) {
+    value = va_arg (argp, const char *);
+    if (asprintf (&s, "%s=%s", key, value) == -1) {
+      nbdkit_error ("asprintf: %m");
+      goto error;
+    }
+
+    /* Search for key in the existing environment.  It's O(n^2) ... */
+    len = strlen (key);
+    for (i = 0; i < ret.size; ++i) {
+      if (strncmp (key, ret.ptr[i], len) == 0 && ret.ptr[i][len] ==
'=') {
+        /* Replace the existing key. */
+        free (ret.ptr[i]);
+        ret.ptr[i] = s;
+        goto found;
+      }
+    }
+
+    /* Else, append a new key. */
+    if (environ_t_append (&ret, s) == -1) {
+      nbdkit_error ("realloc: %m");
+      goto error;
+    }
+
+  found: ;
+  }
+  va_end (argp);
+
+  /* Append a NULL pointer. */
+  if (environ_t_append (&ret, NULL) == -1) {
+    nbdkit_error ("realloc: %m");
+    goto error;
+  }
+
+  /* Return the list of strings. */
+  return ret.ptr;
+
+ error:
+  environ_t_iter (&ret, (void *) free);
+  free (ret.ptr);
+  return NULL;
+}
--
2.25.0