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