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

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

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