From: Scott Worley Date: Sat, 6 May 2017 17:13:43 +0000 (-0700) Subject: Switch to stdio for output. It's simpler X-Git-Url: http://git.scottworley.com/overonion/commitdiff_plain/a99e77b43df141c988b52e663d64d40e79f11895 Switch to stdio for output. It's simpler --- diff --git a/reverse.c b/reverse.c index a933907..9bbf7b9 100644 --- a/reverse.c +++ b/reverse.c @@ -1,6 +1,7 @@ #include "reverse_lib.h" #include +#include #include int main(int argc, char** argv) { @@ -8,7 +9,7 @@ int main(int argc, char** argv) { errx(EX_USAGE, "Usage: reverse filename"); } - reverse_file(argv[1], 1); + reverse_file(argv[1], stdout); return 0; } diff --git a/reverse_lib.c b/reverse_lib.c index dad7e0d..4656356 100644 --- a/reverse_lib.c +++ b/reverse_lib.c @@ -1,26 +1,15 @@ #define _FILE_OFFSET_BITS 64 -#define BUFFER_SIZE 4096 - #include #include +#include #include #include #include #include #include -static void write_all(int fd, const void *buf, size_t count) { - const char* cbuf = buf; - size_t written = 0; - while (written < count) { - int ret = write(fd, &cbuf[written], count - written); - if (ret == -1) err(EX_IOERR, "Could not write"); - written += ret; - } -} - -void reverse_file(const char* input_filename, int output_fd) { +void reverse_file(const char* input_filename, FILE* output_stream) { int fd = open(input_filename, O_RDONLY); if (fd == -1) err(EX_NOINPUT, "Could not open specified file"); @@ -33,17 +22,8 @@ void reverse_file(const char* input_filename, int output_fd) { char *m = mmap(NULL, map_size, PROT_READ, MAP_SHARED, fd, 0); if (m == MAP_FAILED) err(EX_NOINPUT, "Could not mmap input"); - char buf[BUFFER_SIZE]; - off_t buf_offset = 0; for (off_t p = stats.st_size - 1; p >= 0; p--) { - buf[buf_offset++] = m[p]; - if (buf_offset >= BUFFER_SIZE) { - write_all(output_fd, buf, buf_offset); - buf_offset = 0; - } - } - if (buf_offset) { - write_all(output_fd, buf, buf_offset); + if (fputc(m[p], output_stream) == EOF) errx(EX_IOERR, "Could not write"); } if (munmap(m, map_size) == -1) err(EX_IOERR, "Could not unmap input"); diff --git a/reverse_lib.h b/reverse_lib.h index f4d7dd6..9c0f05a 100644 --- a/reverse_lib.h +++ b/reverse_lib.h @@ -1,8 +1,10 @@ #ifndef _OVERONION_REVERSE_LIB_H #define _OVERONION_REVERSE_LIB_H -/* Copy the contents of input_filename to output_fd backwards. +#include + +/* Copy the contents of input_filename to output_stream backwards. * input_filename must be a real file, not a pipe. */ -void reverse_file(const char* input_filename, int output_fd); +void reverse_file(const char* input_filename, FILE* output_stream); #endif /* _OVERONION_REVERSE_LIB_H */ diff --git a/reverse_test.c b/reverse_test.c index fe35bcc..512edaa 100644 --- a/reverse_test.c +++ b/reverse_test.c @@ -13,8 +13,10 @@ char* reverse_to_temp_file(const char* input_file) { char* temp_filename = strdup("/tmp/reverse_test.XXXXXX"); int fd = mkstemp(temp_filename); if (fd == -1) err(EX_IOERR, "Couldn't make a temporary file"); - reverse_file(input_file, fd); - if (close(fd) == -1) err(EX_IOERR, "Couldn't close temporary file"); + FILE* f = fdopen(fd, "w"); + if (f == NULL) err(EX_IOERR, "Couldn't open temporary file"); + reverse_file(input_file, f); + if (fclose(f) == EOF) err(EX_IOERR, "Couldn't close temporary file"); return temp_filename; }