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, int8_t *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, int8_t *buf, uint16_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(struct fcb *fp, int8_t *buffer, uint16_t len)
|
---|
95 | {
|
---|
96 | register uint16_t j, k, l;
|
---|
97 | int16_t clustr;
|
---|
98 | register int32_t curpos;
|
---|
99 |
|
---|
100 | curpos = fp->offset + (fp->curlsn << FILESHFT); /* get position */
|
---|
101 |
|
---|
102 | if (fp->de.bclust EQ 0) { /* see if we need to allocate */
|
---|
103 |
|
---|
104 | #if DEBUGIT
|
---|
105 | if (fsdebug)
|
---|
106 | if (curpos)
|
---|
107 | printf("_filewr(): ERROR - bclust EQ 0 and curpos (%ld) NE 0\n",
|
---|
108 | curpos);
|
---|
109 | #endif
|
---|
110 |
|
---|
111 | if (0 EQ (clustr = _newcls())) { /* allocate a cluster */
|
---|
112 |
|
---|
113 | errno = EIO;
|
---|
114 | return(len);
|
---|
115 | }
|
---|
116 |
|
---|
117 | fp->de.bclust = micons(clustr); /* update FCB */
|
---|
118 | _ptcl12(_thefat, clustr, 0x0FF8); /* update FAT */
|
---|
119 | _fatmod = TRUE;
|
---|
120 | fp->curdsn = _cl2lsn(_thebpb, clustr); /* set curdsn */
|
---|
121 | fp->curcls = clustr;
|
---|
122 | fp->clsec = 0;
|
---|
123 | fp->asects = _thebpb->clsiz;
|
---|
124 | #if DEBUGIT
|
---|
125 | if (fsdebug) {
|
---|
126 |
|
---|
127 | printf("_filewr(): allocated initial cluster=%d, asects=%ld\n",
|
---|
128 | clustr, fp->asects);
|
---|
129 | SnapFCB(fp);
|
---|
130 | }
|
---|
131 | #endif
|
---|
132 | }
|
---|
133 |
|
---|
134 | l = 0; /* zero the length-written counter */
|
---|
135 |
|
---|
136 | #if DEBUGIT
|
---|
137 | if (fsdebug)
|
---|
138 | printf("_filewr(): init pos=%ld, len=%u, curcls=%u, offset=%u\n",
|
---|
139 | curpos, len, fp->curcls, fp->offset);
|
---|
140 | #endif
|
---|
141 |
|
---|
142 | if (fp->offset) { /* see if we have a partial sector to fill */
|
---|
143 |
|
---|
144 | if ((l = (BPSEC - fp->offset)) > len)
|
---|
145 | l = len;
|
---|
146 |
|
---|
147 | if (_putsec(fp, buffer, l)) /* fill up the sector */
|
---|
148 | return(-1);
|
---|
149 | }
|
---|
150 |
|
---|
151 | if (k = (len - l) / BPSEC) { /* write out any full sectors */
|
---|
152 |
|
---|
153 | if ((j = blkwr(fp, buffer + l, k)) NE 0) {
|
---|
154 |
|
---|
155 | l += (k - j) * BPSEC; /* update amount written */
|
---|
156 |
|
---|
157 | if ((curpos + l) > fp->curlen) /* udpate file length */
|
---|
158 | fp->de.flen = miconl(fp->curlen = curpos + l);
|
---|
159 |
|
---|
160 | #if DEBUGIT
|
---|
161 | if (fsdebug)
|
---|
162 | printf("_filewr(): ERROR - curlen=%ld, curlsn=%ld, curdsn=%ld\n",
|
---|
163 | fp->curlen, fp->curlsn, fp->curdsn);
|
---|
164 | #endif
|
---|
165 |
|
---|
166 | return(l);
|
---|
167 | }
|
---|
168 |
|
---|
169 | l += k * BPSEC; /* update amount written */
|
---|
170 | }
|
---|
171 |
|
---|
172 | if (l < len) { /* write out partial sector at end */
|
---|
173 |
|
---|
174 | if (_putsec(fp, buffer + l, len - l)) {
|
---|
175 |
|
---|
176 | if ((curpos + l) > fp->curlen) /* update file length */
|
---|
177 | fp->de.flen = miconl(fp->curlen = curpos + l);
|
---|
178 |
|
---|
179 | #if DEBUGIT
|
---|
180 | if (fsdebug)
|
---|
181 | printf("_filewr(): ERROR - curlen=%ld, curlsn=%ld, curdsn=%ld\n",
|
---|
182 | fp->curlen, fp->curlsn, fp->curdsn);
|
---|
183 | #endif
|
---|
184 |
|
---|
185 | return(l);
|
---|
186 | }
|
---|
187 | }
|
---|
188 |
|
---|
189 | if ((curpos + len) > fp->curlen) /* update file length */
|
---|
190 | fp->de.flen = miconl(fp->curlen = curpos + len);
|
---|
191 |
|
---|
192 | #if DEBUGIT
|
---|
193 | if (fsdebug)
|
---|
194 | printf("_filewr(): final curlen=%ld, flen=$%08lX, curlsn=%ld, curdsn=%ld\n",
|
---|
195 | fp->curlen, fp->de.flen, fp->curlsn, fp->curdsn);
|
---|
196 | #endif
|
---|
197 |
|
---|
198 | return(len);
|
---|
199 | }
|
---|
200 |
|
---|
201 | /*
|
---|
202 | ============================================================================
|
---|
203 | write(fd, buff, len) -- write 'len' bytes from 'buff' on file 'fd'
|
---|
204 | ============================================================================
|
---|
205 | */
|
---|
206 |
|
---|
207 | int16_t write(int16_t fd, int8_t *buff, uint16_t len)
|
---|
208 | {
|
---|
209 | register struct channel *chp;
|
---|
210 |
|
---|
211 | if ((fd < 0) OR (fd > MAXCHAN)) {
|
---|
212 |
|
---|
213 | errno = EBADF;
|
---|
214 | return(-1);
|
---|
215 | }
|
---|
216 |
|
---|
217 | chp = &chantab[fd];
|
---|
218 |
|
---|
219 | return((*wr_tab[chp->c_write])(chp->c_arg, buff, len));
|
---|
220 | }
|
---|
221 |
|
---|