1 | FILE *fopen(name,mode) |
---|
2 | char *name,*mode; |
---|
3 | {char *p; |
---|
4 | int rd,wt,cm; |
---|
5 | rd = wt = cm = 0; |
---|
6 | for ( p = mode; *p; p++ ) { |
---|
7 | switch ( *p ) { |
---|
8 | case 'r': |
---|
9 | rd = 1; break; |
---|
10 | case 'w': |
---|
11 | wt = 1; break; |
---|
12 | case 'c': |
---|
13 | cm = 1; break; |
---|
14 | default: |
---|
15 | return NULL; |
---|
16 | } |
---|
17 | } |
---|
18 | if ( !(rd ^ wt) ) return NULL; |
---|
19 | if ( rd ) return _open(name,cm); |
---|
20 | else return _create(name,cm); |
---|
21 | } |
---|
22 | |
---|
23 | FILE *_open(name,cm) |
---|
24 | char *name; |
---|
25 | int cm; |
---|
26 | {FILE *fp; |
---|
27 | int i; |
---|
28 | for ( i = 0; i < NFILES; i++) |
---|
29 | if ( _fptbl[i] == NULL ) break; |
---|
30 | if ( i >= NFILES) return NULL; |
---|
31 | if ( (fp = malloc(FBSIZE)) == NULL ) return NULL; |
---|
32 | if ( _setname(name,fp->_fcb) == 0 ) return NULL; |
---|
33 | if ( FMS(fp->_fcb,1) < 0 ) return NULL; |
---|
34 | fp->_fcb[59] = cm ? 0 : 0xff; |
---|
35 | fp->_fcb[60] = 0; |
---|
36 | return (_fptbl[i] = fp); |
---|
37 | } |
---|
38 | |
---|
39 | FILE *_create(name,cm) |
---|
40 | char *name; |
---|
41 | int cm; |
---|
42 | {FILE *fp; |
---|
43 | int i; |
---|
44 | for ( i = 0; i < NFILES; i++) |
---|
45 | if ( _fptbl[i] == NULL ) break; |
---|
46 | if ( i >= NFILES) return NULL; |
---|
47 | if ( (fp = malloc(FBSIZE)) == NULL ) return NULL; |
---|
48 | if ( _setname(name,fp->_fcb) == 0 ) return NULL; |
---|
49 | if ( FMS(fp->_fcb,2) < 0 ) |
---|
50 | { if ( (fp[1] != 3) || (FMS(fp->_fcb,12) < 0) ) return NULL; |
---|
51 | _setname(name,fp->_fcb); |
---|
52 | if (FMS(fp->_fcb,2) < 0) return NULL; |
---|
53 | } |
---|
54 | fp->_fcb[15] = 0; |
---|
55 | fp->_fcb[59] = cm ? 0 : 0xff; |
---|
56 | fp->_fcb[60] = 0; |
---|
57 | return (_fptbl[i] = fp); |
---|
58 | } |
---|
59 | |
---|
60 | fclose(fp) |
---|
61 | FILE *fp; |
---|
62 | {int i; |
---|
63 | for ( i = 0; i < NFILES; i++ ) |
---|
64 | if ( fp == _fptbl[i] ) break; |
---|
65 | if ( i >= NFILES ) return EOF; |
---|
66 | _fptbl[i] = NULL; |
---|
67 | if ( (fp == STDIN) || (fp == STDOUT) || (fp == STDERR) ) return 0; |
---|
68 | if ( fp->_fcb[2] == 2 ) fflush(fp); |
---|
69 | if ( FMS(fp->_fcb,4) < 0 ) return EOF; |
---|
70 | mfree(fp); |
---|
71 | return 0; |
---|
72 | } |
---|
73 | |
---|
74 | _setname(name,fcbp) |
---|
75 | char *name; |
---|
76 | char *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 | {int i; |
---|
104 | if ( fp == STDIN ) return EOF; |
---|
105 | if ( fp == STDOUT || fp == STDERR ) return 0; |
---|
106 | if ( fp->_fcb[2] != 2 ) return EOF; |
---|
107 | for ( i = 0; i < fp->_fbp; i++ ) FMS(fp->_fcb,0,fp->fcb[i]); |
---|
108 | fp->_fbp = 0; |
---|
109 | return 0; |
---|
110 | } |
---|
111 | |
---|
112 | unlink(fname) |
---|
113 | char *fname; |
---|
114 | {char p[320]; |
---|
115 | return ((_setname(fname,p) || FMS(p,12) < 0) ? -1 : 0); |
---|
116 | } |
---|
117 | |
---|
118 | rename(s,t) |
---|
119 | char *s,*t; |
---|
120 | {char p[320]; |
---|
121 | return ((_setname(s,p) || _setname(t,p+49) || FMS(p,13) < 0) ? -1 : 0); |
---|
122 | } |
---|