source: buchla-68k/libcio/blkwr.c@ 2a87ef3

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

No more warnings in libcio.

  • Property mode set to 100644
File size: 4.9 KB
Line 
1/*
2 =============================================================================
3 blkwr.c -- write a block of 0..32767 sectors
4 Version 14 -- 1987-12-15 -- D.N. Lynx Crowe
5
6 int
7 blkwr(fcp, buf, ns)
8 struct fcb *fcp;
9 char *buf;
10 int ns;
11
12 Writes 'ns' sectors from file 'fcp' into 'buf'.
13 Returns the number of unwritten sectors,
14 or 0 if all were written.
15 =============================================================================
16*/
17
18#define DEBUGIT 0
19
20#include "ram.h"
21
22#if TBUFFER
23
24/*
25 =============================================================================
26 _secwr(buf, rec) -- write a logical sector into the track buffer
27 =============================================================================
28*/
29
30int32_t _secwr(int8_t *buf, int16_t rec)
31{
32 register int16_t track, side, sector;
33
34 if (_thebpb->dspt NE 9) /* make sure we can do this */
35 return(ERR07);
36
37 if (_thebpb->recsiz NE 512)
38 return(ERR07);
39
40 track = rec / _thebpb->dspc; /* desired track */
41 sector = rec - (track * _thebpb->dspc); /* logical sector */
42
43 if (sector GE _thebpb->dspt) { /* adjust sector and side */
44
45 sector -= _thebpb->dspt;
46 side = 1;
47
48 } else {
49
50 side = 0;
51 }
52
53#if DEBUGIT
54 if (fsdebug) {
55
56 printf("_secwr($%08.8LX, %d): track=%d, side=%d, sector=%d\n",
57 buf, rec, track, side, sector);
58 printf("_secwr(): _b_trak=%d, _b_side=%d, _b_sect=%d, _b_tsec=%d\n",
59 _b_trak, _b_side, _b_sect, _b_tsec);
60 }
61#endif
62
63 if ((track NE _b_trak) OR (side NE _b_side)) /* track in buffer ? */
64 return(0L);
65
66 memcpy((int8_t *)_b_tbuf[sector], buf, 512); /* update the buffer */
67
68#if DEBUGIT
69 if (fsdebug)
70 printf("_secwr(): updated track buffer\n");
71#endif
72
73 return(0L);
74}
75
76#endif
77
78/*
79 =============================================================================
80 blkwr(fcp, buf, ns) -- write 'ns' sectors from file 'fcp' into 'buf'.
81 Returns the number of unwritten sectors, or 0 if all were written.
82 =============================================================================
83*/
84
85int16_t blkwr(struct fcb *fcp, int8_t *buf, int16_t ns)
86{
87 int32_t rc;
88 int16_t clustr;
89
90 while (ns > 0) { /* write a sector at a time */
91
92 if (fcp->asects) {
93
94 if (fcp->curlsn LT fcp->asects) {
95
96#if DEBUGIT
97 if (fsdebug)
98 printf("blkwr(): [1] ns=%d, curlsn=%ld, curdsn=%ld, curcls=%u, buf=$%08lX\n",
99 ns, fcp->curlsn, fcp->curdsn, fcp->curcls, buf);
100#endif
101
102 if ((rc = BIOS(B_RDWR, 1, buf, 1, (int16_t)fcp->curdsn, 0))) {
103
104#if DEBUGIT
105 if (fsdebug)
106 printf("blkwr(): B_RDWR failed, rc=%ld\n", rc);
107#endif
108
109 _berrno = rc; /* log the error */
110 errno = EIO;
111 return(ns); /* return unwritten sector count */
112 }
113
114#if TBUFFER
115 _secwr(buf, (int16_t)fcp->curdsn);
116#endif
117
118 rc = _nsic(fcp, _thebpb, _thefat);
119
120#if DEBUGIT
121 if (fsdebug)
122 printf("blkwr(): _nsic() rc=%ld\n", rc);
123#endif
124
125 if (--ns EQ 0) /* done if no more to do */
126 return(0);
127
128 if (rc < 0) { /* bad cluster ? */
129
130 errno = EIO; /* set error number */
131 return(ns); /* return unwritten sector count */
132 }
133
134 buf += _thebpb->recsiz;
135
136 } else if (fcp->curlsn EQ fcp->asects) {
137
138 if (_alcnew(fcp)) { /* allocate a cluster */
139
140 errno = EIO; /* set error number */
141 return(ns); /* return unwritten sector count */
142 }
143
144 fcp->modefl &= ~FC_EOF; /* clear EOF flag */
145
146#if DEBUGIT
147 if (fsdebug)
148 printf("blkwr(): [2] ns=%d, curlsn=%ld, curdsn=%ld, curcls=%u, buf=$%08lX\n",
149 ns, fcp->curlsn, fcp->curdsn, fcp->curcls, buf);
150#endif
151
152 if ((rc = BIOS(B_RDWR, 1, buf, 1, (int16_t)fcp->curdsn, 0))) {
153
154#if DEBUGIT
155 if (fsdebug)
156 printf("blkwr(): B_RDWR failed, rc=%ld\n", rc);
157#endif
158
159 _berrno = rc; /* log the error */
160 errno = EIO;
161 return(ns); /* return unwritten sector count */
162 }
163
164#if TBUFFER
165 _secwr(buf, (int16_t)fcp->curdsn);
166#endif
167
168 ++fcp->curlsn;
169 ++fcp->clsec;
170 ++fcp->curdsn;
171
172 if (--ns EQ 0)
173 return(0);
174
175 buf += _thebpb->recsiz; /* advance buffer pointer */
176 }
177
178 } else {
179
180 if (0 EQ (clustr = _newcls())) {
181
182 errno = EIO;
183 return(ns);
184 }
185
186 fcp->de.bclust = micon16((uint16_t)clustr);
187 _ptcl12(_thefat, clustr, 0x0FF8);
188 _fatmod = TRUE;
189 fcp->curdsn = _cl2lsn(_thebpb, clustr);
190 fcp->curcls = clustr;
191 fcp->clsec = 0;
192 fcp->asects = _thebpb->clsiz;
193 fcp->modefl &= ~FC_EOF;
194
195 fcp->modefl &= ~FC_EOF; /* clear EOF flag */
196
197#if DEBUGIT
198 if (fsdebug)
199 printf("blkwr(): [3] ns=%d, curlsn=%ld, curdsn=%ld, curcls=%u, buf=$%08lX\n",
200 ns, fcp->curlsn, fcp->curdsn, fcp->curcls, buf);
201#endif
202
203 if ((rc = BIOS(B_RDWR, 1, buf, 1, (int16_t)fcp->curdsn, 0))) {
204
205#if DEBUGIT
206 if (fsdebug)
207 printf("blkwr(): B_RDWR failed, rc=%ld\n", rc);
208#endif
209
210 _berrno = rc; /* log the error */
211 errno = EIO;
212 return(ns); /* return unwritten sector count */
213 }
214
215#if TBUFFER
216 _secwr(buf, (int16_t)fcp->curdsn);
217#endif
218
219 ++fcp->curlsn;
220 ++fcp->clsec;
221 ++fcp->curdsn;
222
223 if (--ns EQ 0)
224 return(0);
225
226 buf += _thebpb->recsiz; /* advance buffer pointer */
227 }
228 }
229
230 return(ns); /* return (will be zero or negative) */
231}
232
Note: See TracBrowser for help on using the repository browser.