-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhexin.c
89 lines (78 loc) · 1.57 KB
/
hexin.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <unistd.h>
#include <stdio.h>
#include <ctype.h>
#define BYTES_PER_LINE 35
enum status { normal, endline, error, endfile };
enum status status = normal;
void ch2hex (unsigned char *output, int ch) {
if ((ch >= '0') && (ch <= '9')) {
*output <<= 4;
*output += ch - '0';
} else {
ch = toupper (ch);
if ((ch < 'A') || (ch > 'F')) {
fprintf (stderr, "Illegal char '%c'\n", ch);
status = error;
} else {
*output <<= 4;
*output += ch - 'A' + 10;
}
}
}
int getbyte (unsigned char *output) {
int ch;
*output = 0;
if (status == error) {
return 0;
}
do {
if ((ch = getchar ()) < 0) {
status = endfile;
return 0;
}
if (ch == '\n') {
status = endline;
return 0;
}
} while ((ch == ' ') || (ch == ':'));
if (status == normal) {
ch2hex (output, ch);
ch = getchar ();
ch2hex (output, ch);
}
return (status == normal);
}
int main (int argc, char *argv []) {
size_t offset = 0;
size_t len;
unsigned char buf [BYTES_PER_LINE];
char ch;
int prompt;
prompt = isatty (0);
while (status != endfile) {
if (status == endline) {
status = normal;
}
if (prompt) {
usleep (1000000L); // Yield to others -- better prompt printing
fprintf (stderr, "%08lx>", offset);
fflush (stderr);
}
len = 0;
while ((len < BYTES_PER_LINE) && (status == normal)) {
if (getbyte (&buf [len])) {
len++;
offset++;
}
}
write (1, buf, len);
if (status == error) {
while (ch = getchar (), (ch >=0) && (ch!='\n')) {
;
}
fprintf (stderr, "*** skipped remainder of line ***\n");
status = normal;
}
}
return 0;
}