9 /* Outputter reads input from stdin and prints it in hex on stdout.
10 * It will collect lines of one character plus anything that arrives
11 * within a second after that first character, limited to a maximum
12 * number of characters that just fits on a 80-char wide display.
16 #define BYTES_PER_LINE 24
18 unsigned long timersofar = 123;
20 void tick (int signal) {
24 struct itimerval tickival = { { 0, 0 } , { 1, 0 } };
25 struct itimerval stopival = { { 0, 0 } , { 0, 0 } };
27 int main (int argc, char *argv []) {
28 unsigned char buf [BYTES_PER_LINE];
34 if (signal (SIGALRM, tick) == SIG_ERR) {
35 perror ("Failed to install interval handler");
38 system ("stty raw -echo");
39 printf ("*** outputter starts -- in hex mode ***\r\n");
42 while (timersofar = timer,
43 len = read (0, buf, BYTES_PER_LINE),
44 (len > 0) || ( errno == SIGALRM ) ) {
46 while (len = read (0, buf, 1), len > 0) {
49 /* Set a timeout before reading more; ignore problems */
50 setitimer (ITIMER_REAL, &tickival, NULL);
51 len2 = read (0, buf + 1, BYTES_PER_LINE - 1);
52 setitimer (ITIMER_REAL, &stopival, NULL);
53 if ((len2 == -1) && (errno == EINTR)) {
59 printf ("%08lx", offset);
61 for (i=0; i<len; i++) {
62 printf (" %02x", buf [i]);
72 perror ("read(2) failed");
74 printf ("*** outputter ends ***\r\n");
76 system ("stty cooked echo");