;       SCCSID = @(#)d_setfhn.asm	6.1 91/02/15
;	SCCSID = @(#)d_setfhn.asm	6.2 90/01/22
; ****************************************************************************
; *									     *
; *			  IBM/Microsoft Confidential			     *
; *									     *
; *		    Copyright (c) IBM Corporation  1987, 1990		     *
; *		    Copyright (c) Microsoft Corp.  1987, 1990		     *
; *			      All Rights Reserved			     *
; *									     *
; ****************************************************************************
;***  D_SETFHN.ASM - API stub routine DosSetFHandState
;
;	Member of API library
;	David Silverstein
;	Copyright MicroSoft Corporation, 1986
;	20-May-1986
;
;	library routine for API simulator
;
;	Called from dos 2.x,3.x compatible dos 5.x applications
;
;	Special Information
;		none
;
;	Modification History
;
;	14-Aug-86 (billme) Modify code to preserve share and access bits
;	(since these are not set by this routine).  Also, swap order
;	error tests: handle first, then parameter.
;
;	06-Jan-87 (billme): Replace initial pushes and final pops
;	with calls to __api_begin and __api_end resp.  This will
;	reduce the size of FAPI and allow support for signals.
;
;	15-Jan-87 (billme): Change reference to __api_dev_table to
;	call to __api_devtab_ptr ... to reduce code size.
;
;	17-June-87 (stevee): fix to insure that all FAPI file handles are
;	less than MAX_HANDLES (p5553)
;

include dos2equ.inc
include error.inc
include api.inc

	EXTRN	__api_devtab_ptr:near
	EXTRN	__api_begin:near
	EXTRN	__api_end:near

;***	DOSSETFHANDSTATE
;
;	simulates DOS 5.0 call from DOS 2.x,3.x environment
;
;	errorcode = unsigned far pascal
;	DosSetFHandState(FileHandle, FileHandleState)
;
;	ENTRY:
;		FileHandle - File handle
;		FileHandleState - File handle state
;
;	RETURN:
;		AX = 0 if no error, else error code (see DOS 5 spec)
;
;	Pseudo Code:
;		save registers
;		use BP as pointer to stack parameters
;		Check for valid file handle
;		Check for valid FileHandleState
;		get and update hi_hands for handle
;		restore registers
;		return
;
;	WARNING:
;		may return error code from Dos 2.x, 3.x (see dos manuals)
;

	title	DOSSETFHANDSTATE
;
;	EXTRN	DOSSETFHANDSTATE:FAR
;
;	PUSH	WORD FileHandle
;	PUSH	WORD FileHandleState
;	CALL	DOSSETFHANDSTATE
;

_TEXT	segment byte public 'CODE'

	assume	CS:_TEXT, DS:nothing

	public	DOSSETFHANDSTATE

	EXTRN	__api_dev_table	  : WORD


FileHandle		equ	[BP+8]
FileHandleState		equ	[BP+6]

INHERIT_FLAG		equ	080h

DOSSETFHANDSTATE	proc	far

	call	__api_begin	; init bp and save bp,ds,bx,cx,dx,si,di,es
				; required to save: bp,bx,di

	mov	bx, [FileHandle]		; BX <- file handle
	cmp	bx, MAX_HANDLES			; if handle is too large
	mov	ax, error_invalid_handle
	jae	sethand3			; then return error

	mov	ax, [FileHandleState]		; get passed file handle state
	test	ax, NOT INHERIT_FLAG		; only inheritance flag spprted
	mov	ax, error_invalid_parameter
	jnz	sethand3			; if any other, return error

	xchg	ax,bx				; AX <- file handle
	call	__api_devtab_ptr		; BX <- address of entry

	mov	ax, [FileHandleState]

	mov	si,_TEXT:[bx].hi_hands		; load old file handle state
	and	si,077h				; preserve share and access
	and	ax,0ff88h			; preserve all but share and acc
	or	ax,si				; merge
	mov	_TEXT:[bx].hi_hands, ax		; load new file handle state
						; in handle table
	xor	ax, ax				; clear error return

sethand3:

	call	__api_end	; check sigs & restore es,di,si,dx,cx,bx,ds,bp

	ret	4	; clear stack of entry parameters (bytes)
DOSSETFHANDSTATE	endp

_TEXT	ends

	end
