source: buchla-68k/orig/IOLIB/TESTCIO.C@ 6d3de83

Last change on this file since 6d3de83 was 3ae31e9, checked in by Thomas Lopatic <thomas@…>, 8 years ago

Imported original source code.

  • Property mode set to 100755
File size: 7.1 KB
RevLine 
[3ae31e9]1/*
2 =============================================================================
3 testcio.c -- test the C I/O functions
4 Version 10 -- 1987-10-28 -- D.N. Lynx Crowe
5 =============================================================================
6*/
7
8#define LATTICEC 0 /* non-zero for Lattice C */
9
10#include "stdio.h"
11#include "stddefs.h"
12#include "errno.h"
13
14#define THEFILE "testcio.tmp" /* file name */
15
16#if LATTICEC
17
18#define fopenb(x,y) fopen(x,y)
19
20#define THEMODE "wb+" /* file open mode */
21#define MAGIC 0x0000FEDC /* our MAGIC number */
22
23#else
24
25#define MAGIC 0xFEDC /* our MAGIC number */
26#define THEMODE "w+" /* file open mode */
27
28#endif
29
30#define AMOUNT 4800 /* must be bigger than 2 clusters */
31
32extern int errno; /* the most recent system error code */
33
34FILE *fp1; /* our file pointer */
35
36/*
37
38*/
39
40int
41getw(stream)
42register FILE *stream;
43{
44 int temp;
45 register char *t;
46
47 temp = 0;
48
49#if LATTICEC
50 t = (char *)&temp + 2;
51#else
52 t = (char *)&temp;
53#endif
54
55 if (EOF EQ (*t++ = getc(stream)))
56 return(EOF);
57
58 if (ferror(stream))
59 return(EOF);
60
61 *t = getc(stream);
62
63 return(temp & 0xFFFF);
64}
65
66#if LATTICEC
67void
68#endif
69putw(w, stream)
70register unsigned w;
71FILE *stream;
72{
73 putc(((w >> 8) & 0xFF), stream);
74
75 if (ferror(stream))
76 return;
77
78 putc((w & 0xFF), stream);
79}
80
81/*
82
83*/
84
85main()
86{
87 int i, j, errs;
88 long where, was, after;
89
90 /* open the file for write update */
91
92 if (NULL EQ (fp1 = fopenb(THEFILE, THEMODE))) {
93
94 printf("ERROR: unable to open [%s] in \042%s\042 mode.\n",
95 THEFILE, THEMODE);
96 exit(1);
97 }
98
99 printf("File [%s] open in \042%s\042 mode.\n", THEFILE, THEMODE);
100
101#if !LATTICEC
102 FILEpr(fp1);
103#endif
104
105 /* now write some data to it */
106
107 for (i = 0; i < AMOUNT; i++) {
108
109 putw(i, fp1);
110
111 if (ferror(fp1)) {
112
113 printf("ERROR: write %d failed. errno = %d.\n",
114 i, errno);
115 exit(1);
116 }
117 }
118
119 fflush(fp1); /* flush the buffer */
120
121 printf("%d words written to file.\n", AMOUNT);
122 after = ftell(fp1);
123 printf("File position after writing = %ld.\n", after);
124
125#if !LATTICEC
126 FILEpr(fp1);
127#endif
128
129 /* seek to the beginning, and read the data back */
130
131 where = 0L;
132
133 if (fseek(fp1, where, 0)) {
134
135 printf("ERROR: could not seek to %ld.\n", where);
136 exit(1);
137 }
138
139 was = ftell(fp1);
140 printf("File position after seek to %ld ($%08.8lx) = %ld ($%08.8lx).\n",
141 where, where, was, was);
142 printf("Reading back data.\n");
143 errs = 0;
144
145 for (i = 0; i < AMOUNT; i++) {
146
147 if (feof(fp1)) {
148
149 printf("ERROR: unexpected EOF at %d.\n", i);
150 exit(1);
151 }
152
153 j = getw(fp1);
154
155 if (ferror(fp1)) {
156
157 printf("ERROR: read error at %d. errno=%d.\n",
158 i, errno);
159 exit(1);
160 }
161
162 if (i NE j) {
163
164 ++errs;
165 was = ftell(fp1);
166
167 if (errs < 11)
168 printf("ERROR: at %ld, expected %d, read %d.\n",
169 was, i, j);
170 }
171 }
172
173#if !LATTICEC
174 FILEpr(fp1);
175#endif
176
177 was = ftell(fp1);
178 printf("File written and read back. File position = %ld ($%08.8lx).\n",
179 was, was);
180 printf("Read back check complete with %d error%s\n",
181 errs, ((errs NE 1) ? "s." : "."));
182
183 if (feof(fp1)) {
184
185 printf("Note: EOF seen at end of read to EOF.\n");
186
187 if (ferror(fp1)) {
188
189 printf("ERROR: ferror() returned non-zero at EOF. errno = %d\n",
190 errno);
191 exit(1);
192 }
193 }
194
195 if (after EQ was)
196 printf("File positions at end of read and write match.\n");
197 else {
198
199 printf("File positions at end of read and write differ.\n");
200 printf(" Write ended at %ld ($%08.8lx), read ended at %ld ($%08.8lx).\n",
201 after, after, was, was);
202 }
203
204 /* now get the word at AMOUNT and print it before we write over it */
205
206 where = AMOUNT;
207
208 if (fseek(fp1, where, 0)) {
209
210 printf("ERROR: could not seek to %ld.\n", where);
211 exit(1);
212 }
213
214 if (feof(fp1)) {
215
216 printf("ERROR: unexpected EOF at %ld after fseek().\n", where);
217 exit(1);
218 }
219
220 j = getw(fp1);
221
222 if (ferror(fp1)) {
223
224 printf("ERROR: read error at %ld. errno=%d.\n",
225 where, errno);
226 exit(1);
227 }
228
229 printf("Random read executed at %ld. Value read was %d\n", where, j);
230
231#if !LATTICEC
232 FILEpr(fp1);
233#endif
234
235 /* now write some data in the middle of the file */
236
237 where = AMOUNT; /* note that AMOUNT is words, where is bytes */
238
239 if (fseek(fp1, where, 0)) {
240
241 printf("ERROR: could not seek to %ld.\n", where);
242 exit(1);
243 }
244
245 i = MAGIC;
246
247 putw(i, fp1);
248
249 if (ferror(fp1)) {
250
251 printf("ERROR: write %d failed. errno = %d.\n",
252 i, errno);
253 exit(1);
254 }
255
256 fflush(fp1);
257
258 printf("Random write of MAGIC executed to %ld.\n", where);
259
260#if !LATTICEC
261 FILEpr(fp1);
262#endif
263
264 /* seek back to what we just wrote */
265
266 where = AMOUNT;
267
268 if (fseek(fp1, where, 0)) {
269
270 printf("ERROR: could not seek to %ld.\n", where);
271 exit(1);
272 }
273
274 if (feof(fp1)) {
275
276 printf("ERROR: unexpected EOF at %ld after fseek().\n", where);
277 exit(1);
278 }
279
280 j = getw(fp1);
281
282 if (ferror(fp1)) {
283
284 printf("ERROR: read error at %ld. errno=%d.\n",
285 where, errno);
286 exit(1);
287 }
288
289 printf("Random read of MAGIC executed at %ld.\n", where);
290
291 if (j NE MAGIC) {
292
293 printf("ERROR: value read (%d) NE MAGIC (%d).\n", j, MAGIC);
294 exit(1);
295 }
296
297 if (feof(fp1)) {
298
299 printf("ERROR: EOF sensed after reading MAGIC at %ld.\n",
300 where);
301 exit(1);
302 }
303
304#if !LATTICEC
305 FILEpr(fp1);
306#endif
307
308 j = getw(fp1);
309
310 if (ferror(fp1)) {
311
312 printf("ERROR: error reading word after MAGIC. errno=%d.\n",
313 errno);
314 exit(1);
315 }
316
317 printf("Word after MAGIC = %d.\n", j);
318
319 /* seek to the end of the file */
320
321 where = after;
322
323 if (fseek(fp1, where, 0)) {
324
325 printf("ERROR: could not seek to %ld.\n", where);
326 exit(1);
327 }
328
329 if (feof(fp1)) {
330
331 printf("Note: EOF at %ld after fseek().\n", where);
332 }
333
334 /* write the MAGIC word at EOF to extend the file */
335
336 i = MAGIC;
337
338 putw(i, fp1);
339
340 if (ferror(fp1)) {
341
342 printf("ERROR: write of MAGIC failed. errno = %d.\n",
343 errno);
344 exit(1);
345 }
346
347 printf("Random write of MAGIC executed to EOF (%ld).\n", where);
348
349#if !LATTICEC
350 FILEpr(fp1);
351#endif
352
353 was = ftell(fp1);
354 printf("File position after write at EOF = %ld ($%08.8lx)\n",
355 was, was);
356
357 fflush(fp1);
358
359 /* seek back to what we just wrote */
360
361 where = after;
362
363 if (fseek(fp1, where, 0)) {
364
365 printf("ERROR: could not seek to %ld.\n", where);
366 exit(1);
367 }
368
369 if (feof(fp1)) {
370
371 printf("ERROR: unexpected EOF at %ld after fseek().\n", where);
372 exit(1);
373 }
374
375 j = getw(fp1);
376
377 if (ferror(fp1)) {
378
379 printf("ERROR: read error at %ld. errno=%d.\n",
380 where, errno);
381 exit(1);
382 }
383
384 printf("Random access read executed at %ld.\n", where);
385
386 if (j NE MAGIC) {
387
388 printf("ERROR: j (%d) NE MAGIC (%d).\n", j, MAGIC);
389 exit(1);
390 }
391
392 if (feof(fp1))
393 printf("Note: EOF sensed after reading MAGIC at %ld.\n",
394 where);
395
396 was = ftell(fp1);
397 printf("Final file position = %ld ($%08.8lx).\n", was, was);
398
399#if !LATTICEC
400 FILEpr(fp1);
401#endif
402
403 j = getw(fp1);
404
405 if (!feof(fp1)) {
406
407 was = ftell(fp1);
408 printf("ERROR: no EOF seen after read at EOF. File at %ld.\n",
409 was);
410 exit(1);
411 }
412
413 printf("EOF seen after read at EOF.\n");
414 exit(0);
415}
Note: See TracBrowser for help on using the repository browser.