blob: ca8514dfa69b8100ee97092ba739cc96d2e247c4 [file] [log] [blame]
Reid Spencer52a7efa2004-08-29 19:20:41 +00001//===- llvm/System/Unix/Program.cpp -----------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
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.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Unix specific portion of the Program class.
11//
12//===----------------------------------------------------------------------===//
13
14//===----------------------------------------------------------------------===//
15//=== WARNING: Implementation here must contain only generic UNIX code that
16//=== is guaranteed to work on *all* UNIX variants.
17//===----------------------------------------------------------------------===//
18
19#include "Unix.h"
20#include <sys/stat.h>
21#include <fcntl.h>
22#include <Config/config.h>
23#ifdef HAVE_SYS_WAIT_H
24#include <sys/wait.h>
25#endif
26
Reid Spencerc0854bf2004-08-29 20:10:07 +000027extern char** environ;
28
Reid Spencer52a7efa2004-08-29 19:20:41 +000029namespace llvm {
30using namespace sys;
31
32// This function just uses the PATH environment variable to find the program.
33Program
34Program::FindProgramByName(const std::string& progName) {
35
36 // Check some degenerate cases
37 if (progName.length() == 0) // no program
38 return Program();
39 Program temp;
40 if (!temp.set_file(progName)) // invalid name
41 return Program();
42 if (temp.executable()) // already executable as is
43 return temp;
44
45 // At this point, the file name is valid and its not executable
46
47 // Get the path. If its empty, we can't do anything to find it.
48 const char *PathStr = getenv("PATH");
49 if (PathStr == 0)
50 return Program();
51
52 // Now we have a colon separated list of directories to search; try them.
53 unsigned PathLen = strlen(PathStr);
54 while (PathLen) {
55 // Find the first colon...
56 const char *Colon = std::find(PathStr, PathStr+PathLen, ':');
57
58 // Check to see if this first directory contains the executable...
59 Program FilePath;
60 if (FilePath.set_directory(std::string(PathStr,Colon))) {
61 FilePath.append_file(progName);
62 if (FilePath.executable())
63 return FilePath; // Found the executable!
64 }
65
66 // Nope it wasn't in this directory, check the next path in the list!
67 PathLen -= Colon-PathStr;
68 PathStr = Colon;
69
70 // Advance past duplicate colons
71 while (*PathStr == ':') {
72 PathStr++;
73 PathLen--;
74 }
75 }
76 return Program();
77}
78
79//
80int
81Program::ExecuteAndWait(const std::vector<std::string>& args) const {
82 if (!executable())
83 throw get() + " is not executable";
84
85#ifdef HAVE_SYS_WAIT_H
86 // Create local versions of the parameters that can be passed into execve()
87 // without creating const problems.
88 const char* argv[ args.size() + 2 ];
89 unsigned index = 0;
90 std::string progname(this->getLast());
91 argv[index++] = progname.c_str();
92 for (unsigned i = 0; i < args.size(); i++)
93 argv[index++] = args[i].c_str();
94 argv[index] = 0;
95
96 // Create a child process.
97 switch (fork()) {
98 // An error occured: Return to the caller.
99 case -1:
100 ThrowErrno(std::string("Couldn't execute program '") + get() + "'");
101 break;
102
103 // Child process: Execute the program.
104 case 0:
105 execve (get().c_str(), (char** const)argv, environ);
106 // If the execve() failed, we should exit and let the parent pick up
107 // our non-zero exit status.
108 exit (errno);
109
110 // Parent process: Break out of the switch to do our processing.
111 default:
112 break;
113 }
114
115 // Parent process: Wait for the child process to terminate.
116 int status;
117 if ((::wait (&status)) == -1)
118 ThrowErrno(std::string("Failed waiting for program '") + get() + "'");
119
120 // If the program exited normally with a zero exit status, return success!
121 if (WIFEXITED (status))
122 return WEXITSTATUS(status);
123 else if (WIFSIGNALED(status))
124 throw std::string("Program '") + get() + "' received terminating signal.";
125 else
126 return 0;
127
128#else
129 throw std::string("Program::ExecuteAndWait not implemented on this platform!\n");
130#endif
131
132}
133
134}
135// vim: sw=2 smartindent smarttab tw=80 autoindent expandtab