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

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

Prototypes for I/O dispatch pointers.

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