]> git.scottworley.com Git - overonion/blobdiff - reverse_lib.c
Pull out make_temporary_file
[overonion] / reverse_lib.c
index 20837ee5f65fe95b237436b0d1685fd2d497a69d..f217b1cf2a46bf4c9c5eea4f53b68b16bd0adceb 100644 (file)
@@ -1,6 +1,7 @@
-#define _GNU_SOURCE
 #define _FILE_OFFSET_BITS 64
 
+#include "temp_file.h"
+
 #include <err.h>
 #include <fcntl.h>
 #include <stdio.h>
@@ -32,23 +33,6 @@ void reverse_file(const char* input_filename, FILE* output_stream) {
   if (close(fd) == -1) err(EX_IOERR, "Could not close input");
 }
 
-/* Create a temporary file in $TMPDIR, or /tmp if TMPDIR is not set.
- * Caller must free temp_filename and fclose temp_file.  Succeeds or terminates
- * the process. */
-static void make_temporary_file(char** temp_filename, FILE** temp_file) {
-  char* TMPDIR = getenv("TMPDIR");
-  if (TMPDIR == NULL) {
-    TMPDIR = "/tmp";
-  }
-  if (asprintf(temp_filename, "%s/reverse.XXXXXX", TMPDIR) == -1) {
-    errx(EX_OSERR, "Could not assemble temporary filename");
-  }
-  int fd = mkstemp(*temp_filename);
-  if (fd == -1) err(EX_IOERR, "Could not make a temporary file");
-  *temp_file = fdopen(fd, "w");
-  if (*temp_file == NULL) err(EX_IOERR, "Could not open temporary file");
-}
-
 /* Copy data from input to output until EOF is reached. */
 static void copy(FILE* input, FILE* output) {
   for (;;) {