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

#include "const.h"
#include "chain.h"
#include "misc.h"
#include "buf.h"
#include "dir.h"
#include "storage.h"
#include "volume.h"
#include "tables.h"
#include "pbmacros.h"
#include "lw.h"
#include "frame.h"
#include "fsdata.h"

#include "pr_DEBUG.h"
#include "pr_MISC.h"

int far
MakeBadSec (USHORT  hVPB,
	    PCHAR   pcParm,
	    USHORT  usLenParm,
	    PUSHORT pusLenParmOut)

{
    PVOLTAB pVol;
    USHORT usRet;
    LSN  lsn;
    UINT iBadSec;

    /* get pointer to current volume */
    if ((pVol = FindPVol (hVPB)) == NULL) {
	return ERROR_INVALID_HANDLE;
    }

    /* Make sure the param pointer is valid */
    if (usLenParm < sizeof (ULONG)) {
	*pusLenParmOut = sizeof (ULONG);
	return ERROR_BAD_LENGTH;
    }

    /* probe the address of where the lsn is stored */
    MAYBLOCK (
	usRet = FSH_PROBEBUF (PB_OPREAD,
			      pcParm,
			      sizeof (ULONG)););
    if (usRet != NO_ERROR) {
	return usRet;
    }

    lsn = *((PULONG) pcParm);

    /* lsn < 0 means remove it from list - otherwise add it */
    if ((LONG) lsn >= 0) {
	/* add lsn to list */
	lsn |= pVol->ulVolBits;

	/* assume table is full */
	usRet = ERROR_HPFS_BADSECLISTFULL;

	for (iBadSec = 0; iBadSec < FAKEERRORCT; iBadSec++) {
	    if (HFList [iBadSec] == 0) {
		HFList [iBadSec] = lsn;
		HFFlag = TRUE;
		return NO_ERROR;
	    }
	}
    } else {
	/* remove lsn from list */
	lsn = (lsn & 0x7FFFFFF) | pVol->ulVolBits;

	/* assume we won't find it */
	usRet = ERROR_HPFS_BADSECNOTINLIST;

	for (iBadSec = 0; iBadSec < FAKEERRORCT; iBadSec++) {
	    if (HFList [iBadSec] == lsn) {
		HFList [iBadSec] = 0;
		return NO_ERROR;
	    }
	}
    }

    return usRet;
}
