917
|
1
|
|
2 #include <stdio.h>
|
|
3 #include <stdlib.h>
|
|
4 #include <sys/time.h>
|
|
5 #include <sys/types.h>
|
|
6 #include <unistd.h>
|
|
7 #include <sys/stat.h>
|
|
8 #include <fcntl.h>
|
|
9
|
|
10 //int open(const char *pathname, int flags);
|
|
11
|
|
12
|
|
13 #define BUFFSIZE (4*65536)
|
|
14 #define NUM_BUFS (16)
|
|
15
|
|
16 unsigned char *buffer[NUM_BUFS];
|
|
17
|
|
18 unsigned int buf_read=0;
|
|
19 unsigned int buf_write=0;
|
|
20 unsigned int buf_read_pos=0;
|
|
21 unsigned int buf_write_pos=0;
|
|
22 int full_buffers=0;
|
|
23
|
|
24 int main(int argc,char* argv[]){
|
|
25
|
|
26 fd_set rfds;
|
|
27 fd_set wfds;
|
|
28 struct timeval tv;
|
|
29 int retval;
|
|
30 int i;
|
|
31 // int empty=1;
|
|
32 int can_read=1;
|
|
33 int eof=0;
|
|
34 int in_fd=0; // stdin
|
|
35
|
|
36 if(argc>1) in_fd=open(argv[1],O_RDONLY|O_NDELAY);
|
|
37
|
|
38 for(i=0;i<NUM_BUFS;i++) buffer[i]=malloc(BUFFSIZE);
|
|
39
|
|
40 while(1){
|
|
41 /* Watch stdin (fd 0) to see when it has input. */
|
|
42 FD_ZERO(&rfds); if(can_read){ FD_SET(in_fd, &rfds);}
|
|
43 FD_ZERO(&wfds); FD_SET(1, &wfds);
|
|
44 /* Wait up to five seconds. */
|
|
45 tv.tv_sec = 1;
|
|
46 tv.tv_usec = 0;
|
|
47 retval = select((in_fd<1?1:in_fd)+1, &rfds, &wfds, NULL, &tv);
|
|
48 /* Don't rely on the value of tv now! */
|
|
49
|
|
50 if (retval){
|
|
51 if(FD_ISSET(in_fd, &rfds) || !full_buffers){
|
|
52 fprintf(stderr,"\n%d r",full_buffers);fflush(stderr);
|
|
53 if(full_buffers==NUM_BUFS){
|
|
54 // buffer is full!
|
|
55 can_read=0;
|
|
56 fprintf(stderr,"\n%d full!\n",full_buffers);fflush(stderr);
|
|
57 } else {
|
|
58 // we can read input.
|
|
59 int len=BUFFSIZE-buf_read_pos;
|
|
60 fprintf(stderr,"R");fflush(stderr);
|
|
61 len=read(in_fd,buffer[buf_read]+buf_read_pos,len);
|
|
62 fprintf(stderr,"(%d)\n",len);fflush(stderr);
|
|
63 if(len>0){
|
|
64 buf_read_pos+=len;
|
|
65 if(buf_read_pos>=BUFFSIZE){
|
|
66 // block is full, find next!
|
|
67 buf_read=(buf_read+1)%NUM_BUFS;
|
|
68 ++full_buffers;
|
|
69 buf_read_pos=0;
|
|
70 fprintf(stderr,"+");fflush(stderr);
|
|
71 }
|
|
72 } else {
|
|
73 eof=1;
|
|
74 }
|
|
75 }
|
|
76 }
|
|
77 if(FD_ISSET(1, &wfds)){
|
|
78 fprintf(stderr,"\n%d w",full_buffers);fflush(stderr);
|
|
79 if(full_buffers==0){
|
|
80 if(eof){
|
|
81 // flush buffer!
|
|
82 int pos=0;
|
|
83 int len;
|
|
84 fprintf(stderr,"\nf");fflush(stderr);
|
|
85 while((len=buf_read_pos-pos)>0){
|
|
86 len=write(1,buffer[buf_write]+pos,len);
|
|
87 fprintf(stderr,"(%d)",len);fflush(stderr);
|
|
88 if(len<=0) break;
|
|
89 pos+=len;
|
|
90 }
|
|
91 exit(1);
|
|
92 }
|
|
93 fprintf(stderr," empty");fflush(stderr);
|
|
94 //empty=1; // we must fill buffers!
|
|
95 } else {
|
|
96 // yeah, we can read from the buffer!
|
|
97 int len=BUFFSIZE-buf_write_pos;
|
|
98 fprintf(stderr,"W");fflush(stderr);
|
|
99 len=write(1,buffer[buf_write]+buf_write_pos,len);
|
|
100 fprintf(stderr,"(%d)",len);fflush(stderr);
|
|
101 if(len>0){
|
|
102 buf_write_pos+=len;
|
|
103 if(buf_write_pos>=BUFFSIZE){
|
|
104 // block is empty, find next!
|
|
105 buf_write=(buf_write+1)%NUM_BUFS;
|
|
106 --full_buffers;
|
|
107 buf_write_pos=0;
|
|
108 fprintf(stderr,"-");fflush(stderr);
|
|
109 can_read=1;
|
|
110 }
|
|
111 }
|
|
112 }
|
|
113 }
|
|
114 } else {
|
|
115 fprintf(stderr,".");fflush(stderr);
|
|
116 }
|
|
117 }
|
|
118
|
|
119 return 0;
|
|
120 }
|
|
121
|