# ASMIX Commands and Utilities
# (c) Sergei Zubkov (Cubbi) 1999-2001
#
# pwd.s - print working directory
#
# $Id: pwd.s,v 1.2 2001/10/03 18:17:23 cubbi Exp $

	.toc
pt_buffer_a:	.tc	pt_buffer_l[TC],pt_buffer_l
pt_buffer_o:	.tc	pt_buffer[TC],pt_buffer
up_buffer_a:	.tc	up_buffer[TC],up_buffer
st_stat_a:	.tc	st_stat[TC],st_stat
root_path_a:	.tc	root_path[TC],root_path
dirent_a:	.tc	dirent[TC],dirent
	.globl __start
.csect __start[DS]
__start:
	.long .__start, TOC[tc0], 0
.csect .text[PR]
.__start:
	li	26,1
	lwz	16,pt_buffer_a(2)
	lwz	16,0(16)
	li	21,0x0A
        stb	21,0(16)
	lwz	17,up_buffer_a(2)
	li	21,0x2E00
	sth	21,0(17)
	lwz	4,st_stat_a(2)
	lwz	3,root_path_a(2)
        li	0,38
	sc
	cmpwi	3,0
	bne	error_exit
	lwz	18,st_stat_a(2)
	lwz	18,0(18)
	lwz	19,st_stat_a(2)
	lwz	19,4(19)
main_cycpwd_cycle:
        lwz	4,st_stat_a(2)
	lwz	3,up_buffer_a(2)
        li	0,39
	sc
	cmpwi	3,0
	bne	error_exit
	lwz	20,st_stat_a(2)
        lwz	20,0(20)
        lwz	22,st_stat_a(2)
	lwz	22,4(22)
	cmpw	0,18,20
	bne	cycpwd_not_finished
	cmpw	0,19,22
	bne	cycpwd_not_finished
        subi	16,16,1
	li	21,0x2F
        stb	21,0(16)
        bl	cycpwd_write
cycpwd_not_finished:
	li	21,0x2E2E
	sth	21,0(17)
	addi	17,17,2
	stb	0,0(17)
	li	4,0
	lwz	3,up_buffer_a(2)
	li	0,28
	sc
	cmpwi	3,0
	blt	error_exit
	mr	23,3
	lwz	4,st_stat_a(2)
	li	0,40
	sc	
	cmpwi	3,0
	bne	error_exit
	li	21,0x2F
	stb	21,0(17)
	addi	17,17,1
	li	24,0
readdir_cycle:
	cmpwi	24,0
	beq	time_to_getdent
	li	4,0
	lhz	3,4(24)
        add	24,3,24
	li	4,0
	lhz	4,4(24)	
	cmpw	0,4,25
	bnl	time_to_getdent
	lhz	4,4(24)
	cmpw	0,4,0
	bne	skip_getdent
time_to_getdent:
	li	5,dirent_l
	lwz	4,dirent_a(2)
	mr	3,23
	li	0,267
	sc
	mr	25,3
	cmpwi	3,0
	beq	notfound
	lwz	24,dirent_a(2)
skip_getdent:
	li	4,0
	mr	5,17
	subi	5,5,1
	mr	6,24
	addi	6,6,7
	lhz	4,6(24)
	addi	4,4,1
rep_loop:
	lbzu	3,1(6)
	stbu	3,1(5)
	subi	4,4,1
	cmpwi	4,0
	bgt	rep_loop
	lwz	4,st_stat_a(2)
	lwz	3,up_buffer_a(2)
	li	0,39
	sc
	cmpwi	3,0
    	bne	readdir_cycle
	lwz	3,st_stat_a(2)
	lwz	3,0(3)
	cmpw	0,3,20
	bne	readdir_cycle
	lwz	3,st_stat_a(2)
        lwz	3,4(3)
	cmpw	0,3,22 	
	bne	readdir_cycle
	stb	0,0(17)
	cmpwi	26,1
	beq	not_first
	subi	16,16,1
	li	21,0x2F
	stb	21,0(16)
not_first:
	li	26,0
	lhz	3,6(24)
	sub	16,16,3 
	li	4,0 
	mr	5,16
	subi	5,5,1
	mr	6,24
	addi	6,6,7 
	mr	4,3 
rep_loop2:
	lbzu	3,1(6)
	stbu	3,1(5)
	subi	4,4,1
	cmpwi	4,0
	bgt	rep_loop2
	mr	3,23 
	li	0,27
	sc
	bl     main_cycpwd_cycle
cycpwd_write:
	lwz	5,pt_buffer_a(2)
	lwz	5,0(5)
	sub	5,5,16
	addi	5,5,1
	mr	4,16
	li	3,1
	li	0,26
	sc
exit:
	li	3,1
	li	0,9
	sc
exit_errno:
exit_ENOENT:
notfound:
error_exit:
	li	3,1
	li	0,9
	sc

	.csect .data[RW]
pwd_path:
        .ascii  "."
	.byte	0
root_path:
	.ascii  "/"
	.byte	0
pt_buffer_l:
	.long pt_buffer+1024-1

        .comm  st_stat, 52,4
        .comm  pt_buffer,1024,1
        .comm  up_buffer,1024,1
        .comm  bpt,4,4
        .comm  dirent,4096,1
dirent_l=4096

