-
Notifications
You must be signed in to change notification settings - Fork 2
/
tstfdkio.c
1322 lines (1241 loc) · 69.2 KB
/
tstfdkio.c
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**************************************************************************************************/
/* GCCLIB Test Suite - Fixed Record File IO */
/* */
/* Part of GCCLIB - VM/370 CMS Native Std C Library; A Historic Computing Toy */
/* */
/* Contributors: See contrib memo */
/* */
/* Released to the public domain. */
/**************************************************************************************************/
#define __FILENAME__ "TSTFDKIO"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <cmssys.h>
#include <errno.h>
#include "tsts.h"
/**************************************************************************************************/
/* FILE * fopen(const char * filespec, const char * access) */
/* */
/* Open the specified file and return a stream associated with that file. */
/* filespec is a pointer to a string containing the specification of the file to be opened: */
/* "CONSOLE" terminal console (read or write) */
/* "PRINTER" printer (write only) */
/* "PUNCH" card punch (write only) */
/* "READER" card reader (read only) */
/* filename filetype [filemode [F|V [recordLength]]] */
/* disk file (read or write), where: */
/* filename is the up to 8 character name. */
/* filetype is the up to 8 character type. */
/* filemode is the up to 2 character disk mode leter and optional number. */
/* Default "A1" */
/* F|V specifies the record format fixed or variable. It is ignored when */
/* opening a file for reading. */
/* reclen specifies the record length. It is required for fixed-length */
/* files, and is taken as the maximum record length for variable- */
/* length files. It is ignored when opening a file for reading */
/* Each of the above items must be separated by one or more blanks. */
/* */
/* access specifies how the file will be accessed (i.e. for input, output, etc): */
/* r|w|a[+][b] */
/* */
/* Returns: */
/* a pointer to the stream associated with the open file, or NULL on failure. */
/* */
/* Notes: */
/* 1. The maximum record length is 65535 bytes. */
/* 2. The maximum number of records in a file is 32768. */
/* 3. The maximum number of 800-byte blocks in a file is 16060. */
/* 4. When writing a text file, a newline character signals the end of the current record. */
/* 5. When reading a text file, a newline character is inserted at the end of each record. */
/* 6. When writing a fixed-length text file the buffer will be padded with blanks if needed. */
/* 7. When writing a fixed-length binary file the buffer will be padded with NULLs if needed. */
/* 8. Opening a file for writing causes the existing file of that name to be erased, if it */
/* exists. */
/**************************************************************************************************/
static void fopen_t() {
FILE *f;
SUB_STRT("fopen()");
/* Open */
MAKEFILE("TEMP FILE A F 80");
ASSERTNOTNULLP("fopen(TEMP FILE A F 512,r)",
f = fopen("TEMP FILE A F 512", "r"), f, if (f) fclose(f));
ASSERTNULL("fopen(NOT MEMO A F 80,r)", f = fopen("NOT MEMO A F 80", "r"), f,
if (f) fclose(f));
ASSERTNOTNULLP("fopen(NEW FILE A F 512,w)",
f = fopen("NEW FILE A F 512", "w"), f, if (f) fclose(f));
ASSERTNOTNULLP("fopen(TEMP FILE A 512,a)",
f = fopen("TEMP FILE A F 512", "a"), f, if (f) fclose(f));
ASSERTNULL("fopen(NOT FILE A F 512,a)", f = fopen("NOT FILE A F 512", "a"),
f, if (f) fclose(f));
ASSERTNOTNULLP("fopen(TEMP FILE A F 80,a)",
f = fopen("TEMP FILE A F 80", "a"), f, if (f) fclose(f));
/* Different Access types */
ASSERTNOTNULLP("fopen(TEMP FILE A F 80,r+)",
f = fopen("TEMP FILE A F 80", "r+"), f, if (f) fclose(f));
ASSERTNOTNULLP("fopen(TEMP FILE A F 80,rb+)",
f = fopen("TEMP FILE A F 80", "rb+"), f, if (f) fclose(f));
ASSERTNOTNULLP("fopen(TEMP FILE A F 80,rb)",
f = fopen("TEMP FILE A F 80", "rb"), f, if (f) fclose(f));
ASSERTNOTNULLP("fopen(NEW FILE A F 512,w+)",
f = fopen("NEW FILE A F 512", "w+"), f, if (f) fclose(f));
ASSERTNOTNULLP("fopen(NEW FILE A F 512,w+b)",
f = fopen("NEW FILE A F 512", "w+b"), f, if (f) fclose(f));
ASSERTNOTNULLP("fopen(NEW FILE A F 512,wb)",
f = fopen("NEW FILE A F 512", "wb"), f, if (f) fclose(f));
ASSERTNOTNULLP("fopen(TEMP FILE A F 512,a+)",
f = fopen("TEMP FILE A F 512", "a+"), f, if (f) fclose(f));
ASSERTNOTNULLP("fopen(TEMP FILE A F 512,a+b)",
f = fopen("TEMP FILE A F 512", "a+b"), f, if (f) fclose(f));
ASSERTNOTNULLP("fopen(TEMP FILE A F 512,ab)",
f = fopen("TEMP FILE A F 512", "ab"), f, if (f) fclose(f));
/* Bad Access */
ASSERTNULL("fopen(TEMP FILE A F 512,x)",
f = fopen("TEMP FILE A F 512", "x"), f, if (f) fclose(f));
ASSERTNULL("fopen(TEMP FILE A F 512,+)",
f = fopen("TEMP FILE A F 512", "+"), f, if (f) fclose(f));
ASSERTNULL("fopen(TEMP FILE A F 512,b)",
f = fopen("TEMP FILE A F 512", "b"), f, if (f) fclose(f));
ASSERTNULL("fopen(TEMP FILE A F 512,)", f = fopen("TEMP FILE A F 512", ""),
f, if (f) fclose(f));
ASSERTNULL("fopen(TEMP FILE A F 512,r+bx)",
f = fopen("TEMP FILE A F 512", "r+bx"), f, if (f) fclose(f));
/* Finally Check if write deletes */
ASSERTNOTNULLP("fopen(TEMP FILE A F 80,w)",
f = fopen("TEMP FILE A F 80", "w"), f, if (f) fclose(f));
ASSERTNULL("fopen(TEMP FILE A F 80,r)", f = fopen("TEMP FILE A F 80", "r"),
f, if (f) fclose(f));
}
/**************************************************************************************************/
/* int fclose(FILE * file) */
/* Returns: */
/* 0 if all is well, EOF if there is an error. */
/**************************************************************************************************/
static void fclose_t() {
FILE *f;
SUB_STRT("fclose()");
ASSERTNOTNULLP("fopen(TEMP FILE A F 80,w)",
f = fopen("TEMP FILE A F 80", "w"), f,);
ASSERTNOTEOFP("fclose(f)", , fclose(f),);
ASSERTEOF("fputs(SHOULD NOT SEE,f)", , fputs("SHOULD NOT SEE\n", f),);
ASSERTEOF("fclose(f)", , fclose(f),);
}
/**************************************************************************************************/
/* int fflush(FILE * stream) */
/* */
/* Writes any unwritten characters to the specified stream. */
/* */
/* Returns: */
/* 0, indicating that all is well, or a return code from the write operation. */
/* */
/* Notes: */
/* 1. If the stream is not open for output, fflush is a NOP. */
/**************************************************************************************************/
static void fflush_t() {
FILE *test;
FILE *reader;
char buf[200];
SUB_STRT("fflush()");
ASSERTNOTNULLP("fopen(NEW FILE A F 80,w)",
test = fopen("NEW FILE A F 80", "w"), test,);
ASSERTNOTEOFP("fputc()", , FPUTC_WR(TEST_SHORT_STRING, test),);
ASSERTZEROP("fflush()", , fflush(test),);
ASSERTZEROP("fclose()", , fclose(test),);
ASSERTNOTNULLP("fopen(NEW FILE,r)", reader = fopen("NEW FILE", "r"),
reader,);
ASSERTNOTNULLP("fgets(reader)", , fgets(buf, 200, reader),);
ASSERTZERO("strcmp()", , strcmp(buf, TEST_SHORT_STRING_NL),);
ASSERTNULL("fgets(reader)", , fgets(buf, 200, reader),);
ASSERTNOTZERO("feof(reader)", , feof(reader),);
ASSERTZEROP("fclose(reader)", , fclose(reader),);
}
/**************************************************************************************************/
/* int ferror(FILE * stream) */
/* */
/* Returns the error status of the specified stream. */
/* stream a pointer to the open input stream. */
/* */
/* Returns: */
/* the error status, or 0 if there is no error. */
/**************************************************************************************************/
static void ferror_t() {
FILE *test;
SUB_STRT("ferror()");
ASSERTNOTNULLP("fopen(NEW FILE A F 80,w)",
test = fopen("NEW FILE A F 80", "w"), test,);
ASSERTEOF("fgetc()", , fgetc(test),);
ASSERTNOTZERO("ferror()", , ferror(test),);
ASSERTZEROP("fclose()", , fclose(test),);
}
/**************************************************************************************************/
/* void clearerr(FILE * stream) */
/* */
/* Resets the error status of the specified stream to 0. */
/* stream a pointer to the open input stream. */
/**************************************************************************************************/
static void clearerr_t() {
FILE *test;
SUB_STRT("clearerr()");
ASSERTNOTNULLP("fopen(NEW FILE A F 80,w)",
test = fopen("NEW FILE A F 80", "w"), test,);
ASSERTEOF("fgetc()", , fgetc(test),);
clearerr(test);
ASSERTZERO("ferror()", , ferror(test),);
ASSERTZEROP("fclose()", , fclose(test),);
}
/**************************************************************************************************/
/* int fgetc(FILE * stream) */
/* */
/* Read the next character from the specified output stream. */
/* stream a pointer to the open output stream. */
/* */
/* Returns: */
/* the character, or EOF if there is an error. */
/* */
/* Notes: */
/* 1. A newline character is added at the end of each physical line read when reading TEXT */
/* files. */
/**************************************************************************************************/
static void fgetc_t() {
FILE *test;
FILE *out;
char buf[300];
char buf2[300];
SUB_STRT("fgetc() - text");
ASSERTNOTNULLP("fopen(TEST FILE A F 120,w)",
out = fopen("TEST FILE A F 120", "w"), out,);
ASSERTNOTEOFP("fputs()", , fputs(TEST_STRING, out),);
ASSERTZEROP("fclose()", , fclose(out),);
ASSERTNOTNULLP("fopen(TEST FILE A,r)", test = fopen("TEST FILE A", "r"),
test,);
ASSERTNOTNULL("FGETC_RD()", , FGETC_RD(buf, 200, test),);
ASSERTZERO("strcmp()", , strcmp(buf, TEST_STRING_NL),);
ASSERTZEROP("fclose()", , fclose(test),);
SUB_STRT("fgetc() - binary");
ASSERTNOTNULLP("fopen(TEST FILE A F 120,w)",
out = fopen("TEST FILE A F 120", "w"), out,);
ASSERTNOTEOFP("fputs()", , fputs(TEST_STRING, out),);
ASSERTNOTEOFP("fputs()", , fputs(TEST_STRING, out),);
ASSERTZEROP("fclose()", , fclose(out),);
ASSERTNOTNULLP("fopen(TEST FILE A,rb)", test = fopen("TEST FILE A", "rb"),
test,);
ASSERTNOTNULL("FGETC_RD()", ,
FGETC_RD(buf, strlen(TEST_STRING) * 2, test),);
strcpy(buf2, TEST_STRING);
strcat(buf2, TEST_STRING);
ASSERTZERO("strcmp()", , strcmp(buf, buf2),);
ASSERTZEROP("fclose()", , fclose(test),);
}
/**************************************************************************************************/
/* char * fgets(char * str, int num, FILE * stream) */
/* */
/* Read up to 'num' - 1 characters from the specified file stream and place them into 'str', */
/* terminating them with a NULL. fgets() stops when it reaches the end of a line, in which case */
/* 'str' will contain a newline character. Otherwise, fgets() will stop when it reaches 'num'-1 */
/* characters or encounters the EOF character. */
/* */
/* Returns: */
/* the a pointer to 'str', or NULL if there is an error or EOF. */
/* */
/* Notes: */
/* 1. fgets is only appropriate for a text file. */
/**************************************************************************************************/
static void fgets_t() {
FILE *test;
FILE *out;
char buf[300];
char buf2[300];
char buf3[300];
SUB_STRT("fgets() - text");
ASSERTNOTNULLP("fopen(TEST FILE A F 120,w)",
out = fopen("TEST FILE A F 120", "w"), out,);
ASSERTNOTEOFP("fputs()", , fputs(TEST_STRING, out),);
ASSERTZEROP("fclose()", , fclose(out),);
ASSERTNOTNULLP("fopen(TEST FILE A,r)", test = fopen("TEST FILE A", "r"),
test,);
ASSERTNOTNULL("fgets()", , fgets(buf, 200, test),);
ASSERTZERO("strcmp()", , strcmp(buf, TEST_STRING_NL),);
ASSERTZEROP("fclose()", , fclose(test),);
SUB_STRT("fgets() - binary");
ASSERTNOTNULLP("fopen(TEST FILE A F 120,w)",
out = fopen("TEST FILE A F 120", "w"), out,);
ASSERTNOTEOFP("fputs()", , fputs(TEST_STRING, out),);
ASSERTNOTEOFP("fputs()", , fputs(TEST_STRING, out),);
ASSERTZEROP("fclose()", , fclose(out),);
ASSERTNOTNULLP("fopen(TEST FILE A,rb)", test = fopen("TEST FILE A", "rb"),
test,);
ASSERTNOTNULL("fgets()", , fgets(buf, 300, test),);
ASSERTNOTNULL("fgets()", , fgets(buf3, (strlen(TEST_STRING) * 2) + 1 - 120,
test),); /* rest of the data */
strcat(buf, buf3);
strcpy(buf2, TEST_STRING);
strcat(buf2, TEST_STRING);
ASSERTZERO("strcmp()", , strcmp(buf, buf2),);
ASSERTZEROP("fclose()", , fclose(test),);
}
/**************************************************************************************************/
/* int feof(FILE * stream) */
/* */
/* Report whether end of file has been reached for the specified input stream. */
/* stream a pointer to the open input stream. */
/* */
/* Returns: */
/* 1 if EOF has been reached, 0 otherwise. */
/**************************************************************************************************/
static void feof_t() {
FILE *test;
char buf[81];
SUB_STRT("feof()");
ASSERTNOTNULLP("fopen(TEMP FILE A F 80,w)",
test = fopen("TEMP FILE A F 80", "w"), test,);
ASSERTZERO("feof()", , feof(test),);
ASSERTNOTEOFP("fputs()", , fputs(TEST_SHORT_STRING, test),);
ASSERTZEROP("fclose()", , fclose(test),);
ASSERTNOTNULLP("fopen(TEMP FILE A,r)", test = fopen("TEMP FILE A", "r"),
test,);
ASSERTZERO("feof()", , feof(test),);
ASSERTNOTNULL("fgets()", , fgets(buf, 81, test),);
ASSERTZERO("feof()", , feof(test),);
ASSERTNULL("fgets()", , fgets(buf, 81, test),);
ASSERTNOTZERO("feof()", , feof(test),);
ASSERTZEROP("fclose()", , fclose(test),);
DELFILE("TEMP FILE A");
}
/**************************************************************************************************/
/* int ungetc(int c, FILE * stream) */
/* */
/* Push a character back onto the open input stream 'stream', such that it is the next character */
/* to be read. */
/* c the character to be pushed back onto the stream. */
/* stream a pointer to the open input stream. */
/* */
/* Returns: */
/* c if successful, EOF if not. */
/* */
/* Notes: */
/* 1. Only 1 character can be in the unget buffer. If a second call to ungetc is made before */
/* the previous character is read, it is not placed in the buffer and EOF is returned. */
/**************************************************************************************************/
static void ungetc_t() {
FILE *test;
char buf[200];
char buf2[200];
SUB_STRT("ungetc() - opened for writing");
ASSERTNOTNULLP("fopen(TEMP FILE A F 80,w)",
test = fopen("TEMP FILE A F 80", "w"), test,);
ASSERTEOF("ungetc(y)", , ungetc('y', test),);
ASSERTZEROP("fclose()", , fclose(test),);
SUB_STRT("ungetc() - text");
ASSERTNOTNULLP("fopen(TEMP FILE A F 80,w)",
test = fopen("TEMP FILE A F 80", "w"), test,);
ASSERTNOTEOFP("fputs()", , fputs(TEST_SHORT_STRING, test),);
ASSERTZEROP("fclose()", , fclose(test),);
ASSERTNOTNULLP("fopen(TEMP FILE A,r)", test = fopen("TEMP FILE A", "r"),
test,);
ASSERTNOTEOF("ungetc(x)", , ungetc('x', test),);
ASSERTEOF("ungetc(y)", , ungetc('y', test),);
ASSERTNOTNULL("fgets()", , fgets(buf, 200, test),);
ASSERTNOTEOF("ungetc(z)", , ungetc('z', test),);
buf2[0] = 'x';
buf2[1] = 0;
strcat(buf2, TEST_SHORT_STRING_NL);
ASSERTZERO("strcmp()", , strcmp(buf, buf2),);
ASSERTZEROP("fclose()", , fclose(test),);
SUB_STRT("ungetc() - binary");
ASSERTNOTNULLP("fopen(TEMP FILE A F 80,w)",
test = fopen("TEMP FILE A F 80", "w"), test,);
ASSERTNOTEOFP("fputs()", , fputs(TEST_SHORT_STRING, test),);
ASSERTZEROP("fclose()", , fclose(test),);
ASSERTNOTNULLP("fopen(TEMP FILE A,rb)", test = fopen("TEMP FILE A", "rb"),
test,);
ASSERTNOTEOF("ungetc(x)", , ungetc('x', test),);
ASSERTEOF("ungetc(y)", , ungetc('y', test),);
ASSERTNOTNULL("fgets()", ,
fgets(buf, strlen(TEST_SHORT_STRING) + 2, test),);
ASSERTNOTEOF("ungetc(z)", , ungetc('z', test),);
buf2[0] = 'x';
buf2[1] = 0;
strcat(buf2, TEST_SHORT_STRING);
ASSERTZERO("strcmp()", , strcmp(buf, buf2),);
ASSERTZEROP("fclose()", , fclose(test),);
DELFILE("TEMP FILE A");
}
/**************************************************************************************************/
/* int fputc(int c, FILE * stream) */
/* */
/* Write a character to the open output stream. This is the same as putc(). */
/* c the character to be written. */
/* stream a pointer to the open output stream. */
/* */
/* Returns: */
/* the character, or EOF if there is an error. */
/**************************************************************************************************/
static void fputc_t() {
FILE *test;
char buf[201];
char buf2[201];
char buf3[201];
int i;
SUB_STRT("fputc() - text");
ASSERTNOTNULLP("fopen(TEMP FILE A F 110,w)",
test = fopen("TEMP FILE A F 110", "w"), test,);
FPUTC_T(test);
ASSERTZEROP("fclose()", , fclose(test),);
ASSERTNOTNULLP("fopen(TEMP FILE A,r)", test = fopen("TEMP FILE A", "r"),
test,);
FGETS_T(test, 110);
ASSERTZEROP("fclose()", , fclose(test),);
SUB_STRT("fputc() - binary");
ASSERTNOTNULLP("fopen(TEMP FILE A F 110,wb)",
test = fopen("TEMP FILE A F 110", "wb"), test,);
buf2[0] = 0;
for (i = 0; i < 20; i++) {
FPUTC_WR("123456789+", test);
strcat(buf2, "123456789+");
}
ASSERTZEROP("fclose()", , fclose(test),);
ASSERTNOTNULLP("fopen(TEMP FILE A,rb)", test = fopen("TEMP FILE A", "rb"),
test,);
ASSERTNOTNULL("fgets()", , fgets(buf, 201, test),);
ASSERTNOTNULL("fgets()", , fgets(buf3, strlen(buf2) - 110 + 1, test),);
strcat(buf, buf3);
ASSERTZERO("strcmp()", , strcmp(buf, buf2),);
ASSERTZEROP("fclose()", , fclose(test),);
}
/**************************************************************************************************/
/* int fputs(const char * str, FILE * stream) */
/* */
/* Write a string to the open output stream. */
/* str the string to be written. */
/* stream a pointer to the open output stream. */
/* */
/* Returns: */
/* non-negative on success, EOF on failure. */
/**************************************************************************************************/
static void fputs_t() {
FILE *test;
char buf[201];
char buf2[201];
char buf3[201];
int i;
SUB_STRT("fputs() - text");
ASSERTNOTNULLP("fopen(TEMP FILE A F 110,w)",
test = fopen("TEMP FILE A F 110", "w"), test,);
FPUTS_T(test);
ASSERTZEROP("fclose()", , fclose(test),);
ASSERTNOTNULLP("fopen(TEMP FILE A,r)", test = fopen("TEMP FILE A", "r"),
test,);
FGETS_T(test, 110);
SUB_STRT("fputs() - binary");
ASSERTZEROP("fclose()", , fclose(test),);
ASSERTNOTNULLP("fopen(TEMP FILE A F 110,wb)",
test = fopen("TEMP FILE A F 110", "wb"), test,);
buf2[0] = 0;
for (i = 0; i < 20; i++) {
fputs("123456789+", test);
strcat(buf2, "123456789+");
}
ASSERTZEROP("fclose()", , fclose(test),);
ASSERTNOTNULLP("fopen(TEMP FILE A,rb)", test = fopen("TEMP FILE A", "rb"),
test,);
ASSERTNOTNULL("fgets()", , fgets(buf, 201, test),);
ASSERTNOTNULL("fgets()", , fgets(buf3, strlen(buf2) - 110 + 1, test),);
strcat(buf, buf3);
ASSERTZERO("strcmp()", , strcmp(buf, buf2),);
ASSERTZEROP("fclose()", , fclose(test),);
}
/**************************************************************************************************/
/* int fwrite(const void * buffer, size_t size, size_t count, FILE * stream) */
/* */
/* Write 'count' objects of size 'size' from 'buffer' to the open output stream. */
/* buffer the array of objects to be written. */
/* size the size of each object. */
/* count the number of objects to be written. */
/* stream a pointer to the open output stream. */
/* */
/* Returns: */
/* the number of objects written. */
/* */
/* Notes: */
/* 1. Use fwrite for writing binary data. Newlines in the output stream are treated as any */
/* other character. */
/* 2. When the record length for the file is reached, that record is written to disk and a */
/* new record is started. Thus the file is treated as a stream of bytes. */
/* */
/* int fread(void * buffer, size_t size, size_t count, FILE * stream) */
/* */
/* Read 'num' objects of size 'size' from the open input stream and write them to 'buffer'. */
/* buffer the array into which the objects are to be read. */
/* size the size of each object. */
/* count the number of objects to be read. */
/* stream a pointer to the open input stream. */
/* */
/* Returns: */
/* the number of objects read. */
/* */
/* Notes: */
/* 1. Use fread for writing binary data. Newlines in the input stream are treated as any */
/* other character. */
/* 2. When the end of a record is reached, reading continues uninterrupted from the next */
/* record. Thus the file is treated as a stream of bytes. */
/**************************************************************************************************/
static void fwriteread_t() {
FILE *test;
const char *item = "1234567890abcdefghijklmnopqrstuvwxyz";
const char *item2 = "1234567890\nahijklmnopqrstuvwxyz";
const int count = 20;
char *buffer;
char *buffer2;
int i;
SUB_STRT("fwrite() and fread()");
buffer = malloc(count * strlen(item));
buffer2 = malloc(count * strlen(item));
buffer[0] = 0;
buffer2[0] = 0;
buffer2[count * strlen(item)] = 0;
for (i = 0; i < count; i++) strcat(buffer, item);
ASSERTNOTNULLP("fopen(TEMP FILE A F 105,wb)",
test = fopen("TEMP FILE A F 105", "wb"), test,);
ASSERTNOTZERO("fwrite()", ,
fwrite(buffer, strlen(item), count, test) == count,);
ASSERTZEROP("fclose()", , fclose(test),);
ASSERTNOTNULLP("fopen(TEMP FILE A,rb)", test = fopen("TEMP FILE A", "rb"),
test,);
ASSERTNOTZERO("fread()", ,
fread(buffer2, strlen(item), count, test) == count,);
ASSERTZERO("strcmp()", , strcmp(buffer, buffer2),);
ASSERTZEROP("fclose()", , fclose(test),);
buffer2[strlen(item2)] = 0;
ASSERTNOTNULLP("fopen(TEMP FILE A F 105,w)",
test = fopen("TEMP FILE A F 105", "w"), test,);
ASSERTNOTZERO("fwrite()", , fwrite(item2, strlen(item2), 1, test) == 1,);
ASSERTZEROP("fclose()", , fclose(test),);
ASSERTNOTNULLP("fopen(TEMP FILE A,r)", test = fopen("TEMP FILE A", "r"),
test,);
ASSERTNOTZERO("fread()", , fread(buffer2, strlen(item2), 1, test) == 1,);
ASSERTZERO("strcmp()", , strcmp(item2, buffer2),);
ASSERTZEROP("fclose()", , fclose(test),);
free(buffer);
free(buffer2);
}
/**************************************************************************************************/
/* long ftell(FILE * stream) */
/* */
/* Return the current file position for the specified stream. */
/* stream a pointer to the open stream. */
/* */
/* Returns: */
/* the position of the next character to be read or written, or -1 if there is an error. */
/* */
/* Notes: */
/* 1. ftell is only valid for a disk file that is open for reading. */
/* 2. Functioally, ftell is equivalent to fgetpos. */
/**************************************************************************************************/
static void ftell_t() {
FILE *test;
SUB_STRT("ftell()");
ASSERTNOTNULLP("fopen(TEMP FILE A F 80,wb)",
test = fopen("TEMP FILE A F 80", "wb"), test,);
ASSERTNOTEOFP("fputs()", , fputs("PREFIX", test),);
ASSERTNOTZERO("ftell()", , ftell(test) == 6,);
ASSERTZEROP("fclose()", , fclose(test),);
}
/**************************************************************************************************/
/* int fseek(FILE * stream, long offset, int origin) */
/* */
/* Moves the file position indicator for the specified stream to the specified position relative */
/* to 'origin', which may be: */
/* SEEK_SET Seek from the start of the file. */
/* SEEK_CUR Seek from the current location. */
/* SEEK_END Seek from the end of the file. */
/* */
/* Returns: */
/* 0 on success, non-zero on failure. */
/* */
/* Note: */
/* 1. fseek is only valid for fixed record length disk files */
/**************************************************************************************************/
static void fseek_t() {
FILE *test;
const char *item = "1234567890abcdefghijklmnopqrstuvwxyz";
const int count = 20;
char *buffer;
int i;
fpos_t position;
char c;
SUB_STRT("fseek()");
buffer = malloc(count * strlen(item));
buffer[0] = 0;
for (i = 0; i < count; i++) strcat(buffer, item);
ASSERTNOTNULLP("fopen(TEMP FILE A F 1024,wb+)",
test = fopen("TEMP FILE A F 1024", "wb+"), test,);
ASSERTNOTEOFP("fputs()", , fputs("PREFIX", test),);
ASSERTNOTZERO("fwrite()", ,
fwrite(buffer, strlen(item), count, test) == count,);
ASSERTZEROP("fseek(SEEK_CUR)", , fseek(test, -1, SEEK_CUR),);
ASSERTNOTZERO("fgetc()", , fgetc(test) == 'z',);
ASSERTZEROP("fseek(SEEK_SET)", , fseek(test, 0, SEEK_SET),);
ASSERTNOTZERO("fgetc()", , fgetc(test) == 'P',);
ASSERTZEROP("fseek(SEEK_END)", , fseek(test, -1, SEEK_END),);
ASSERTNOTZERO("fgetc()", , fgetc(test) == 0,);
ASSERTZEROP("fclose()", , fclose(test),);
ASSERTNOTNULLP("fopen(TEMP FILE A F 80,wb+)",
test = fopen("TEMP FILE A F 80", "wb+"), test,);
ASSERTNOTEOFP("fputs()", ,
fputs("PREFIXABCDEFGHIJKLMNOPQRSTUVWXYZPREFIXABCDEFGHIJKLMNOPQRSTUVWXYZPREFIXABCDEFGHIJKLMNOPQRSTUVWXYZPREFIXABCDEFGHIJKLMNOPQRSTUVWXYZ",
test),);
ASSERTNOTZERO("fwrite()", ,
fwrite(buffer, strlen(item), count, test) == count,);
ASSERTZEROP("fseek(SEEK_CUR)", , fseek(test, -1, SEEK_CUR),);
ASSERTNOTZERO("fgetc()", c = fgetc(test), c == 'z',);
ASSERTZEROP("fseek(SEEK_SET)", , fseek(test, 5, SEEK_SET),);
ASSERTNOTZERO("fgetc()", c = fgetc(test), c == 'X',);
ASSERTZEROP("fseek(SEEK_END)", , fseek(test, -1, SEEK_END),);
ASSERTNOTZERO("fgetc()", c = fgetc(test), c == 0,);
ASSERTZEROP("fclose()", , fclose(test),);
free(buffer);
}
/**************************************************************************************************/
/* int fgetpos(FILE * stream, fpos_t * position) */
/* */
/* The function fills the fpos_t object pointed by pos with the information needed from the */
/* stream's position indicator to restore the stream to its current position with a call to */
/* fsetpos */
/* */
/* Returns: */
/* 0 on success, non-zero on failure. */
/* */
/* */
/* int fsetpos(FILE * stream, fpos_t * position) */
/* */
/* Restores the current position in the stream to pos.
/* The internal file position indicator associated with stream is set to the position represented */
/* by pos, which is a pointer to an fpos_t object whose value shall have been previously obtained */
/* by a call to fgetpos. */
/* The end-of-file internal indicator of the stream is cleared after a successful call to this */
/* function, and all effects from previous calls to ungetc on this stream are dropped. */
/* */
/* Returns: */
/* 0 on success, non-zero on failure. */
/**************************************************************************************************/
static void fgetsetpos_t() {
FILE *test;
const char *item = "1234567890abcdefghijklmnopqrstuvwxyz";
const int count = 20;
char *buffer;
char *buffer2;
int i;
fpos_t position;
SUB_STRT("fgetpos() and fsetpos()");
buffer = malloc(count * strlen(item));
buffer2 = malloc(count * strlen(item));
buffer[0] = 0;
buffer2[0] = 0;
buffer2[count * strlen(item)] = 0;
for (i = 0; i < count; i++) strcat(buffer, item);
ASSERTNOTNULLP("fopen(TEMP FILE A F 1024,wb+)",
test = fopen("TEMP FILE A F 1024", "wb+"), test,);
ASSERTNOTEOFP("fputs()", , fputs("PREFIX", test),);
ASSERTZERO("fgetpos()", , fgetpos(test, &position) != 0,);
ASSERTNOTZERO("fwrite()", ,
fwrite(buffer, strlen(item), count, test) == count,);
ASSERTZERO("fsetpos()", , fsetpos(test, &position) != 0,);
ASSERTNOTZEROP("fread()", ,
(i = fread(buffer2, strlen(item), count, test)) == count,);
ASSERTZERO("strcmp()", , strcmp(buffer, buffer2),);
ASSERTZEROP("fclose()", , fclose(test),);
ASSERTNOTNULLP("fopen(TEMP FILE A F 80,wb+)",
test = fopen("TEMP FILE A F 80", "wb+"), test,);
ASSERTNOTEOFP("fputs()", ,
fputs("PREFIXABCDEFGHIJKLMNOPQRSTUVWXYZPREFIXABCDEFGHIJKLMNOPQRSTUVWXYZPREFIXABCDEFGHIJKLMNOPQRSTUVWXYZPREFIXABCDEFGHIJKLMNOPQRSTUVWXYZ",
test),);
ASSERTZERO("fgetpos()", , fgetpos(test, &position) != 0,);
ASSERTNOTZERO("fwrite()", ,
fwrite(buffer, strlen(item), count, test) == count,);
ASSERTZERO("fsetpos()", , fsetpos(test, &position) != 0,);
ASSERTNOTZERO("fread()", ,
fread(buffer2, strlen(item), count, test) == count,);
ASSERTZERO("strcmp()", , strcmp(buffer, buffer2),);
ASSERTZEROP("fclose()", , fclose(test),);
free(buffer);
free(buffer2);
}
/**************************************************************************************************/
/* int fgetrec(FILE * stream) */
/* */
/* The function returns the current record number (1 base) */
/* Non-standard GCC CMS extention */
/* */
/* Returns: */
/* record number or 0 on failure / not block device */
/* */
/* int fsetrec(FILE * stream, int recnum) */
/* */
/* The function sets/moves to the record number (1 base) */
/* Non-standard GCC CMS extention */
/* */
/* Returns: */
/* record number or 0 on failure / not block device */
/**************************************************************************************************/
static void fgetsetrec_t() {
FILE *test;
const char *item = "1234567890abcdefghijklmnopqrstuvwxyz\n";
int record;
char buffer[100];
SUB_STRT("fgetrec() and fsetrec()");
ASSERTNOTNULLP("fopen(TEMP FILE A F 80,w+)",
test = fopen("TEMP FILE A F 80", "w+"), test,);
ASSERTNOTEOFP("fputs()", , fputs("PREFIX\n", test),);
ASSERTNOTZERO("fgetrec()", , record = fgetrec(test),);
ASSERTNOTEOFP("fputs()", , fputs(item, test),);
ASSERTNOTZERO("fsetrec()", , fsetrec(test, record),);
ASSERTNOTNULL("fgets()", , fgets(buffer, 100, test),);
ASSERTZERO("strcmp()", , strcmp(buffer, item),);
ASSERTZEROP("fclose()", , fclose(test),);
}
/**************************************************************************************************/
/* void rewind(FILE * stream) */
/* */
/* Move the file position indicator to the beginning of the specified stream, and clears the */
/* error and EOF flags associated with that stream. */
/* stream a pointer to the open stream. */
/**************************************************************************************************/
static void rewind_t() {
FILE *test;
const char *item = "1234567890abcdefghijklmnopqrstuvwxyz";
const int count = 20;
char *buffer;
char *buffer2;
int i;
SUB_STRT("rewind()");
buffer = malloc(count * strlen(item));
buffer2 = malloc(count * strlen(item));
buffer[0] = 0;
buffer2[0] = 0;
buffer2[count * strlen(item)] = 0;
for (i = 0; i < count; i++) strcat(buffer, item);
ASSERTNOTNULLP("fopen(TEMP FILE A F 80,wb+)",
test = fopen("TEMP FILE A F 80", "wb+"), test,);
ASSERTNOTZERO("fwrite()", ,
fwrite(buffer, strlen(item), count, test) == count,);
ASSERTNOTZERO("rewind()", rewind(test),
1,); /* No direct way to detect an error! */
ASSERTNOTZERO("fread()", ,
fread(buffer2, strlen(item), count, test) == count,);
ASSERTZERO("strcmp()", , strcmp(buffer, buffer2),);
ASSERTZEROP("fclose()", , fclose(test),);
free(buffer);
free(buffer2);
}
/**************************************************************************************************/
/* void setbuf(FILE * file, char *buffer) */
/* */
/* Specifies the buffer to be used by the stream for I/O operations, used as a cache. Or, */
/* alternatively, if buffer is a null pointer, buffering is disabled for the stream */
/* The buffer is assumed to be at least BUFSIZ bytes in size */
/* */
/* A call to this function is equivalent to calling setvbuf with _IOFBF as mode and BUFSIZ as */
/* size (when buffer is not a null pointer), or equivalent to calling it with _IONBF as mode */
/* (when it is a null pointer). */
/* */
/**************************************************************************************************/
static void setbuf_t() {
FILE *test;
char *buffer;
SUB_STRT("setbuf()");
buffer = malloc(BUFSIZ);
ASSERTNOTNULLP("fopen(TEMP FILE A F 80,w)",
test = fopen("TEMP FILE A F 80", "w"), test,);
setbuf(test, buffer); /* No error detection! */
ASSERTZEROP("fclose()", , fclose(test),);
free(buffer);
}
/**************************************************************************************************/
/* int setvbuf(FILE * file, char * buffer, int mode, size_t size) */
/* */
/* Specifies a buffer for stream. The function allows to specify the mode and size of the buffer */
/* (in bytes). */
/* */
/* If buffer is a null pointer, the function automatically allocates a buffer */
/* (using size as a hint on the size to use). Otherwise, the array pointed by buffer may be */
/* used as a buffer of size bytes. */
/* */
/* Notes */
/* Buffer is used a a cache for Fixed Record Length files only */
/* Modes _IOFBF and _IOLBF are equivalent and are a line cache for reading */
/* */
/* Returns: */
/* If the buffer is correctly assigned to the file, a zero value is returned. */
/* Otherwise, a non-zero value is returned */
/**************************************************************************************************/
static void setvbuf_t() {
FILE *test;
char *buffer;
SUB_STRT("setvbuf()");
ASSERTNOTNULLP("fopen(TEMP FILE A F 80,w)",
test = fopen("TEMP FILE A F 80", "w"), test,);
ASSERTZERO("setvbuf()", , setvbuf(test, 0, _IOLBF, BUFSIZ),);
ASSERTZEROP("fclose()", , fclose(test),);
}
/**************************************************************************************************/
/* randon access / cache testing functions */
/**************************************************************************************************/
static void makeRecord(char *record, int recno, int length, char *c) {
int i;
length -= 6;
sprintf(record, "%05d", recno);
for (i = 0; i < length; i++) strcat(record, c);
strcat(record, ">");
}
static void writeRecordText(FILE *file, int recno, int length, char *c) {
char *record = malloc(length + 2);
makeRecord(record, recno, length, c);
fputs(record, file);
free(record);
}
static int readRecordText(FILE *file, int recno, int length, char *c) {
char *record = malloc(length + 2);
char *fromFile = malloc(length + 2);
int rc;
makeRecord(record, recno, length, c);
strcat(record, "\n");
if (!fgets(fromFile, length + 2, file)) {
printf("fgets() returned null\n");
free(record);
free(fromFile);
return 1;
}
rc = strcmp(record, fromFile);
if (rc) {
printf("from file (%s)\n", fromFile);
printf("should be (%s)\n", record);
}
free(record);
free(fromFile);
return rc;
}
static void writeRecordBin(FILE *file, int recno, int length, char *c) {
char *record = malloc(length + 1);
makeRecord(record, recno, length, c);
fwrite(record, length, 1, file);
free(record);
}
static int readRecordBin(FILE *file, int recno, int length, char *c) {
char *record = malloc(length + 1);
char *fromFile = malloc(length + 1);
int rc;
makeRecord(record, recno, length, c);
if (!fread(fromFile, length, 1, file)) {
printf("fread() returned 0\n");
free(record);
free(fromFile);
return 1;
}
fromFile[length] = 0;
rc = strcmp(record, fromFile);
if (rc) {
printf("from file (%s)\n", fromFile);
printf("should be (%s)\n", record);
}
free(record);
free(fromFile);
return rc;
}
static void raccess_t() {
FILE *test;
int i;
long r;
int rc;
srand(10); /* Seed random numbers to a set state - repeatability */
SUB_STRT("random access test");
ASSERTNOTNULLP("fopen(TEMP FILE A F 80,w+)",
test = fopen("TEMP FILE A F 80", "w+"), test,);
for (i = 1; i < 100; i++) writeRecordText(test, i, 80, "-");
rewind(test);
TST_STRT("serial read text");
rc = 0;
for (i = 1; i < 100; i++) {
rc = readRecordText(test, i, 80, "-");
if (rc) {
TST_ERR("mismatch");
break;
}
}
if (!rc) TST_OK();
ASSERTZEROP("fclose()", , fclose(test),);
ASSERTNOTNULLP("fopen(TEMP FILE A F 80,wb+)",
test = fopen("TEMP FILE A F 80", "wb+"), test,);
for (i = 1; i < 100; i++) writeRecordBin(test, i, 80, "-");
rewind(test);
TST_STRT("serial read binary");
rc = 0;
for (i = 1; i < 100; i++) {
rc = readRecordBin(test, i, 80, "-");
if (rc) {
TST_ERR("mismatch");
break;
}
}
if (!rc) TST_OK();
ASSERTZEROP("fclose()", , fclose(test),);
ASSERTNOTNULLP("fopen(TEMP FILE A F 80,wb+)",
test = fopen("TEMP FILE A F 80", "wb+"), test,);
for (i = 1; i < 100; i++) writeRecordBin(test, i, 80, "-");
rewind(test);
TST_STRT("random read binary (all cache hits)");
rc = 0;
for (i = 0; i < 500; i++) {
r = (rand() % 99) + 1;
rc = fseek(test, (r - 1) * 80, SEEK_SET);
if (rc) {
TST_ERR("fseek error");
break;
}
rc = readRecordBin(test, r, 80, "-");
if (rc) {
TST_ERR("mismatch");
break;
}
}
if (!rc) TST_OK();
ASSERTZEROP("fclose()", , fclose(test),);
ASSERTNOTNULLP("fopen(TEMP FILE A F 80,wb+)",
test = fopen("TEMP FILE A F 80", "wb+"), test,);
for (i = 1; i < 100; i++) writeRecordBin(test, i, 80, "-");
ASSERTZEROP("fclose()", , fclose(test),);
ASSERTNOTNULLP("fopen(TEMP FILE A F 80,rb)",
test = fopen("TEMP FILE A F 80", "rb"), test,);
TST_STRT("random read binary (many cache misses)");
rc = 0;
for (i = 0; i < 500; i++) {
r = (rand() % 99) + 1;
rc = fseek(test, (r - 1) * 80, SEEK_SET);
if (rc) {
TST_ERR("fseek error");
break;
}
rc = readRecordBin(test, r, 80, "-");
if (rc) {
TST_ERR("mismatch");
break;
}
}
if (!rc) TST_OK();
ASSERTZEROP("fclose()", , fclose(test),);
ASSERTNOTNULLP("fopen(TEMP FILE A F 80,wb+)",
test = fopen("TEMP FILE A F 80", "wb+"), test,);
for (i = 1; i < 100; i++) writeRecordBin(test, i, 80, "-");
rewind(test);
TST_STRT("random read/write/read binary (all cache hits)");
rc = 0;
for (i = 0; i < 500; i++) {
r = (rand() % 99) + 1;
rc = fseek(test, (r - 1) * 80, SEEK_SET);
if (rc) {
TST_ERR("fseek error");
break;
}