include	clib.inc
include	dos.inc
include	string.inc
include	conio.inc
include	wsub.inc
include	fblk.inc
include	errno.inc
include	disk.inc
ifdef __DZ__
 include plugins/plugins.inc
endif
ifdef __ZIP__
 __ARC__ = 1
else
ifdef __DLL__
 __ARC__ = 1
endif
endif
	public	cp_emaxfb

_DATA	SEGMENT

cp_emaxfb label byte
	db "This subdirectory contains more",10
	db "than %d files/directories.",10
	db "Only %d of the files is read.",0

_DATA	ENDS

_TEXT	SEGMENT

wsvolid proc pascal uses si di wsub:dword
local	path[WMAXPATH]:byte
local	ff:S_FFBLK
	lea di,ff
	les bx,wsub
	invoke strfcat, addr path, es:[bx].ws_path, addr cp_stdmask
	invoke findfirst, dx::ax, ss::di, _A_VOLID
    	.if ax == 0 && [di].ff_attrib & _A_VOLID
	    mov al,path
	    or  al,20h
	    sub al,'a'
	    assert al,MAXDRIVES,jb,"wsread"
	    mov ah,SIZE S_DISK
	    mul	ah
	    mov	si,ax
	    add	si,offset drvinfo
	    mov	[si].di_name[2],' '
	    invoke strcpy, addr [si].di_name[3], addr [di].ff_name
	.endif
	ret
wsvolid endp

wsreadff proc pascal uses si di wsub:dword, attrib:word
local	path[WMAXPATH]:byte
local	ff:S_FFBLK
	invoke wsvolid, wsub
	lea di,ff
	les bx,wsub
	invoke strfcat, addr path, es:[bx].ws_path, addr cp_stdmask
	invoke findfirst, dx::ax, ss::di, attrib
	.while !ax
	    .if !(byte ptr [di].ff_attrib & _A_VOLID)
		mov ax,'.'
		.break .if [di].ff_name != al
		.if [di].ff_name[1] != ah
		    .break .if word ptr [di].ff_name[1] != ax
		.endif
	    .endif
	    invoke findnext, ss::di
	.endw
	sub ax,'.'
	.while !ax
	    .if !(byte ptr [di].ff_attrib & _A_SUBDIR)
		les bx,wsub
		invoke cmpwarg, addr ss:[di].ff_name, es:[bx].ws_mask
		jz @F
	    .endif
	    les bx,wsub
	    les bx,es:[bx].ws_flag
	    mov ax,es:[bx]
	    and ax,7F00h
	    invoke fballocff, ss::di, ax
	    .break .if !ax
	    les bx,wsub
	    mov cx,es:[bx].ws_count
	    les bx,es:[bx].ws_fcb
	    shl cx,2
	    add bx,cx
	    stom es:[bx]
	    les bx,wsub
	    inc es:[bx].ws_count
	    mov ax,es:[bx].ws_count
	    .break .if es:[bx].ws_maxfb <= ax
	  @@:
	    invoke findnext, ss::di
	.endw
	les bx,wsub
	mov ax,es:[bx]
	ret
wsreadff endp

ifdef __LFN__

wsreadwf proc pascal uses si di wsub:dword, attrib:word
local	path[WMAXPATH]:byte
local	wf:S_WFBLK
	invoke wsvolid,wsub
	lea di,wf
	les bx,wsub
	invoke strfcat,addr path,es:[bx].ws_path,addr cp_stdmask
	invoke wfindfirst,addr path,ss::di,attrib
	mov si,ax
	inc ax
	.if ax
	    xor ax,ax
	    .while !ax
	    	.if !(byte ptr [di] & _A_VOLID)
		    mov ax,'.'
		    .break .if [di].wf_name != al
		    .if [di].wf_name[1] != ah
		    	.break .if word ptr [di].wf_name[1] != ax
		    .endif
		.endif
		invoke wfindnext,ss::di,si
	    .endw
	    sub ax,'.'
	    .while !ax
	    	.if !(byte ptr [di] & _A_SUBDIR)
		    les bx,wsub
		    invoke cmpwarg,addr ss:[di].wf_name,es:[bx].ws_mask
		    jz @F
		.endif
		les bx,wsub
		les bx,es:[bx].ws_flag
		mov ax,es:[bx]
		and ax,7F00h
		invoke fballocwf,ss::di,ax
		.break .if !ax
	    	les bx,wsub
	    	mov cx,es:[bx].ws_count
	    	les bx,es:[bx].ws_fcb
	    	shl cx,2
	    	add bx,cx
	    	stom es:[bx]
	    	les bx,wsub
	    	inc es:[bx].ws_count
	    	mov ax,es:[bx].ws_count
	    	.break .if es:[bx].ws_maxfb <= ax
	      @@:
		invoke wfindnext, ss::di, si
	    .endw
	    invoke wcloseff,si
	.endif
	les bx,wsub
	mov ax,es:[bx]
	ret
