NEW
  AUTO 3,1
				.LIST OFF
				.OP	65C02
				.OR	$2000
				.TF bin/stat
*--------------------------------------
				.INB inc/macros.i
				.INB inc/a2osx.i
				.INB inc/mli.i
				.INB inc/mli.e.i
*--------------------------------------
				.DUMMY
				.OR ZPBIN
ZS.START
ArgIndex		.BS 1
ArgFile			.BS 1
ZPPtr1			.BS 2

bAll			.BS 1
bAccess			.BS 1
bMod			.BS 1
bUID			.BS 1
bGID			.BS 1
bSize			.BS 1
bBlocks			.BS 1
bCTime			.BS 1
bMTime			.BS 1
bATime			.BS 1
	
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 #S.PS.F.EVENT		S.PS.F
				.DA #0
				.DA CS.END-CS.START		CS
				.DA DS.END-DS.START		DS
				.DA #64					SS
				.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.USAGE		.DA MSG.USAGE
L.MSG.Filename	.DA MSG.Filename
L.MSG.Access	.DA MSG.Access
L.MSG.Mod		.DA MSG.Mod
L.MSG.UIDGIDSIZE 	.DA MSG.UIDGIDSIZE
L.FMT.DateTime	.DA FMT.DateTime
L.MSG.CMATimes	.DA MSG.CMATimes
L.MSG.D			.DA MSG.D
L.MSG.U			.DA MSG.U
				.DA 0
*--------------------------------------
CS.INIT			clc
				rts
*--------------------------------------
CS.RUN			dec bAll			

.1				inc ArgIndex
				lda ArgIndex
				>SYSCALL ArgV
				bcs .3
				>STYA ZPPtr1

				lda (ZPPtr1)
				cmp #'-'
				bne .2

				jsr CS.RUN.CheckOpt
				bcc .1

.99				>PUSHW L.MSG.USAGE
				>PUSHBI 0
				>SYSCALL PrintF
				lda #E.SYN
				sec
				rts

.2				lda ArgFile
				bne .99

				lda ArgIndex
				sta ArgFile
				bra .1
				
.3				lda ArgFile
				beq .99
				
				>SYSCALL ArgV
				>PUSHYA
				>PUSHW pData
				>SYSCALL Stat
				bcs .9
				
				jsr CS.RUN.CONVERT
				
				bit bAll
				bmi .7
				
				jsr CS.RUN.PRINT
				bcs .9

				bra .8

.7				jsr CS.RUN.DUMP
				bcs .9
				
.8				lda #0
				sec
.9				
CS.RUN.RTS		rts
*--------------------------------------
CS.RUN.PRINT	bit bAccess
				bpl .1
				
				>LDYA.G ATTR
				>SYSCALL PutS
				bcs CS.RUN.RTS
				
.1				bit bMod
				bpl .2
				
				>LDYA.G MOD
				>SYSCALL PutS
				bcs CS.RUN.RTS
				
.2				bit bUID
				bpl .3
				
				>PUSHW L.MSG.D
				>PUSHW.G StatBuf+S.STAT.UID
				>PUSHBI 2
				>SYSCALL PrintF
.9				bcs CS.RUN.RTS
				
.3				bit bGID
				bpl .4
				
				>PUSHW L.MSG.D
				>PUSHW.G StatBuf+S.STAT.GID
				>PUSHBI 2
				>SYSCALL PrintF
				bcs .9

.4				bit bSize
				bpl .5
				
				>PUSHW L.MSG.U
				>PUSHL.G StatBuf+S.STAT.SIZE
				>PUSHBI 4
				>SYSCALL PrintF
				bcs CS.RUN.PRINT.RTS

.5				bit bBlocks
				bpl CS.RUN.PRINT2
				
				>PUSHW L.MSG.U
				>PUSHL.G StatBuf+S.STAT.BLOCKS
				>PUSHBI 4
				>SYSCALL PrintF
				bcs CS.RUN.PRINT.RTS

CS.RUN.PRINT2	bit bCTime
				bpl .7
				
				>LDYA.G TIME.Create
				>SYSCALL PutS
				bcs .9
				
.7				bit bMTime
				bpl .8
				
				>LDYA.G TIME.Mod
				>SYSCALL PutS
				bcs .9
				
.8				bit bATime
				bpl .9
				
				>LDYA.G TIME.Access
				>SYSCALL PutS
				
.9
CS.RUN.PRINT.RTS
				rts
*--------------------------------------
CS.RUN.DUMP		lda ArgFile
				>SYSCALL ArgV
				>PUSHYA
				>PUSHWI 0				Allocate
				>SYSCALL RealPath
				bcs CS.RUN.PRINT.RTS

				>STYA ZPPtr1
				phx
				>PUSHW L.MSG.Filename
				>PUSHW ZPPtr1
				>PUSHBI 2
				>SYSCALL PrintF
				pla
				>SYSCALL FreeMem
				
				>PUSHW L.MSG.Access
				>PUSHEA.G ATTR
				>PUSHBI 2
				>SYSCALL PrintF

				>PUSHW L.MSG.Mod
				>PUSHEA.G MOD
				>PUSHBI 2
				>SYSCALL PrintF
				
				>PUSHW L.MSG.UIDGIDSIZE
				>PUSHW.G StatBuf+S.STAT.UID
				>PUSHW.G StatBuf+S.STAT.GID
				>PUSHL.G StatBuf+S.STAT.SIZE
				>PUSHL.G StatBuf+S.STAT.BLOCKS
				>PUSHBI 12
				>SYSCALL PrintF
				
				>PUSHW L.MSG.CMATimes
				>PUSHEA.G TIME.Create
				>PUSHEA.G TIME.Mod
				>PUSHEA.G TIME.Access
				>PUSHBI 6
				>SYSCALL PrintF
				
				rts				
