Issue #1, proper display for negative INTEGER values in derdump
[hexio] / hexout.c
1 #include <unistd.h>
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include <sys/time.h>
5 #include <signal.h>
6 #include <errno.h>
7
8
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.
13  */
14
15
16 #define BYTES_PER_LINE 24
17
18 unsigned long timersofar = 123;
19
20 void tick (int signal) {
21         timersofar++;
22 }
23
24 struct itimerval tickival = { { 0, 0 } , { 1, 0 } };
25 struct itimerval stopival = { { 0, 0 } , { 0, 0 } };
26
27 int main (int argc, char *argv []) {
28         unsigned char buf [BYTES_PER_LINE];
29 /*
30         unsigned long timer;
31 */
32         size_t len;
33         size_t offset = 0;
34         if (signal (SIGALRM, tick) == SIG_ERR) {
35                 perror ("Failed to install interval handler");
36                 exit (1);
37         }
38         system ("stty raw -echo");
39         printf ("*** outputter starts -- in hex mode ***\r\n");
40         fflush (stdout);
41 /*
42         while (timersofar = timer,
43                         len = read (0, buf, BYTES_PER_LINE),
44                         (len > 0) || ( errno == SIGALRM ) ) {
45 */
46         while (len = read (0, buf, 1), len > 0) {
47                 size_t i;
48                 int len2;
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)) {
54                         len2 = 0;
55                 }
56                 if (len2 >= 0) {
57                         len += len2;
58                 }
59                 printf ("%08lx", offset);
60                 offset += len;
61                 for (i=0; i<len; i++) {
62                         printf (" %02x", buf [i]);
63                 }
64                 printf ("\r\n");
65                 fflush (stdout);
66                 if (len2 < 0) {
67                         len = -1;
68                         break;
69                 }
70         }
71         if (len < 0) {
72                 perror ("read(2) failed");
73         } else {
74                 printf ("*** outputter ends ***\r\n");
75         }
76         system ("stty cooked echo");
77         return 0;
78 }