From: Scott Worley Date: Sat, 6 May 2017 18:42:47 +0000 (-0700) Subject: Make a function call interface X-Git-Url: http://git.scottworley.com/overonion/commitdiff_plain/b398ee5ae7abfa019f84628204f3c1cd146ed39e?ds=inline Make a function call interface --- diff --git a/Makefile b/Makefile index fee4ea9..71a482e 100644 --- a/Makefile +++ b/Makefile @@ -14,7 +14,7 @@ depend: *.c .PHONY: clean distclean depend -reverse: reverse.o +reverse: reverse.o reverse_lib.o $(CC) -o $@ $^ $(CFLAGS) %.o: %.c diff --git a/reverse.c b/reverse.c index ac55a10..2d83751 100644 --- a/reverse.c +++ b/reverse.c @@ -1,70 +1,14 @@ -#define _FILE_OFFSET_BITS 64 +#include "reverse_lib.h" -#define BUFFER_SIZE 4096 - -#include -#include -#include -#include -#include #include #include -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; - } -} - - int main(int argc, char** argv) { if (argc != 2) { errx(EX_USAGE, "Usage: reverse filename"); } - int fd = open(argv[1], O_RDONLY); - if (fd == -1) { - err(EX_NOINPUT, "Could not open specified file"); - } - - struct stat stats; - if (fstat(fd, &stats) == -1) { - err(EX_NOINPUT, "Could not stat input"); - } - - long page_size = sysconf(_SC_PAGE_SIZE); - off_t pages = (stats.st_size - 1) / page_size + 1; - long map_size = pages * page_size; - 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(1, buf, buf_offset); - buf_offset = 0; - } - } - if (buf_offset) { - write_all(1, buf, buf_offset); - } - - if (munmap(m, map_size) == -1) { - err(EX_IOERR, "Could not unmap input"); - } - if (close(fd) == -1) { - err(EX_IOERR, "Could not close input"); - } + reverse_file(argv[1]); return 0; } diff --git a/reverse_lib.c b/reverse_lib.c new file mode 100644 index 0000000..a800db8 --- /dev/null +++ b/reverse_lib.c @@ -0,0 +1,63 @@ +#define _FILE_OFFSET_BITS 64 + +#define BUFFER_SIZE 4096 + +#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 fd = open(input_filename, O_RDONLY); + if (fd == -1) { + err(EX_NOINPUT, "Could not open specified file"); + } + + struct stat stats; + if (fstat(fd, &stats) == -1) { + err(EX_NOINPUT, "Could not stat input"); + } + + long page_size = sysconf(_SC_PAGE_SIZE); + off_t pages = (stats.st_size - 1) / page_size + 1; + long map_size = pages * page_size; + 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(1, buf, buf_offset); + buf_offset = 0; + } + } + if (buf_offset) { + write_all(1, buf, buf_offset); + } + + if (munmap(m, map_size) == -1) { + err(EX_IOERR, "Could not unmap input"); + } + if (close(fd) == -1) { + err(EX_IOERR, "Could not close input"); + } +} diff --git a/reverse_lib.h b/reverse_lib.h new file mode 100644 index 0000000..58345fa --- /dev/null +++ b/reverse_lib.h @@ -0,0 +1,8 @@ +#ifndef _OVERONION_REVERSE_LIB_H +#define _OVERONION_REVERSE_LIB_H + +/* Copy the contents of input_filename to stdout backwards. + * input_filename must be a real file, not a pipe. */ +void reverse_file(const char* input_filename); + +#endif /* _OVERONION_REVERSE_LIB_H */