Project

General

Profile

Bug #8476 ยป thing.c

test program - Jeff Woolsey, 2017-07-08 05:13 PM

 
1
#include <stdio.h>
2
#include <errno.h>
3
#include <termios.h>
4
#include <fcntl.h>
5

    
6
//	We are going to assume here that file descriptors 0, 1, 2 map to file structures STDIN, STDOUT, and STDERR.
7
//	We could probably get the descriptors from the structures, but this is a quick-and-dirty test program.
8

    
9
struct termios term;
10

    
11
int i=1;
12

    
13
void main()
14

    
15
{
16
	if (-1 == tcgetattr(0, &term)) { fprintf(stderr,"problem %d with getattr\n", errno); }
17
	term.c_cc[VMIN] = 0;
18
	term.c_cc[VTIME] = 0;
19
	term.c_lflag &= ~ICANON;
20
	term.c_lflag &= ~ECHO;
21
	if (-1 == tcsetattr (0, TCSANOW, &term)) { fprintf (stderr, "error %d setting term attributes", errno); }
22
	/* set up non-blocking read */
23
	int flags = fcntl(0, F_GETFL); /* get current file status flags */ 
24
	flags |= O_NONBLOCK;	/* turn off blocking flag */ 
25
 	flags &= ~8192;
26
	if (-1 ==fcntl(0, F_SETFL, flags) ) { fprintf (stderr, "error %d setting file attributes", errno); }
27
		/* set up non-blocking read */
28

    
29
	
30
	char c[]="X";
31
	int j=0;
32
	while (1==1) {
33
		usleep(200000);
34
		if (-1!=read(0, &c, 1) || EAGAIN != errno) { i=-i; }
35
		printf("%d\n",j+=i);
36
	}
37
}
    (1-1/1)