From 2377b838ff45bc3407e0c65028eac858c47c9d4e Mon Sep 17 00:00:00 2001 From: Scott Worley Date: Sun, 7 May 2017 11:27:58 -0700 Subject: [PATCH] Pull out make_temporary_file --- Makefile | 4 ++-- reverse_lib.c | 20 ++------------------ temp_file.c | 22 ++++++++++++++++++++++ temp_file.h | 11 +++++++++++ 4 files changed, 37 insertions(+), 20 deletions(-) create mode 100644 temp_file.c create mode 100644 temp_file.h diff --git a/Makefile b/Makefile index fe978d3..6e2eacb 100644 --- a/Makefile +++ b/Makefile @@ -17,8 +17,8 @@ test: reverse_test .PHONY: clean distclean depend test -reverse: reverse.o reverse_lib.o -reverse_test: reverse_test.o reverse_lib.o +reverse: reverse.o reverse_lib.o temp_file.o +reverse_test: reverse_test.o reverse_lib.o temp_file.o $(CC) -o $@ $^ $(CFLAGS) %.o: %.c diff --git a/reverse_lib.c b/reverse_lib.c index 20837ee..f217b1c 100644 --- a/reverse_lib.c +++ b/reverse_lib.c @@ -1,6 +1,7 @@ -#define _GNU_SOURCE #define _FILE_OFFSET_BITS 64 +#include "temp_file.h" + #include #include #include @@ -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 (;;) { diff --git a/temp_file.c b/temp_file.c new file mode 100644 index 0000000..68bfb88 --- /dev/null +++ b/temp_file.c @@ -0,0 +1,22 @@ +#define _GNU_SOURCE + +#include "temp_file.h" + +#include +#include +#include +#include + +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"); +} diff --git a/temp_file.h b/temp_file.h new file mode 100644 index 0000000..d4fe41e --- /dev/null +++ b/temp_file.h @@ -0,0 +1,11 @@ +#ifndef _OVERONION_TEMP_FILE_H +#define _OVERONION_TEMP_FILE_H + +#include + +/* 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. */ +void make_temporary_file(char** temp_filename, FILE** temp_file); + +#endif /* _OVERONION_TEMP_FILE_H */ -- 2.44.1