1 | /*
|
---|
2 | ============================================================================
|
---|
3 | write.c -- write() and friends
|
---|
4 | Version 17 -- 1987-10-27 -- D.N. Lynx Crowe
|
---|
5 | ============================================================================
|
---|
6 | */
|
---|
7 |
|
---|
8 | #define DEBUGIT 0
|
---|
9 |
|
---|
10 | #include "biosdefs.h"
|
---|
11 | #include "io.h"
|
---|
12 | #include "errno.h"
|
---|
13 | #include "stddefs.h"
|
---|
14 |
|
---|
15 | int16_t _filewr(struct fcb *fp, int8_t *buffer, uint16_t len);
|
---|
16 |
|
---|
17 | extern int16_t _badfd(void);
|
---|
18 | extern int16_t _conwr(int16_t kind, int8_t *buff, int16_t len);
|
---|
19 | extern int16_t blkwr(struct fcb *fcp, int8_t *buf, int16_t ns);
|
---|
20 | extern int16_t ReadRN(struct fcb *fcp, int8_t *buf);
|
---|
21 | extern int16_t WriteRN(struct fcb *fcp, int8_t *buf);
|
---|
22 | extern int16_t _newcls(void);
|
---|
23 | extern int16_t micons(int16_t wi);
|
---|
24 | extern void _ptcl12(uint16_t *fat, uint16_t cl, uint16_t val);
|
---|
25 |
|
---|
26 | extern int32_t miconl(int32_t wi);
|
---|
27 |
|
---|
28 | extern void *memset(void *vp, int8_t c, int16_t n);
|
---|
29 |
|
---|
30 | extern int16_t _fatmod;
|
---|
31 |
|
---|
32 | extern uint16_t _thefat[];
|
---|
33 |
|
---|
34 | extern struct bpb *_thebpb;
|
---|
35 |
|
---|
36 | #if DEBUGIT
|
---|
37 | extern short fsdebug;
|
---|
38 | #endif
|
---|
39 |
|
---|
40 | static int16_t (*wr_tab[])() = { /* write routine dispatch table */
|
---|
41 |
|
---|
42 | _badfd, /* 0 - invalid entry */
|
---|
43 | _filewr, /* 1 - disk file */
|
---|
44 | _conwr /* 2 - console device */
|
---|
45 | };
|
---|
46 |
|
---|
47 | /* |
---|
48 |
|
---|
49 | */
|
---|
50 |
|
---|
51 | /*
|
---|
52 | ============================================================================
|
---|
53 | _putsec(fp, buf, len) -- write 'len' bytes from 'buf' on file 'fp'
|
---|
54 | ============================================================================
|
---|
55 | */
|
---|
56 |
|
---|
57 | int16_t _putsec(struct fcb *fp, int8_t *buf, uint16_t len)
|
---|
58 | {
|
---|
59 | #if DEBUGIT
|
---|
60 | if (fsdebug)
|
---|
61 | printf("_putsec($%08lX, $%08lx, %d): initial curlsn=%ld\n",
|
---|
62 | fp, buf, len, fp->curlsn);
|
---|
63 | #endif
|
---|
64 |
|
---|
65 | if ((errno = ReadRN(fp, Wrkbuf)) EQ 1) { /* try to read sector */
|
---|
66 |
|
---|
67 | #if DEBUGIT
|
---|
68 | if (fsdebug)
|
---|
69 | printf("_putsec(): ReadRN saw EOF at curlsn=%ld, asects=%ld\n",
|
---|
70 | fp->curlsn, fp->asects);
|
---|
71 | #endif
|
---|
72 |
|
---|
73 | errno = 0; /* we're at EOF */
|
---|
74 | memset(Wrkbuf, 0x1A, BPSEC); /* pad end of sector */
|
---|
75 |
|
---|
76 | } else if (errno)
|
---|
77 | return(FAILURE);
|
---|
78 |
|
---|
79 | memcpy(Wrkbuf + fp->offset, buf, len); /* move in the new data */
|
---|
80 |
|
---|
81 | if ((errno = WriteRN(fp, Wrkbuf)) NE 0) { /* write the sector */
|
---|
82 |
|
---|
83 | #if DEBUGIT
|
---|
84 | if (fsdebug)
|
---|
85 | printf("_putsec(): WriteRN() FAILED (%d) - curlsn=%ld\n",
|
---|
86 | errno, fp->curlsn);
|
---|
87 | #endif
|
---|
88 |
|
---|
89 | return(FAILURE);
|
---|
90 | }
|
---|
91 |
|
---|
92 | if ((fp->offset = (fp->offset + len) & (BPSEC - 1)) EQ 0) {
|
---|
93 |
|
---|
94 | ++fp->curlsn; /* update file position */
|
---|
95 |
|
---|
96 | if (_seek(fp) < 0) {
|
---|
97 |
|
---|
98 | #if DEBUGIT
|
---|
99 | if (fsdebug)
|
---|
100 | printf("_putsec(): _seek() failed - curlsn=%ld, asects=%ld\n",
|
---|
101 | fp->curlsn, fp->asects);
|
---|
102 | #endif
|
---|
103 |
|
---|
104 | return(FAILURE);
|
---|
105 | }
|
---|
106 | }
|
---|
107 |
|
---|
108 | #if DEBUGIT
|
---|
109 | if (fsdebug)
|
---|
110 | printf("_putsec(): final curlsn=%ld, offset=%d, len=%d\n",
|
---|
111 | fp->curlsn, fp->offset, len);
|
---|
112 | #endif
|
---|
113 |
|
---|
114 | return(SUCCESS);
|
---|
115 | }
|
---|
116 |
|
---|
117 | /* |
---|
118 |
|
---|
119 | */
|
---|
120 |
|
---|
121 | /*
|
---|
122 | ============================================================================
|
---|
123 | _filewr(fp, buffer, len) -- write 'len' bytes on file 'fp'
|
---|
124 | from 'buffer'.
|
---|
125 | ============================================================================
|
---|
126 | */
|
---|
127 |
|
---|
128 | int16_t _filewr(struct fcb *fp, int8_t *buffer, uint16_t len)
|
---|
129 | {
|
---|
130 | register uint16_t j, k, l;
|
---|
131 | int16_t clustr;
|
---|
132 | register int32_t curpos;
|
---|
133 |
|
---|
134 | curpos = fp->offset + (fp->curlsn << FILESHFT); /* get position */
|
---|
135 |
|
---|
136 | if (fp->de.bclust EQ 0) { /* see if we need to allocate */
|
---|
137 |
|
---|
138 | #if DEBUGIT
|
---|
139 | if (fsdebug)
|
---|
140 | if (curpos)
|
---|
141 | printf("_filewr(): ERROR - bclust EQ 0 and curpos (%ld) NE 0\n",
|
---|
142 | curpos);
|
---|
143 | #endif
|
---|
144 |
|
---|
145 | if (0 EQ (clustr = _newcls())) { /* allocate a cluster */
|
---|
146 |
|
---|
147 | errno = EIO;
|
---|
148 | return(len);
|
---|
149 | }
|
---|
150 |
|
---|
151 | fp->de.bclust = micons(clustr); /* update FCB */
|
---|
152 | _ptcl12(_thefat, clustr, 0x0FF8); /* update FAT */
|
---|
153 | _fatmod = TRUE;
|
---|
154 | fp->curdsn = _cl2lsn(_thebpb, clustr); /* set curdsn */
|
---|
155 | fp->curcls = clustr;
|
---|
156 | fp->clsec = 0;
|
---|
157 | fp->asects = _thebpb->clsiz;
|
---|
158 | #if DEBUGIT
|
---|
159 | if (fsdebug) {
|
---|
160 |
|
---|
161 | printf("_filewr(): allocated initial cluster=%d, asects=%ld\n",
|
---|
162 | clustr, fp->asects);
|
---|
163 | SnapFCB(fp);
|
---|
164 | }
|
---|
165 | #endif
|
---|
166 | }
|
---|
167 |
|
---|
168 | l = 0; /* zero the length-written counter */
|
---|
169 |
|
---|
170 | #if DEBUGIT
|
---|
171 | if (fsdebug)
|
---|
172 | printf("_filewr(): init pos=%ld, len=%u, curcls=%u, offset=%u\n",
|
---|
173 | curpos, len, fp->curcls, fp->offset);
|
---|
174 | #endif
|
---|
175 |
|
---|
176 | if (fp->offset) { /* see if we have a partial sector to fill */
|
---|
177 |
|
---|
178 | if ((l = (BPSEC - fp->offset)) > len)
|
---|
179 | l = len;
|
---|
180 |
|
---|
181 | if (_putsec(fp, buffer, l)) /* fill up the sector */
|
---|
182 | return(-1);
|
---|
183 | }
|
---|
184 |
|
---|
185 | if (k = (len - l) / BPSEC) { /* write out any full sectors */
|
---|
186 |
|
---|
187 | if ((j = blkwr(fp, buffer + l, k)) NE 0) {
|
---|
188 |
|
---|
189 | l += (k - j) * BPSEC; /* update amount written */
|
---|
190 |
|
---|
191 | if ((curpos + l) > fp->curlen) /* udpate file length */
|
---|
192 | fp->de.flen = miconl(fp->curlen = curpos + l);
|
---|
193 |
|
---|
194 | #if DEBUGIT
|
---|
195 | if (fsdebug)
|
---|
196 | printf("_filewr(): ERROR - curlen=%ld, curlsn=%ld, curdsn=%ld\n",
|
---|
197 | fp->curlen, fp->curlsn, fp->curdsn);
|
---|
198 | #endif
|
---|
199 |
|
---|
200 | return(l);
|
---|
201 | }
|
---|
202 |
|
---|
203 | l += k * BPSEC; /* update amount written */
|
---|
204 | }
|
---|
205 | /* |
---|
206 |
|
---|
207 | */
|
---|
208 | if (l < len) { /* write out partial sector at end */
|
---|
209 |
|
---|
210 | if (_putsec(fp, buffer + l, len - l)) {
|
---|
211 |
|
---|
212 | if ((curpos + l) > fp->curlen) /* update file length */
|
---|
213 | fp->de.flen = miconl(fp->curlen = curpos + l);
|
---|
214 |
|
---|
215 | #if DEBUGIT
|
---|
216 | if (fsdebug)
|
---|
217 | printf("_filewr(): ERROR - curlen=%ld, curlsn=%ld, curdsn=%ld\n",
|
---|
218 | fp->curlen, fp->curlsn, fp->curdsn);
|
---|
219 | #endif
|
---|
220 |
|
---|
221 | return(l);
|
---|
222 | }
|
---|
223 | }
|
---|
224 |
|
---|
225 | if ((curpos + len) > fp->curlen) /* update file length */
|
---|
226 | fp->de.flen = miconl(fp->curlen = curpos + len);
|
---|
227 |
|
---|
228 | #if DEBUGIT
|
---|
229 | if (fsdebug)
|
---|
230 | printf("_filewr(): final curlen=%ld, flen=$%08lX, curlsn=%ld, curdsn=%ld\n",
|
---|
231 | fp->curlen, fp->de.flen, fp->curlsn, fp->curdsn);
|
---|
232 | #endif
|
---|
233 |
|
---|
234 | return(len);
|
---|
235 | }
|
---|
236 |
|
---|
237 | /* |
---|
238 |
|
---|
239 | */
|
---|
240 |
|
---|
241 | /*
|
---|
242 | ============================================================================
|
---|
243 | write(fd, buff, len) -- write 'len' bytes from 'buff' on file 'fd'
|
---|
244 | ============================================================================
|
---|
245 | */
|
---|
246 |
|
---|
247 | int16_t write(int16_t fd, int8_t *buff, uint16_t len)
|
---|
248 | {
|
---|
249 | register struct channel *chp;
|
---|
250 |
|
---|
251 | if ((fd < 0) OR (fd > MAXCHAN)) {
|
---|
252 |
|
---|
253 | errno = EBADF;
|
---|
254 | return(-1);
|
---|
255 | }
|
---|
256 |
|
---|
257 | chp = &chantab[fd];
|
---|
258 |
|
---|
259 | return((*wr_tab[chp->c_write])(chp->c_arg, buff, len));
|
---|
260 | }
|
---|