Index: libcio/writern.c
===================================================================
--- libcio/writern.c	(revision df097bf59e892ec2e9bbb4a5696044afa1d5bf16)
+++ libcio/writern.c	(revision 050d522689195ab136c6ab25023fc920eeba251a)
@@ -1,31 +1,27 @@
 /*
-   ============================================================================
-	write.c -- write() and friends
-	Version 17 -- 1987-10-27 -- D.N. Lynx Crowe
-   ============================================================================
+   =============================================================================
+	writern.c -- write a random sector onto a file
+	Version 6 -- 1987-12-15 -- D.N. Lynx Crowe
+
+	int
+	WriteRN(fcp, buf)
+	struct fcb *fcp;
+	char *buf;
+
+		Writes a sector onto file 'fcp' from 'buf'.  Seeks as needed.
+		Returns SUCCESS (0) if OK,  FAILURE (-1) for errors.
+   =============================================================================
 */
 
 #define	DEBUGIT		0
 
+#include "stddefs.h"
 #include "biosdefs.h"
-#include "io.h"
 #include "errno.h"
-#include "stddefs.h"
+#include "errdefs.h"
+#include "fspars.h"
 
-int	_filewr();
-
-extern	int	_badfd(), _conwr(), blkwr();
-extern	int	ReadRN(), WriteRN();
-extern	int	_newcls(), micons(), _ptcl12();
-
-extern	long	miconl();
-
-extern	char	*memset();
-
-extern	short	_fatmod;
-
-extern	unsigned	_thefat[];
-
-extern	struct	bpb	*_thebpb;
+extern	long	_berrno;
+extern	int	_seek();
 
 #if DEBUGIT
@@ -33,10 +29,7 @@
 #endif
 
-static	int	(*wr_tab[])() = {	/* write routine dispatch table */
-
-	_badfd,		/* 0 - invalid entry */
-	_filewr,	/* 1 - disk file */
-	_conwr		/* 2 - console device */
-};
+#if	TBUFFER
+extern	long	_secwr();		/* update buffer function */
+#endif
 
 /* 
@@ -45,61 +38,44 @@
 
 /*
-   ============================================================================
-	_putsec(fp, buf, len) -- write 'len' bytes from 'buf' on file 'fp'
-   ============================================================================
+   =============================================================================
+	WriteRN(fcp, buf) -- Writes a sector onto file 'fcp' from 'buf'.
+	Seeks as needed.  Returns SUCCESS (0) if OK,  FAILURE (-1) for errors.
+   =============================================================================
 */
 
 int
-_putsec(fp, buf, len)
-register struct fcb *fp;
+WriteRN(fcp, buf)
+struct fcb *fcp;
 char *buf;
