aboutsummaryrefslogtreecommitdiff
path: root/lib/internal_cmd.c
diff options
context:
space:
mode:
authorJoseph Hunkeler <jhunkeler@gmail.com>2020-03-27 00:26:07 -0400
committerJoseph Hunkeler <jhunkeler@gmail.com>2020-03-27 00:26:07 -0400
commitfb90e211ede3292048336c4ccaed92803e70b978 (patch)
tree12a63b58ed4ddeabe0ef6a60b6c810234e2ea3a4 /lib/internal_cmd.c
parent6590e30a998f8390c6e18d3686e6075b09653e38 (diff)
downloadspmc-fb90e211ede3292048336c4ccaed92803e70b978.tar.gz
Use getenv_pair() to construct a basic runtime environment
Diffstat (limited to 'lib/internal_cmd.c')
-rw-r--r--lib/internal_cmd.c56
1 files changed, 55 insertions, 1 deletions
diff --git a/lib/internal_cmd.c b/lib/internal_cmd.c
index a75ebe2..b2305a0 100644
--- a/lib/internal_cmd.c
+++ b/lib/internal_cmd.c
@@ -145,6 +145,39 @@ void mkruntime_interface_usage(void) {
}
/**
+ * Generate a "key=value" string. If `_str` exists in the environment return a
+ * a string like:
+ *
+ * ~~~{.c}
+ * the_key=its_value
+ * ~~~
+ *
+ * However, when `_str` does not exist in the environment it creates an empty
+ * variable using setenv() and returns:
+ *
+ * ~~~{.c}
+ * the_key=
+ * ~~~
+ *
+ * @param _str a shell environment variable
+ * @return `key=value` string
+ */
+static char *getenv_pair(const char *_str) {
+ char *str = strdup(_str);
+ char *result = NULL;
+
+ if (getenv(str) == NULL) {
+ if (setenv(str, "", 1) < 0) {
+ perror("setenv failed");
+ return NULL;
+ }
+ }
+ result = join((char *[]){str, getenv(str)}, "=");
+ free(str);
+ return result;
+}
+
+/**
*
* @param argc
* @param argv
@@ -156,7 +189,19 @@ int mkruntime_interface(int argc, char **argv) {
return -1;
}
- RuntimeEnv *rt = runtime_copy(__environ);
+ // Environment variables listed here should also be referenced by the runtime_set calls below.
+ // Do not needlessly append to this array.
+ char *passthrough_runtime[] = {
+ getenv_pair("PATH"),
+ getenv_pair("MANPATH"),
+ getenv_pair("PKG_CONFIG_PATH"),
+ getenv_pair("ACLOCAL_PATH"),
+ getenv_pair("CFLAGS"),
+ getenv_pair("LDFLAGS"),
+ NULL,
+ };
+
+ RuntimeEnv *rt = runtime_copy(passthrough_runtime);
if (rt == NULL) {
return -1;
}
@@ -196,6 +241,10 @@ int mkruntime_interface(int argc, char **argv) {
runtime_export(rt, NULL);
runtime_free(rt);
+ for (size_t i = 0; passthrough_runtime[i] != NULL; i++) {
+ free(passthrough_runtime[i]);
+ }
+
free(spm_pkgconfigdir);
free(spm_ccpath);
spm_hierarchy_free(fs);
@@ -370,6 +419,11 @@ int internal_cmd(int argc, char **argv) {
int arg_count = argc - 1;
char **arg_array = &argv[1];
+ // Normalize the argument counter when there's no arguments to speak of
+ if (arg_count < 0) {
+ arg_count = 0;
+ }
+
if (strcmp(command, "mkprefixbin") == 0 || strcmp(command, "mkprefixtext") == 0) {
return mkprefix_interface(arg_count, arg_array);
}