checkhex.c
2.14 KB
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
#include <errno.h>
#include <getopt.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static void usage(void);
static int getLeftRight(FILE *, int *, int *);
#define LINE_SIZE 80
int
main(int argc, char **argv)
{
int c, r;
char *genFileName, *goldFileName;
FILE *genFile, *goldFile;
char genLine[LINE_SIZE];
int genLeft, genRight;
int betweenFlag = 1;
int goldLeft = 0, goldRight = 0;
int goldLeftPrev, goldRightPrev;
int lineNumber = -1;
while ((c = getopt(argc, argv, "")) != EOF) {
switch(c) {
case '?':
usage();
}
}
if (argc - optind != 2)
usage();
genFileName = argv[optind];
if ((genFile = fopen(genFileName, "r")) == NULL) {
fprintf(stderr, "checkhex: %s: %s\n", genFileName, strerror(errno));
exit(1);
}
goldFileName = argv[optind+1];
if ((goldFile = fopen(goldFileName, "r")) == NULL) {
fprintf(stderr, "checkhex: %s: %s\n", goldFileName, strerror(errno));
exit(1);
}
while (getLeftRight(genFile, &genLeft, &genRight) != EOF) {
lineNumber += 2;
if (!betweenFlag) {
if (getLeftRight(goldFile, &goldLeft, &goldRight) == EOF) {
betweenFlag = 1;
rewind(goldFile);
}
}
if (betweenFlag) {
if ((genLeft == goldLeft) && (genRight == goldRight)) {
continue;
} else {
goldLeftPrev = goldLeft;
goldRightPrev = goldRight;
while ((r = getLeftRight(goldFile, &goldLeft, &goldRight))
!= EOF) {
if ((goldLeftPrev != goldLeft) ||
(goldRightPrev != goldRight))
break;
}
betweenFlag = 0;
}
}
if ((genLeft != goldLeft) || (genRight != goldRight)) {
fprintf(stderr,
"checkhex: mismatch on line %d of %s (%04x,%04x) vs (%04x,%04x)\n",
lineNumber, genFileName, genLeft, genRight, goldLeft, goldRight);
exit(1);
}
}
return(0);
}
static void
usage(void)
{
fprintf(stderr, "checkhex <generated .hex file> <golden .hex file>\n");
exit(1);
}
static int
getLeftRight(FILE *f, int *leftp, int *rightp)
{
int r;
if ((r = fscanf(f, "%x\n%x", leftp, rightp)) == EOF)
return(EOF);
else if (r != 2) {
fprintf(stderr, "checkhex: syntax error\n");
exit(1);
} else {
return(0);
}
}