# ASMIX Commands and Utilities
# (c) Sergei Zubkov (Cubbi) 1999
#
# dirname.s - return the directory portion of pathname
#
# $Id: dirname.s,v 1.1 2001/10/04 19:42:15 cubbi Exp $

	.globl _start
_start:
	cld
	popl	%ecx
	decl	%ecx
	jz	error_exit
	popl	%edi
arguments_cycle:
	pushl	%ecx
	movl	$-1,%ecx
	xorl	%eax,%eax
	repne
	scasb
	popl	%ecx
	call	strlen
	testl	%ecx,%ecx
	jne	not_empty_string_start
	movw	$0x002E,(%edi)
	jmp	end_of_parse
not_empty_string_start:
	call	remove_trailing
	call	strlen
	testl	%ecx,%ecx
	jne	not_empty_string
	movw	$0x002F,(%edi)
	jmp	end_of_parse
not_empty_string:
	call	strlen
	pushl	%ecx
	pushl	%edi
	incl	%ecx
	movb	$0x2F,%al
	repne
	scasb
	popl	%edi
	testl	%ecx,%ecx
	jne	some_slashes_left
	movw	$0x002E,(%edi)
	jmp	end_of_parse
some_slashes_left:
	popl	%ecx
	pushl	%edi
	call	strlen
	addl	%ecx,%edi
	decl	%edi
remove_trailing_letters:
	cmpb	$0x2F,(%edi)
	je	no_more_trailing_letters
	movb	$0,(%edi)
	decl	%edi
	jmp	remove_trailing_letters
no_more_trailing_letters:
	popl	%edi
	call	remove_trailing
	call	strlen
	testl	%ecx,%ecx
	jne	not_empty_string_again
	movw	$0x002F,(%edi)
	jmp	end_of_parse
not_empty_string_again:
end_of_parse:
	call	strlen
	movb	$012,(%edi,%ecx,1)
	incl	%ecx
	pushl	%ecx
	pushl	%edi
	pushl	$1
        movl    $4,%eax
	pushl	%ebx
	lcall	$7,$0
	add	$16,%esp
exit:
	xorl	%ebx,%ebx
real_exit:
	pushl	%ebx
	movl	$1,%eax
	pushl	%ebx
	lcall	$7,$0
error_exit:
	xorl	%ebx,%ebx
	incl	%ebx
	jmp real_exit
strlen:
	pushl	%edi
	movl	$-1,%ecx
	xorl	%eax,%eax
	repne
	scasb
	movl	%edi,%ecx
	subl	(%esp),%ecx
	decl	%ecx
	popl	%edi
	ret
remove_trailing:
	pushl	%edi
	call	strlen
	addl	%ecx,%edi
	decl	%edi
rem_trailing:
	cmpb	$0x2F,(%edi)	
	jne	no_more_trailing
	movb	$0,(%edi)
	decl	%edi
	jmp	rem_trailing
no_more_trailing:
	popl	%edi
	ret
