4 * A program for reading input from devices which don't like constant
5 * stopping and starting, such as tape drives. readbuffer is:
6 * Copyright (C) 1997-1998,2000-2001 Ian Jackson <ian@chiark.greenend.org.uk>
8 * readbuffer is part of chiark backup, a system for backing up GNU/Linux and
9 * other UN*X-compatible machines, as used on chiark.greenend.org.uk.
11 * Copyright (C) 1997-1998,2000-2001 Ian Jackson <ian@chiark.greenend.org.uk>
12 * Copyright (C) 1999 Peter Maydell <pmaydell@chiark.greenend.org.uk>
14 * This is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as
16 * published by the Free Software Foundation; either version 2,
17 * or (at your option) any later version.
19 * This is distributed in the hope that it will be useful, but
20 * WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public
25 * License along with this file; if not, write to the Free Software
26 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
31 #include <sys/types.h>
38 #define BUFFER 16*1024*1024
39 #define WAITEMPTY ((BUFFER*1)/4)
41 static inline int min(int a, int b) { return a<=b ? a : b; }
43 static void nonblock(int fd) {
45 r= fcntl(fd,F_GETFL,0); if (r == -1) { perror("fcntl getfl"); exit(1); }
47 if (fcntl(fd,F_SETFL,r) == -1) { perror("fcntl setfl"); exit(1); }
50 int main(int argc, const char *const *argv) {
51 static unsigned char buf[BUFFER];
53 unsigned char *wp, *rp;
54 int used,r,reading,seeneof;
58 used=0; wp=rp=buf; reading=1; seeneof=0;
59 nonblock(0); nonblock(1);
61 if (argv[1] && !strcmp(argv[1],"--mlock")) {
62 if (mlock(buf,sizeof(buf))) { perror("mlock"); exit(1); }
65 if (argv[1]) { fputs("usage: readbuffer [--mlock]\n",stderr); exit(1); }
67 while (!seeneof || used) {
73 /*fprintf(stderr,"\t buffers full - stopping\n");*/
77 FD_ZERO(&writefds); if (used) FD_SET(1,&writefds);
78 /*fprintf(stderr,"used %6d reading %d seeneof %d wp %8lx rp %8lx read %d write %d\n",
79 used,reading,seeneof,(unsigned long)(wp-buf),(unsigned long)(rp-buf),
80 FD_ISSET(0,&readfds),FD_ISSET(1,&writefds));*/
81 r= select(2,&readfds,&writefds,0,0);
82 /*fprintf(stderr,"\t readable %d writeable %d\n",
83 FD_ISSET(0,&readfds),FD_ISSET(1,&writefds));*/
85 if (errno == EINTR) continue;
86 perror("select"); exit(1);
88 if (FD_ISSET(0,&readfds)) {
89 r= read(0,rp,min(BUFFER-1-used,buf+BUFFER-rp));
91 /*fprintf(stderr,"\t eof detected\n");*/
94 if (!(errno == EAGAIN || errno == EINTR)) { perror("read"); exit(1); }
95 /*fprintf(stderr,"\t read transient error\n");*/
97 /*fprintf(stderr,"\t read %d\n",r);*/
100 if (rp == buf+BUFFER) rp=buf;
103 if (FD_ISSET(1,&writefds)) {
105 r= write(1,wp,min(used,buf+BUFFER-wp));
107 if (!(errno == EAGAIN || errno == EINTR)) { perror("write"); exit(1); }
108 /*fprintf(stderr,"\t write transient error\n");*/
110 /*fprintf(stderr,"\t wrote %d\n",r);*/
113 if (wp == buf+BUFFER) wp=buf;
115 if (used < WAITEMPTY && !seeneof) {
116 /*fprintf(stderr,"\t starting writes\n");*/