1 | FILE *fopen(name,mode) |
---|
2 | char *name,*mode; |
---|
3 | {FILE *fcbp; |
---|
4 | char *p; |
---|
5 | int rd,wt,cm; |
---|
6 | rd = wt = cm = 0; |
---|
7 | for ( p = mode; *p; p++ ) { |
---|
8 | switch ( *p ) { |
---|
9 | case 'r': |
---|
10 | rd = 1; break; |
---|
11 | case 'w': |
---|
12 | wt = 1; break; |
---|
13 | case 'c': |
---|
14 | cm = 1; break; |
---|
15 | default: |
---|
16 | return NULL; |
---|
17 | } |
---|
18 | } |
---|
19 | if ( !(rd ^ wt) ) return NULL; |
---|
20 | if ( rd ) return _open(name,cm); |
---|
21 | else return _create(name,cm); |
---|
22 | } |
---|
23 | |
---|
24 | FILE *_open(name,cm) |
---|
25 | char *name; |
---|
26 | int cm; |
---|
27 | {FILE *fcbp; |
---|
28 | int i; |
---|
29 | for ( i = 0; i < NFILES; i++) |
---|
30 | if ( _fcbtbl[i] == NULL ) break; |
---|
31 | if ( i >= NFILES) return NULL; |
---|
32 | if ( (fcbp = malloc(FCBSIZE)) == NULL ) return NULL; |
---|
33 | if ( _setname(name,fcbp) == 0 ) return NULL; |
---|
34 | if ( FMS(fcbp,1) < 0 ) return NULL; |
---|
35 | fcbp[59] = cm ? 0 : 0xff; |
---|
36 | fcbp[60] = 0; |
---|
37 | return (_fcbtbl[i] = fcbp); |
---|
38 | } |
---|
39 | |
---|
40 | FILE *_create(name,cm) |
---|
41 | char *name; |
---|
42 | int cm; |
---|
43 | {FILE *fcbp; |
---|
44 | int i; |
---|
45 | for ( i = 0; i < NFILES; i++) |
---|
46 | if ( _fcbtbl[i] == NULL ) break; |
---|
47 | if ( i >= NFILES) return NULL; |
---|
48 | if ( (fcbp = malloc(FCBSIZE)) == NULL ) return NULL; |
---|
49 | if ( _setname(name,fcbp) == 0 ) return NULL; |
---|
50 | if ( FMS(fcbp,2) < 0 ) |
---|
51 | { if ( (fcbp[1] != 3) || (FMS(fcbp,12) < 0) ) return NULL; |
---|
52 | _setname(name,fcbp); |
---|
53 | if (FMS(fcbp,2) < 0) return NULL; |
---|
54 | } |
---|
55 | fcbp[15] = 0; |
---|
56 | fcbp[59] = cm ? 0 : 0xff; |
---|
57 | fcbp[60] = 0; |
---|
58 | return (_fcbtbl[i] = fcbp); |
---|
59 | } |
---|
60 | |
---|
61 | fclose(fcbp) |
---|
62 | FILE *fcbp; |
---|
63 | {int i; |
---|
64 | for ( i = 0; i < NFILES; i++ ) |
---|
65 | if ( fcbp == _fcbtbl[i] ) break; |
---|
66 | if ( i >= NFILES ) return EOF; |
---|
67 | _fcbtbl[i] = NULL; |
---|
68 | if ( (fcbp == STDIN) || (fcbp == STDOUT) || (fcbp == STDERR) ) return 0; |
---|
69 | if ( FMS(fcbp,4) < 0 ) return EOF; |
---|
70 | mfree(fcbp); |
---|
71 | return 0; |
---|
72 | } |
---|
73 | |
---|
74 | _setname(name,fcbp) |
---|
75 | char *name; |
---|
76 | FILE *fcbp; |
---|
77 | {int i; |
---|
78 | while(isspace(*name)) ++name; |
---|
79 | if (isdigit(*name)) |
---|
80 | { fcbp[3] = *name++ - '0'; |
---|
81 | if (*name++ != '.') return 0; |
---|
82 | } |
---|
83 | else fcbp[3] = 0xff; |
---|
84 | for (i = 4; i < 15; ++i) fcbp[i] = 0; |
---|
85 | if (!isalpha(*name)) return -1; |
---|
86 | for (i = 4; i < 12; ++i) |
---|
87 | { if (!*name || (*name == '.')) break; |
---|
88 | fcbp[i] = *name++; |
---|
89 | } |
---|
90 | while (*name && (*name != '.')) ++name; |
---|
91 | if (*name == '.') |
---|
92 | { ++name; |
---|
93 | for (i = 12; i < 15; ++i) |
---|
94 | { if (!*name) break; |
---|
95 | fcbp[i] = *name++; |
---|
96 | } |
---|
97 | } |
---|
98 | return 1; |
---|
99 | } |
---|
100 | |
---|
101 | fflush(fp) |
---|
102 | FILE *fp; |
---|
103 | { return 0; |
---|
104 | } |
---|
105 | |
---|
106 | unlink(fname) |
---|
107 | char *fname; |
---|
108 | {char p[320]; |
---|
109 | return ((_setname(fname,p) || FMS(p,12) < 0) ? -1 : 0); |
---|
110 | } |
---|
111 | |
---|
112 | rename(s,t) |
---|
113 | char *s,*t; |
---|
114 | {char p[320]; |
---|
115 | return ((_setname(s,p) || _setname(t,p+49) || FMS(p,13) < 0) ? -1 : 0); |
---|
116 | } |
---|