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

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

Point of no return.

  • Property mode set to 100644
File size: 5.6 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
15int _filewr(struct fcb *fp, char *buffer, unsigned len);
16
17extern int _badfd(void);
18extern int _conwr(int kind, char *buff, int len);
19extern int blkwr(struct fcb *fcp, char *buf, int ns);
20extern int ReadRN(struct fcb *fcp, char *buf);
21extern int WriteRN(struct fcb *fcp, char *buf);
22extern int _newcls(void);
23extern short micons(short wi);
24extern void _ptcl12(unsigned *fat, unsigned cl, unsigned val);
25
26extern long miconl(long wi);
27
28extern char *memset(char *sp, char c, int n);
29
30extern short _fatmod;
31
32extern unsigned _thefat[];
33
34extern struct bpb *_thebpb;
35
36#if DEBUGIT
37extern short fsdebug;
38#endif
39
40static int (*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
57int _putsec(struct fcb *fp, char *buf, unsigned 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
128int _filewr(struct fcb *fp, char *buffer, unsigned len)
129{
130 register unsigned j, k, l;
131 int clustr;
132 register long 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
247int write(int fd, char *buff, unsigned 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}
Note: See TracBrowser for help on using the repository browser.