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