From 452cd173b3b06b1ad9e87e8c770040405bb5a2a6 Mon Sep 17 00:00:00 2001 From: Peter Volkov Date: Tue, 17 Sep 2024 12:01:35 +0300 Subject: [PATCH] Update PATH to include python_impl dependent script path --- src/python-exec.c | 22 ++++++++++++++++++++++ src/python-exec.in | 1 + 2 files changed, 23 insertions(+) diff --git a/src/python-exec.c b/src/python-exec.c index ccb800d..ebee5e9 100644 --- a/src/python-exec.c +++ b/src/python-exec.c @@ -13,6 +13,7 @@ #include #include #include +#include #ifdef HAVE_READLINK # include @@ -548,7 +549,28 @@ static void load_configuration(const char* scriptname) */ static void execute(char* script, char** argv) { + char *env_path, *path, *script_path, *updated_path; + + /* dirname requires copy of path as it may modify it */ + path = calloc(strlen(script) + 1, sizeof(char)); + strcpy(path, script); + script_path = dirname(path); + + env_path = getenv("PATH"); + /* + 1 for ':' */ + updated_path = calloc(strlen(env_path) + strlen(script_path) + 1 + 1, sizeof(char)); + + strcat(updated_path, env_path); + strncat(updated_path, ":", 1); + strcat(updated_path, script_path); + free(path); + + setenv("PATH", updated_path, 1); + free(updated_path); + execv(script, argv); + /* reset PATH to initial value */ + setenv("PATH", env_path, 1); /* warn about other errors but try hard to run something */ if (errno != ENOENT && isatty(fileno(stderr))) diff --git a/src/python-exec.in b/src/python-exec.in index 109acee..019edcd 100644 --- a/src/python-exec.in +++ b/src/python-exec.in @@ -90,6 +90,7 @@ while data is None: raise sys.argv[0] = target +os.environ["PATH"] += os.pathsep + os.path.join('/usr/lib/python-exec', EPYTHON) # in python3.9+, __file__ paths are absolute if sys.version_info >= (3, 9): target = os.path.abspath(target)