From 46fc51dd969923d44e553732cfc17cb68c5e5cf0 Mon Sep 17 00:00:00 2001 From: Arfrever Frehtes Taifersar Arahesis Date: Sun, 21 Mar 2010 11:48:50 +0000 Subject: Set GENTOO_PYTHON_PROCESS_NAME environmental variable (bug #286424). --- configure.ac | 7 +++++-- python-wrapper.c | 23 +++++++++++++++++++++-- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/configure.ac b/configure.ac index 662653a..46291cc 100644 --- a/configure.ac +++ b/configure.ac @@ -1,4 +1,4 @@ -AC_INIT([eselect-python], [20091230]) +AC_INIT([eselect-python], [20100321]) CFLAGS="${CFLAGS--march=native -O3 -pipe -fno-ident}" LDFLAGS="${LDFLAGS--Wl,-O1,--as-needed,--gc-sections,--hash-style=gnu}" @@ -9,7 +9,10 @@ MKDIR="${MKDIR:-${INSTALL} -d}" AC_USE_SYSTEM_EXTENSIONS -# strndup() was introduced in POSIX.1-2008. +# setenv() was introduced in POSIX.1-2008. +AC_CHECK_FUNCS([setenv]) + +# strndup() was introduced in POSIX.1-2008 and is also an implicitly declared built-in function in GCC. AC_MSG_CHECKING([for strndup]) old_CFLAGS="${CFLAGS}" CFLAGS="${CFLAGS} -Wall -Werror -Wextra" diff --git a/python-wrapper.c b/python-wrapper.c index e472c9b..ecc26bc 100644 --- a/python-wrapper.c +++ b/python-wrapper.c @@ -1,4 +1,4 @@ -/* Copyright 1999-2009 Gentoo Foundation +/* Copyright 1999-2010 Gentoo Foundation * Distributed under the terms of the GNU General Public License v2 */ #include @@ -165,9 +165,10 @@ const char* find_latest(const char* exe) return ret; } -int main(__attribute__((unused)) int argc, char** argv) +int main(int argc, char** argv) { const char* EPYTHON = getenv("EPYTHON"); + int script_name_index = -1; if (! valid_interpreter(EPYTHON)) { FILE* f = fopen(ENVD_CONFIG, "r"); @@ -198,6 +199,24 @@ int main(__attribute__((unused)) int argc, char** argv) return EXIT_ERROR; } + /* Set GENTOO_PYTHON_PROCESS_NAME environmental variable, if a script with a Python shebang is probably being executed. + * argv[0] can be "python", when "#!/usr/bin/env python" shebang is used. */ + if (argc >= 2 && (argv[0][0] == '/' || strcmp(argv[0], "python") == 0) && (argv[1][0] == '/' || strncmp(argv[1], "./", 2) == 0)) + script_name_index = 1; + else if (argc >= 3 && argv[0][0] == '/' && argv[1][0] == '-' && (argv[2][0] == '/' || strncmp(argv[2], "./", 2) == 0)) + script_name_index = 2; + if (script_name_index > 0) + { + char* script_name = strrchr(argv[script_name_index], '/') + 1; +#ifdef HAVE_SETENV + setenv("GENTOO_PYTHON_PROCESS_NAME", script_name, 1); +#else + char* script_name_variable = malloc(sizeof(char) * (strlen("GENTOO_PYTHON_PROCESS_NAME=") + strlen(script_name))); + sprintf(script_name_variable, "GENTOO_PYTHON_PROCESS_NAME=%s", script_name); + putenv(script_name_variable); +#endif + } + const char* path = find_path(argv[0]); if (path) { -- cgit v1.2.3