diff options
author | Joseph Hunkeler <jhunkeler@gmail.com> | 2024-11-13 11:30:34 -0500 |
---|---|---|
committer | Joseph Hunkeler <jhunkeler@gmail.com> | 2024-11-13 11:30:34 -0500 |
commit | 35471289b619994c4f04fd2b6cb6d04a16cb1b33 (patch) | |
tree | 5631311b89b2b8e82be59aa8565f08301f5360ae | |
parent | 8af575409e01bf4e539c73bd3490a7885d9f79a9 (diff) | |
download | stasis-35471289b619994c4f04fd2b6cb6d04a16cb1b33.tar.gz |
Rename path_manip() to env_manipulate_pathstr()
* Add key argument to generalize the function
-rw-r--r-- | include/utils.h | 15 | ||||
-rw-r--r-- | src/lib/core/conda.c | 4 | ||||
-rw-r--r-- | src/lib/core/utils.c | 11 |
3 files changed, 16 insertions, 14 deletions
diff --git a/include/utils.h b/include/utils.h index 2756347..e26b3c5 100644 --- a/include/utils.h +++ b/include/utils.h @@ -30,10 +30,6 @@ #define STASIS_XML_PRETTY_PRINT_PROG "xmllint" #define STASIS_XML_PRETTY_PRINT_ARGS "--format" -#define PM_APPEND 1 << 0 -#define PM_PREPEND 1 << 1 -#define PM_ONCE 1 << 2 - /** * Change directory. Push path on directory stack. * @@ -396,13 +392,20 @@ int mkdirs(const char *_path, mode_t mode); */ char *find_version_spec(char *package_name); +// mode flags for env_manipulate_pathstr +#define PM_APPEND 1 << 0 +#define PM_PREPEND 1 << 1 +#define PM_ONCE 1 << 2 + /** -* Manipulate the PATH environment variable +* Add paths to the head or tail of an environment variable. +* +* @param key environment variable to manipulate * @param path to insert (does not need to exist) * @param mode PM_APPEND `$path:$PATH` * @param mode PM_PREPEND `$PATH:path` * @param mode PM_ONCE do not manipulate if `path` is present in PATH variable */ -int path_manip(char *path, int mode); +int env_manipulate_pathstr(const char *key, char *path, int mode); #endif //STASIS_UTILS_H diff --git a/src/lib/core/conda.c b/src/lib/core/conda.c index 94653c4..c98be1c 100644 --- a/src/lib/core/conda.c +++ b/src/lib/core/conda.c @@ -211,7 +211,7 @@ static int conda_prepend_bin(const char *root) { char conda_bin[PATH_MAX] = {0}; snprintf(conda_bin, sizeof(conda_bin) - 1, "%s/bin", root); - if (path_manip(conda_bin, PM_PREPEND | PM_ONCE)) { + if (env_manipulate_pathstr("PATH", conda_bin, PM_PREPEND | PM_ONCE)) { return -1; } return 0; @@ -221,7 +221,7 @@ static int conda_prepend_condabin(const char *root) { char conda_condabin[PATH_MAX] = {0}; snprintf(conda_condabin, sizeof(conda_condabin) - 1, "%s/condabin", root); - if (path_manip(conda_condabin, PM_PREPEND | PM_ONCE)) { + if (env_manipulate_pathstr("PATH", conda_condabin, PM_PREPEND | PM_ONCE)) { return -1; } return 0; diff --git a/src/lib/core/utils.c b/src/lib/core/utils.c index 793b445..18731e6 100644 --- a/src/lib/core/utils.c +++ b/src/lib/core/utils.c @@ -809,7 +809,7 @@ char *find_version_spec(char *str) { return strpbrk(str, "@~=<>!"); } -int path_manip(char *path, int mode) { +int env_manipulate_pathstr(const char *key, char *path, int mode) { if (isempty(path)) { SYSERROR("%s", "New PATH element cannot be zero-length or NULL"); return -1; @@ -824,9 +824,9 @@ int path_manip(char *path, int mode) { char *system_path_new = NULL; if (mode & PM_APPEND) { - asprintf(&system_path_new, "%s:%s", system_path_old, path); + asprintf(&system_path_new, "%s%s%s", system_path_old, PATH_SEP, path); } else if (mode & PM_PREPEND) { - asprintf(&system_path_new, "%s:%s", path, system_path_old); + asprintf(&system_path_new, "%s%s%s", path, PATH_SEP, system_path_old); } if (!system_path_new) { @@ -840,9 +840,8 @@ int path_manip(char *path, int mode) { return 0; } } - - if (setenv("PATH", system_path_new, 1) < 0) { - SYSERROR("Unable to prepend to PATH: %s", path); + if (setenv(key, system_path_new, 1) < 0) { + SYSERROR("Unable to %s to PATH: %s", mode & PM_APPEND ? "append" : "prepend", path); guard_free(system_path_new); return -1; } |