source: buchla-68k/orig/IOLIB/WRITE.C@ d21fc6f

Last change on this file since d21fc6f was 3ae31e9, checked in by Thomas Lopatic <thomas@…>, 8 years ago

Imported original source code.

  • Property mode set to 100755
File size: 5.6 KB
RevLine 
[3ae31e9]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();
16
17extern int _badfd(), _conwr(), blkwr();
18extern int ReadRN(), WriteRN();
19extern int _newcls(), micons(), _ptcl12();
20
21extern long miconl();
22
23extern char *memset();
24
25extern short _fatmod;
26
27extern unsigned _thefat[];
28
29extern struct bpb *_thebpb;
30
31#if DEBUGIT
32extern short fsdebug;
33#endif
34
35static int (*wr_tab[])() = { /* write routine dispatch table */
36
37 _badfd, /* 0 - invalid entry */
38 _filewr, /* 1 - disk file */
39 _conwr /* 2 - console device */
40};
41
42/*
43
44*/
45
46/*
47 ============================================================================
48 _putsec(fp, buf, len) -- write 'len' bytes from 'buf' on file 'fp'
49 ============================================================================
50*/
51
52int
53_putsec(fp, buf, len)
54register struct fcb *fp;
55char *buf;
56unsigned len;
57{
58#if DEBUGIT
59 if (fsdebug)
60 printf("_putsec($%08lX, $%08lx, %d): initial curlsn=%ld\n",
61 fp, buf, len, fp->curlsn);
62#endif
63
64 if ((errno = ReadRN(fp, Wrkbuf)) EQ 1) { /* try to read sector */
65
66#if DEBUGIT
67 if (fsdebug)
68 printf("_putsec(): ReadRN saw EOF at curlsn=%ld, asects=%ld\n",
69 fp->curlsn, fp->asects);
70#endif
71
72 errno = 0; /* we're at EOF */
73 memset(Wrkbuf, 0x1A, BPSEC); /* pad end of sector */
74
75 } else if (errno)
76 return(FAILURE);
77
78 memcpy(Wrkbuf + fp->offset, buf, len); /* move in the new data */
79
80 if ((errno = WriteRN(fp, Wrkbuf)) NE 0) { /* write the sector */
81
82#if DEBUGIT
83 if (fsdebug)
84 printf("_putsec(): WriteRN() FAILED (%d) - curlsn=%ld\n",
85 errno, fp->curlsn);
86#endif
87
88 return(FAILURE);
89 }
90
91 if ((fp->offset = (fp->offset + len) & (BPSEC - 1)) EQ 0) {
92
93 ++fp->curlsn; /* update file position */
94
95 if (_seek(fp) < 0) {
96
97#if DEBUGIT
98 if (fsdebug)
99 printf("_putsec(): _seek() failed - curlsn=%ld, asects=%ld\n",
100 fp->curlsn, fp->asects);
101#endif
102
103 return(FAILURE);
104 }
105 }
106
107#if DEBUGIT
108 if (fsdebug)
109 printf("_putsec(): final curlsn=%ld, offset=%d, len=%d\n",
110 fp->curlsn, fp->offset, len);
111#endif
112
113 return(SUCCESS);
114}
115
116/*
117
118*/
119
120/*
121 ============================================================================
122 _filewr(fp, buffer, len) -- write 'len' bytes on file 'fp'
123 from 'buffer'.
124 ============================================================================
125*/
126
127int
128_filewr(fp, buffer, len)
129register struct fcb *fp;
130register char *buffer;
131register unsigned len;
132{
133 register unsigned j, k, l;
134 int clustr;
135 register long 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
250int
251write(fd, buff, len)
252int fd;
253char *buff;
254unsigned len;
255{
256 register struct channel *chp;
257
258 if ((fd < 0) OR (fd > MAXCHAN)) {
259
260 errno = EBADF;
261 return(-1);
262 }
263
264 chp = &chantab[fd];
265
266 return((*wr_tab[chp->c_write])(chp->c_arg, buff, len));
267}
Note: See TracBrowser for help on using the repository browser.