1 //===- llvm/System/Unix/Program.cpp -----------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by Reid Spencer and is distributed under the
6 // University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the Unix specific portion of the Program class.
12 //===----------------------------------------------------------------------===//
14 //===----------------------------------------------------------------------===//
15 //=== WARNING: Implementation here must contain only generic UNIX code that
16 //=== is guaranteed to work on *all* UNIX variants.
17 //===----------------------------------------------------------------------===//
19 #include <llvm/Config/config.h>
23 #ifdef HAVE_SYS_WAIT_H
27 extern char** environ;
32 // This function just uses the PATH environment variable to find the program.
34 Program::FindProgramByName(const std::string& progName) {
36 // Check some degenerate cases
37 if (progName.length() == 0) // no program
40 if (!temp.setFile(progName)) // invalid name
42 if (temp.executable()) // already executable as is
45 // At this point, the file name is valid and its not executable
47 // Get the path. If its empty, we can't do anything to find it.
48 const char *PathStr = getenv("PATH");
52 // Now we have a colon separated list of directories to search; try them.
53 unsigned PathLen = strlen(PathStr);
55 // Find the first colon...
56 const char *Colon = std::find(PathStr, PathStr+PathLen, ':');
58 // Check to see if this first directory contains the executable...
60 if (FilePath.setDirectory(std::string(PathStr,Colon))) {
61 FilePath.appendFile(progName);
62 if (FilePath.executable())
63 return FilePath; // Found the executable!
66 // Nope it wasn't in this directory, check the next path in the list!
67 PathLen -= Colon-PathStr;
70 // Advance past duplicate colons
71 while (*PathStr == ':') {
81 Program::ExecuteAndWait(const Path& path,
82 const std::vector<std::string>& args,
83 const char ** envp ) {
84 if (!path.executable())
85 throw path.toString() + " is not executable";
87 #ifdef HAVE_SYS_WAIT_H
88 // Create local versions of the parameters that can be passed into execve()
89 // without creating const problems.
90 const char* argv[ args.size() + 2 ];
92 std::string progname(path.getLast());
93 argv[index++] = progname.c_str();
94 for (unsigned i = 0; i < args.size(); i++)
95 argv[index++] = args[i].c_str();
98 // Create a child process.
100 // An error occured: Return to the caller.
102 ThrowErrno(std::string("Couldn't execute program '") + path.toString() +
106 // Child process: Execute the program.
108 char** env = environ;
111 execve (path.c_str(), (char** const)argv, env);
112 // If the execve() failed, we should exit and let the parent pick up
113 // our non-zero exit status.
117 // Parent process: Break out of the switch to do our processing.
122 // Parent process: Wait for the child process to terminate.
124 if ((::wait (&status)) == -1)
125 ThrowErrno(std::string("Failed waiting for program '") + path.toString()
128 // If the program exited normally with a zero exit status, return success!
129 if (WIFEXITED (status))
130 return WEXITSTATUS(status);
131 else if (WIFSIGNALED(status))
132 throw std::string("Program '") + path.toString() +
133 "' received terminating signal.";
138 throw std::string("Program::ExecuteAndWait not implemented on this platform!\n");
144 // vim: sw=2 smartindent smarttab tw=80 autoindent expandtab