include	clib.inc
include	string.inc
include	dos.inc
include	dir.inc
include	stdlib.inc

	extrn	envpath: dword

_TEXT	SEGMENT

file_exist:
	push	bx
	push	ss
	lea	ax,[bp-128]
	push	ax
	call	filexist
	cmp	ax,1
	pop	bx
	ret

init_file:
	push	es
	push	ss
	lea	ax,[bp-256]
	push	ax
	push	0
	push	0
	push	ss
	add	ax,128
	push	ax
	call	strfcat
	pop	es
	ret

search:	push	bx
	push	ss
	lea	ax,[bp-256]
	push	ax
	push	0
	call	fullpath
	call	init_file
	les	di,envpath
    search_test:
	push	ss
	lea	ax,[bp-256]
	push	ax
	call	filexist
	cmp	ax,1
	je	search_found
	cmp	byte ptr es:[di],';'
	jnz	search_nul?
	inc	di
    search_nul?:
	cmp	ah,es:[di]
	jz	search_nul
	xor	bx,bx
	lea	si,[bp-256]
    search_cpy:
	mov	al,es:[bx+di]
	test	al,al
	jz	search_eof
	cmp	al,';'
	je	search_eof
	mov	[bx+si],al
	inc	bx
	jmp	search_cpy
    search_found:
	mov	ax,bp
	sub	ax,256
	mov	dx,ss
	jmp	search_end
    search_nul:
	xor	ax,ax
	mov	dx,ax
    search_end:
	test	ax,ax
	pop	bx
	ret
    search_eof:
	add	di,bx
	mov	[bx+si],ah
	call	init_file
	jmp	search_test

_TEXT	ENDS

PPROC	searchp, fname:dword
local	path[256]:byte
	push	si
	push	di
	les	bx,fname
	test	bx,bx
	jz	searchp_nul
	mov	al,es:[bx]
	test	al,al
	jz	searchp_nul
	cmp	al,'.'
	jz	searchp_nul
	cmp	al,'\'
	jne	searchp_do
    searchp_nul:
	xor	ax,ax
	cwd
	jmp	searchp_end
    searchp_do:
	push	ss
	lea	ax,[bp-128]
	push	ax
	push	es
	push	bx
	call	strcpy
	push	dx
	push	ax
	mov	si,ax
	call	strlen
	mov	di,si
	add	di,ax
	cmp	ax,5
	jb	searchp_02
	mov	al,'.'
	cmp	al,[di-4]
	jne	searchp_02
	mov	ax,si
	mov	dx,ss
	call	isexec
	cmp	ax,2
	jae	searchp_03
    searchp_02:
	mov	byte ptr [di],'.'
	inc	di
	mov	bx,di
	mov	word ptr [di],'OC'
	mov	word ptr [di+2],'M'
	call	file_exist
	je	searchp_05
	mov	word ptr [di],'XE'
	mov	word ptr [di+2],'E'
	call	file_exist
	je	searchp_05
	call	search
	jnz	searchp_end
	mov	word ptr [bx],'OC'
	mov	word ptr [bx+2],'M'
	jmp	searchp_04
    searchp_03:
	call	file_exist
	je	searchp_05
    searchp_04:
	call	search
	jmp	searchp_end
    searchp_05:
	mov	ax,bp
	sub	ax,128
	mov	dx,ss
    searchp_end:
	pop	di
	pop	si
	ret
PEND	searchp

	END
