4 * Copyright (C) 1997,1998 Ian Jackson <ian@chiark.greenend.org.uk>
6 * This is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2,
9 * or (at your option) any later version.
11 * This is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public
17 * License along with this file; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include <sys/types.h>
31 #define BUFFER 6*1024*1024
32 #define WAITEMPTY ((BUFFER*1)/4)
34 static inline int min(int a, int b) { return a<=b ? a : b; }
36 static void nonblock(int fd) {
38 r= fcntl(fd,F_GETFL,0); if (r == -1) { perror("fcntl getfl"); exit(1); }
40 if (fcntl(fd,F_SETFL,r) == -1) { perror("fcntl setfl"); exit(1); }
44 static unsigned char buf[BUFFER];
46 unsigned char *wp, *rp;
47 int used,r,reading,seeneof;
51 used=0; wp=rp=buf; reading=1; seeneof=0;
52 nonblock(0); nonblock(1);
53 while (!seeneof || used) {
59 /*fprintf(stderr,"\t buffers full - stopping\n");*/
63 FD_ZERO(&writefds); if (used) FD_SET(1,&writefds);
64 /*fprintf(stderr,"used %6d reading %d seeneof %d wp %8lx rp %8lx read %d write %d\n",
65 used,reading,seeneof,(unsigned long)(wp-buf),(unsigned long)(rp-buf),
66 FD_ISSET(0,&readfds),FD_ISSET(1,&writefds));*/
67 r= select(2,&readfds,&writefds,0,0);
68 /*fprintf(stderr,"\t readable %d writeable %d\n",
69 FD_ISSET(0,&readfds),FD_ISSET(1,&writefds));*/
71 if (errno == EINTR) continue;
72 perror("select"); exit(1);
74 if (FD_ISSET(0,&readfds)) {
75 r= read(0,rp,min(BUFFER-1-used,buf+BUFFER-rp));
77 /*fprintf(stderr,"\t eof detected\n");*/
80 if (!(errno == EAGAIN || errno == EINTR)) { perror("read"); exit(1); }
81 /*fprintf(stderr,"\t read transient error\n");*/
83 /*fprintf(stderr,"\t read %d\n",r);*/
86 if (rp == buf+BUFFER) rp=buf;
89 if (FD_ISSET(1,&writefds)) {
91 r= write(1,wp,min(used,buf+BUFFER-wp));
93 if (!(errno == EAGAIN || errno == EINTR)) { perror("write"); exit(1); }
94 /*fprintf(stderr,"\t write transient error\n");*/
96 /*fprintf(stderr,"\t wrote %d\n",r);*/
99 if (wp == buf+BUFFER) wp=buf;
101 if (used < WAITEMPTY && !seeneof) {
102 /*fprintf(stderr,"\t starting writes\n");*/