;       SCCSID = @(#)d_rmdir.asm	6.1 91/02/15
;	SCCSID = @(#)d_rmdir.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_rmdir.asm - API stub routine DosRmDir
;
;	Member of API library
;	Steve Salisbury & Bill Mershon
;	Copyright MicroSoft Corporation, 1986
;	18-Feb-1986
;
;	library routine for API simulator
;
;	Called from dos 2.x,3.x compatible dos 5.x applications
;
;	Special Information
;		none
;
;	Modification History
;	7-1-86 (billme): add code to return error if length of given path
;	is greater than 127 characters
;
;	30-Oct-86 (billme): add dbcs support (see #DBCS#)
;
;	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.
;
; @@	8-May-87 (Tarng): Check reserved dword are zeros.
;			  Check filename/pathname is in the 8.3 range.
;
;	12-12-88 (KLee) : P3000 Change MAXPATHLENGTH
;


include dos2equ.inc
INCLUDE basemaca.inc
INCLUDE error.inc
INCLUDE curdir.inc			; cpd12 p3000

	EXTRN	__api_begin:near
	EXTRN	__api_end:near
	EXTRN	name_check:far		;@@ added support for 8.3 filename

;***	DOSRMDIR - stub to emulate DOSRMDIR function call
;
;	simulates DOS 5.0 call from DOS 2.x,3.x environment
;
;	errorcode = unsigned far pascal
;	DosRmDir (DirName,Reserved)
;
;	ENTRY:
;		DirName - directory name
;		Reserved - null double word
;
;	RETURN:
;		AX = 0 if no error, else error code (see DOS 5 spec)
;
;	EXIT:
;
;	USES:
;		AX
;
;	Pseudo Code:
;		save registers
;		use BP as pointer to stack parameters
;		load registers for int 21 call
;		execute int 21 with function 3ah
;		restore registers
;		return
;
;	WARNING:
;		may return error code from Dos 2.x, 3.x (see dos manuals)
;
;	EFFECTS:
;		none
;

	title	DOSRMDIR - stub to emulate DOSRMDIR function call
;
;	EXTRN	DOSRMDIR:FAR
;
;	PUSH	@ ASCIZ Name
;	PUSH	DWORD 0
;	CALL	DOSRMDIR
;

	EXTRN	api_testdbcs_b:far

_TEXT	segment byte public 'CODE'

	assume	CS:_TEXT,DS:NOTHING

	public	DOSRMDIR
	EXTRN	_api_getvrsn	  : NEAR

; MAXPATHLEN	  equ	  128		  ; 127 + end char cpd12 p3000

DataArea	equ	[BP+10]
Reserved0	equ	[BP+6]

DOSRMDIR	proc	far

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

; check validity of reserved words
	mov	ax,ERROR_INVALID_PARAMETER	;@@
	mov	bx, Reserved0			;@@reserved words not zeros
	or	bx,bx				;@@
	jnz	ErrorExit			;@@ then return error
	or	bx,Reserved0+2			;@@
	jnz	ErrorExit			;@@ then return error

	; insure that string length is <= 127
	cld				; prepare for scasb 'increment'
	les	di,[DataArea]		; ES:DI <- pointer to pathname
	mov	cx,MAXPATHLEN		; CX <- maximum path length
	xor	al,al			; AL <- zero (end of string char)
	repne	scasb			; search for end of string
	jz	rmdir1
	mov	ax,ERROR_PATH_NOT_FOUND ; set and return error
	jmp	short ErrorExit
rmdir1:					; proceed with directory removal
	lds	dx,[DataArea]		; DS:DX <- pointer to pathname
; verify the filename in 8.3 range
	push	si			;@@ save si
	mov	ah,3			;@@ meta char is invalid
	mov	si,dx			;@@ ds:si = pointer to filename
	call	name_check		;@@ check name in 8.3 range
	pop	si			;@@ restor si
	jc	ErrorExit		;@@ filename is illega

	mov	ah,D2_RemoveDir
	int	21H
	jc	ErrorExit
	xor	ax,ax
	jmp	short rmdir5

ErrorExit:
	; There is a bug in Dos 3.2 which causes an incorrect error code
	; to be reported when removal of a path name with wild card chars
	; is attempted.

	cmp	ax,ERROR_CURRENT_DIRECTORY
	jnz	rmdir5
	call	_api_getvrsn			; get Dos version
	cmp	ax, 314h			; is this Dos 3.20?
	jnz	rmdir4				; error code ok

	; here we scan the path looking for the occurance of a '*' or a '?'

	mov	si, dx				; ds:si <= pointer to pathname
rmdir2:
	lodsb
; #DBCS#
	call	api_testdbcs_b			; if ill formed DB char
	jz	rmdir4				; then return error
	jc	rmdir2				; else if DB char then loop

	or	al,al				; is this the end of the path?
	jz	rmdir4				;  then no wild char chars
	cmp	al,'*'
	je	rmdir3
	cmp	al,'?'
	jne	rmdir2
rmdir3:
	mov	ax,ERROR_PATH_NOT_FOUND
	jmp	short rmdir5

rmdir4:
	mov	ax,ERROR_CURRENT_DIRECTORY

rmdir5:

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

	ret	8	; clear stack of entry parameters (bytes)
DOSRMDIR	endp

_TEXT	ends

	end
