readfile.c 1.83 KB
#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);
}