NEW
  AUTO 3,1
				.LIST OFF
				.OP	65C02
				.OR	$2000
				.TF bin/ls
*--------------------------------------
				.INB inc/macros.i
				.INB inc/a2osx.i
				.INB inc/kernel.i
				.INB inc/mli.i
				.INB inc/mli.e.i
*--------------------------------------
X.ENTER.SUBDIR	.EQ 1
X.COPY.TO.DEST	.EQ 0
X.DELETE.SOURCE	.EQ 0
*--------------------------------------
MAX.COL			.EQ 4
*--------------------------------------
				.DUMMY
				.OR ZPBIN
ZS.START
ZPPtr1			.BS 2
ZPPtr2			.BS 2
ZPFileName		.BS 2
ZPFileStat		.BS 2

ZPPW			.BS 2

bCRLF			.BS 1
hLineBuf		.BS 1
ZPLineBuf		.BS 2
ZS.END
				.ED
*--------------------------------------
*			File Header (16 Bytes)
*--------------------------------------
CS.START		cld
				jmp (.1,x)
				.DA #$61				6502,Level 1 (65c02)
				.DA #1					BIN Layout Version 1
				.DA #0					S.PS.F.EVENT
				.DA #0
				.DA CS.END-CS.START		Code Size (without Constants)
				.DA DS.END-DS.START		Data SegmentSize
				.DA #64					Stack Size
				.DA #ZS.END-ZS.START	Zero Page Size
				.DA 0
*--------------------------------------
* Relocation Table
*--------------------------------------
.1				.DA CS.INIT
				.DA CS.RUN
				.DA CS.DOEVENT		
				.DA	CS.QUIT
L.MSG.REG		.DA MSG.REG
L.MSG.REGEXT	.DA MSG.REGEXT
L.MSG.DIR		.DA MSG.DIR
L.MSG.DIREXT	.DA MSG.DIREXT
L.MSG.BDEV		.DA MSG.BDEV
L.MSG.BDEVEXT	.DA MSG.BDEVEXT
L.MSG.ENTER		.DA MSG.ENTER
L.PRODOS.FT.TXT	.DA PRODOS.FT.TXT
L.FMT.Date		.DA FMT.Date
L.FMT.Time		.DA FMT.Time
L.FMT.string6	.DA FMT.string6
L.FMT.int16		.DA FMT.int16
J.CS.RUN.PRINT	.DA CS.RUN.PRINT.REG
				.DA CS.RUN.PRINT.DIR
				.DA CS.RUN.PRINT.CDEV
				.DA CS.RUN.PRINT.BDEV
				.DA CS.RUN.PRINT.CDEV
				.DA CS.RUN.PRINT.CDEV
				.DA CS.RUN.PRINT.CDEV
				.DA CS.RUN.PRINT.CDEV
				.DA 0
*--------------------------------------
CS.INIT			clc
				rts
*--------------------------------------
CS.RUN
.1				>INC.G ArgCount
				>SYSCALL ArgV
				bcs .6

				>STYA ZPPtr1
				lda (ZPPtr1)
				cmp #'-'
				bne .4

				ldy #1
				lda (ZPPtr1),y
				
				ldx #OptionVars-OptionList-1
				
.2				cmp OptionList,x
				beq .3
				dex
				bpl .2
				
				lda #E.SYN
				sec
				rts
				
.3				ldy OptionVars,x
				lda #$80
				sta (pData),y
				bra .1

.4				>LDYA ZPPtr1
				jsr InitSrcDirYA
				bcc .1					scan for any other args
.9				rts

.6				>LDA.G index			do we have a Source dir ?
				bne .8

				ldy #S.PS.hCWD
				lda (pPS),y
				>SYSCALL GetMemPtr
				jsr InitSrcDirYA
				bcs .9
				
.8				>PUSHEA.G TIME.SysTime
				>SYSCALL time
				
				>LDYAI S.PW
				>SYSCALL GetMem
				bcs .9
				>STYA ZPPW
				txa
				>STA.G hPW
				
				>LDYAI 256
				>SYSCALL GetMem
				bcs .9
				>STYA ZPLineBuf
				stx hLineBuf
				
				ldy #S.PS.hStdOut
				lda (pPS),y
				tax
				lda OF.Table.hFD-1,x
				>SYSCALL GetMemPtr
				>STYA ZPPtr1

				lda (ZPPtr1)			S.FD.T
				beq CS.RUN.LOOP			S.FD.T.REG

				cmp #S.FD.T.PIPE
				beq CS.RUN.LOOP
				
				dec bCRLF
