annotate TOOLS/cache.c @ 7886:32eceb109114

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