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