[f40a309] | 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 "stddefs.h"
|
---|
| 21 | #include "biosdefs.h"
|
---|
| 22 | #include "errno.h"
|
---|
| 23 | #include "errdefs.h"
|
---|
| 24 | #include "fspars.h"
|
---|
| 25 |
|
---|
| 26 | extern int _nsic(), _alcnew(), _newcls();
|
---|
| 27 |
|
---|
| 28 | extern long _berrno; /* last file system bios error number */
|
---|
| 29 |
|
---|
| 30 | extern struct bpb *_thebpb; /* current bios parameter block */
|
---|
| 31 |
|
---|
| 32 | extern short _thefat[]; /* current file allocation table */
|
---|
| 33 |
|
---|
| 34 | extern short _fatmod; /* FAT modified flag */
|
---|
| 35 |
|
---|
| 36 | #if DEBUGIT
|
---|
| 37 | extern short fsdebug;
|
---|
| 38 | #endif
|
---|
| 39 |
|
---|
| 40 | #if TBUFFER
|
---|
| 41 |
|
---|
| 42 | /* WARNING: this ONLY works for 512 byte sectors, 9 sectors per track */
|
---|
| 43 |
|
---|
| 44 | extern short _b_tbuf[9][256]; /* the track buffer */
|
---|
| 45 |
|
---|
| 46 | extern short _b_trak; /* current track */
|
---|
| 47 | extern short _b_side; /* current side */
|
---|
| 48 | extern short _b_sect; /* current sector */
|
---|
| 49 | extern short _b_tsec; /* base sector for current track */
|
---|
| 50 |
|
---|
| 51 | #endif
|
---|
| 52 |
|
---|
| 53 | /* |
---|
| 54 |
|
---|
| 55 | */
|
---|
| 56 |
|
---|
| 57 | #if TBUFFER
|
---|
| 58 |
|
---|
| 59 | /*
|
---|
| 60 | =============================================================================
|
---|
| 61 | _secwr(buf, rec) -- write a logical sector into the track buffer
|
---|
| 62 | =============================================================================
|
---|
| 63 | */
|
---|
| 64 |
|
---|
| 65 | long
|
---|
| 66 | _secwr(buf, rec)
|
---|
| 67 | register char *buf;
|
---|
| 68 | register short rec;
|
---|
| 69 | {
|
---|
| 70 | register short track, side, sector;
|
---|
| 71 |
|
---|
| 72 | if (_thebpb->dspt NE 9) /* make sure we can do this */
|
---|
| 73 | return(ERR07);
|
---|
| 74 |
|
---|
| 75 | if (_thebpb->recsiz NE 512)
|
---|
| 76 | return(ERR07);
|
---|
| 77 |
|
---|
| 78 | track = rec / _thebpb->dspc; /* desired track */
|
---|
| 79 | sector = rec - (track * _thebpb->dspc); /* logical sector */
|
---|
| 80 |
|
---|
| 81 | if (sector GE _thebpb->dspt) { /* adjust sector and side */
|
---|
| 82 |
|
---|
| 83 | sector -= _thebpb->dspt;
|
---|
| 84 | side = 1;
|
---|
| 85 |
|
---|
| 86 | } else {
|
---|
| 87 |
|
---|
| 88 | side = 0;
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | #if DEBUGIT
|
---|
| 92 | if (fsdebug) {
|
---|
| 93 |
|
---|
| 94 | printf("_secwr($%08.8LX, %d): track=%d, side=%d, sector=%d\n",
|
---|
| 95 | buf, rec, track, side, sector);
|
---|
| 96 | printf("_secwr(): _b_trak=%d, _b_side=%d, _b_sect=%d, _b_tsec=%d\n",
|
---|
| 97 | _b_trak, _b_side, _b_sect, _b_tsec);
|
---|
| 98 | }
|
---|
| 99 | #endif
|
---|
| 100 |
|
---|
| 101 | if ((track NE _b_trak) OR (side NE _b_side)) /* track in buffer ? */
|
---|
| 102 | return(0L);
|
---|
| 103 |
|
---|
| 104 | memcpy((char *)_b_tbuf[sector], buf, 512); /* update the buffer */
|
---|
| 105 |
|
---|
| 106 | #if DEBUGIT
|
---|
| 107 | if (fsdebug)
|
---|
| 108 | printf("_secwr(): updated track buffer\n");
|
---|
| 109 | #endif
|
---|
| 110 |
|
---|
| 111 | return(0L);
|
---|
| 112 | }
|
---|
| 113 |
|
---|
| 114 | #endif
|
---|
| 115 |
|
---|
| 116 | /* |
---|
| 117 |
|
---|
| 118 | */
|
---|
| 119 |
|
---|
| 120 | /*
|
---|
| 121 | =============================================================================
|
---|
| 122 | blkwr(fcp, buf, ns) -- write 'ns' sectors from file 'fcp' into 'buf'.
|
---|
| 123 | Returns the number of unwritten sectors, or 0 if all were written.
|
---|
| 124 | =============================================================================
|
---|
| 125 | */
|
---|
| 126 |
|
---|
| 127 | int
|
---|
| 128 | blkwr(fcp, buf, ns)
|
---|
| 129 | struct fcb *fcp;
|
---|
| 130 | char *buf;
|
---|
| 131 | int ns;
|
---|
| 132 | {
|
---|
| 133 | long rc;
|
---|
| 134 | short clustr;
|
---|
| 135 |
|
---|
| 136 | while (ns > 0) { /* write a sector at a time */
|
---|
| 137 |
|
---|
| 138 | if (fcp->asects) {
|
---|
| 139 |
|
---|
| 140 | if (fcp->curlsn LT fcp->asects) {
|
---|
| 141 |
|
---|
| 142 | #if DEBUGIT
|
---|
| 143 | if (fsdebug)
|
---|
| 144 | printf("blkwr(): [1] ns=%d, curlsn=%ld, curdsn=%ld, curcls=%u, buf=$%08lX\n",
|
---|
| 145 | ns, fcp->curlsn, fcp->curdsn, fcp->curcls, buf);
|
---|
| 146 | #endif
|
---|
| 147 |
|
---|
| 148 | if (rc = BIOS(B_RDWR, 1, buf, 1, (short)fcp->curdsn, 0)) {
|
---|
| 149 |
|
---|
| 150 | #if DEBUGIT
|
---|
| 151 | if (fsdebug)
|
---|
| 152 | printf("blkwr(): B_RDWR failed, rc=%ld\n", rc);
|
---|
| 153 | #endif
|
---|
| 154 |
|
---|
| 155 | _berrno = rc; /* log the error */
|
---|
| 156 | errno = EIO;
|
---|
| 157 | return(ns); /* return unwritten sector count */
|
---|
| 158 | }
|
---|
| 159 |
|
---|
| 160 | #if TBUFFER
|
---|
| 161 | _secwr(buf, (short)fcp->curdsn);
|
---|
| 162 | #endif
|
---|
| 163 |
|
---|
| 164 | rc = _nsic(fcp, _thebpb, _thefat);
|
---|
| 165 |
|
---|
| 166 | #if DEBUGIT
|
---|
| 167 | if (fsdebug)
|
---|
| 168 | printf("blkwr(): _nsic() rc=%ld\n", rc);
|
---|
| 169 | #endif
|
---|
| 170 |
|
---|
| 171 | if (--ns EQ 0) /* done if no more to do */
|
---|
| 172 | return(0);
|
---|
| 173 |
|
---|
| 174 | if (rc < 0) { /* bad cluster ? */
|
---|
| 175 |
|
---|
| 176 | errno = EIO; /* set error number */
|
---|
| 177 | return(ns); /* return unwritten sector count */
|
---|
| 178 | }
|
---|
| 179 |
|
---|
| 180 | buf += _thebpb->recsiz;
|
---|
| 181 | /* |
---|
| 182 |
|
---|
| 183 | */
|
---|
| 184 | } else if (fcp->curlsn EQ fcp->asects) {
|
---|
| 185 |
|
---|
| 186 | if (_alcnew(fcp)) { /* allocate a cluster */
|
---|
| 187 |
|
---|
| 188 | errno = EIO; /* set error number */
|
---|
| 189 | return(ns); /* return unwritten sector count */
|
---|
| 190 | }
|
---|
| 191 |
|
---|
| 192 | fcp->modefl &= ~FC_EOF; /* clear EOF flag */
|
---|
| 193 |
|
---|
| 194 | #if DEBUGIT
|
---|
| 195 | if (fsdebug)
|
---|
| 196 | printf("blkwr(): [2] ns=%d, curlsn=%ld, curdsn=%ld, curcls=%u, buf=$%08lX\n",
|
---|
| 197 | ns, fcp->curlsn, fcp->curdsn, fcp->curcls, buf);
|
---|
| 198 | #endif
|
---|
| 199 |
|
---|
| 200 | if (rc = BIOS(B_RDWR, 1, buf, 1, (short)fcp->curdsn, 0)) {
|
---|
| 201 |
|
---|
| 202 | #if DEBUGIT
|
---|
| 203 | if (fsdebug)
|
---|
| 204 | printf("blkwr(): B_RDWR failed, rc=%ld\n", rc);
|
---|
| 205 | #endif
|
---|
| 206 |
|
---|
| 207 | _berrno = rc; /* log the error */
|
---|
| 208 | errno = EIO;
|
---|
| 209 | return(ns); /* return unwritten sector count */
|
---|
| 210 | }
|
---|
| 211 |
|
---|
| 212 | #if TBUFFER
|
---|
| 213 | _secwr(buf, (short)fcp->curdsn);
|
---|
| 214 | #endif
|
---|
| 215 |
|
---|
| 216 | ++fcp->curlsn;
|
---|
| 217 | ++fcp->clsec;
|
---|
| 218 | ++fcp->curdsn;
|
---|
| 219 |
|
---|
| 220 | if (--ns EQ 0)
|
---|
| 221 | return(0);
|
---|
| 222 |
|
---|
| 223 | buf += _thebpb->recsiz; /* advance buffer pointer */
|
---|
| 224 | }
|
---|
| 225 | /* |
---|
| 226 |
|
---|
| 227 | */
|
---|
| 228 | } else {
|
---|
| 229 |
|
---|
| 230 | if (0 EQ (clustr = _newcls())) {
|
---|
| 231 |
|
---|
| 232 | errno = EIO;
|
---|
| 233 | return(ns);
|
---|
| 234 | }
|
---|
| 235 |
|
---|
| 236 | fcp->de.bclust = micons(clustr);
|
---|
| 237 | _ptcl12(_thefat, clustr, 0x0FF8);
|
---|
| 238 | _fatmod = TRUE;
|
---|
| 239 | fcp->curdsn = _cl2lsn(_thebpb, clustr);
|
---|
| 240 | fcp->curcls = clustr;
|
---|
| 241 | fcp->clsec = 0;
|
---|
| 242 | fcp->asects = _thebpb->clsiz;
|
---|
| 243 | fcp->modefl &= ~FC_EOF;
|
---|
| 244 |
|
---|
| 245 | fcp->modefl &= ~FC_EOF; /* clear EOF flag */
|
---|
| 246 |
|
---|
| 247 | #if DEBUGIT
|
---|
| 248 | if (fsdebug)
|
---|
| 249 | printf("blkwr(): [3] ns=%d, curlsn=%ld, curdsn=%ld, curcls=%u, buf=$%08lX\n",
|
---|
| 250 | ns, fcp->curlsn, fcp->curdsn, fcp->curcls, buf);
|
---|
| 251 | #endif
|
---|
| 252 |
|
---|
| 253 | if (rc = BIOS(B_RDWR, 1, buf, 1, (short)fcp->curdsn, 0)) {
|
---|
| 254 |
|
---|
| 255 | #if DEBUGIT
|
---|
| 256 | if (fsdebug)
|
---|
| 257 | printf("blkwr(): B_RDWR failed, rc=%ld\n", rc);
|
---|
| 258 | #endif
|
---|
| 259 |
|
---|
| 260 | _berrno = rc; /* log the error */
|
---|
| 261 | errno = EIO;
|
---|
| 262 | return(ns); /* return unwritten sector count */
|
---|
| 263 | }
|
---|
| 264 |
|
---|
| 265 | #if TBUFFER
|
---|
| 266 | _secwr(buf, (short)fcp->curdsn);
|
---|
| 267 | #endif
|
---|
| 268 |
|
---|
| 269 | ++fcp->curlsn;
|
---|
| 270 | ++fcp->clsec;
|
---|
| 271 | ++fcp->curdsn;
|
---|
| 272 |
|
---|
| 273 | if (--ns EQ 0)
|
---|
| 274 | return(0);
|
---|
| 275 |
|
---|
| 276 | buf += _thebpb->recsiz; /* advance buffer pointer */
|
---|
| 277 | }
|
---|
| 278 | }
|
---|
| 279 |
|
---|
| 280 | return(ns); /* return (will be zero or negative) */
|
---|
| 281 | }
|
---|