//===----------------------------------------------------------------------===//
#include "Windows.h"
+#include "llvm/Support/FileSystem.h"
#include <cstdio>
#include <fcntl.h>
#include <io.h>
using namespace sys;
// This function just uses the PATH environment variable to find the program.
-Path sys::FindProgramByName(const std::string& progName) {
+std::string sys::FindProgramByName(const std::string &progName) {
// Check some degenerate cases
if (progName.length() == 0) // no program
- return Path();
- Path temp;
- if (!temp.set(progName)) // invalid name
- return Path();
+ return "";
+ std::string temp = progName;
// Return paths with slashes verbatim.
if (progName.find('\\') != std::string::npos ||
progName.find('/') != std::string::npos)
// See if it wasn't found.
if (len == 0)
- return Path();
+ return "";
// See if we got the entire path.
if (len < MAX_PATH)
- return Path(buffer);
+ return std::string(buffer);
// Buffer was too small; grow and retry.
while (true) {
// It is unlikely the search failed, but it's always possible some file
// was added or removed since the last search, so be paranoid...
if (len2 == 0)
- return Path();
+ return "";
else if (len2 <= len)
- return Path(b);
+ return std::string(b);
len = len2;
}
}
-static HANDLE RedirectIO(const Path *path, int fd, std::string* ErrMsg) {
+static HANDLE RedirectIO(const StringRef *path, int fd, std::string* ErrMsg) {
HANDLE h;
if (path == 0) {
DuplicateHandle(GetCurrentProcess(), (HANDLE)_get_osfhandle(fd),
return h;
}
- const char *fname;
- if (path->isEmpty())
+ std::string fname;
+ if (path->empty())
fname = "NUL";
else
- fname = path->c_str();
+ fname = *path;
SECURITY_ATTRIBUTES sa;
sa.nLength = sizeof(sa);
sa.lpSecurityDescriptor = 0;
sa.bInheritHandle = TRUE;
- h = CreateFile(fname, fd ? GENERIC_WRITE : GENERIC_READ, FILE_SHARE_READ,
- &sa, fd == 0 ? OPEN_EXISTING : CREATE_ALWAYS,
+ h = CreateFile(fname.c_str(), fd ? GENERIC_WRITE : GENERIC_READ,
+ FILE_SHARE_READ, &sa, fd == 0 ? OPEN_EXISTING : CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL, NULL);
if (h == INVALID_HANDLE_VALUE) {
MakeErrMsg(ErrMsg, std::string(fname) + ": Can't open file for " +
}
-static bool Execute(void *&Data,
- const Path& path,
+static bool Execute(void **Data,
+ StringRef Program,
const char** args,
const char** envp,
- const Path** redirects,
+ const StringRef** redirects,
unsigned memoryLimit,
std::string* ErrMsg) {
- if (Data) {
- Win32ProcessInfo* wpi = reinterpret_cast<Win32ProcessInfo*>(Data);
- CloseHandle(wpi->hProcess);
- delete wpi;
- Data = 0;
- }
-
- if (!path.canExecute()) {
+ if (!sys::fs::can_execute(Program)) {
if (ErrMsg)
*ErrMsg = "program not executable";
return false;
fflush(stdout);
fflush(stderr);
- BOOL rc = CreateProcess(path.c_str(), command, NULL, NULL, TRUE, 0,
+ std::string ProgramStr = Program;
+ BOOL rc = CreateProcess(ProgramStr.c_str(), command, NULL, NULL, TRUE, 0,
envblock, NULL, &si, &pi);
DWORD err = GetLastError();
if (!rc) {
SetLastError(err);
MakeErrMsg(ErrMsg, std::string("Couldn't execute program '") +
- path.str() + "'");
+ ProgramStr + "'");
return false;
}
- Win32ProcessInfo* wpi = new Win32ProcessInfo;
- wpi->hProcess = pi.hProcess;
- wpi->dwProcessId = pi.dwProcessId;
- Data = wpi;
+ if (Data) {
+ Win32ProcessInfo* wpi = new Win32ProcessInfo;
+ wpi->hProcess = pi.hProcess;
+ wpi->dwProcessId = pi.dwProcessId;
+ *Data = wpi;
+ }
// Make sure these get closed no matter what.
ScopedCommonHandle hThread(pi.hThread);
}
}
+ // Don't leak the handle if the caller doesn't want it.
+ if (!Data)
+ CloseHandle(pi.hProcess);
+
return true;
}
-static int WaitAux(void *&Data, const Path &path,
- unsigned secondsToWait,
- std::string* ErrMsg) {
- if (Data == 0) {
- MakeErrMsg(ErrMsg, "Process not started!");
- return -1;
- }
-
- Win32ProcessInfo* wpi = reinterpret_cast<Win32ProcessInfo*>(Data);
- HANDLE hProcess = wpi->hProcess;
-
+static int WaitAux(Win32ProcessInfo *wpi, unsigned secondsToWait,
+ std::string *ErrMsg) {
// Wait for the process to terminate.
+ HANDLE hProcess = wpi->hProcess;
DWORD millisecondsToWait = INFINITE;
if (secondsToWait > 0)
millisecondsToWait = secondsToWait * 1000;
return 1;
}
-static int Wait(void *&Data, const Path &path,
- unsigned secondsToWait,
- std::string* ErrMsg) {
- int Ret = WaitAux(Data, path, secondsToWait, ErrMsg);
+static int Wait(void *&Data, StringRef Program, unsigned secondsToWait,
+ std::string *ErrMsg) {
+ Win32ProcessInfo *wpi = reinterpret_cast<Win32ProcessInfo *>(Data);
+ int Ret = WaitAux(wpi, secondsToWait, ErrMsg);
- Win32ProcessInfo* wpi = reinterpret_cast<Win32ProcessInfo*>(Data);
CloseHandle(wpi->hProcess);
delete wpi;
Data = 0;
}
namespace llvm {
-error_code ChangeStdinToBinary(){
+error_code sys::ChangeStdinToBinary(){
int result = _setmode( _fileno(stdin), _O_BINARY );
if (result == -1)
return error_code(errno, generic_category());
return make_error_code(errc::success);
}
-error_code ChangeStdoutToBinary(){
+error_code sys::ChangeStdoutToBinary(){
int result = _setmode( _fileno(stdout), _O_BINARY );
if (result == -1)
return error_code(errno, generic_category());
return make_error_code(errc::success);
}
-error_code ChangeStderrToBinary(){
+error_code sys::ChangeStderrToBinary(){
int result = _setmode( _fileno(stderr), _O_BINARY );
if (result == -1)
return error_code(errno, generic_category());