strlen proc pascal uses di string:dword
	cld
	sub al,al
	les di,string
	mov cx,-1
	repne scasb
	mov ax,cx
	not ax
	dec ax
	ret
strlen endp

strcpy proc pascal uses ds si di s1:dword, s2:dword
	cld
	sub al,al
	mov cx,-1
	les di,s2
	repne scasb
	les di,s1
	lds si,s2
	not cx
	shr cx,1
	rep movsw
	jc strcpy_mov
    strcpy_end:
	mov dx,word ptr s1+2
	mov ax,word ptr s1
	ret
    strcpy_mov:
	movsb
	jmp strcpy_end
strcpy endp

strrchr proc pascal uses di s1:dword, char:word
	les di,s1
	xor ax,ax
	cwd
	cld
	mov cx,-1
	repne scasb
	not cx
	dec di
	std
	mov al,byte ptr char
	repne scasb
	mov al,0
	jne @F
	mov dx,word ptr S1+2
	mov ax,di
	inc ax
      @@:
	test ax,ax
	ret
strrchr endp

strcat proc pascal uses ds si di s1:dword, s2:dword
	cld
	sub al,al
	les di,s1
	lds si,s2
	mov cx,-1
	repne scasb
	dec di
      @@:
	mov al,[si]
	mov es:[di],al
	inc di
	inc si
	test al,al
	jnz @B
	mov dx,word ptr s1+2
	mov ax,word ptr s1
	ret
strcat endp

stricmp proc pascal uses si di ds s1:dword, s2:dword
	les di,s1
	lds si,s2
	mov al,-1
    @@:
	test al,al
	jz @F
	mov al,[si]
	mov ah,es:[di]
	inc si
	inc di
	cmp ah,al
	je @B
	cmpaxi
	je @B
	sbb al,al
	sbb al,-1
      @@:
	cbw
	ret
stricmp endp

strncmp proc pascal uses si di ds S1:dword, S2:dword, count:word
	mov cx,count
	.if cx
	    mov dx,cx
	    les di,S1
	    mov si,di
	    xor ax,ax
	    repne scasb
	    neg cx
	    add cx,dx
	    mov di,si
	    lds si,S2
	    repe cmpsb
	    mov al,[si-1]
	    xor cx,cx
	    cmp al,es:[di-1]
	    ja @F
	    je strncmp_end
	    sub cx,2
	  @@:
	    not cx
	.endif
    strncmp_end:
	mov ax,cx
	ret
strncmp endp

strnicmp proc pascal uses si di ds S1:dword, S2:dword, count:word
    lds si,S1
    les di,S2
    mov cx,count
    inc cx
      @@:
	dec cx
	jz @F
	mov al,[si]
	mov ah,es:[di]
	inc si
	inc di
	test al,al
	jz @F
	cmp ah,al
	je @B
	cmpaxi
	je @B
      @@:
	sub al,ah
	cbw
	ret
strnicmp endp

strfcat proc pascal uses ds si di s1:dword, s2:dword, s3:dword
	cld
	sub al,al
	les di,s1
	mov dx,di
	mov cx,-1
	.if word ptr s2 ; path
	    les di,s2
	    repne scasb
	    les di,s1
	    lds si,s2
	    not cx
	    rep movsb
	.else
	    repne scasb
	.endif
	dec di
	.if di != dx
	    mov byte ptr es:[di],'\'
	    mov al,es:[di-1]
	    .if al == '\' || al == '/'
		dec di
	    .endif
	    inc di
	.endif
	lds si,s3
      @@:
	mov al,[si]
	mov es:[di],al
	inc di
	inc si
	test al,al
	jnz @B
	mov es:[di],al
	mov ax,word ptr s1
	mov dx,word ptr s1+2
	ret
strfcat endp

unixconvert proc
	les	bx,[bp+4]
	jmp	unixconvert_01
    unixconvert_00:
	cmp	al,ah
	jne	unixconvert_01
	mov	es:[bx-1],dl
    unixconvert_01:
	mov	al,es:[bx]
	inc	bx
	or	al,al
	jnz	unixconvert_00
	mov	dx,es
	mov	ax,[bp+4]
	ret
unixconvert endp

unixtodos proc pascal, string:dword
	mov	ah,'/'
	mov	dl,'\'
	call	unixconvert
	ret
unixtodos endp

dostounix proc pascal, string:dword
	mov	ah,'\'
	mov	dl,'/'
	call	unixconvert
	ret
dostounix endp

cmpwarg	proc pascal, filep:dword, maskp:dword
	push	ds
	push	si
	push	di
	lds	si,filep
	les	di,maskp
    cmpwarg_next:
	mov	al,[si]
	mov	ah,es:[di]
	inc	si
	inc	di
	cmp	ah,'*'
	je	cmpwarg_star
	or	al,al
	jz	cmpwarg_zero
	or	ah,ah
	jz	cmpwarg_zero
	cmp	ah,'?'
	je	cmpwarg_next
	cmp	ah,'.'
	je	cmpwarg_04
	cmp	al,'.'
	je	cmpwarg_fail
	or	ax,2020h
	cmp	ah,al
	je	cmpwarg_next
	jmp	cmpwarg_fail
    cmpwarg_zero:
	or	ax,ax
	jnz	cmpwarg_fail
    cmpwarg_ok:
	mov	ax,1
    cmpwarg_end:
	or	ax,ax
	pop	di
	pop	si
	pop	ds
	ret
    cmpwarg_04:
	cmp	al,'.'
	je	cmpwarg_next
    cmpwarg_fail:
	xor	ax,ax
	jmp	cmpwarg_end
    cmpwarg_star:
	mov	ah,es:[di]
	or	ah,ah
	jz	cmpwarg_ok	; find '.' --> '*' | '*abc' | '*.txt'
	inc	di
	cmp	ah,'.'
	jne	cmpwarg_star
	xor	dx,dx		; found
    cmpwarg_type:
	or	al,al
	jz	cmpwarg_test
	cmp	al,ah
	je	cmpwarg_dxsi
    cmpwarg_lods:
	mov	al,[si]
	inc	si
	jmp	cmpwarg_type
    cmpwarg_dxsi:
	mov	dx,si
	jmp	cmpwarg_lods
    cmpwarg_test:
	or	dx,dx
	mov	si,dx
	jnz	cmpwarg_next
	mov	ah,es:[di]
	inc	di
	cmp	ah,'*'
	je	cmpwarg_star
	jmp	cmpwarg_zero
cmpwarg	endp

