source: buchla-68k/libcio/write.c@ e225e77

Last change on this file since e225e77 was e225e77, checked in by Thomas Lopatic <thomas@…>, 7 years ago

Added missing includes and declarations.

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