include	clib.inc
include	string.inc
include	fblk.inc
include	dos.inc
include	wsub.inc

_TEXT	SEGMENT

WSSORT_XCHG:
	mov	cx,2
	mov	bx,di
	shl     bx,cl
	mov	ax,[bp+8]
	shl     ax,cl
	mov	cx,ax
    ifdef __386__
	mov	eax,[si+bx]
	add	bx,cx
	mov	edx,[si+bx]
	mov	[si+bx],eax
	sub	bx,cx
	mov	[si+bx],edx
    else
	mov	ax,[si+bx]
	mov	dx,[si+bx+2]
	add	bx,cx
	xchg	ax,[si+bx]
	xchg	dx,[si+bx+2]
	sub	bx,cx
	mov	[si+bx],ax
	mov	[si+bx+2],dx
    endif
	ret

WSSORT_NAMEA:
	mov	bx,di

WSSORT_NAMEAB:
	shl	bx,2
    ifdef __386__
	mov	eax,[si+bx]
    else
	mov	ax,[si+bx]
	mov	dx,[si+bx+2]
    endif
	add	ax,fb_name
	ret

WSSORT_NAMEB:
	mov	bx,[bp+8]
	add	bx,di
	jmp	WSSORT_NAMEAB

WSSORT_UPDIR:
	call	WSSORT_NAMEA
	mov	cx,offset cp_dotdot
    ifdef __386__
	invoke	stricmp,eax,ss::cx
    else
	invoke	stricmp,dx::ax,ss::cx
    endif
	ret

WSSORT_SUBDIR:
	mov	bx,di
	mov	cx,2
	shl	bx,cl
	les	bx,[si+bx]
	mov	ax,es:[bx]
	mov	bx,[bp+8]
	add	bx,di
	shl	bx,cl
	les	bx,[si+bx]
	mov	dx,es:[bx]
	and	dx,_A_SUBDIR
	and	ax,_A_SUBDIR
	jz	CMPSUBD_01
	test	dx,dx
	jz	CMPSUBD_02
    CMPSUBD_00:
	or	al,1
	ret
    CMPSUBD_01:
	test	dx,dx
	jz	CMPSUBD_00
	call	WSSORT_XCHG
    CMPSUBD_02:
	xor	ax,ax
	ret

WSSORT_NAME:
	call	WSSORT_NAMEA
    ifdef __386__
	push	eax
	call	WSSORT_NAMEB
	push	eax
    else
	push	dx
	push	ax
	call	WSSORT_NAMEB
	push	dx
	push	ax
    endif
	call	stricmp
	ret

WSSORT_DATE:
	mov	bx,di
	mov	cx,2
	shl	bx,cl
	les	bx,[si+bx]
	mov	ax,es:[bx.fb_time]
	mov	dx,es:[bx.fb_date]
	mov	bx,[bp+8]
	add	bx,di
	shl	bx,cl
	les	bx,[si+bx]
	cmp	dx,es:[bx.fb_date]
	je	WSSORT_TIME
	ret

WSSORT_TIME:
	cmp	ax,es:[bx.fb_time]
	ret

WSSORT_SIZE:
	mov	bx,di
	mov	cl,2
	shl	bx,cl
	les	bx,[si+bx]
    ifdef __386__
	mov	eax,es:[bx.fb_size]
	mov	bx,[bp+8]
	add	bx,di
	shl	bx,cl
	les	bx,[si+bx]
	cmp	eax,es:[bx.fb_size]
    else
	mov	ax,word ptr es:[bx.fb_size]
	mov	dx,word ptr es:[bx.fb_size+2]
	mov	bx,[bp+8]
	add	bx,di
	shl	bx,cl
	les	bx,[si+bx]
	cmp	dx,word ptr es:[bx.fb_size+2]
	jne	@F
	cmp	ax,word ptr es:[bx.fb_size]
      @@:
    endif
	ret

WSSORT_TYPE:
	call	WSSORT_NAMEB
    ifdef __386__
	invoke	strrchr, eax, '.'
	push	dx
	push	ax
	call	WSSORT_NAMEA
	invoke	strrchr, eax, '.'
    else
	invoke	strrchr, dx::ax, '.'
	push	dx
	push	ax
	call	WSSORT_NAMEA
	invoke	strrchr, dx::ax, '.'
    endif
	pop	cx
	pop	bx
	jnz	WSSORT_TYPE_01
	test	cx,cx
	jz	WSSORT_TYPE_03
	dec	ax
    WSSORT_TYPE_00:
	test	ax,ax
	ret
    WSSORT_TYPE_01:
	test	bx,bx
	jnz	WSSORT_TYPE_03
	mov	ax,1
	jmp	WSSORT_TYPE_00
    WSSORT_TYPE_03:
	invoke	stricmp, dx::ax, bx::cx
	jmp	WSSORT_TYPE_00

wssortfcb proc pascal uses ds si di fcb:DWORD, count:WORD, flag:WORD
	lds	si,fcb
	mov	ax,count
	shr	ax,1
    wssortfcb_00:
	mov	[bp+8],ax
	test	ax,ax
	jz	wssortfcb_11
	mov	[bp+10],ax
    wssortfcb_01:
	mov	ax,[bp+10]
	cmp	ax,[bp+6]
	jae	wssortfcb_10
	mov	di,ax
	sub	di,[bp+8]
    wssortfcb_02:
	cmp	di,0
	jl	wssortfcb_09
	call	WSSORT_UPDIR
	jz	wssortfcb_09
	call	WSSORT_SUBDIR
	jz	wssortfcb_05
	mov	ax,[bp+4]
	test	dx,dx
	jz	wssortfcb_12
	test	ax,_W_SORTSUB
	jz	wssortfcb_03
    wssortfcb_12:
	and	ax,_W_SORTSIZE
	cmp	ax,_W_SORTTYPE
	je	wssortfcb_08
	cmp	ax,_W_SORTDATE
	je	wssortfcb_07
	cmp	ax,_W_SORTSIZE
	je	wssortfcb_06
    wssortfcb_03:
	call	WSSORT_NAME
	jl	wssortfcb_09
    wssortfcb_04:
	call	WSSORT_XCHG
    wssortfcb_05:
	sub	di,[bp+8]
	jmp	wssortfcb_02
    wssortfcb_06:
	call	WSSORT_SIZE
	je	wssortfcb_03
	jb	wssortfcb_04
	jmp	wssortfcb_09
    wssortfcb_07:
	call	WSSORT_DATE
	je	wssortfcb_03
	jb	wssortfcb_04
	jmp	wssortfcb_09
    wssortfcb_08:
	call	WSSORT_TYPE
	jg	wssortfcb_04
	jnl	wssortfcb_03
    wssortfcb_09:
	inc	word ptr [bp+10]
	jmp	wssortfcb_01
    wssortfcb_10:
	mov	ax,[bp+8]
	shr	ax,1
	jmp	wssortfcb_00
    wssortfcb_11:
	ret
wssortfcb endp

_TEXT	ENDS

PPROC	wssort, wsub:dword
	les	bx,wsub
	cmp	es:[bx.ws_count],2
	jl	@F
	pushm	es:[bx.ws_fcb]
	push	es:[bx.ws_count]
	les	bx,es:[bx.ws_flag]
	push	word ptr es:[bx]
	call	wssortfcb
    @@:
	ret
PEND	wssort

	END
