return Path();
}
-static bool RedirectFD(const std::string &File, int FD, std::string* ErrMsg) {
- if (File.empty()) return false; // Noop
+static bool RedirectIO(const Path *Path, int FD, std::string* ErrMsg) {
+ if (Path == 0)
+ // Noop
+ return false;
+ std::string File;
+ if (Path->isEmpty())
+ // Redirect empty paths to /dev/null
+ File = "/dev/null";
+ else
+ File = Path->toString();
// Open the file
int InFD = open(File.c_str(), FD == 0 ? O_RDONLY : O_WRONLY|O_CREAT, 0666);
case 0: {
// Redirect file descriptors...
if (redirects) {
- if (redirects[0]) {
- if (redirects[0]->isEmpty()) {
- if (RedirectFD("/dev/null",0,ErrMsg)) { return -1; }
- } else {
- if (RedirectFD(redirects[0]->toString(), 0,ErrMsg)) { return -1; }
- }
- }
- if (redirects[1]) {
- if (redirects[1]->isEmpty()) {
- if (RedirectFD("/dev/null",1,ErrMsg)) { return -1; }
- } else {
- if (RedirectFD(redirects[1]->toString(),1,ErrMsg)) { return -1; }
- }
- }
+ if (RedirectIO(redirects[0], 0, ErrMsg)) { return -1; }
+ if (RedirectIO(redirects[1], 1, ErrMsg)) { return -1; }
if (redirects[1] && redirects[2] &&
*(redirects[1]) != *(redirects[2])) {
- if (redirects[2]->isEmpty()) {
- if (RedirectFD("/dev/null",2,ErrMsg)) { return -1; }
- } else {
- if (RedirectFD(redirects[2]->toString(), 2,ErrMsg)) { return -1; }
- }
+ if (RedirectIO(redirects[2], 2, ErrMsg)) { return -1; }
} else if (-1 == dup2(1,2)) {
MakeErrMsg(ErrMsg, "Can't redirect");
return -1;