X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ian/git?p=chiark-utils.git;a=blobdiff_plain;f=cprogs%2Frwbuffer.c;h=cb88027c9361d5168ab2ea85a2437cadedc2cff6;hp=d91a96eb639fc42dc8fdb3833456bf8fc164f647;hb=e483a046db5ff0972702a0dc29ba4e00b698d997;hpb=737b84851cd8b3dd1d9f1920201127eac693dffb diff --git a/cprogs/rwbuffer.c b/cprogs/rwbuffer.c index d91a96e..cb88027 100644 --- a/cprogs/rwbuffer.c +++ b/cprogs/rwbuffer.c @@ -27,17 +27,6 @@ * */ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - #include "rwbuffer.h" #ifndef RWBUFFER_SIZE_MB_DEF @@ -54,6 +43,8 @@ size_t buffersize; fd_set readfds; fd_set writefds; +static int opt_mlock=0; + int min(int a, int b) { return a<=b ? a : b; } static void usage(FILE *f) { @@ -67,7 +58,7 @@ static void usageerr(const char *what) { exit(12); } -static void nonblock(int fd, int yesno) { +void nonblock(int fd, int yesno) { int r; r= fcntl(fd,F_GETFL,0); if (r == -1) { perror("fcntl getfl"); exit(8); } if (yesno) r |= O_NDELAY; @@ -79,11 +70,21 @@ static void unnonblock(void) { nonblock(0,0); nonblock(1,0); } +void startupcore(void) { + buf= xmalloc(buffersize); + + if (opt_mlock) { + if (mlock(buf,buffersize)) { perror("mlock"); exit(2); } + } + + used=0; wp=rp=buf; seeneof=0; + if (atexit(unnonblock)) { perror("atexit"); exit(16); } +} + void startup(const char *const *argv) { const char *arg; char *ep; unsigned long opt_buffersize=RWBUFFER_SIZE_MB_DEF; - int opt_mlock=0; assert(argv[0]); @@ -100,14 +101,7 @@ void startup(const char *const *argv) { } buffersize= opt_buffersize*1024*1024; - buf= xmalloc(buffersize); - - if (opt_mlock) { - if (mlock(buf,buffersize)) { perror("mlock"); exit(2); } - } - - used=0; wp=rp=buf; seeneof=0; - if (atexit(unnonblock)) { perror("atexit"); exit(16); } + startupcore(); nonblock(0,1); nonblock(1,1); }