--- htmltidy.module.orig
+++ htmltidy.module
@@ -555,37 +555,42 @@
     return 2;
   }
 
-  //TODO rewrite to use stdin
-  $dirty_filename = tempnam(file_directory_temp(), 'drup');
-  $f = fopen($dirty_filename, 'w');
-  fwrite($f, $input);
-  fclose($f);
-
-  // warnings are saved to file
-  $warnings_filename = tempnam(file_directory_temp(), 'warn');
-  $args[] = '-f '. $warnings_filename;
-
   // Run Tidy with the right options.
-  $command = $path .' '. implode(' ', $args) .' '. $dirty_filename;
-  system($command, $return_value);
-
+  $command = $path .' '. implode(' ', $args);
+  
+  $descriptorspec = array(
+    0 => array("pipe", "r"),  // stdin is a pipe that the child will read from
+    1 => array("pipe", "w"),  // stdout is a pipe that the child will write to
+    2 => array("pipe", "w")   // stderr is a file to write to
+  );
+  
+  $process = proc_open($command, $descriptorspec, $pipes);
+  fwrite($pipes[0], $input);
+  fclose($pipes[0]);
+  $stdout = stream_get_contents($pipes[1]);
+  $stderr = stream_get_contents($pipes[2]);
+  $return_value = proc_close($process);  
+  
   // return_value 0 means success. 1 means warning. 2 means error, the file
   // will be there, but not have been touched.
   switch ($return_value) {
   case 0:
     $warnings = $errors = array();
-    $output = file_get_contents($dirty_filename);
+    $output = $stdout;
     break;
 
   case 1:
     $errors = array();
-    $warnings = array_map('trim', file($warnings_filename));
-    $output = file_get_contents($dirty_filename);
+    foreach(array_filter(split("\n", $stderr)) as $line) { 
+      $warnings[] = trim($line); 
+    }
+    $output = $stdout;
     break;
 
   case 2:
     // separate errors and warnings into two different arrays
-    foreach (file($warnings_filename) as $line) {
+    
+    foreach(array_filter(split("\n", $stdout)) as $line) {
       $line = trim($line);
       if (preg_match('|^line \d+ column \d+ - Warning:|', $line)) {
         $warnings[] = $line;
@@ -598,10 +603,6 @@
     break;
   }
 
-  // delete the temporary files.
-  unlink($dirty_filename);
-  unlink($warnings_filename);
-
   return $return_value;
 }
 /**
