;       SCCSID = @(#)d_qcdir.asm	6.1 91/02/15
;	SCCSID = @(#)d_qcdir.asm	1.4 90/01/22
; ****************************************************************************
; *									     *
; *			  IBM/Microsoft Confidential			     *
; *									     *
; *		    Copyright (c) IBM Corporation  1987, 1990		     *
; *		    Copyright (c) Microsoft Corp.  1987, 1990		     *
; *			      All Rights Reserved			     *
; *									     *
; ****************************************************************************
;***  d_qcdir.asm - API stub routine DosQCurDir
;
;	Member of API library
;	Bill Mershon
;	Copyright MicroSoft Corporation, 1986
;	27-Feb-1986
;
;	library routine for API simulator
;
;	Called from dos 2.x,3.x compatible dos 5.x applications
;
;	Special Information
;		none
;
;	Modification History
;
;	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.
;
;
;	04-May-87 (robertc): Install hooks for DOSErrorClass Support.
;	All int 21h errors are passed through api_get_extended to get the
;	extended errorcode and to provide the class, action, and locus for
;	the dynamic error FIFO queue.
;

INCLUDE dos2equ.inc
INCLUDE basemaca.inc
INCLUDE error.inc

	EXTRN	api_get_extended:far	;@@ added support for DOSErrorClass
	EXTRN	__api_begin:near
	EXTRN	__api_end:near


DPLEN	equ	64		; directory path length


;***	DOSQCURDIR - stub to emulate DOSQCURDIR function call
;
;	simulates dos 5.0 DOSQCURDIR using int 21, function 047h
;
;	errorcode = unsigned far pascal
;	DosQCurDir (DriveNumber,DirPath,DirPathLen)
;
;	ENTRY:
;		DriveNumber - drive number (0=default, 1=A, etc)
;		DirPath - directory path buffer
;		DirPathLen - directory path length (should be >= DPLEN)
;
;	RETURN:
;		AX = 0 if no error, else error code (see DOS 5 spec)
;
;	EXIT:
;		@DirPathLen set to DPLEN (if @DirPathLen too large)
;		DirPath set to current directory path (unless error)
;
;	USES:
;		AX
;
;	Pseudo Code:
;		save registers
;		use BP as pointer to stack parameters
;		if *DirPathLen < DPLEN
;			store DPLEN at DirPathLen
;			return error
;		load registers for int 21 call
;		execute int 21 with function 047h
;		restore registers
;		return
;
;	WARNING:
;		may return error code from Dos 2.x, 3.x (see dos manuals)
;
;	EFFECTS:
;		none
;

	title	DOSQCURDIR - stub to emulate DOSQCURDIR function call
;
;	EXTRN	DOSQCURDIR:FAR
;
;	PUSH	WORD DriveNumber
;	PUSH	@ OTHER DirPath
;	PUSH	@ WORD DirPathLen
;	CALL	DOSQCURDIR
;

; The purpose of this segment is to share a reusable data area between api
; library routines. This is a win since it decreases the total api library
; overhead in the application's executable.

APIBUF	segment byte common 'CODE'

__api_buffer	db	DPLEN dup(?)

APIBUF	ends


_TEXT	segment byte public 'CODE'
	assume	CS:_TEXT,DS:NOTHING

	public	DOSQCURDIR

DriveNumber	equ	[BP+14]
DirPath		equ	[BP+10]
DirPathLen	equ	[BP+6]

DOSQCURDIR	proc	far

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

; load buffer with current directory

	mov	ax, SEG APIBUF
	mov	ds, ax			; DS:SI <- buffer
	mov	si, offset APIBUF:__api_buffer
	mov	dx,[DriveNumber]	; DX <- drive number
	mov	ah,D2_GetCurDir
	int	21H
	jc	ErrorExit

; search for end of string in buffer

	mov	ax, seg APIBUF		; ES:DI <- buffer
	mov	es, ax
	mov	di, offset APIBUF:__api_buffer
	mov	cx,DPLEN		; CX <- buffer length
	xor	al,al			; AX <- search value (0)
	cld
	repne	scasb

; compare length of string to given buffer length

	mov	dx,DPLEN
	sub	dx,cx			; DX <- length of string
	lds	bx,[DirPathLen]
	mov	ax,DS:[bx]		; AX <- path length
	cmp	ax,dx
	jge	LengthOK		; given buffer is large enough
	mov	DS:[bx],dx		; return length needed
	mov	ax,ERROR_BUFFER_OVERFLOW
	jmp	NormalExit		;@@ added support for DOSErrorClass

; copy from our buffer to the given buffer

LengthOK:
	mov	cx,dx			; CX <- length of string
	mov	ax, SEG APIBUF
	mov	ds, ax			; DS:SI <- our buffer
	mov	si, offset APIBUF:__api_buffer
	les	di,[DirPath]		; ES:DI <- given buffer
	cld
	rep	movsb
OkayExit:
	xor	ax,ax		; set "no error"
	jmp	NormalExit		;@@ Added support for DOSErrorClass
ErrorExit:				;@@ added support for DOSErrorClass
	call	api_get_extended	;@@ added support for DOSErrorClass
NormalExit:				;@@ added support for DOSErrorClass

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

	ret	10		; clear stack of entry parameters (bytes)
DOSQCURDIR	endp

_TEXT	ends

	end

