From 31617e3b8ff01bb016446a0d2cb687b25aee42c6 Mon Sep 17 00:00:00 2001 From: Joe Onorato Date: Fri, 21 Oct 2016 15:18:18 -0700 Subject: Another mac fix Test: make (on linux) Change-Id: I3f0561f3a1fc08dad5d3dd8ba860da56b51d273c --- tools/bit/adb.cpp | 2 +- tools/bit/command.cpp | 40 +++++++++++++++++++++++++++++++++++++--- tools/bit/command.h | 3 +++ 3 files changed, 41 insertions(+), 4 deletions(-) (limited to 'tools/bit') diff --git a/tools/bit/adb.cpp b/tools/bit/adb.cpp index eb96dae2189c..0c8424de566d 100644 --- a/tools/bit/adb.cpp +++ b/tools/bit/adb.cpp @@ -308,7 +308,7 @@ run_instrumentation_test(const string& packageName, const string& runner, const const char* prog = cmd.GetProg(); char* const* argv = cmd.GetArgv(); char* const* env = cmd.GetEnv(); - execvpe(prog, argv, env); + exec_with_path_search(prog, argv, env); print_error("Unable to run command: %s", prog); exit(1); } else { diff --git a/tools/bit/command.cpp b/tools/bit/command.cpp index c5c12b4fad72..9a8449bf9356 100644 --- a/tools/bit/command.cpp +++ b/tools/bit/command.cpp @@ -22,10 +22,13 @@ #include #include #include -#include #include +#include +#include #include +extern char **environ; + Command::Command(const string& prog) :prog(prog) { @@ -118,7 +121,7 @@ get_command_output(const Command& command, int* err, bool quiet) const char* prog = command.GetProg(); char* const* argv = command.GetArgv(); char* const* env = command.GetEnv(); - execvpe(prog, argv, env); + exec_with_path_search(prog, argv, env); if (!quiet) { print_error("Unable to run command: %s", prog); } @@ -166,7 +169,7 @@ run_command(const Command& command) const char* prog = command.GetProg(); char* const* argv = command.GetArgv(); char* const* env = command.GetEnv(); - execvpe(prog, argv, env); + exec_with_path_search(prog, argv, env); print_error("Unable to run command: %s", prog); exit(1); } else { @@ -181,3 +184,34 @@ run_command(const Command& command) } } +int +exec_with_path_search(const char* prog, char const* const* argv, char const* const* envp) +{ + if (prog[0] == '/') { + return execve(prog, (char*const*)argv, (char*const*)envp); + } else { + char* pathEnv = strdup(getenv("PATH")); + if (pathEnv == NULL) { + return 1; + } + char* dir = pathEnv; + while (dir) { + char* next = strchr(dir, ':'); + if (next != NULL) { + *next = '\0'; + next++; + } + if (dir[0] == '/') { + struct stat st; + string executable = string(dir) + "/" + prog; + if (stat(executable.c_str(), &st) == 0) { + execve(executable.c_str(), (char*const*)argv, (char*const*)envp); + } + } + dir = next; + } + free(pathEnv); + return 1; + } +} + diff --git a/tools/bit/command.h b/tools/bit/command.h index eb0b88f8d1ac..fb44900b0806 100644 --- a/tools/bit/command.h +++ b/tools/bit/command.h @@ -54,5 +54,8 @@ string get_command_output(const Command& command, int* err, bool quiet=false); */ int run_command(const Command& command); +// Mac OS doesn't have execvpe. This is the same as execvpe. +int exec_with_path_search(const char* prog, char const* const* argv, char const* const* envp); + #endif // COMMAND_H -- cgit v1.2.3-59-g8ed1b