/*static char *SCCSID = "@(#)closeda.c	6.1 90/11/17";*/
/*static char *SCCSID = "@(#)closeda.c      13.3 90/02/27";*/
#define _BTXT _BTXT
#define INCL_DOSERRORS
#include <os2.h>
#include "api.h"
#include "filemode.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 "superb.h"
#include "lw.h"
#include "frame.h"
#include "fsdata.h"
#include "assert.h"
#include "pbmacros.h"
#include "heap.h"
#include "pbmsg.h"

#include "pr_FCN.h"
#include "pr_FSSUB.h"
#include "pr_HEAP.h"
#include "pr_debug.h"
#include "pr_MISC.h"


/* put stand-alone fs_xxx functions in separate code segment */

/*
 ***	CloseDirect - Close (Direct Access) File Handle
 *
 *	CloseDirect is temporary code to handle Direct Access closes.
 *
 *	ENTRY	pfht = file handle
 *		fsType = Write Type flags (see OPEN_???? flags)
 *
 *	EXIT	Returns 0 if OK
 *		Returns non-zero error code if error
 *
 *	HISTORY
 *	    19-Sep-89 by JR (John Rogers, JohnRo@Microsoft)
 *		- Avoid warnings from Microsoft C 5.2 (on slash-slash comments).
 *	    11-Nov-1988 by ThomasF - add volume support
 *	    31-Oct-1988 14:59:58 by Mark Hitch - initial code
 */

USHORT far
CloseDirect (pfht, fsType)
PFHT pfht;		/* file handle */
ULONG fsType;		/* file type flags */
{
    POFT poft;

#ifdef DEBUG
    if ((pfht->usMode & OPEN_DIRECT) == 0)
	INTERR (__FILE__": CLOSE_DIRECT flag not set");
#endif

    /* we assume that if we got the request, it must be for a pinball volume,
     * and since we only handle one, it must be this one!
     */

    poft = pfht->poft;
    pfrm->pvol = poft->pVolume;     /* setup pvol for lower level routines */

    RetHeap (pfht);

    /* if the lock bit or the pending bit is set then block */
    if ((poft->bFlag & (OTF_LCK + OTF_PLK)) != 0)
        INTERR (PBMSG_CLOSEDA_POFTLOCKED);

    if (--poft->cusClientFHTs != 0)
        INTERR (PBMSG_CLOSEDA_MANYCLIENTS);

    if (poft->bFlag & OTF_PND)
    INTERR (PBMSG_CLOSEDA_LOCKPENDING);

    /* Wait until no one else is using OFT */
    if (poft->cusHolds != 0 || poft->cWaits != 0)
        INTERR (PBMSG_CLOSEDA_SOMEONEWAITS);

    RetHeap (poft);

    return NO_ERROR;

    fsType;
}
