diff options
author | Joe Onorato <joeo@google.com> | 2016-10-21 22:32:32 +0000 |
---|---|---|
committer | Android (Google) Code Review <android-gerrit@google.com> | 2016-10-21 22:32:34 +0000 |
commit | 8e16c1d458fe5fc6c8c5f1dfc13949709bfb6a6d (patch) | |
tree | 3ee5267f7b839df0e52c0d3e76c36367b495f70f | |
parent | 5066f9513b48f24bc9462da182e090a77a26ffbb (diff) | |
parent | 31617e3b8ff01bb016446a0d2cb687b25aee42c6 (diff) |
Merge "Another mac fix"
-rw-r--r-- | tools/bit/adb.cpp | 2 | ||||
-rw-r--r-- | tools/bit/command.cpp | 40 | ||||
-rw-r--r-- | tools/bit/command.h | 3 |
3 files changed, 41 insertions, 4 deletions
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 <errno.h> #include <string.h> #include <stdlib.h> -#include <unistd.h> #include <sys/types.h> +#include <sys/stat.h> +#include <unistd.h> #include <sys/wait.h> +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 |