Make I/O redirection handling in sys::Program a bit more consistent. No
authorMatthijs Kooijman <matthijs@stdin.nl>
Thu, 12 Jun 2008 10:47:18 +0000 (10:47 +0000)
committerMatthijs Kooijman <matthijs@stdin.nl>
Thu, 12 Jun 2008 10:47:18 +0000 (10:47 +0000)
functional changes. Win32 code is untested, but should work fine.

In the unix variant, rename RedirectFD to RedirectIO and let that function
handle empty and null paths instead of doing that in the caller 3 times. This
is the same as win32 already does it.

In the win32 variant, use Path::isEmpty() instead of checking the resulting
c_str() manually. This is the same as unix already does it.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@52230 91177308-0d34-0410-b5e6-96231b3b80d8

lib/System/Unix/Program.inc
lib/System/Win32/Program.inc

index d0dade1f8cadc2467e5c0c988a388d4a82f509cf..6ff69ca133e443878d683e9aea3f2178626ec8c2 100644 (file)
@@ -84,8 +84,16 @@ Program::FindProgramByName(const std::string& progName) {
   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);
@@ -162,27 +170,11 @@ Program::ExecuteAndWait(const Path& path,
     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;
index cb002132bfef05840d34e81a2f244fed5b58802e..52eb9677ffb5ff23ed68f31e583a73cb1ca18e90 100644 (file)
@@ -77,10 +77,12 @@ static HANDLE RedirectIO(const Path *path, int fd, std::string* ErrMsg) {
                     0, TRUE, DUPLICATE_SAME_ACCESS);
     return h;
   }
-
-  const char *fname = path->toString().c_str();
-  if (*fname == 0)
+  
+  const char *fname;
+  if (path->isEmpty())
     fname = "NUL";
+  else
+    fname = path->toString().c_str();
 
   SECURITY_ATTRIBUTES sa;
   sa.nLength = sizeof(sa);