From: Scott Worley Date: Sat, 6 May 2017 06:58:24 +0000 (-0700) Subject: Take the output_fd as an argument X-Git-Url: http://git.scottworley.com/overonion/commitdiff_plain/2061508697fa23aff04a6c3e15fd1dc9c93ce1d4?ds=inline;hp=b398ee5ae7abfa019f84628204f3c1cd146ed39e Take the output_fd as an argument --- diff --git a/reverse.c b/reverse.c index 2d83751..a933907 100644 --- a/reverse.c +++ b/reverse.c @@ -8,7 +8,7 @@ int main(int argc, char** argv) { errx(EX_USAGE, "Usage: reverse filename"); } - reverse_file(argv[1]); + reverse_file(argv[1], 1); return 0; } diff --git a/reverse_lib.c b/reverse_lib.c index a800db8..e69922b 100644 --- a/reverse_lib.c +++ b/reverse_lib.c @@ -22,7 +22,7 @@ static void write_all(int fd, const void *buf, size_t count) { } } -void reverse_file(const char* input_filename) { +void reverse_file(const char* input_filename, int output_fd) { int fd = open(input_filename, O_RDONLY); if (fd == -1) { err(EX_NOINPUT, "Could not open specified file"); @@ -46,12 +46,12 @@ void reverse_file(const char* input_filename) { for (off_t p = stats.st_size - 1; p >= 0; p--) { buf[buf_offset++] = m[p]; if (buf_offset >= BUFFER_SIZE) { - write_all(1, buf, buf_offset); + write_all(output_fd, buf, buf_offset); buf_offset = 0; } } if (buf_offset) { - write_all(1, buf, buf_offset); + write_all(output_fd, buf, buf_offset); } if (munmap(m, map_size) == -1) { diff --git a/reverse_lib.h b/reverse_lib.h index 58345fa..f4d7dd6 100644 --- a/reverse_lib.h +++ b/reverse_lib.h @@ -1,8 +1,8 @@ #ifndef _OVERONION_REVERSE_LIB_H #define _OVERONION_REVERSE_LIB_H -/* Copy the contents of input_filename to stdout backwards. +/* Copy the contents of input_filename to output_fd backwards. * input_filename must be a real file, not a pipe. */ -void reverse_file(const char* input_filename); +void reverse_file(const char* input_filename, int output_fd); #endif /* _OVERONION_REVERSE_LIB_H */