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