Imported Debian patch 5.1-8
[debian/gcpegg] / regtest.c
1 #include <stdio.h>
2 #ifdef __USE_BSD
3 #undef __USE_BSD
4 #endif
5 #define __USE_BSD
6 #include <termios.h>
7 #undef __USE_BSD
8 #include <unistd.h>
9 #include <fcntl.h>
10 #define __USE_BSD
11 #include <errno.h>
12 #undef __USE_BSD
13
14 #include <sys/types.h>
15 #include <sys/stat.h>
16 #include <sys/ioctl.h>
17 #include <time.h>
18 #include <string.h>
19 #include <utmp.h>
20 #include <stdlib.h>
21 #include <signal.h>
22 #include <syslog.h>
23 #include "paths.h"
24
25 int Sample(int fd, int bits);
26
27 int main(int argc, char *argv[]) {
28   int TTY_fd;
29   int i, res, n1, mean;
30   struct termios tt;
31   speed_t baud;
32
33   if (argc != 3) {
34     printf("Usage: %s <tty> <baud>\n", argv[0]);
35     exit(-1);
36   }
37
38   switch(atoi(argv[2])) {
39   case 1200: baud = B1200; break;
40   case 2400: baud = B2400; break;
41   case 4800: baud = B4800; break;
42   case 9600: baud = B9600; break;
43   case 19200: baud = B19200; break;
44   case 38400: baud = B38400; break;
45   case 115200: baud = B115200; break;
46   default:
47     printf("%s: Baud rate %s not supported.\n", argv[0], argv[2]);
48     exit(-1);
49   }
50
51   if ((TTY_fd = open(argv[1], O_RDWR | O_NDELAY)) < 0) {
52     fprintf(stderr, "%s: %s\n", argv[1], strerror(errno));
53     exit(1);
54   }
55
56   res = tcgetattr(TTY_fd, &tt);
57   res = cfsetospeed(&tt, baud); 
58   cfmakeraw(&tt);
59   tt.c_oflag &= (~(TABDLY | ONLCR));
60   res = tcsetattr(TTY_fd, TCSANOW, &tt);
61
62   mean = 0;
63   for (i = 0; i < 100; i++) {
64     n1 = Sample(TTY_fd, 200);
65     printf("%3d ", n1);
66     fflush(stdout);
67     if (i % 20 == 19) printf("\n");
68     mean += n1;
69   }
70   printf("Mean of first 100 REG200 trials = %f\n", (double)mean / 100.0);
71
72   exit(0);
73   for (i = 100; i < 200; i++) {
74     n1 = Sample(TTY_fd, i);
75     printf("%3d: %3d\n", i, n1);
76   }
77   return 0;
78 }
79
80 static int oldbits = 0;
81 static int bitsleft = 0;
82 int Sample(int fd, int bits) {
83   int bc, sum, n1;
84   unsigned char c1;
85
86   sum = bc = 0;
87   while (bc < bits) {
88     if (bitsleft) {
89       sum += (oldbits & 0x01);
90       oldbits >>= 1;
91       bitsleft--;
92       bc++;
93     } else {
94       do {
95         n1 = read(fd, &c1, 1);
96       } while (n1 == 0 || (n1 == -1 && errno == EAGAIN));
97       if (n1 == -1) {
98         /* Fatal error occurred, die now? */
99         exit(-errno);
100       }
101       oldbits = c1;
102       bitsleft = 8;
103 #if 0
104       printf("Sampled %c%c%c%c%c%c%c%c\n",
105              ((oldbits & 0x80)?'1':'0'),
106              ((oldbits & 0x40)?'1':'0'),
107              ((oldbits & 0x20)?'1':'0'),
108              ((oldbits & 0x10)?'1':'0'),
109              ((oldbits & 0x08)?'1':'0'),
110              ((oldbits & 0x04)?'1':'0'),
111              ((oldbits & 0x02)?'1':'0'),
112              ((oldbits & 0x01)?'1':'0'));
113 #endif
114     }
115   }
116
117   return sum;
118 }
119
120