*--------------------------------------
CS.RUN.LOOP		ldy #S.PS.hStdIn
				lda (pPS),y
				>SYSCALL feof
				bcs .99					I/O error

				tay
				bne .2					no char

				>SYSCALL GetChar
				cmp #$03				Ctrl-C
				bne .1					Abort....

				rts						CS

.1				cmp #$13				Ctrl-S
				bne .2

				>LDA.G bPause
				eor	#$ff
				sta (pData),y
				bne CS.RUN.LOOP

.2				>LDA.G bPause
				bne CS.RUN.LOOP

				jsr GetEntry
				bcs .9

				jsr FilterMatch
				bcs .8					no match, skip....

				>LDA.G bAllmostAll
				bmi .4

				lda (ZPFileName)
				cmp #'.'
				beq .8

.4				ldy #S.STAT.MODE+1
				lda (ZPFileStat),y

				and #$70
				lsr
				lsr
				lsr
				tax
				jsr CS.RUN.PRINT.JMP
				bcs .99

.8				jsr GetNextEntry

				bcc .92

.9				ldy #bLong
				lda (pData),y
				bmi .91

				jsr CS.RUN.NewLine
				bcs .99

.91				jsr LeaveSubDir
				bcs .98

				jsr BasePath..
				jsr GetNextEntry
.92				jmp CS.RUN.LOOP
.98				lda #0
				sec
.99				rts
*--------------------------------------
CS.RUN.PRINT.JMP
				jmp (J.CS.RUN.PRINT,x)
*--------------------------------------
CS.RUN.PRINT.REG
				ldy #bLong
				lda (pData),y
				bmi .1
				
				>LDA.G bColumn
				bmi .23

				>LDA.G bFullPath
				bpl .22
				
				jsr CS.RUN.PRINT.SRCPATH
				
.23				>LDYA ZPFileName
				>SYSCALL PutS
				rts

.22				>PUSHW L.MSG.REG
				>PUSHW ZPFileName
				>PUSHBI 2
				>SYSCALL PrintF
				bcs .9
				jmp CS.RUN.UpdateColCnt
*--------------------------------------
.1				
*				>PUSHW ZPLineBuf
				>PUSHW L.MSG.REGEXT
	
				jsr Mod2CSTR			(2)

				jsr CS.RUN.PushUidGid	(4)

				ldy #S.STAT.SIZE+3
				ldx #4

.2				lda (ZPFileStat),y
				>PUSHA
				dey
				dex
				bne .2					(4)

				jsr CS.RUN.PUSHDATES	(4)

				ldy #S.STAT.P.TYPE
				lda (ZPFileStat),y
				jsr FileType2PSTR
				>PUSHYA					(2)

				ldy #S.STAT.P.AUXTYPE+1
				lda (ZPFileStat),y
				>PUSHA
				dey
				lda (ZPFileStat),y
				>PUSHA					(2)

				>PUSHW ZPFileName		(2)

				>PUSHBI 20
				>SYSCALL PrintF
				bcs .9
				
				jmp CS.RUN.NewLine.1
	
.9				rts
*--------------------------------------
CS.RUN.PRINT.DIR
				>LDA.G bLong
				bmi .4

				>LDA.G bColumn
				bmi .23
				>LDA.G bFullPath
				bpl .22

				jsr CS.RUN.PRINT.SRCPATH

.23				>LDYA ZPFileName
				>SYSCALL PutS
				bcs .9
				>LDA.G bRecurse
				bpl .8
				lda (ZPFileName)
				cmp #'.'
				beq .8
				>LDYA ZPFileName
				jmp EnterSubDirYA		
.8				clc
.9				rts

.22				>PUSHW L.MSG.DIR
				>PUSHW ZPFileName
				>PUSHBI 2
				>SYSCALL PrintF
				bcs .9

				ldy #0

.1				iny
				lda (ZPFileName),y
				bne .1

