- Timestamp:
- 07/01/2017 11:16:45 PM (7 years ago)
- Branches:
- master
- Children:
- 6dc5ea7
- Parents:
- df097bf
- git-author:
- Thomas Lopatic <thomas@…> (07/01/2017 11:16:39 PM)
- git-committer:
- Thomas Lopatic <thomas@…> (07/01/2017 11:16:45 PM)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
libcio/writern.c
rdf097bf rc091ef8 1 1 /* 2 ============================================================================ 3 write.c -- write() and friends 4 Version 17 -- 1987-10-27 -- D.N. Lynx Crowe 5 ============================================================================ 2 ============================================================================= 3 writern.c -- write a random sector onto a file 4 Version 6 -- 1987-12-15 -- D.N. Lynx Crowe 5 6 int 7 WriteRN(fcp, buf) 8 struct fcb *fcp; 9 char *buf; 10 11 Writes a sector onto file 'fcp' from 'buf'. Seeks as needed. 12 Returns SUCCESS (0) if OK, FAILURE (-1) for errors. 13 ============================================================================= 6 14 */ 7 15 8 16 #define DEBUGIT 0 9 17 18 #include "stddefs.h" 10 19 #include "biosdefs.h" 11 #include "io.h"12 20 #include "errno.h" 13 #include "stddefs.h" 21 #include "errdefs.h" 22 #include "fspars.h" 14 23 15 int _filewr(); 16 17 extern int _badfd(), _conwr(), blkwr(); 18 extern int ReadRN(), WriteRN(); 19 extern int _newcls(), micons(), _ptcl12(); 20 21 extern long miconl(); 22 23 extern char *memset(); 24 25 extern short _fatmod; 26 27 extern unsigned _thefat[]; 28 29 extern struct bpb *_thebpb; 24 extern long _berrno; 25 extern int _seek(); 30 26 31 27 #if DEBUGIT … … 33 29 #endif 34 30 35 static int (*wr_tab[])() = { /* write routine dispatch table */ 36 37 _badfd, /* 0 - invalid entry */ 38 _filewr, /* 1 - disk file */ 39 _conwr /* 2 - console device */ 40 }; 31 #if TBUFFER 32 extern long _secwr(); /* update buffer function */ 33 #endif 41 34 42 35 /* … … 45 38 46 39 /* 47 ============================================================================ 48 _putsec(fp, buf, len) -- write 'len' bytes from 'buf' on file 'fp' 49 ============================================================================ 40 ============================================================================= 41 WriteRN(fcp, buf) -- Writes a sector onto file 'fcp' from 'buf'. 42 Seeks as needed. Returns SUCCESS (0) if OK, FAILURE (-1) for errors. 43 ============================================================================= 50 44 */ 51 45 52 46 int 53 _putsec(fp, buf, len)54 register struct fcb *fp;47 WriteRN(fcp, buf) 48 struct fcb *fcp; 55 49 char *buf; 56 unsigned len;57 50 { 58 #if DEBUGIT 59 if (fsdebug) 60 printf("_putsec($%08lX, $%08lx, %d): initial curlsn=%ld\n", 61 fp, buf, len, fp->curlsn); 62 #endif 51 int sv; /* seek return code */ 52 long brc; /* bios return code */ 63 53 64 if ((errno = ReadRN(fp, Wrkbuf)) EQ 1) { /* try to read sector */ 54 if (sv = _seek(fcp)) { /* try to find the sector we want */ 55 56 if (sv < 0) { /* seek error ? */ 65 57 66 58 #if DEBUGIT 67 59 if (fsdebug) 68 printf(" _putsec(): ReadRN saw EOF at curlsn=%ld, asects=%ld\n",69 fp->curlsn, fp->asects);60 printf("WriteRN(): _seek FAILED (%d) - curlsn=%ld, curdsn=%ld\n", 61 sv, fcp->curlsn, fcp->curdsn); 70 62 #endif 71 63 72 errno = 0; /* we're at EOF */ 73 memset(Wrkbuf, 0x1A, BPSEC); /* pad end of sector */ 74 75 } else if (errno) 64 errno = EIO; /* I/O error or seek past EOF */ 76 65 return(FAILURE); 77 66 78 memcpy(Wrkbuf + fp->offset, buf, len); /* move in the new data*/67 } else if (sv EQ 2) { /* at hard EOF ? */ 79 68 80 if ((errno = WriteRN(fp, Wrkbuf)) NE 0) { /* write the sector */69 if (_alcnew(fcp)) { /* allocate a new cluster */ 81 70 71 errno = EIO; 72 return(FAILURE); 73 } 82 74 #if DEBUGIT 83 75 if (fsdebug) 84 printf(" _putsec(): WriteRN() FAILED (%d) - curlsn=%ld\n",85 errno, fp->curlsn);76 printf("WriteRN(): cluster allocated - curcls=%d, clsec=%d\n", 77 fcp->curcls, fcp->clsec); 86 78 #endif 87 79 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 DEBUGIT98 if (fsdebug)99 printf("_putsec(): _seek() failed - curlsn=%ld, asects=%ld\n",100 fp->curlsn, fp->asects);101 #endif102 103 return(FAILURE);104 80 } 105 81 } … … 107 83 #if DEBUGIT 108 84 if (fsdebug) 109 printf(" _putsec(): final curlsn=%ld, offset=%d, len=%d\n",110 f p->curlsn, fp->offset, len);85 printf("WriteRN(): curlsn=%ld, curdsn=%ld\n", 86 fcp->curlsn, fcp->curdsn); 111 87 #endif 112 88 113 return(SUCCESS); 114 } 89 /* write the sector */ 115 90 116 /* 117 118 */ 119 120 /* 121 ============================================================================ 122 _filewr(fp, buffer, len) -- write 'len' bytes on file 'fp' 123 from 'buffer'. 124 ============================================================================ 125 */ 126 127 int 128 _filewr(fp, buffer, len) 129 register struct fcb *fp; 130 register char *buffer; 131 register 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 */ 91 if (brc = BIOS(B_RDWR, 1, buf, 1, (short)fcp->curdsn, 0)) { 140 92 141 93 #if DEBUGIT 142 94 if (fsdebug) 143 if (curpos) 144 printf("_filewr(): ERROR - bclust EQ 0 and curpos (%ld) NE 0\n", 145 curpos); 95 printf("WriteRN(): B_RDWR FAILED - brc=%ld\n", brc); 146 96 #endif 147 97 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 98 _berrno = brc; /* log the error */ 99 errno = EIO; /* ... as an I/O error */ 100 return(FAILURE); /* return: ERROR */ 169 101 } 170 102 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); 103 #if TBUFFER 104 _secwr(buf, (short)fcp->curdsn); 177 105 #endif 178 106 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); 107 return(SUCCESS); /* return: SUCCESS */ 238 108 } 239 240 /*241 242 */243 244 /*245 ============================================================================246 write(fd, buff, len) -- write 'len' bytes from 'buff' on file 'fd'247 ============================================================================248 */249 250 int251 write(fd, buff, len)252 int fd;253 char *buff;254 unsigned 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 TracChangeset
for help on using the changeset viewer.