/*static char *SCCSID = "@(#)fs_write.c	6.1 90/11/18";*/
/*static char *SCCSID = "@(#)fs_write.c	13.3 90/01/25";*/
#define INCL_DOSERRORS
#include <os2def.h>
#include <bse.h>
#include <fsd.h>

#include <fsh.h>

#include "heap.h"
#include "filemode.h"
#include "api.h"
#include "const.h"
#include "misc.h"
#include "chain.h"
#include "buf.h"
#include "dir.h"
#include "storage.h"
#include "volume.h"
#include "tables.h"
#include "pb_ifs.h"
#include "lw.h"
#include "frame.h"
#include "pbmacros.h"
#include "fsdata.h"
#include "pr_CACHE.h"
#include "pr_FCN.h"
#include "pr_HEAP.h"
#include "pr_misc.h"
#include "pr_DEBUG.h"
#include "pr_MFS.h"

int far pascal
FS_WRITE (psffsi, psffsd, pcData, pusLen, IOflag)
struct sffsi far *	psffsi;
struct sffsd far *	psffsd;
CHAR far *		pcData;
USHORT far *		pusLen;
USHORT IOflag;
{
	USHORT usRet;
	USHORT cchWrite = *pusLen;
	FRAME  frm;

	frm.usHardErr = NO_ERROR;
	frm.fRecheckAddr = FALSE;

	/* do the write */
	usRet = Write (((PPB_SFD)(psffsd->sfd_work))->pfht,
		       cchWrite,
		       psffsi -> sfi_position,
		       pcData,
		       (USHORT)IOFLAG_TO_OPENMODE(IOflag),
		       &frm);

	if (usRet == NO_ERROR) {

	    /* update the seek pointer */
	    psffsi->sfi_position += cchWrite;

	    /*
	     * if the new position is beyond the size of the file then we must
	     * have grown the file so indicate this by updating the size.
	     *
	     */

	    if (psffsi->sfi_size < psffsi->sfi_position) {
		    psffsi->sfi_size = psffsi->sfi_position;
	    }
	} else if( usRet == ERROR_DISK_FULL ) {
	    /*
	     * Out of disk space is not really an error,
	     * it just keeps us from writing any bytes.
	     */
	    usRet = NO_ERROR;
	    *pusLen = 0;
	}

	return (usRet);

}
