tools/adflib: build only host variant which is used by Sam440 target
[AROS.git] / test / dos / rwverify.c
blob91b0e57c46ee7b10fcfcdf24717cc08f58b5435e
1 /*
2 Copyright © 1995-2014, The AROS Development Team. All rights reserved.
3 $Id$
4 */
6 #include <exec/types.h>
7 #include <dos/dosextens.h>
8 #include <dos/bptr.h>
9 #include <proto/exec.h>
10 #include <proto/dos.h>
11 #include <stdio.h>
13 #define CHUNK 16
15 void hexdump(UBYTE *buf, int size) {
16 int i,j;
17 int count;
19 for (i = 0; i < size; i += CHUNK) {
20 printf("0x%06x ", i);
22 count = ((size-i) > CHUNK ? CHUNK : size-i);
24 for (j = 0; j < count; j++) {
25 if (j == CHUNK / 2) fputc(' ', stdout);
26 printf("%02x ", buf[i+j]);
29 for (j = count; j < CHUNK; j++) {
30 if (j == CHUNK / 2) fputc(' ', stdout);
31 fputs(" ", stdout);
34 fputc('\n', stdout);
38 int round1(char *filename) {
39 BPTR fh;
40 UBYTE buf[1024];
41 LONG bytes;
42 int i, n;
44 fputs("\nround 1: 1024 buffers of 1024 bytes each\n\n", stdout);
46 fh = Open(filename, MODE_NEWFILE);
47 if (fh == BNULL) {
48 printf("couldn't open '%s' for write (%ld)\n", filename, (long)IoErr());
49 return 1;
52 printf("opened '%s' for write\n", filename);
54 fputs("writing... ", stdout);
55 fflush(stdout);
57 for (n = 0; n < 1024; n++) {
58 for (i = 0; i < 1024; i++)
59 buf[i] = (i + n) & 0xff;
61 bytes = Write(fh, buf, 1024);
62 if (bytes < 0) {
63 printf("buffer %d: error (%ld)\n", n, (long)IoErr());
64 Close(fh);
65 return 1;
68 if (bytes < 1024) {
69 printf("buffer %d: short write! error is %ld\n", n, (long)IoErr());
70 Close(fh);
71 return 1;
75 fputs("1024 buffers written\n", stdout);
77 Close(fh);
79 fh = Open(filename, MODE_OLDFILE);
80 if (fh == BNULL) {
81 printf("couldn't open '%s' for read (%ld)\n", filename, (long)IoErr());
82 return 1;
85 printf("opened '%s' for read\n", filename);
87 fputs("reading... ", stdout);
88 fflush(stdout);
90 for (n = 0; n < 1024; n++) {
91 bytes = Read(fh, buf, 1024);
92 if (bytes < 0) {
93 printf("buffer %d: error (%ld)\n", n, (long)IoErr());
94 Close(fh);
95 return 1;
97 if (bytes < 1024) {
98 printf("buffer %d: short read! error is %ld\n", n, (long)IoErr());
99 Close(fh);
100 return 1;
103 for (i = 0; i < 1024; i++)
104 if (buf[i] != ((i + n) & 0xff)) {
105 printf("buffer %d: verify error!\n file pos %d, expected 0x%02x, got 0x%02x\n", n, n * 1024 + i, ((i + n) & 0xff), buf[i]);
106 printf("buffer dump (0x%02x bytes):\n", 1024);
107 hexdump(buf, 1024);
108 Close(fh);
109 return 1;
113 fputs("1024 buffers read and verified\n", stdout);
115 Close(fh);
117 DeleteFile(filename);
119 fputs("cleaned up\n", stdout);
121 return 0;
124 int round2(char *filename) {
125 BPTR fh;
126 UBYTE buf[1024];
127 LONG bytes;
128 int i, n;
130 fputs("\nround 2: 8192 buffers, increasing length from 0-1023 bytes\n\n", stdout);
132 fh = Open(filename, MODE_NEWFILE);
133 if (fh == BNULL) {
134 printf("couldn't open '%s' for write (%ld)\n", filename, (long)IoErr());
135 return 1;
138 printf("opened '%s' for write\n", filename);
140 fputs("writing... ", stdout);
141 fflush(stdout);
143 for (n = 0; n < 8192; n++) {
144 for (i = 0; i < (n & 1023); i++)
145 buf[i] = (i + n) & 0xff;
147 bytes = Write(fh, buf, (n & 1023));
148 if (bytes < 0) {
149 printf("buffer %d: error (%ld)\n", n, (long)IoErr());
150 Close(fh);
151 return 1;
154 if (bytes < (n & 1023)) {
155 printf("buffer %d: short write! error is %ld\n", n, (long)IoErr());
156 Close(fh);
157 return 1;
161 fputs("1024 buffers written\n", stdout);
163 Close(fh);
165 fh = Open(filename, MODE_OLDFILE);
166 if (fh == BNULL) {
167 printf("couldn't open '%s' for read (%ld)\n", filename, (long)IoErr());
168 return 1;
171 printf("opened '%s' for read\n", filename);
173 fputs("reading... ", stdout);
174 fflush(stdout);
176 for (n = 0; n < 8192; n++) {
177 bytes = Read(fh, buf, (n & 1023));
178 if (bytes < 0) {
179 printf("buffer %d: error (%ld)\n", n, (long)IoErr());
180 Close(fh);
181 return 1;
183 if (bytes < (n & 1023)) {
184 printf("buffer %d: short read! error is %ld\n", n, (long)IoErr());
185 Close(fh);
186 return 1;
189 for (i = 0; i < (n & 1023); i++)
190 if (buf[i] != ((i + n) & 0xff)) {
191 printf("buffer %d: verify error!\n file pos %d, expected 0x%02x, got 0x%02x\n", n, n * (n & 1023) + i, ((i + n) & 0xff), buf[i]);
192 printf("buffer dump (0x%02x bytes):\n", (n & 1023));
193 hexdump(buf, 1024);
194 Close(fh);
195 return 1;
199 fputs("8192 buffers read and verified\n", stdout);
201 Close(fh);
203 //DeleteFile(filename);
205 fputs("cleaned up\n", stdout);
207 return 0;
210 int main(int argc, char **argv) {
211 if (argc != 2) {
212 printf("usage: %s filename\n", argv[0]);
213 return 1;
216 if (round1(argv[1]) || round2(argv[1]))
217 return 1;
219 return 0;