From fb90e211ede3292048336c4ccaed92803e70b978 Mon Sep 17 00:00:00 2001 From: Joseph Hunkeler Date: Fri, 27 Mar 2020 00:26:07 -0400 Subject: Use getenv_pair() to construct a basic runtime environment --- lib/internal_cmd.c | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 55 insertions(+), 1 deletion(-) (limited to 'lib/internal_cmd.c') 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 @@ -144,6 +144,39 @@ void mkruntime_interface_usage(void) { printf("usage: mkruntime {root_dir}\n"); } +/** + * 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 @@ -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); } -- cgit