.11				iny
				cpy #19
				bcs .2

				lda #C.SPACE
				phy
				>SYSCALL PutChar
				ply
				bcc .11
				rts

.2				jsr CS.RUN.UpdateColCnt
				bcc .5
				rts
*--------------------------------------
.4				>PUSHW L.MSG.DIREXT
	
				jsr Mod2CSTR
				
				jsr CS.RUN.PushUidGid
				jsr CS.RUN.PUSHDATES
				>PUSHW ZPFileName
				>PUSHBI 12
				>SYSCALL PrintF
				bcs .90
				
				jsr CS.RUN.NewLine.1
				bcs .90
				
.5				>LDA.G bRecurse
				bpl .80
				lda (ZPFileName)
				cmp #'.'
				beq .80
				jsr CS.RUN.NewLine
				bcs .90
				>LDYA ZPFileName
				jsr EnterSubDirYA
				bcs .90
				jmp CS.RUN.ENTER.MSG

.80				clc
.90				rts
*--------------------------------------
CS.RUN.PRINT.CDEV
				clc
				rts
*--------------------------------------
CS.RUN.PRINT.BDEV
				>LDA.G bLong
				bmi .20

				>LDA.G bColumn
				bmi .23
				>LDA.G bFullPath
				bpl .22

				jsr CS.RUN.PRINT.SRCPATH

.23				>LDYA ZPFileName
				>SYSCALL PutS
				bcs .9
				>LDA.G bRecurse
				bpl .8
				lda (ZPFileName)
				cmp #'.'
				beq .8
				>LDYA ZPFileName
				jmp EnterSubDirYA
.8				clc
.9				rts				

.22				>PUSHW L.MSG.BDEV
				>PUSHW ZPFileName
				>PUSHBI 2
				>SYSCALL PrintF
				bcs .9

				ldy #0

.1				iny
				lda (ZPFileName),y
				bne .1

.11				iny
				cpy #19
				bcs .2

				lda #C.SPACE
				phy
				>SYSCALL PutChar
				ply
				bcc .11
				rts

.2				jsr CS.RUN.UpdateColCnt
*				bcc .5
				rts
*--------------------------------------
.20				>PUSHW L.MSG.BDEVEXT
				>PUSHW ZPFileName
				
				ldy #S.STAT.P.SLOT
				>PUSHB (ZPFileStat),y
				iny						DRIVE
				>PUSHB (ZPFileStat),y

				ldy #S.STAT.BLOCKS+1
				>PUSHB (ZPFileStat),y
				dey
				>PUSHB (ZPFileStat),y

				ldy #S.STAT.P.DEVBLOCKS+1
				>PUSHB (ZPFileStat),y
				dey
				>PUSHB (ZPFileStat),y
				
				>PUSHBI 8

				>SYSCALL PrintF
				bcs .90
				
				jsr CS.RUN.NewLine.1
				bcs .90

				ldy #bRecurse
				lda (pData),y
				bpl .80
				>LDYA ZPFileName
				jsr EnterSubDirYA
				bcs .90
				jmp CS.RUN.ENTER.MSG
.80				clc
.90				rts
*--------------------------------------
CS.RUN.PRINT.SRCPATH
				ldy #hSrcBasePath
				lda (pData),y
				>SYSCALL GetMemPtr
				>PUSHYA
				>PUSHBI 0
				>SYSCALL PrintF
				rts
*--------------------------------------
CS.RUN.PushUidGid
				ldy #S.STAT.UID
				lda (ZPFileStat),y
				>PUSHA
				>PUSHW ZPPW
				>SYSCALL GetPWUID
				bcs .2

				>PUSHEA.G USER
				jsr CS.RUN.PushUidGidStr
				bra .3
				
.2				>PUSHEA.G USER
				>PUSHW L.FMT.int16
				
				ldy #S.STAT.UID+1
				lda (ZPFileStat),y
				>PUSHA
				dey
				lda (ZPFileStat),y
				>PUSHA
				>PUSHBI 2
				>SYSCALL sprintf

.3				ldy #S.STAT.GID
				lda (ZPFileStat),y
				>PUSHA
				>PUSHW ZPPW
				>SYSCALL GetGRGID
				bcs .5
				
				>PUSHEA.G GROUP
				jsr CS.RUN.PushUidGidStr
				
				bra .8
				