-unsigned len;
 {
-#if DEBUGIT
-	if (fsdebug)
-		printf("_putsec($%08lX, $%08lx, %d):  initial curlsn=%ld\n",
-			fp, buf, len, fp->curlsn);
-#endif
+	int	sv;		/* seek return code */
+	long	brc;		/* bios return code */
 
-	if ((errno = ReadRN(fp, Wrkbuf)) EQ 1) {	/* try to read sector */
+	if (sv = _seek(fcp)) {		/* try to find the sector we want */
+
+		if (sv < 0) {			/* seek error ? */
 
 #if DEBUGIT
 	if (fsdebug)
-		printf("_putsec():  ReadRN saw EOF at curlsn=%ld, asects=%ld\n",
-			fp->curlsn, fp->asects);
+		printf("WriteRN():  _seek FAILED (%d) - curlsn=%ld, curdsn=%ld\n",
+			sv, fcp->curlsn, fcp->curdsn);
 #endif
 
-		errno = 0;			/* we're at EOF */
-		memset(Wrkbuf, 0x1A, BPSEC);	/* pad end of sector */
-
-	} else if (errno)
+			errno = EIO;		/* I/O error or seek past EOF */
 			return(FAILURE);
 
-	memcpy(Wrkbuf + fp->offset, buf, len);	/* move in the new data */
+		} else if (sv EQ 2) {		/* at hard EOF ? */
 
-	if ((errno = WriteRN(fp, Wrkbuf)) NE 0) {	/* write the sector */
+			if (_alcnew(fcp)) {	/* allocate a new cluster */
 
+				errno = EIO;
+				return(FAILURE);
+			}
 #if DEBUGIT
 	if (fsdebug)
-		printf("_putsec():  WriteRN() FAILED (%d) - curlsn=%ld\n",
-			errno, fp->curlsn);
+		printf("WriteRN():  cluster allocated - curcls=%d, clsec=%d\n",
+			fcp->curcls, fcp->clsec);
 #endif
 
-		return(FAILURE);
-	}
-
-	if ((fp->offset = (fp->offset + len) & (BPSEC - 1)) EQ 0) {
-
-		++fp->curlsn;		/* update file position */
-
-		if (_seek(fp) < 0) {
-
-#if DEBUGIT
-	if (fsdebug)
-		printf("_putsec():  _seek() failed - curlsn=%ld, asects=%ld\n",
-			fp->curlsn, fp->asects);
-#endif
-
-			return(FAILURE);
 		}
 	}
@@ -107,161 +83,26 @@
 #if DEBUGIT
 	if (fsdebug)
-		printf("_putsec():  final curlsn=%ld, offset=%d, len=%d\n",
-			fp->curlsn, fp->offset, len);
+		printf("WriteRN():  curlsn=%ld, curdsn=%ld\n",
+			fcp->curlsn, fcp->curdsn);
 #endif
 
-	return(SUCCESS);
-}
+	/* write the sector */
 
-/* 
-
-*/
-
-/*
-   ============================================================================
-	_filewr(fp, buffer, len) -- write 'len' bytes on file 'fp'
-	from 'buffer'.
-   ============================================================================
-*/
-
-int
-_filewr(fp, buffer, len)
-register struct fcb *fp;
-register char *buffer;
-register unsigned len;
-{
-	register unsigned j, k, l;
-	int clustr;
-	register long curpos;
-
-	curpos = fp->offset + (fp->curlsn << FILESHFT);	/* get position */
-
-	if (fp->de.bclust EQ 0) {	/* see if we need to allocate */
+	if (brc = BIOS(B_RDWR, 1, buf, 1, (short)fcp->curdsn, 0)) {
 
 #if DEBUGIT
 	if (fsdebug)
-		if (curpos)
-			printf("_filewr():  ERROR - bclust EQ 0 and curpos (%ld) NE 0\n",
-				curpos);
+		printf("WriteRN():  B_RDWR FAILED - brc=%ld\n", brc);
 #endif
 
-		if (0 EQ (clustr = _newcls())) {	/* allocate a cluster */
-
-			errno = EIO;
-			return(len);
-		}
-
-		fp->de.bclust = micons(clustr);		/* update FCB */
-		_ptcl12(_thefat, clustr, 0x0FF8);	/* update FAT */
-		_fatmod = TRUE;
-		fp->curdsn = _cl2lsn(_thebpb, clustr);	/* set curdsn */
-		fp->curcls = clustr;
-		fp->clsec = 0;
-		fp->asects = _thebpb->clsiz;
-#if DEBUGIT
-	if (fsdebug) {
-
-		printf("_filewr():  allocated initial cluster=%d, asects=%ld\n",
-			clustr, fp->asects);
-		SnapFCB(fp);
-	}
-#endif
+		_berrno = brc;			/* log the error */
+		errno = EIO;			/* ... as an I/O error */
+		return(FAILURE);		/* return:  ERROR */
 	}
 
-	l = 0;			/* zero the length-written counter */
-
-#if DEBUGIT
-	if (fsdebug)
-		printf("_filewr():  init pos=%ld, len=%u, curcls=%u, offset=%u\n",
-			curpos, len, fp->curcls, fp->offset);
+#if	TBUFFER
+	_secwr(buf, (short)fcp->curdsn);
 #endif
 
-	if (fp->offset) {	/* see if we have a partial sector to fill */
-
-		if ((l = (BPSEC - fp->offset)) > len)
-			l = len;
-
-		if (_putsec(fp, buffer, l))	/* fill up the sector */
-			return(-1);
-	}
-
-	if (k = (len - l) / BPSEC) {	/* write out any full sectors */
-
-		if ((j = blkwr(fp, buffer + l, k)) NE 0) {
-
-			l += (k - j) * BPSEC;	/* update amount written */
-
-			if ((curpos + l) > fp->curlen) 	/* udpate file length */
-				fp->de.flen = miconl(fp->curlen = curpos + l);
-
-#if DEBUGIT
-	if (fsdebug)
-		printf("_filewr():  ERROR - curlen=%ld, curlsn=%ld, curdsn=%ld\n",
-			fp->curlen, fp->curlsn, fp->curdsn);
-#endif
-
-			return(l);
-		}
-
-		l += k * BPSEC;		/* update amount written */
-	}
-/* 
-
-*/
-	if (l < len) {			/* write out partial sector at end */
-
-		if (_putsec(fp, buffer + l, len - l)) {
-
-			if ((curpos + l) > fp->curlen)	/* update file length */
-				fp->de.flen = miconl(fp->curlen = curpos + l);
-
-#if DEBUGIT
-	if (fsdebug)
-		printf("_filewr():  ERROR - curlen=%ld, curlsn=%ld, curdsn=%ld\n",
-			fp->curlen, fp->curlsn, fp->curdsn);
-#endif
-
-			return(l);
-		}
-	}
-
-	if ((curpos + len) > fp->curlen) 		/* update file length */
-		fp->de.flen = miconl(fp->curlen = curpos + len);
-
-#if DEBUGIT
-	if (fsdebug)
-		printf("_filewr():  final curlen=%ld, flen=$%08lX, curlsn=%ld, curdsn=%ld\n",
-			fp->curlen, fp->de.flen, fp->curlsn, fp->curdsn);
-#endif
-
-	return(len);
+	return(SUCCESS);		/* return:  SUCCESS */
 }
-
-/* 
-
-*/
-
-/*
-   ============================================================================
-	write(fd, buff, len) -- write 'len' bytes from 'buff' on file 'fd'
-   ============================================================================
-*/
-
-int
-write(fd, buff, len)
-int fd;
-char *buff;
-unsigned len;
-{
-	register struct channel *chp;
-
-	if ((fd < 0) OR (fd > MAXCHAN)) {
-
-		errno = EBADF;
-		return(-1);
-	}
-
-	chp = &chantab[fd];
-
-	return((*wr_tab[chp->c_write])(chp->c_arg, buff, len));
-}
