readfile.c
1.83 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
#include "testlib.c"
char *pname;
char *dname = DEVICE_NAME;
int total_errors = 0;
static void
read_one_file(BBCHandle h, int nflag)
{
int rv;
int cid = 0xabc;
char fname[32];
char bbname[32];
int len;
int loops = 0;
int errors = 0;
len = BLK_SIZE;
sprintf(fname, "testfile.%d", cid);
if (!nflag) {
printf("%s: create content file %s, len %d\n", pname, fname, len);
mkfile(fname, len);
if ((rv = storecont(h, fname, cid)) < 0) {
printf("%s: storecont %s fails: %d\n", pname, fname, rv);
exit(1);
}
}
printf("%s: read file %s repetitively\n", pname, fname);
sprintf(bbname, "%08x.app", cid);
while (1) {
if ((loops++ & 0x1ff) == 0) printf("%s: read loop count %d\r", pname, loops);
rv = readfile(h, bbname);
if (rv < 0) {
total_errors++;
printf("\n%s: read file error %d on %s (total errs so far %d)\n", pname, rv, bbname, total_errors);
if (errors++ == 0) {
printf("%s: first error, keep reading\n", pname);
continue;
}
printf("%s: failed once already, rewrite the file and then retry\n", pname);
return;
}
}
return;
}
int
main(int argc, char **argv)
{
BBCHandle h;
int rv;
int nflag = 0;
pname = argv[0];
while (--argc > 0) {
++argv;
if (!strcmp(*argv, "-n"))
nflag++;
else
dname = *argv;
}
setbuf(stdout, NULL);
setbuf(stderr, NULL);
while (1) {
h = BBCInit(dname, BBC_SYNC);
printf("%s: BBCInit(%s) returns %d\n", pname, dname, h);
if (h < 0) continue;
while (!BBCCardPresent(h))
printf("%s: BBCCardPresent returns 0\n", pname);
rv = BBCSetLed(h, BBC_LED_ORANGE);
if (!nflag) {
printf("%s: format card ...\n", pname);
rv = BBCFormatCard(h);
printf("%s: BBCFormatCard returns %d\n", pname, rv);
}
while (1)
read_one_file(h, nflag);
rv = BBCSetLed(h, BBC_LED_OFF);
rv = BBCClose(h);
printf("%s: BBCClose returns %d\n", pname, rv);
}
exit(0);
}