*--------------------------------------
CS.RUN.CONVERT	jsr Access2CSTR
				jsr Mod2CSTR
				jmp Times2CSTR
*--------------------------------------
Access2CSTR		ldy #S.STAT.P.ACCESS
				lda (pData),y

				ldy #ATTR+15
				ldx #15

.1				lsr
				pha
				bcc .2
				
				lda ATTRS,x
				sta (pData),y
				dex
				dey
				lda ATTRS,x
				sta (pData),y
				bra .3
				
.2				lda #'-'
				sta (pData),y
				dex
				dey
				sta (pData),y
				
.3				pla
				
				dey
				dex
				bpl .1

				rts
*--------------------------------------
Mod2CSTR		ldy #S.STAT.MODE+1
				lda (pData),y
				lsr
				php
				
				lsr
				lsr
				lsr
				tax
				lda TYPES,x
				>STA.G MOD
				
				plp
				
				ldx #8

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

				ldy #MOD+1

.1				pha
				lda #'-'
				bcc .2

				lda ACCESS,x

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

				rts
*--------------------------------------
Times2CSTR		ldy #TIME.Access-1		Set NODATE as default
				jsr CS.RUN.PrintAMCTime.NODATE
				ldy #TIME.Mod-1			Set NODATE as default
				jsr CS.RUN.PrintAMCTime.NODATE
				ldy #TIME.Create-1		Set NODATE as default
				jsr CS.RUN.PrintAMCTime.NODATE

				>LDA.G StatBuf+S.STAT.ATIME+S.TIME.MONTH
				beq .1					Month=0.....invalid date/time

				>PUSHEA.G TIME.Access
				>PUSHW L.FMT.DateTime
				>PUSHEA.G StatBuf+S.STAT.ATIME
				>SYSCALL StrFTime

.1				>LDA.G StatBuf+S.STAT.MTIME+S.TIME.MONTH
				beq .2					Month=0.....invalid date/time

				>PUSHEA.G TIME.Mod
				>PUSHW L.FMT.DateTime
				>PUSHEA.G StatBuf+S.STAT.MTIME
				>SYSCALL StrFTime

.2				>LDA.G StatBuf+S.STAT.CTIME+S.TIME.MONTH
				beq .8					Month=0.....invalid date/time

				>PUSHEA.G TIME.Create
				>PUSHW L.FMT.DateTime
				>PUSHEA.G StatBuf+S.STAT.CTIME
				>SYSCALL StrFTime

.8				rts
*--------------------------------------
CS.RUN.PrintAMCTime.NODATE
				ldx #$ff

.1				inx
				iny
				lda MSG.NODATE,x
				sta (pData),y
				bne .1

				rts
*--------------------------------------
CS.QUIT			
.8				clc
				rts
*--------------------------------------
CS.DOEVENT		sec
				rts
*--------------------------------------
CS.RUN.CheckOpt	ldy #1 
				lda (ZPPtr1),y

				ldx #OptionVars-OptionList-1

.2				cmp OptionList,x
				beq .3

				dex
				bpl .2

				sec
				rts

.3				ldy OptionVars,x
				lda #$ff
				sta 0,y

				stz bAll

				clc
				rts
*--------------------------------------
CS.END
*--------------------------------------
OptionList		.AS "AaMmUuGgSsBbCcMmLl"
OptionVars		.DA #bAccess,#bAccess
				.DA #bMod,#bMod
				.DA #bUID,#bUID
				.DA #bGID,#bGID
				.DA #bSize,#bSize
				.DA #bBlocks,#bBlocks
				.DA #bCTime,#bCTime
				.DA #bMTime,#bMTime
				.DA #bATime,#bATime
ATTRS			.AS "DeReBn----InWrRd"
TYPES			.AS "-dbclssp"
ACCESS			.AS "xwrxwrxwr"
MSG.NODATE		.AZ "<No-Date>"
FMT.DateTime	.AZ "%d-%b-%y %H:%M:%S"
*--------------------------------------
MSG.USAGE		.AS "Usage : STAT File\r\n"
				.AS "   -A : Access Flags\r\n"
				.AS "   -M : Mod Flags\r\n"
				.AS "   -U : UID\r\n"
				.AS "   -G : GID\r\n"
				.AS "   -S : Size in bytes\r\n"
				.AS "   -B : Size in blocks\r\n"
				.AS "   -C : Creation date/time\r\n"
				.AS "   -M : Modification date/time\r\n"
				.AZ "   -L : Last access date/time\r\n"
*--------------------------------------
MSG.Filename	.AZ "Filename : %s\r\n"
MSG.Access		.AZ "Access   : %s\r\n"
MSG.Mod			.AZ "Mod      : %s\r\n"
MSG.UIDGIDSIZE	.AS "UID      : %10D\r\n"
				.AS "GID      : %10D\r\n"
				.AS "Size     : %10u\r\n"
				.AZ "Blocks   : %10u\r\n"
MSG.CMATimes	.AS "Created  : %s\r\n"
				.AS "Modified : %s\r\n"
				.AZ "Accessed : %s\r\n"
MSG.D			.AZ "%D\r\n"
MSG.U			.AZ "%u\r\n"
*--------------------------------------
				.DUMMY
				.OR 0
DS.START
StatBuf			.BS S.STAT
ATTR			.BS 17
MOD				.BS 11					drwxrwxrwx + \0
TIME.Access		.BS 20
TIME.Mod		.BS 20
TIME.Create		.BS 20
DS.END
				.ED
*--------------------------------------
MAN
SAVE usr/src/bin/stat.s
ASM
