X-Git-Url: http://git.scottworley.com/overonion/blobdiff_plain/3adfbe9d667f73d8049153e924fb65886ebb67b6..a99e77b43df141c988b52e663d64d40e79f11895:/reverse_lib.c?ds=sidebyside 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");