From: Bartosz Golaszewski <bgolaszewski@xxxxxxxxxxxx> Currently we only support running the test cases for gpio-tools from the top-level source directory. Some users want to install the test executable and run the tests from other locations (/bin, /usr/bin or custom path). This patch makes the test suite look in the source tree path first, then check the directory in which the program resides and last iterate over all directories in $PATH. Cc: Anders Roxell <anders.roxell@xxxxxxxxxx> Signed-off-by: Bartosz Golaszewski <bgolaszewski@xxxxxxxxxxxx> --- tests/gpiod-test.c | 40 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 37 insertions(+), 3 deletions(-) diff --git a/tests/gpiod-test.c b/tests/gpiod-test.c index 4c51f4a..7c7b54c 100644 --- a/tests/gpiod-test.c +++ b/tests/gpiod-test.c @@ -449,14 +449,48 @@ static void gpiotool_proc_dup_fds(int in_fd, int out_fd, int err_fd) static char *gpiotool_proc_get_path(const char *tool) { - char *path, *progpath, *progdir; + char *progpath, *progdir, *toolpath, *pathenv, *tok; + /* + * First check if we're running the tool from the top source + * directory. + */ progpath = xstrdup(program_invocation_name); progdir = dirname(progpath); - path = xappend(NULL, "%s/../../tools/%s", progdir, tool); + + toolpath = xappend(NULL, "%s/../../tools/%s", progdir, tool); + if (access(toolpath, R_OK | X_OK) == 0) + goto out; + free(toolpath); + + /* Is the tool in the same directory maybe? */ + toolpath = xappend(NULL, "%s/%s", progdir, tool); + if (access(toolpath, R_OK | X_OK) == 0) + goto out; + free(toolpath); free(progpath); - return path; + /* Next iterate over directories in $PATH. */ + pathenv = getenv("PATH"); + if (!pathenv) + return NULL; + + progpath = xstrdup(pathenv); + tok = strtok(progpath, ":"); + while (tok) { + toolpath = xappend(NULL, "%s/%s", tok, tool); + if (access(toolpath, R_OK) == 0) + goto out; + + free(toolpath); + tok = strtok(NULL, ":"); + } + + toolpath = NULL; + +out: + free(progpath); + return toolpath; } static NORETURN void gpiotool_proc_exec(const char *path, va_list va) -- 2.20.1