-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintel.c
115 lines (102 loc) · 2.32 KB
/
intel.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
int getopt(int nargc, char * const nargv[], const char *ostr) ;
static char sccsID[] = "@(#)intel.c 1.3 07/04/88";
/* intel -o<offset> -s<offset> -0123 */
#include <stdio.h>
#include <ctype.h>
#define DATA_RECORD 0
#define EOF_RECORD 1
#define START_RECORD 3
static int q[4];
static int offset;
static int start = -1;
#define RECLEN 16
record(count, address, type, bytes)
int count;
int address;
int type;
int bytes[];
{
int sum;
int i;
sum = count + address/256 + address%256 + type;
printf(":%02X%04X%02X", count, address, type);
for (i = 0; i < count; i++) {
sum += bytes[i];
sum %= 256;
printf("%02X", bytes[i]);
}
printf("%02X\n", (-sum) & 0xff);
}
dumpit()
{
int bytes[RECLEN];
int bytesi = 0;
int quarter = 0;
int address = 0;
int c;
while ((c = getchar()) != EOF) {
if (q[quarter]) {
bytes[bytesi++] = c;
if (bytesi == RECLEN) {
record(bytesi, address+offset, DATA_RECORD, bytes);
address += bytesi;
bytesi = 0;
}
}
quarter = (quarter + 1) % 4;
}
if (bytesi != 0) {
record(bytesi, address+offset, DATA_RECORD, bytes);
}
if (start != -1) {
record(0, start, START_RECORD, 0);
}
record(0, 0, EOF_RECORD, 0);
}
main(argc, argv)
int argc;
char *argv[];
{
extern char *optarg;
extern int optind, opterr;
char *terminal;
int c;
int error;
error = 0;
opterr = 0;
while ((c = getopt(argc, argv, "0123o:s:")) != EOF) {
switch (c) {
case '0': q[0] = 1;
break;
case '1': q[1] = 1;
break;
case '2': q[2] = 1;
break;
case '3': q[3] = 1;
break;
case 'o': offset = strtol(optarg, &terminal, 0);
if (*terminal != 0 && ! isspace(*terminal)) {
error = 1;
}
break;
case 's': start = strtol(optarg, &terminal, 0);
if (*terminal != 0 && ! isspace(*terminal)) {
error = 1;
}
break;
case '?': error = 1;
break;
}
}
if (error) {
fprintf(stderr,
"USAGE: %s [-0] [-1] [-2] [-3] [-o <offset>] [-s <offset>]\n",
argv[0]);
exit(1);
}
if (q[0] == 0 && q[1] == 0 && q[2] == 0 && q[3] == 0) {
q[0] = q[1] = q[2] = q[3] = 1;
}
dumpit();
exit(0);
}