summaryrefslogtreecommitdiffstats
path: root/TOOLS/blocking.c
blob: d7fb6aaf83542075831979cc4b22b69fd1626544 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>

#define BUFFSIZE (32*65536)

unsigned char *buffer[1];

int main(int argc,char* argv[]){

           fd_set rfds;
           struct timeval tv;
           int retval;
	   int in_fd=0; // stdin

	   buffer[0]=malloc(BUFFSIZE);
	   
	   if(argc>1) in_fd=open(argv[1],O_RDONLY|O_NONBLOCK);

while(1){
           FD_ZERO(&rfds); FD_SET(in_fd, &rfds);
           tv.tv_sec = 1;
           tv.tv_usec = 0;
           retval = select(in_fd+1, &rfds, NULL, NULL, &tv);

           if (retval){
	       if(FD_ISSET(in_fd, &rfds)){
		       // we can read input.
	               int len;
	               fprintf(stderr,"r");fflush(stderr);
		       len=read(in_fd,buffer[0],BUFFSIZE);
	               fprintf(stderr,"(%d)",len);fflush(stderr);
		}
	   } else {
	           fprintf(stderr,".");fflush(stderr);
	   }

           fprintf(stderr,"\n");fflush(stderr);
}

return 0;
}