source: buchla-68k/orig/LATTICE/TESTCIO.C@ 3ae31e9

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

Imported original source code.

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