forked from adesutherland/CMS-370-GCCLIB
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcmsio.c
More file actions
1783 lines (1576 loc) · 76.8 KB
/
cmsio.c
File metadata and controls
1783 lines (1576 loc) · 76.8 KB
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
/**************************************************************************************************/
/* CMSIO.C - CMS Low Level IO */
/* */
/* Part of GCCLIB - VM/370 CMS Native Std C Library; A Historic Computing Toy */
/* */
/* Contributors: See contrib memo */
/* */
/* Released to the public domain. */
/**************************************************************************************************/
/*
TODO
tmpname - Static Name and counter to CRAB. Check already opened
*/
#include <cmsruntm.h>
#include <cmssys.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <float.h>
#include <time.h>
#include <errno.h>
/* Define Drivers */
extern CMSDRIVER dskiodrv;
extern CMSDRIVER prtiodrv;
extern CMSDRIVER puniodrv;
extern CMSDRIVER rdriodrv;
extern CMSDRIVER coniodrv;
static CMSDRIVERS __iodrivers[] =
{
{"File", &dskiodrv},
{"Printer", &prtiodrv},
{"Punch", &puniodrv},
{"Reader", &rdriodrv},
{"Console", &coniodrv},
{0, NULL} /* End of drivers */
};
/* FILE checker */
#define badfile(f) ( (f)==NULL || (f)->device==NULL || (f)->validator1!='F' || (f)->validator2!='@' )
/* Add file to file list */
static void add_file(FILE *file) {
FILE *tail = GETGCCCRAB()->filehandles;
if (tail) {
while (tail->next) tail = tail->next;
tail->next = file;
file->next = 0;
file->prev = tail;
} else { /* First file! */
GETGCCCRAB()->filehandles = file;
file->next = 0;
file->prev = 0;
}
}
/* Remove file from file list */
static void remove_file(FILE *file) {
FILE *item = GETGCCCRAB()->filehandles;
while (item) {
if (item == file) {
if (item->prev) item->prev->next = item->next;
else
GETGCCCRAB()->filehandles = item->next;
if (item->next) item->next->prev = item->prev;
file->next = 0;
file->prev = 0;
break;
}
item = item->next;
}
}
/* Close all files - for exit routine */
void _clfiles() {
FILE *item = GETGCCCRAB()->filehandles;
while (item) {
fclose(item);
item = GETGCCCRAB()->filehandles;
}
}
/* See if a file is open */
int _isopen(char *fileid) {
FILE *item = GETGCCCRAB()->filehandles;
while (item) {
if (strcmp(item->fileid, fileid) == 0) {
return 1;
}
item = item->next;
}
return 0;
}
/********************************************************************/
/* Init cache */
/* Returns - number of entries supported by the cache */
/* 0 too small (<10 records). The cache was not created */
/* -1 on system failure */
/********************************************************************/
static int init_cache(FILE *file, size_t cache_size, void *buffer) {
CMSFILECACHE *cache;
CMSCACHEENTRY *entry;
CMSCACHEENTRY *lastentry;
int i = 0;
/* Need to work out how many entries I can store */
int entrysize = file->filemaxreclen + sizeof(CMSCACHEENTRY);
int noentries = (cache_size - sizeof(CMSFILECACHE)) / entrysize;
if (noentries < 5) return 0; /* Not enouth space to make it worthwhile! */
if (buffer) {
memset(buffer, 0, cache_size);
cache = buffer;
cache->provided_cache_size = cache_size;
} else {
/* Round down the cache size */
cache_size = (noentries * entrysize) + sizeof(CMSFILECACHE);
cache = (CMSFILECACHE *) calloc(1, cache_size); /* Zeros the memory */
if (!cache) return -1;
cache->provided_cache_size = 0;
}
cache->noentries = noentries;
cache->entrysize = entrysize;
/* Setup Entry lists */
/* First Entry */
entry = (CMSCACHEENTRY *) ((void *) cache + sizeof(CMSFILECACHE));
cache->oldestused = entry;
/* loop through the next enries */
for (i = 1; i <
noentries; i++) { /* i is 1 because the first entry has been done */
lastentry = entry;
entry = (CMSCACHEENTRY *) ((void *) lastentry + entrysize);
lastentry->nextlastused = entry;
entry->prevlastused = lastentry;
}
cache->newestused = entry;
file->cache = cache;
return noentries;
}
/********************************************************************/
/* Free Cache */
/********************************************************************/
static void free_cache(FILE *file) {
if (file->cache) {
if (!file->cache->provided_cache_size) free(file->cache);
file->cache = 0;
}
}
/********************************************************************/
/* Put the record at the top of the cache chains */
/********************************************************************/
static void move_top_of_lists(CMSFILECACHE *cache, CMSCACHEENTRY *e) {
int bucket;
/* Bucket List */
if (e->previnbucket) { /* If no previnbucket I'm at the begining already */
bucket = e->recnum % FCACHEBUCKETS;
/* Remove me from the list */
e->previnbucket->nextinbucket = e->nextinbucket;
if (e->nextinbucket) e->nextinbucket->previnbucket = e->previnbucket;
/* Put me at the top of the list */
e->nextinbucket = cache->bucket[bucket];
e->previnbucket = 0;
cache->bucket[bucket] = e;
}
/* Last Used List */
if (e->nextlastused) { /* If no nextlastused I'm at the top already */
/* Remove me from the list */
e->nextlastused->prevlastused = e->prevlastused;
if (e->prevlastused) e->prevlastused->nextlastused = e->nextlastused;
else {
/* I was at the tail so need to update it */
cache->oldestused = e->nextlastused;
e->nextlastused->prevlastused = 0;
}
/* Put me at the top of the list */
e->prevlastused = cache->newestused;
e->nextlastused = 0;
cache->newestused = e;
}
}
/********************************************************************/
/* Get record from cache uses FILE.recnum to get required record */
/* Return 0 in success (hit) */
/* 1 on miss */
/* -1 on system error */
/********************************************************************/
static int read_cache(FILE *file) {
CMSCACHEENTRY *topEntity;
CMSCACHEENTRY *e;
CMSFILECACHE *cache = file->cache;
char *record;
int bucket;
if (!cache) return -1;
bucket = file->recnum % FCACHEBUCKETS;
topEntity = cache->bucket[bucket];
for (e = topEntity; e; e = e->nextinbucket) {
if (e->recnum == file->recnum) {
/* Cache Hit */
cache->hits++;
move_top_of_lists(cache, e);
record = (char *) ((void *) e + sizeof(CMSCACHEENTRY));
memcpy(file->buffer, record, e->reclen);
file->reclen = e->reclen;
file->maxreclen = e->maxreclen;
file->buffer[file->reclen] = 0;
return 0;
}
}
cache->misses++;
return 1;
}
/********************************************************************/
/* Write record to cache uses FILE.recnum to get record number */
/* Return 0 in success, otherwise -1 on failure */
/********************************************************************/
static int write_cache(FILE *file) {
CMSCACHEENTRY *topEntity;
CMSCACHEENTRY *e;
CMSFILECACHE *cache = file->cache;
char *record;
int bucket;
if (!cache) return -1;
bucket = file->recnum % FCACHEBUCKETS;
topEntity = cache->bucket[bucket];
for (e = topEntity; e; e = e->nextinbucket) {
if (e->recnum == file->recnum) {
/* Cache Hit */
move_top_of_lists(cache, e);
record = (char *) ((void *) e + sizeof(CMSCACHEENTRY));
memcpy(record, file->buffer, file->reclen);
e->reclen = file->reclen;
e->maxreclen = file->maxreclen;
return 0;
}
}
/* Ok we need to get a new record from the cache */
e = cache->oldestused; /* Oldest Used */
if (e->recnum) { /* Used before, need to remove it from its bucket */
if (e->previnbucket) e->previnbucket->nextinbucket = e->nextinbucket;
else cache->bucket[e->recnum % FCACHEBUCKETS] = e->nextinbucket;
if (e->nextinbucket) e->nextinbucket->previnbucket = e->previnbucket;
}
e->recnum = file->recnum;
/* Add to bucket */
e->nextinbucket = cache->bucket[bucket];
e->previnbucket = 0;
cache->bucket[bucket] = e;
move_top_of_lists(cache, e);
record = (char *) ((void *) e + sizeof(CMSCACHEENTRY));
memcpy(record, file->buffer, file->reclen);
e->reclen = file->reclen;
e->maxreclen = file->maxreclen;
return 0;
}
/********************************************************************/
/* Parse access mode string */
/* returns the access mode or -1 on error */
/********************************************************************/
static int parse_access_mode(const char *accessmode) {
char has_p = 0; /* i.e. a '+' */
char has_b = 0;
int mode = 0;
switch (accessmode[0]) {
case 'r':
mode |= ACCESS_READ;
break;
case 'w':
mode |= ACCESS_WRITE;
mode |= ACCESS_TRUNCATE;
break;
case 'a':
mode |= ACCESS_WRITE;
mode |= ACCESS_APPEND;
break;
default:
return -1;
}
if (accessmode[1]) {
switch (accessmode[1]) {
case '+':
has_p = 1;
break;
case 'b':
has_b = 1;
break;
default:
return -1;
break;
}
if (accessmode[2]) {
switch (accessmode[2]) {
case '+':
if (has_p) return -1;
has_p = 1;
break;
case 'b':
if (has_b) return -1;
has_b = 1;
break;
default:
return -1;
break;
}
if (accessmode[3]) return -1;
}
}
if (!has_b) mode |= ACCESS_TEXT;
if (has_p) {
mode |= ACCESS_READ;
mode |= ACCESS_WRITE;
mode |= ACCESS_READWRITE;
}
return mode;
}
/**************************************************************************************************/
/* Worker funtion to open stream */
/* Return 0 - All OK, Otherwise 1 (error) */
/**************************************************************************************************/
static int do_open(const char *filespec, const char *access, FILE *theFile) {
int w = 0;
int c = 0;
int p = 0;
char fsword[6][10] = {"", "", "", "", "", ""};
int accessMode;
int d;
int rc;
/* Get the first 6 words from the filespec, for each word the first 9 charcters */
/* This allows the drivers to detect filenames with more than 8 characters and */
/* too many words/options */
for (w = 0; filespec[c] && w < 6; w++) {
/* Leading Spaces */
for (; filespec[c] && filespec[c] == ' '; c++);
/* Word */
for (p = 0; filespec[c] && p < 9 && filespec[c] != ' '; c++, p++)
fsword[w][p] = toupper(filespec[c]);
fsword[w][p] = 0;
/* Word Overspill */
for (; filespec[c] && filespec[c] != ' '; c++);
}
/* Get the access mode argument. */
accessMode = parse_access_mode(access);
if (accessMode < 0) {
errno = EINVAL;
return 1;
}
/* Setup/clean FILE* */
memset(theFile, 0, sizeof(FILE));
theFile->access = accessMode;
theFile->ungetchar = -1;
/* Ask the drivers to open the file in-turn */
for (d = 0; __iodrivers[d].name[0]; d++) {
rc = __iodrivers[d].driver->open_func(fsword, theFile);
if (!rc) return 1;
else if (rc >
0) { /* -1 means the request is not for this driver, try the next one */
theFile->device = __iodrivers[d].driver;
theFile->validator1 = 'F';
theFile->validator2 = '@';
add_file(theFile);
if (theFile->status & STATUS_CACHE) {
size_t cache_size = theFile->filemaxreclen *
100; /* A cache for about 100 records */
if (cache_size > 64 * 1024)
cache_size = 64 * 1024; /* 64k max */
if (cache_size < 8 * 1024) cache_size = 8 * 1024; /* 8k min */
init_cache(theFile, cache_size, NULL);
}
return 0;
}
}
/* No driver accepted the request */
errno = EINVAL;
return 1;
}
FILE *
fopen(const char *filespec, const char *access)
/**************************************************************************************************/
/* 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. */
/**************************************************************************************************/
{
FILE *theFile;
int rc;
theFile = (FILE *) malloc(sizeof(FILE));
rc = do_open(filespec, access, theFile);
if (rc) {
free_cache(theFile);
theFile->validator1 = 0;
theFile->validator2 = 0;
free(theFile);
return NULL;
}
return theFile;
}
/**************************************************************************************************/
/* int fclose(FILE * file) */
/* */
/* Close the open file in an appropriate and orderly fashion. */
/* stream is a pointer to the open output stream. */
/* */
/* Returns: */
/* 0 if all is well, EOF if there is an error. */
/**************************************************************************************************/
int fclose(FILE *file) {
int rc;
if (badfile(file)) {
errno = EINVAL;
return EOF;
}
if (fflush(file) != 0)
return EOF; /* error flushing the stream */
rc = file->device->close_func(file);
free_cache(file);
remove_file(file);
file->device == NULL;
file->validator1 = 0;
file->validator2 = 0;
free(file);
return rc;
} /* end of fclose */
/**************************************************************************************************/
/* write block/record to disk */
/* Returns: */
/* 0, indicating that all is well, or a return code from the write operation. */
/**************************************************************************************************/
static int record_write(FILE *file) {
int rc;
if (!(file->status & STATUS_DIRTY))
return 0; /* nothing to do if no unwritten data */
rc = file->device->write_func(file);
if (!rc) {
file->status &= 0xFFFF - STATUS_DIRTY;
if (file->cache) {
write_cache(file);
}
}
return rc;
}
int
fflush(FILE *file)
/**************************************************************************************************/
/* 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. */
/**************************************************************************************************/
{
if (badfile(file)) {
errno = EINVAL;
return EOF;
}
if (!(file->access & ACCESS_WRITE))
return 0; /* nothing to do if not open for writing */
return record_write(file);
}
/**************************************************************************************************/
/* read block/record from disk */
/* Returns: */
/* 0, indicating that all is well, or a return code from the read operation */
/**************************************************************************************************/
static int record_read(FILE *file) {
int rc = 1;
int num;
if (file->cache) rc = read_cache(file);
if (rc) {
rc = file->device->read_func(file);
if (!rc && file->cache) write_cache(file);
}
num = file->reclen;
if (!rc && (file->access & ACCESS_TEXT)) {
/* We trim trailing spaces and add a newline - makes fixed format records easier to handle */
for (num--; num > -1 && file->buffer[num] == ' '; num--);
file->buffer[++num] = '\n';
file->reclen = num++; /* reclen does not include \n */
}
file->buffer[num] = 0;
/* Post Read Processing */
if (file->device->postread_func) file->device->postread_func(file);
return rc;
}
/**************************************************************************************************/
/* 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 fgetpos(FILE *file, fpos_t *position) {
if (badfile(file)) {
errno = EINVAL;
return 1;
}
if (file->recnum == -1) {
errno = ENOTBLK;
return 1;
}
position->file = file;
position->recpos = file->recpos;
position->recnum = file->recnum;
return 0;
}
/**************************************************************************************************/
/* 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. */
/**************************************************************************************************/
int fsetpos(FILE *file, const fpos_t *position) {
int rc;
if (badfile(file)) {
errno = EINVAL;
return 1;
}
if (position == NULL) {
errno = EINVAL;
return 1;
}
if (position->file != file) {
errno = EINVAL;
return 1;
}
if (position->recnum < 1) {
errno = EINVAL;
return 1;
}
if (position->recnum > file->records) {
errno = EINVAL;
return 1;
}
file->ungetchar = -1;
file->status &= 0xFFFF - STATUS_EOF;
fflush(file);
file->recnum = position->recnum;
rc = record_read(file);
file->recpos = position->recpos;
return rc;
}
/**************************************************************************************************/
/* void append(FILE * stream) */
/* */
/* Non-standard GCC CMS extention */
/* Move the file position indicator to the end of the specified stream, and clears the */
/* error and EOF flags associated with that stream. This is the oposite of rewind() */
/* stream a pointer to the open stream. */
/**************************************************************************************************/
void append(FILE *file) {
if (badfile(file)) return;
if (file->recnum != -1) {
fflush(file);
file->recnum = file->records + 1;
file->reclen = -1; /* Empty record */
file->recpos = 0;
} else file->recpos = file->reclen;
file->ungetchar = -1;
file->status &= 0xFFFF - STATUS_EOF;
file->error = 0;
return;
}
/**************************************************************************************************/
/* int fateof(FILE * stream) */
/* Non-standard GCC CMS extention */
/* Detects if at EOF - unlike feof() it predicts if the next read will cause an EOF */
/* */
/* Returns 1 if at EOF, EOF (-1) on error, or 0 is not at EOF */
/* */
/**************************************************************************************************/
int fateof(FILE *stream) {
int lrecl;
if (badfile(stream)) {
errno = EINVAL;
return EOF;
}
if (stream->status & STATUS_EOF) return 1;
if (stream->records == -1)
return 0; /* Not a block device so can't detect if at EOF */
if (stream->recnum != stream->records) return 0; /* not last record */
lrecl = stream->reclen;
if ((stream->access & ACCESS_TEXT) && lrecl) lrecl++;
if (stream->recpos >= lrecl) return 1;
else return 0;
}
/**************************************************************************************************/
/* FILE* fgethandle(char *fileName) */
/* Non-standard GCC CMS extention */
/* */
/* Finds an open file that matches fileName */
/* It does not return stdin, stdout or stderr if assigned to the CONSOLE */
/* */
/* Return the FILE handle or NULL if the file is not opened */
/* */
/**************************************************************************************************/
FILE *fgethandle(char *fileName) {
int w = 0;
int c = 0;
int p = 0;
char fsword[3][10] = {"", "", ""};
FILE *item;
char buffer[19];
/* Get the first 3 words from the filespec, for each word the first 9 charcters */
/* This allows us to detect filenames with more than 8 characters */
/* This function anly considers the first 3 words ignoring any recfm or lrecl stuff */
for (w = 0; fileName[c] && w < 3; w++) {
/* Leading Spaces */
for (; fileName[c] && fileName[c] == ' '; c++);
/* Word */
for (p = 0; fileName[c] && p < 9 && fileName[c] != ' '; c++, p++)
fsword[w][p] = toupper(fileName[c]);
fsword[w][p] = 0;
/* Word Overspill */
for (; fileName[c] && fileName[c] != ' '; c++);
}
/* Basic Validation */
if (strlen(fsword[0]) > 8) return NULL;
if (strlen(fsword[0]) == 0) return NULL;
if (strlen(fsword[1]) > 8) return NULL;
if (strlen(fsword[2]) > 2) return NULL;
item = GETGCCCRAB()->filehandles;
if (strlen(fsword[1]) == 0) {
/* Special files like PUNCH */
while (item) {
if (strcmp(item->fileid, fsword[0]) == 0) {
return item;
}
item = item->next;
}
} else if (strcmp(fsword[2], "*") == 0) {
/* "*" for a disk */
sprintf(buffer, "%-8s%-8s", fsword[0], fsword[1]);
while (item) {
if (strncmp(item->fileid, buffer, 16) == 0) {
return item;
}
item = item->next;
}
} else {
/* File */
if (strlen(fsword[2]) == 0) {
/* Drive A is the default */
fsword[2][0] = 'A';
fsword[2][1] = 0;
} else fsword[2][1] = 0; /* Ignore mode number */
sprintf(buffer, "%-8s%-8s%-1s", fsword[0], fsword[1], fsword[2]);
while (item) {
if (strncmp(item->fileid, buffer, 17) == 0) {
return item;
}
item = item->next;
}
}
return 0;
}
/**************************************************************************************************/
/* int fgetrecs(FILE * stream) */
/* Non-standard GCC CMS extention */
/* */
/* Gets the number of records in a file */
/* */
/* Returns the number of records or EOF on error */
/* */
/**************************************************************************************************/
int fgetrecs(FILE *stream) {
if (badfile(stream)) {
errno = EINVAL;
return EOF;
}
return stream->records;
}
/**************************************************************************************************/
/* int fgetlen(FILE * stream) */
/* */
/* Non-standard GCC CMS extention */
/* Gets the number of bytes/characters in a file */
/* Only works of fixed record length files */
/* */
/* Returns the number of bytes/characters or EOF on error */
/* */
/**************************************************************************************************/
int fgetlen(FILE *stream) {
int len;
if (badfile(stream)) {
errno = EINVAL;
return EOF;
}
fflush(stream); /* TODO Need to consider if this should be done */
len = stream->device->getend_func(stream);
return len;
}
/**************************************************************************************************/
/* int fcachehits(FILE * stream) */
/* */
/* The function returns the precentage of cache read hits achieved */
/* Non-standard GCC CMS extention */
/* */
/* Returns: */
/* Percentage of cache hits or -1 on error (device without a cache) */
/**************************************************************************************************/
int fcachehits(FILE *file) {
CMSFILECACHE *cache;
if (badfile(file)) return -1;
cache = file->cache;
if (!cache) return -1;
if (cache->hits + cache->misses == 0) return 0;
return (cache->hits * 100) / (cache->hits + cache->misses);
}
/**************************************************************************************************/
/* 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 fgetrec(FILE *file) {
fpos_t position;
if (fgetpos(file, &position)) return 0;
return position.recnum;
}
/**************************************************************************************************/
/* 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 */
/**************************************************************************************************/
int fsetrec(FILE *file, const int recnum) {
fpos_t position;
if (recnum > file->records + 1) {
errno = EINVAL;
return 0;
}
if (recnum < 1) {
errno = EINVAL;
return 0;
}
if (recnum > file->records) {
append(file);
return 0;
}
position.file = file;
position.recnum = recnum;
position.recpos = 0;
if (fsetpos(file, &position)) return 0;
return position.recnum;
}
void
clearerr(FILE *stream)
/**************************************************************************************************/
/* void clearerr(FILE * stream) */
/* */
/* Resets the error status of the specified stream to 0. */
/* stream a pointer to the open input stream. */
/**************************************************************************************************/
{
if (badfile(stream)) errno = EINVAL;
else {
stream->error = 0;
stream->status &= 0xFFFF - STATUS_EOF;
}
}
int
feof(FILE *stream)
/**************************************************************************************************/
/* 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. */
/**************************************************************************************************/
{
if (badfile(stream)) {
errno = EINVAL;
return EOF;
}
return (stream->status & STATUS_EOF) ? 1 : 0;
}
int
ferror(FILE *stream)
/**************************************************************************************************/
/* 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. */
/**************************************************************************************************/
{
if (badfile(stream)) {
errno = EINVAL;
return EOF;
}
return stream->error;
}
int
fgetc(FILE *file)
/**************************************************************************************************/
/* 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. */
/**************************************************************************************************/
{
int c;
int lrecl;
if (badfile(file)) {
errno = EINVAL;
return EOF;
}
if (!(file->access & ACCESS_READ)) {
file->error = 9;
errno = EBADF;
return EOF;
}
if (file->ungetchar >=
0) { /* was a character pushed back onto the file? */
c = file->ungetchar;
file->ungetchar = -1;
return c;
}
if (file->access & ACCESS_TEXT) lrecl = file->reclen + 1;
else lrecl = file->reclen;
if ((file->recnum == 0) || (file->recpos >= lrecl)) {
if (file->recnum != -1) file->recnum++;
if (record_read(file) == EOF) return EOF;
file->recpos = 0;
}
c = file->buffer[file->recpos++];
return c;
}
/**************************************************************************************************/
/* int nextreclen(FILE * file) */