-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathfileio.h
829 lines (682 loc) · 14.8 KB
/
fileio.h
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
/**
* @file fileio.h
* @brief File I/O.
* @author Miguel I. Garcia Lopez / FloppySoftware
*
* File I/O for MESCC (Mike's Enhanced
* Small C Compiler for Z80 & CP/M).
*
* Support standard macros:
* - FILE
* - EOF
* - FILENAME_MAX
*
* Supports following macros:
* - #define CC_STDIO To support stdin, stdout & stderr.
* - #define CC_FCX To support FCX (user number in file names, see cpm.h).
* - #define CC_FCX_DIR To support named directories in file names (see cpm.h).
* - #define CC_FILEIO_SMALL To exclude fread(), fwrite() and fgets(). ===DEPRECATED===
* - #define CC_FOPEN_A To include "a" and "ab" modes for fopen().
* - #define CC_FREAD To include fread().
* - #define CC_FWRITE To include fwrite().
* - #define CC_FGETS To include fgets().
* - #define CC_FPUTS To include fputs().
* - #define CC_FSIZE To include fsize().
*
* Revisions:
* - 19 Mar 2001 : Last revision.
* - 17 Apr 2004 : Added functions: fread, fwrite, remove, rename.
* - 16 Apr 2007 : GPL'd.
* - 20 Apr 2007 : Quit "rt" and "wt" modes. Now "r" and "w" are text modes.
* - 10 Nov 2013 : Solved bug related to DMA in fopen, rename, remove.
* - 19 Nov 2013 : Reworked for UX.
* - 11 Dec 2013 : Added function: fgets.
* - 08 Dec 2014 : Added stdin, stdout, stderr support if CC_STDIO is defined.
* - 09 Dec 2014 : Added support to FCX if CC_FCX defined.
* - 18 Dec 2014 : Modified #define EOF (-1) to -1.
* - 16 Jan 2015 : Solved related memory bug in rename.
* - 16 Jan 2015 : Added FILENAME_MAX ANSI C definition.
* - 17 Feb 2015 : Solved bug in rename.
* - 07 Mar 2015 : Modified FILENAME_MAX value (now includes ZERO according to ANSI).
* - 09 Apr 2015 : Test ambiguous file names in fopen().
* - 03 May 2015 : Solved bug in fgets - last character was lost in long lines.
* - 17 Nov 2015 : Added FILENAME_MAX value when CC_FCX_DIR is defined.
* - 24 Dec 2015 : Added CC_FILEIO_SMALL define to exclude fread(), fwrite() and fgets().
* - 04 Jan 2016 : Removed some code from fread() and fwrite().
* - 08 Jan 2016 : Include mem.h library.
* - 19 Jul 2016 : Added "a" and "ab" modes.
* Added support for CC_FOPEN_A, CC_FREAD, CC_FWRITE and CC_FGETS defines.
* Removed CC_FILEIO_SMALL define.
* - 23 Jul 2016 : Added fputs() and support for CC_FPUTS define.
* - 10 Dec 2016 : Documented. Optimized. GPL v3.
* - 15 Dec 2016 : Optimize NULL comparisons, fgetc(), fputc().
* - 18 Feb 2018 : Document public macros. Rename internal macros and include them in cleaning. Rework remove() and rename(). Added FOPEN_MAX.
* - 03 May 2018 : Make CC_FPUTS effective (use #ifdef instead of #if).
* - 27 Dec 2018 : Added fsize().
* - 13 Sep 2021 : Bugfix in fopen "a" text mode when there are no 0x1A bytes in the last record.
* - 16 Sep 2021 : Bugfix in fsize() regarding CP/M v2 / v3 compatibility.
*
* Copyright (c) 1999-2021 Miguel I. Garcia Lopez / FloppySoftware.
*
* Licensed under the GNU General Public License v3.
*
* http://www.floppysoftware.es
*/
#ifndef FILEIO_H
#define FILEIO_H
/* Dependencies
------------
*/
#ifndef CPM_H
#include <cpm.h>
#endif
#ifndef ALLOC_H
#include <alloc.h>
#endif
#ifndef MEM_H
#include <mem.h>
#endif
#ifdef CC_STDIO
#ifndef CONIO_H
#include <conio.h>
#endif
#endif
/* Public defines
--------------
*/
#define FILE unsigned char
#define EOF -1
#define FOPEN_MAX 99
#ifdef CC_FCX
#ifdef CC_FCX_DIR
#define FILENAME_MAX 22 /* dusrname:filename.typ + ZERO */
#else
#define FILENAME_MAX 17 /* duu:filename.typ + ZERO */
#endif
#else
#define FILENAME_MAX 15 /* d:filename.typ + ZERO */
#endif
/* Private defines
---------------
*/
#define _XF_READ 1 /* Read mode */
#define _XF_WRITE 2 /* Write mode */
#define _XF_BIN 4 /* Binary mode */
#define _XF_EOF 8 /* End of file */
#define _XF_ERR 16 /* I/O error */
#define _XF_IMOD 0 /* Mode (1 byte) */
#define _XF_IPOS 1 /* Position in buffer (1 byte) */
#define _XF_IBUF 2 /* Buffer (128 bytes) */
#define _XF_IFCX 130 /* FCX (37 bytes: USER (1) + FCB (36)) */
#ifdef CC_FCX
#define _XF_ISIZ 167 /* Data block size */
#define _XF_IRND 164 /* Random record # in FCX */
#define _FILEOP bdos_fcx_a
#define _MAKEFCB setfcx
#define _FCB_SIZE 37
#else
#define _XF_ISIZ 166 /* Data block size */
#define _XF_IRND 163 /* Random record # in FCB */
#define _FILEOP bdos_a
#define _MAKEFCB setfcb
#define _FCB_SIZE 36
#endif
/**
* @fn FILE *fopen(char *fname, char *fmode)
* @brief Open file.
*
* Valid 'fmode' values are:
* - "rb" : Binary reading.
* - "r" : Text reading.
* - "wb" : Binary writing.
* - "w" : Text writing.
* - "a" : Append text writing.
* - "ab" : Append binary writing.
*
* In text mode, the following translations are performed:
* - Reading: '\r' is ignored, '\n' is end of line.
* - Writing: '\n' is converted to '\r' + '\n'.
*
* @param fname - filename
* @param fmode - file access mode
* @return Pointer to FILE on success, else NULL.
*/
fopen(fname,fmode)
char *fname, *fmode;
{
int mode;
FILE *fp;
#ifdef CC_FOPEN_A
int i;
unsigned int *wp;
#endif
// Mode
if(*fmode == 'r')
mode = _XF_READ;
else if(*fmode=='w')
mode = _XF_WRITE;
#ifdef CC_FOPEN_A
else if(*fmode == 'a')
mode = _XF_WRITE;
#endif
else
return NULL;
if(*(fmode + 1) == 'b')
mode |= _XF_BIN;
else if(*(fmode + 1))
return NULL;
// Filename can't be ambiguous
if(xfnamb(fname))
return NULL;
// Get memory
if(!(fp = malloc(_XF_ISIZ)))
return NULL;
// Make FCB
if(_MAKEFCB(fname, fp + _XF_IFCX))
{
free(fp);
return NULL;
}
// Open file
bdos_hl(BF_DMA, fp+_XF_IBUF);
if(mode & _XF_READ)
{
if(_FILEOP(BF_OPEN, fp + _XF_IFCX) == 255)
{
free(fp);
return NULL;
}
fp[_XF_IPOS] = 128; // No data in buffer
}
#ifdef CC_FOPEN_A
else if(*fmode == 'a')
{
fp[_XF_IPOS] = 0; // No data in buffer
if(_FILEOP(BF_OPEN, fp + _XF_IFCX) != 255)
{
_FILEOP(BF_FSIZE, fp + _XF_IFCX);
wp = fp + _XF_IRND;
if(*(fmode + 1) != 'b' && *wp)
{
--(*wp);
_FILEOP(BF_READRND, fp + _XF_IFCX);
for(i = 0; i < 128; ++i)
{
if(*(fp + _XF_IBUF + i) == 0x1A)
{
fp[_XF_IPOS] = i;
break;
}
}
if(i == 128)
++(*wp);
}
}
else if(_FILEOP(BF_CREATE, fp + _XF_IFCX) == 255)
{
free(fp);
return NULL;
}
}
#endif
else
{
if(_FILEOP(BF_FIND1ST, fp + _XF_IFCX) != 255)
{
if(_FILEOP(BF_DELETE, fp + _XF_IFCX) == 255)
{
free(fp);
return NULL;
}
}
if(_FILEOP(BF_CREATE, fp + _XF_IFCX) == 255)
{
free(fp);
return NULL;
}
fp[_XF_IPOS] = 0; // No data in buffer
}
// Set file mode
fp[_XF_IMOD] = mode;
// Return pointer to FILE
return fp;
}
/**
* @fn int fgetc(FILE *fp)
* @brief Read character from file.
* @param fp - pointer to FILE
* @return character on success, else EOF on end of file or error
*/
fgetc(fp)
FILE *fp;
{
int c;
#ifdef CC_STDIO
// Console?
if(!fp)
{
if((c = getch()) == '\r')
c = '\n';
else if (c == 0x1A)
return EOF;
if(c == '\n')
putch('\r');
return putch(c);
}
#endif
// File opened for reading and without errors?
if(!(fp[_XF_IMOD] & _XF_READ) || fp[_XF_IMOD] & (_XF_EOF + _XF_ERR))
return EOF;
// Read binary
if((fp[_XF_IMOD] & _XF_BIN))
return xfgetc(fp);
// Read text
while((c = xfgetc(fp)) == '\r')
;
if(c == 0x1A)
{
fp[_XF_IMOD] |= _XF_EOF;
c = EOF;
}
// Return character
return c;
}
// int xfgetc(FILE *fp) : Helper for fgetc() - return next character, or EOF on end of file or error.
xfgetc(fp)
FILE *fp;
{
#ifdef CC_FOPEN_A
unsigned int *wp;
#endif
// Read next record if needed
if(fp[_XF_IPOS] == 128)
{
bdos_hl(BF_DMA, fp+_XF_IBUF);
#ifdef CC_FOPEN_A
if(_FILEOP(BF_READRND, fp + _XF_IFCX))
#else
if(_FILEOP(BF_READSEQ, fp + _XF_IFCX))
#endif
{
fp[_XF_IMOD] |= _XF_EOF;
return EOF;
}
#ifdef CC_FOPEN_A
wp = fp + _XF_IRND; ++(*wp);
#endif
fp[_XF_IPOS] = 0;
}
// Get character from buffer and increment pointer
return *(fp + _XF_IBUF + fp[_XF_IPOS]++);
}
/**
* @fn int fputc(int c, FILE *fp)
* @brief Write character to file.
* @param c - character
* @param fp - pointer to FILE
* @return c on success, else EOF
*/
fputc(c,fp)
int c;
FILE *fp;
{
#ifdef CC_STDIO
// Console?
if(!fp)
{
if(c == '\n')
putch('\r');
return putch(c);
}
#endif
// File opened for writing and without errors?
if(!(fp[_XF_IMOD] & _XF_WRITE) || fp[_XF_IMOD] & (_XF_EOF + _XF_ERR))
return EOF;
// Write binary
if((fp[_XF_IMOD] & _XF_BIN))
return xfputc(c, fp);
// Write text
if(c == '\n')
{
if(xfputc('\r', fp) == EOF)
return EOF;
}
return xfputc(c, fp);
}
// int xfputc(int c, FILE *fp) : Helper for fputc() - return character, or EOF on error.
xfputc(c,fp)
int c;
FILE *fp;
{
#ifdef CC_FOPEN_A
unsigned int *wp;
#endif
// Store character in buffer and increment pointer
*(fp + _XF_IBUF + fp[_XF_IPOS]++) = c;
// Write record if needed
if(fp[_XF_IPOS] == 128)
{
bdos_hl(BF_DMA, fp + _XF_IBUF);
#ifdef CC_FOPEN_A
if(_FILEOP(BF_WRITERND, fp + _XF_IFCX))
#else
if(_FILEOP(BF_WRITESEQ, fp + _XF_IFCX))
#endif
{
fp[_XF_IMOD] |= _XF_ERR;
return EOF;
}
#ifdef CC_FOPEN_A
wp = fp + _XF_IRND; ++(*wp);
#endif
fp[_XF_IPOS] = 0;
}
// Return character
return c;
}
/**
* @fn int fclose(FILE *fp)
* @brief Close file.
* @param fp - pointer to FILE
* @return 0 on success, else EOF
*/
fclose(fp)
FILE *fp;
{
#ifdef CC_STDIO
// Console?
if(!fp)
return 0;
#endif
// Writing mode?
if(fp[_XF_IMOD] & _XF_WRITE)
{
while(fp[_XF_IPOS])
{
if(xfputc(0x1A, fp) == EOF)
return EOF;
}
}
// Close file
bdos_hl(BF_DMA, fp + _XF_IBUF);
if(_FILEOP(BF_CLOSE, fp + _XF_IFCX) == 255)
return EOF;
// Free buffer memory
free(fp);
// Success
return 0;
}
/**
* @fn int feof(FILE *fp)
* @brief Test end of file.
* @param fp - pointer to FILE
* @return != 0 if true, else 0
*/
feof(fp)
FILE *fp;
{
#ifdef CC_STDIO
// Console?
if(!fp)
return 0;
#endif
return fp[_XF_IMOD] & _XF_EOF;
}
/**
* @fn int ferror(FILE *fp)
* @brief Test file error.
* @param fp - pointer to FILE
* @return != 0 if true, else 0
*/
ferror(fp)
FILE *fp;
{
#ifdef CC_STDIO
// Console?
if(!fp)
return 0;
#endif
return fp[_XF_IMOD] & _XF_ERR;
}
#ifdef CC_FREAD
/**
* @fn int fread(char *ptr, int size, int nobj, FILE *fp)
* @brief Read from file.
* @param ptr - destination buffer
* @param size - object size in bytes
* @param nobj - # of objects to read
* @param fp - pointer to FILE
* @return # of objects read
*/
fread(ptr, size, nobj, fp)
unsigned char *ptr;
int size, nobj;
FILE *fp;
{
int cobj, csize, c;
for(cobj = 0; cobj != nobj; ++cobj)
{
for(csize = 0; csize != size; ++csize)
{
if((c = fgetc(fp)) == EOF)
return cobj;
*ptr++ = c;
}
}
return cobj;
}
#endif
#ifdef CC_FWRITE
/**
* @fn int fwrite(char *ptr, int size, int nobj, FILE *fp)
* @brief Write to file.
* @param ptr - source buffer
* @param size - object size in bytes
* @param nobj - # of objects to write
* @param fp - pointer to FILE
* @return # of objects written
*/
fwrite(ptr, size, nobj, fp)
unsigned char *ptr;
int size, nobj;
FILE *fp;
{
int cobj, csize;
for(cobj = 0; cobj != nobj; ++cobj)
{
for(csize = 0; csize != size; ++csize)
{
if(fputc(*ptr++, fp) == EOF)
return cobj;
}
}
return cobj;
}
#endif
#ifdef CC_FGETS
/**
* @fn char *fgets(char *str, int size, FILE *fp)
* @brief Read string from file.
* @param str - destination buffer
* @param size - # of max. characters to read
* @param fp - pointer to FILE
* @return address of 'str' on success, else NULL
*/
fgets(str, size, fp)
char *str; int size; FILE *fp;
{
int c;
char *cs;
cs = str;
while(--size)
{
if((c = fgetc(fp)) == EOF)
break;
if((*cs++ = c) == '\n')
break;
}
if(c == EOF && cs == str)
return NULL;
*cs = '\0';
return str;
}
#endif
#ifdef CC_FPUTS
/**
* @fn int fputs(char *str, FILE *fp)
* @brief Write string to file.
* @param str - source buffer
* @param fp - pointer to FILE
* @return # of characters written on success, else EOF
*/
fputs(str, fp)
char *str; FILE *fp;
{
int i;
for(i = 0; *str; ++i) {
if(fputc(*str++, fp) == EOF)
return EOF;
}
return i;
}
#endif
/**
* @fn int remove(char *fname)
* @brief Delete a file.
* @param fname - filename
* @return 0 on success, else != 0
*/
remove(fname)
char *fname;
{
BYTE *fc;
int code;
code = 0xFF; /* Error by default */
if((fc = malloc(_FCB_SIZE)))
{
if(!_MAKEFCB(fname, fc))
{
bdos_hl(BF_DMA, 0x80);
code = _FILEOP(BF_DELETE, fc);
}
free(fc);
}
return code != 0xFF ? 0 : -1;
}
#ifdef CC_FCX
#define _REN_BUF_SIZE 54 /* 37 + 17 */
#define _REN_OFFSET 17
#else
#define _REN_BUF_SIZE 52 /* 36 + 16 */
#define _REN_OFFSET 16
#endif
/**
* @fn int rename(char *oldname, char *newname)
* @brief Rename a file.
* @param oldname - old filename
* @param newname - new filename
* @return 0 on success, else != 0
*/
rename(oldname, newname)
char *oldname, *newname;
{
BYTE *fcb;
int code;
code = 0xFF; /* Error by default */
if((fcb = malloc(_REN_BUF_SIZE)))
{
if(!_MAKEFCB(oldname, fcb))
{
if(!_MAKEFCB(newname, fcb + _REN_OFFSET))
{
/* FIXME : drive + user unchecked -- must match! */
#ifdef CC_FCX
memcpy(fcb + 17, fcb + 18, 16);
#endif
bdos_hl(BF_DMA, 0x0080);
code = _FILEOP(BF_RENAME, fcb);
}
}
free(fcb);
}
return code != 0xFF ? 0 : -1;
}
#undef _REN_BUF_SIZE
#undef _REN_OFFSET
#ifdef CC_FSIZE
#ifdef CC_FCX
#define _FSZ_RR 34
#else
#define _FSZ_RR 33
#endif
/**
* @fn int fsize(char *fname)
* @brief Get the file size in units of 128 bytes.
* @param fname - filename
* @return file size on success, else -1
*/
fsize(fname)
char *fname;
{
BYTE *fc;
int recs;
recs = -1; /* Error by default */
if((fc = malloc(_FCB_SIZE)))
{
if(!_MAKEFCB(fname, fc))
{
bdos_hl(BF_DMA, 0x80);
/*
BDOS fn. 35 - Compute file size:
- CP/M v2 returns nothing about success / error.
- CP/M v3 returns success / error codes in the A register.
So, for compatibility, first search the file and if it exists,
we assume we will get the file size.
*/
if(_FILEOP(BF_FIND1ST, fc) != 0xFF)
{
_FILEOP(BF_FSIZE, fc);
recs = fc[_FSZ_RR] + (fc[_FSZ_RR + 1] << 8);
}
}
free(fc);
}
return recs;
}
#undef _FSZ_RR
#endif
// int xfnamb(char *fn) : check if fn is an ambiguous filename -- return 1 if true, else 0.
xfnamb(fn)
char *fn;
{
#ifdef STRING_H
if(strchr(fn, '?') || strchr(fn, '*'))
return 1;
#else
while(*fn)
{
if(*fn == '?' || *fn == '*')
return 1;
++fn;
}
#endif
return 0;
}
// Cleaning
#undef _XF_READ
#undef _XF_WRITE
#undef _XF_BIN
#undef _XF_EOF
#undef _XF_ERR
#undef _XF_IMOD
#undef _XF_IPOS
#undef _XF_IBUF
#undef _XF_IFCX
#undef _XF_ISIZ
#undef _XF_IRND
#undef _FILEOP
#undef _MAKEFCB
#undef _FCB_SIZE
#endif