Write to a temporary file.
[ssic.git] / src / ssic.pl
index 8ecc30e92104be274b2178284bcb3cb6bc9f9c45..285c20fa4bdc5d2e779ec0ab59e8283fbe4536ff 100644 (file)
@@ -127,8 +127,8 @@ sub compile
        if ($output eq "-") {
                $output_fh = *STDOUT;
        } else {
-               if (not open($output_fh, ">", $output)) {
-                       error(4, "%s: %s\n", $output, $!);
+               if (not open($output_fh, ">", $output . "~")) {
+                       error(4, "%s: %s\n", $output . "~", $!);
                }
        }
 
@@ -152,6 +152,17 @@ sub compile
        }
 
        print($output_fh $ssi->process(<$input_fh>));
+
+       if ($input ne "-") {
+               close($input_fh);
+       }
+       if ($output ne "-") {
+               close($output_fh);
+       }
+
+       if (not rename($output . "~", $output)) {
+               error(4, "%s: %s\n", $output, $!);
+       }
 }
 
 main();