.5				>PUSHEA.G GROUP
				>PUSHW L.FMT.int16
				
				ldy #S.STAT.GID+1
				lda (ZPFileStat),y
				>PUSHA
				dey
				lda (ZPFileStat),y
				>PUSHA
				>PUSHBI 2
				>SYSCALL sprintf
				
.8				>PUSHEA.G USER
				>PUSHEA.G GROUP
				clc
				rts
				
CS.RUN.PushUidGidStr
				>PUSHW L.FMT.string6
				>PUSHW ZPPW
				>PUSHBI 2
				>SYSCALL sprintf
				rts
*--------------------------------------
CS.RUN.PUSHDATES
				ldy #TIME.Mod-1			Set NODATE as default
				jsr CS.RUN.NODATES.INIT
				
				ldy #TIME.Create-1		Set NODATE as default
				jsr CS.RUN.NODATES.INIT
				
				ldy #S.STAT.MTIME+S.TIME.MONTH
				lda (ZPFileStat),y
				beq .1					Month=0.....invalid date/time

				>PUSHEA.G TIME.Mod
				
				lda ZPFileStat
				clc
				adc #S.STAT.MTIME
				tay
				lda ZPFileStat+1
				adc #0
				jsr CS.RUN.DATEFMT.SELECT
				>SYSCALL StrFTime

.1				ldy #S.STAT.CTIME+S.TIME.MONTH
				lda (ZPFileStat),y
				beq .2					Month=0.....invalid date/time

				>PUSHEA.G TIME.Create
				
				lda ZPFileStat
				clc
				adc #S.STAT.CTIME
				tay
				lda ZPFileStat+1
				adc #0
				jsr CS.RUN.DATEFMT.SELECT
				>SYSCALL StrFTime

.2				>PUSHEA.G TIME.Mod
				>PUSHEA.G TIME.Create
				rts
*--------------------------------------
CS.RUN.NODATES.INIT
				ldx #$ff

.1				inx
				iny
				lda MSG.NODATE,x
				sta (pData),y
				bne .1
				
				rts
*--------------------------------------
CS.RUN.DATEFMT.SELECT
				>STYA ZPPtr1

				ldy #S.TIME.CENTURY
				lda (ZPPtr1),y
				ldy #TIME.SysTime+S.TIME.CENTURY
				cmp (pData),y
				bne .9

				ldy #S.TIME.YEAR
				lda (ZPPtr1),y
				ldy #TIME.SysTime+S.TIME.YEAR
				cmp (pData),y
				bne .9

				ldy #S.TIME.MONTH
				lda (ZPPtr1),y
				ldy #TIME.SysTime+S.TIME.MONTH
				cmp (pData),y
				bne .9

				ldy #S.TIME.DAY
				lda (ZPPtr1),y
				ldy #TIME.SysTime+S.TIME.DAY
				cmp (pData),y
				bne .9
				
.8				>PUSHW L.FMT.Time
				>PUSHW ZPPtr1
				rts
				
.9				>PUSHW L.FMT.Date
				>PUSHW ZPPtr1
				rts
*--------------------------------------
CS.RUN.UpdateColCnt
				>INC.G ColCount
				cmp #MAX.COL
				bne CS.RUN.NewLine.8
	
				lda #0
				sta (pData),y
				bra CS.RUN.NewLine.1
*--------------------------------------
CS.RUN.ENTER.MSG
				>PUSHW L.MSG.ENTER
				ldy #hSrcBasePath
				lda (pData),y
				>SYSCALL GetMemPtr
				>PUSHYA
				>PUSHBI 2
				
				>SYSCALL PrintF
				bcc	CS.RUN.NewLine.1
				rts
*--------------------------------------
CS.RUN.NewLine	>LDA.G ColCount
				beq CS.RUN.NewLine.8
CS.RUN.NewLine.1
				lda #C.CR
				>SYSCALL putchar
				
				bit bCRLF
				bpl CS.RUN.NewLine.8
				
				lda #C.LF
				>SYSCALL putchar

CS.RUN.NewLine.8
				clc
				rts
*--------------------------------------
CS.DOEVENT		sec
				rts
