chiark / gitweb /
cprogs/Makefile: clean deletes $PROGRAMS
[chiark-utils.git] / cprogs / readbuffer.c
1 /*
2  * readbuffer.c
3  *
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>
7  *
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.
10  * chiark backup is:
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>
13  *
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 3,
17  * or (at your option) any later version.
18  *
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.
23  *
24  * You should have received a copy of the GNU General Public
25  * License along with this file; if not, consult the Free Software
26  * Foundation's website at www.fsf.org, or the GNU Project website at
27  * www.gnu.org.
28  *
29  */
30
31 #include "rwbuffer.h"
32
33 const char *progname= "readbuffer";
34
35 static size_t waitempty;
36
37 int main(int argc, const char *const *argv) {
38   int r,reading;
39
40   startup(argv);
41   waitempty= (buffersize*1)/4;
42   reading=1;
43   maxselfd=2;
44   
45   while (!seeneof || used) {
46     
47     FD_ZERO(&readfds);
48     if (reading) {
49       if (used<buffersize-1) {
50         FD_SET(0,&readfds);
51       } else {
52         reading=0;
53       }
54     }
55     FD_ZERO(&writefds); if (used) FD_SET(1,&writefds);
56
57     callselect();
58
59     if (FD_ISSET(0,&readfds)) {
60       r= read(0,rp,min(buffersize-1-used,buf+buffersize-rp));
61       if (!r) {
62         seeneof=1; reading=0;
63       } else if (r<0) {
64         if (!(errno == EAGAIN || errno == EINTR)) { perror("read"); exit(1); }
65       } else {
66         used+= r;
67         rp+= r;
68         if (rp == buf+buffersize) rp=buf;
69       }
70     }
71
72     if (FD_ISSET(1,&writefds)) {
73       assert(used);
74       r= write(1,wp,min(used,buf+buffersize-wp));
75       if (r<=0) {
76         if (!(errno == EAGAIN || errno == EINTR)) { perror("write"); exit(1); }
77       } else {
78         used-= r;
79         wp+= r;
80         if (wp == buf+buffersize) wp=buf;
81       }
82       if (used < waitempty && !seeneof) {
83         reading=1;
84       }
85     }
86   }
87   exit(0);
88 }