# ASMIX Commands and Utilities
# (c) Sergei Zubkov (Cubbi) 1999-2001
#
# rmdir.s - remove empty directories
#
# $Id: rmdir.s,v 1.2 2001/10/16 19:23:57 cubbi Exp $

	.globl _start
_start:
	cld
	xorl	%esi,%esi
	popl	%ecx
	decl	%ecx
	jz	error_noop
	popl	%edi
arguments_cycle:
	pushl	%ecx
	movl	$-1,%ecx
	xorl	%eax,%eax
	repnz
	scasb
	popl	%ecx
	cmpb	$1,end_of_options
	je	skip_options
	cmpw	$0x2D2D,(%edi)
	jne	not_dashdash
	movb	$1,end_of_options
	loop	arguments_cycle
not_dashdash:
	cmpw	$0x702D,(%edi)
	jne	option_p_absent
	incl	%esi
	loop	arguments_cycle
option_p_absent:
	cmpb    $0x2D,(%edi)
	je      error_unk
skip_options:
	jcxz    error_noop
	pushl	%ecx
	pushl	%edi
	movl	$-1,%ecx
	xorl	%eax,%eax
	repnz
	scasb
	movl	%edi,%ebp
	popl	%edi
	popl	%ecx
dirname_loop:
	pushl	%edi
	movl	$79,%eax
	pushl	%ebx
	lcall	$7,$0
	testl   %eax,%eax
	jnz     error_exit
	testl	%esi,%esi
	je	dumb_mode
	pushl	%ecx
	pushl	%edi
	movl	$-1,%ecx
	xorl	%eax,%eax
	repnz
	scasb
	movl	%edi,%ecx
	subl	(%esp),%ecx
	incl	%ecx
	std
	movb	$0x2F,%al
	repnz
	scasb
	cld
	cmpl	(%esp),%edi
	jb	finished_path
	incl	%edi
	movb	$0,(%edi)
	popl	%edi
	popl	%ecx
	jmp	dirname_loop
finished_path:
	popl	%edi
	popl	%ecx
	movl	%ebp,%edi
	decl	%edi
dumb_mode:
	decl	%ecx
	jnz	arguments_cycle
exit:
	xorl	%ebx,%ebx
real_exit:
	pushl	%ebx
	movl	$1,%eax	
	pushl	%ebx
	lcall	$7,$0
error_exit:
	movl    $1,%ebx
	jmp     real_exit
error_noop:
	movl    $3,%ebx
	jmp     real_exit
error_unk:
	movl    $2,%ebx
	jmp     real_exit

	.data
end_of_options:
	.byte	0
option_p:
	.byte	0