*--------------------------------------
CS.QUIT			jsr LeaveSubDir
				bcc CS.QUIT
				>LDA.G hFilter
				beq .3

				>SYSCALL FreeMem

.3				>LDA.G hPW
				beq .4
				
				>SYSCALL FreeMem
				
.4				lda hLineBuf
				beq .8
				
				>SYSCALL FreeMem
				
.8				clc
				rts
*--------------------------------------
FileType2PSTR	ldy #0
.1				cmp PRODOS.FT.HEX,y
				beq .8
				iny
				cpy #PRODOS.FT.TXT-PRODOS.FT.HEX
				bne .1
				pha
				lsr
				lsr
				lsr
				lsr
				ora #$30
				cmp #$3A
				bcc .2
				adc #6
.2				sta PRODOS.FT.DFLT+2
				pla
				and #$0F
				ora #$30
				cmp #$3A
				bcc .3
				adc #6
.3				sta PRODOS.FT.DFLT+3
.8				tya
				asl
				asl						CC
				adc L.PRODOS.FT.TXT
				tay
				lda L.PRODOS.FT.TXT+1
				adc #0
				rts
*--------------------------------------
Mod2CSTR		ldy #S.STAT.MODE+1
				lda (ZPFileStat),y
				lsr
				php						C = RU

				lsr
				lsr
				lsr
				tax
				lda TYPES,x
				>STA.G MOD

				plp

				ldx #8

				ldy #S.STAT.MODE
				lda (ZPFileStat),y

				ldy #MOD+1

.1				pha
				lda #'-'
				bcc .2
				
				lda ACCESS,x

.2				sta (pData),y
				
				iny
				pla
				asl
				
				dex
				bpl .1

				>PUSHEA.G MOD
				rts
*--------------------------------------
				.INB usr/src/bin/x.fileenum.s
*--------------------------------------
CS.END
*--------------------------------------
OptionList		.AS "ACFLRacflr"
OptionVars		.DA #bAllmostAll,#bColumn,#bFullPath,#bLong,#bRecurse,#bAllmostAll,#bColumn,#bFullPath,#bLong,#bRecurse
*--------------------------------------
MSG.REGEXT		.AZ "%s %6s %6s %10u %s %s %S %H %s"
MSG.REG			.AZ "%19s"
MSG.DIREXT		.AZ "%s %6s %6s            %s %s  <dir>   %s"
MSG.DIR			.AZ "\e[32m%s/\e[0m"
MSG.BDEVEXT		.AZ "/%15s S%dD%d Blocks Used:%5D Total:%5D"
MSG.BDEV		.AZ "\e[32m%s/\e[0m"
MSG.ENTER		.AZ "Directory:%s"
*--------------------------------------
PRODOS.FT.HEX	.HS 0406FAFCFDE2CBCCCFFF
PRODOS.FT.TXT	>PSTR "txt"
				>PSTR "bin"
				>PSTR "asm"
				>PSTR "bas"
				>PSTR "var"
				>PSTR "atk"
				>PSTR "pix"
				>PSTR "fon"
				>PSTR "pak"
				>PSTR "sys"
PRODOS.FT.DFLT	>PSTR "$  "
MSG.NODATE		.AZ "<no-date>"
FMT.Date		.AZ "%d-%b-%y"
FMT.Time		.AZ "%H:%M:%S "
FMT.string6		.AZ "%6s"
FMT.int16		.AZ "%6D"
TYPES			.AS "-dbclssp"
ACCESS			.AS "xwrxwrxwr"
*--------------------------------------
				.DUMMY
				.OR 0
DS.START
ArgCount		.BS 1

MOD				.BS 11					drwxrwxrwx0
USER			.BS 7
GROUP			.BS 7

TIME.Create		.BS 20
TIME.Mod		.BS 20
TIME.SysTime	.BS S.TIME

bPause			.BS 1
bAllmostAll		.BS 1
bColumn			.BS 1
bFullPath		.BS 1
bLong			.BS 1
bRecurse		.BS 1

ColCount		.BS 1
hPW				.BS 1

				.INB usr/src/bin/x.fileenum.g
DS.END
				.ED
*--------------------------------------
MAN
SAVE usr/src/bin/ls.s
ASM
