# ASMIX Commands and Utilities
# (c) Sergei Zubkov (Cubbi) 1999-2001
#
# basename.s - return non-directory portion of a pathname
#
# $Id: basename.s,v 1.3 2001/10/16 19:23:36 cubbi Exp $

       .toc
        .globl __start
.csect __start[DS]
__start:
        .long .__start,TOC[tc0],0
.csect .text[PR]
.__start:
	cmpwi	3,1
	beq	error_exit
	lwz	16,4(4)
	mr	7,16
	subi	7,7,1
trail_strlen_loop:
	lbzu	8,1(7)
	cmpwi	8,0
	bne	trail_strlen_loop
rem_trailing:
	subi	7,7,1
	lbz	8,0(7)
	cmpwi	8,0x2F
	beq	rem_trailing
	addi	7,7,1
	mr	19,7 
	cmpw	7,16
	bne	something_besides_slashes
	li	8,0x2F0A
	sth	8,0(7)
	li	5,2
	bl	imm_output
something_besides_slashes:
z_scan:
	lbzu	8,-1(7)
	cmpwi	8,0x2F
	beq	found_slash
	cmpw	7,16
	bne	z_scan
	bl	end_of_parse
found_slash:
	addi	7,7,1
	mr	16,7
	
end_of_parse:
	cmpwi	3,2
	beq	skip_suffix
	lwz	17,8(4)
	subi	7,17,1
suffix_strlen_loop:
	lbzu	8,1(7)
	cmpwi	8,0
	bne	suffix_strlen_loop
	mr	18,19
subi 17,17,1
suffix_match_loop:
	lbzu	8,-1(7) 
	lbzu	9,-1(18)
	cmpw	7,17
	beq	1f
	cmpw	8,9
	beq	suffix_match_loop
	bl	skip_suffix
1:	addi	18,18,1
	cmpw	16,18
	beq	skip_suffix
	mr	19,18
skip_suffix:
	li	7,10
	stb	7,0(19) 
	sub	5,19,16
	addi	5,5,1
imm_output:
	mr	4,16
	li	3,1
	li	0,26 
	sc
exit:
	li	3,0   
real_exit:
        li      0,9
        sc
error_exit:
        li      3,1  
	bl	real_exit
