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