#include <windows.h>

void win_close(HFILE hf)
{
    _lclose(hf);
}
int win_open(char __far *fname, unsigned mode, HFILE __far *hf)
{
    HFILE rhf;

    rhf = _lopen(fname, READ|OF_SHARE_COMPAT);
    if (rhf == HFILE_ERROR) return(1);
    *hf = rhf;
    return(0);
}
int win_read(HFILE hf, void __far *diskbuff, unsigned size, unsigned __far *bytes_in)
{
    WORD wBytes;

    wBytes = _lread(hf,diskbuff,size);
    if (wBytes == -1) return(-1);
    *bytes_in = wBytes;
    return(0);
}
void __far *win_malloc(unsigned size)
{
    HGLOBAL hg;
    void __far *buf;

    hg = GlobalAlloc(GMEM_FIXED|GMEM_SHARE,size + sizeof(HGLOBAL));
    if (hg == 0) return(0);
    buf = GlobalLock(hg);
    if (buf == 0) {
	GlobalFree(hg);
	return(0);
    }
    (*(HGLOBAL __far *)buf) = hg;
    return((void __far *)(((char __far *)buf) + sizeof(HGLOBAL)));
}
void win_free(void __far *buf)
{
    HGLOBAL __far *phg;
    HGLOBAL hg;

    phg = (HGLOBAL __far *)((char __far *)buf-sizeof(HGLOBAL));
    hg = *phg;
    GlobalUnlock(hg);
    GlobalFree(hg);
}