wsreadwf endp

endif

wsreadsub proc pascal wsub:dword
	invoke wsfree,wsub
  ifdef __ROT__
	les bx,wsub
	les bx,es:[bx].ws_path
	.if word ptr es:[bx+2] == '\'
	    invoke fbupdir,_A_ROOTDIR
	.else
	    invoke fbupdir,0
	.endif
  else
	invoke fbupdir,0
  endif
	.if ax
	    les bx,wsub
	    inc es:[bx].ws_count
	    les bx,es:[bx].ws_fcb
	    stom es:[bx]
	    les bx,wsub
	    les bx,es:[bx].ws_mask
	    mov ax,'*'
	    .if es:[bx] == ah
		mov es:[bx+2],ax
		mov es:[bx],al
		mov byte ptr es:[bx+1],'.'
	    .endif
	    les bx,wsub
	    les bx,es:[bx].ws_flag
	    mov dx,es:[bx]
	    mov ax,_A_ALLFILES
	    .if !(dx & _W_HIDDEN)
		mov ax,_A_STDFILES
	    .endif
	  ifdef __LFN__
	    .if dx & _W_LONGNAME
		invoke wsreadwf,wsub,ax
	    .else
		invoke wsreadff,wsub,ax
	    .endif
	  else
	    invoke wsreadff,wsub,ax
	  endif
	.endif
	les bx,wsub
	mov ax,es:[bx]
	ret
wsreadsub endp

wsread proc dist pascal public wsub:dword
	push	si
	xor	si,si
  ifdef __ARC__
	les	bx,wsub
	les	bx,es:[bx].ws_flag
	mov	ax,es:[bx]
	and	ax,_W_ARCHIVE
	jz	wsread_local
   ifdef __ZIP__
	cmp	ax,_W_ARCHZIP
	jne	@F
	invoke	wzipread, wsub
	jmp	wsread_test
      @@:
   endif
   ifdef __DLL__
	and	ax,_W_ARCHEXT
	jz	wsread_local
	mov	ah,_DLL_READ
	mov	bx,word ptr wsub
	int	DLL
	jc	wsread_fail
   endif
    wsread_test:
	cmp	ax,ER_READARCH
	jne	wsread_sort
    wsread_fail:
	les	bx,wsub
	les	bx,es:[bx].ws_flag
    wsread_local:
	mov	ax,es:[bx]
	and	ax,not (_W_ARCHIVE or _W_ROOTDIR)
	mov	es:[bx],ax
  endif
	invoke	wsreadsub,wsub
    wsread_sort:
	mov	si,ax
	cmp	si,1
	jle	@F
	les	bx,wsub
	les	bx,es:[bx].ws_flag
	test	word ptr es:[bx],_W_NOSORT
	jnz	@F
	invoke	wssort,wsub
      @@:
	les	bx,wsub
	mov	ax,es:[bx].ws_maxfb
	cmp	ax,si
	jnz	wsread_end
	push	ax
	push	ax
	mov	ax,offset cp_warning
	mov	dx,offset cp_emaxfb
	call	@stdmsg
	add	sp,4
    wsread_end:
	mov	ax,si
	pop	si
	ret
wsread	endp

_TEXT	ENDS

	END
