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

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

Added include files for global functions and variables.

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