Another mac fix

Test: make (on linux)
Change-Id: I3f0561f3a1fc08dad5d3dd8ba860da56b51d273c
This commit is contained in:
Joe Onorato
2016-10-21 15:18:18 -07:00
parent 95376c1d36
commit 31617e3b8f
3 changed files with 41 additions and 4 deletions

View File

@@ -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 {

View File

@@ -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;
}
}

View File

@@ -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