NEW
  AUTO 3,1
*/--------------------------------------
* # ChTyp
* Change The type of a ProDOS File
* ## C
* `int chtyp(const char *filepath, short int filetype);`
* ## ASM
* **In:**
* `>PUSHW filepath`
* `>PUSHB filetype`
* `>SYSCALL chtyp`
* ## RETURN VALUE
*\--------------------------------------
K.ChTyp			jsr PFT.CheckPath2
				bcs .99
				>PULLB .1+1

				inc pStack
				inc pStack

				>MLICALL MLIGETFILEINFO
				bcs .9

.1				lda #$ff
				sta K.MLI.PARAMS+S.FI.T
				>MLICALL MLISETFILEINFO
.9				rts

.99				>RET 1					discard filetype
*--------------------------------------
* # ChMod
* change permissions of a file
* ## C
* `int chmod(const char *pathname, int mode);`
* ## ASM
* **In:**
* `>PUSHW pathname`
* `>PUSHW mode`
* `>SYSCALL chmod`
* ## RETURN VALUE
*--------------------------------------
* only 777-555
*--------------------------------------
K.ChMod			jsr PFT.CheckPath2
				bcs K.ChMod.RET4

				>PULLW K.S.STAT+S.STAT.MODE

				inc pStack
				inc pStack

				lda K.S.STAT+S.STAT.MODE+1
				and /S.STAT.MODE.RU
				beq .9
				lda K.S.STAT+S.STAT.MODE
				and #S.STAT.MODE.XO+S.STAT.MODE.RO+S.STAT.MODE.XG+S.STAT.MODE.RG+S.STAT.MODE.XU+S.STAT.MODE.RU
				cmp #S.STAT.MODE.XO+S.STAT.MODE.RO+S.STAT.MODE.XG+S.STAT.MODE.RG+S.STAT.MODE.XU+S.STAT.MODE.RU
				bne .9
				>MLICALL MLIGETFILEINFO
				bcs .90
				lda K.MLI.PARAMS+S.FI.A
				and #$3D					not S.FI.A.RN+S.FI.A.D+S.FI.A.W
				ora #S.FI.A.R
				tax
				lda K.S.STAT+S.STAT.MODE
				tay
				bit #S.STAT.MODE.WG
				beq .1

				txa
				ora #S.FI.A.RN
				tax
				tya
.1				bit #S.STAT.MODE.WU
				beq .2
				txa
				ora #S.FI.A.D
				tax
				tya
.2				bit #S.STAT.MODE.WO
				beq .3
				txa
				ora #S.FI.A.W
				tax
.3				stx K.MLI.PARAMS+S.FI.A

				>MLICALL MLISETFILEINFO
				rts
.9				lda #E.SYN
				sec
.90				rts
K.ChMod.RET4	>RET 4					discard statbuf
*--------------------------------------
* # FStat
* Return information about a hFILE
* ## C
* `int fstat(short int hFILE, struct stat *statbuf);`
* ## ASM
* **In:**
* `>PUSHB hFILE`
* `>PUSHW statbuf`
* `>SYSCALL fstat`
* ## RETURN VALUE
*--------------------------------------
K.FStat			>PULLW K.Stat.Store.2+1
				>PULLA
				jsr PFT.CheckNodeA
				bcs K.Stat.Store.RTS

				tax
				lda OF.Table.hPath-1,x
				jsr K.GetMemPtr
				jsr PFT.YAToMLIPATH

				jsr SHARED.Stat.I
				bcs K.Stat.Store.RTS

				lda (pFD)				#S.FD.T
				bne K.Stat.Store

				ldy #S.FD.REG.REF
				lda (pFD),y
				sta K.MLI.PARAMS+1

				>MLICALL MLIGETEOF
				bcs K.Stat.Store.RTS

				ldx #2

.1				lda K.MLI.PARAMS+2,x
				sta K.S.STAT+S.STAT.SIZE,x
				dex
				bpl .1
				
K.Stat.Store	ldy #S.STAT-1

K.Stat.Store.1	lda K.S.STAT,y
K.Stat.Store.2	sta $ffff,y				SELF MODIFIED
				dey
				bpl K.Stat.Store.1

*				clc
K.Stat.Store.RTS
				rts
*/--------------------------------------
* # Stat
* Return information about a file
* ## C
* `int stat(const char *pathname, struct stat *statbuf);`
* ## ASM
* **In:**
* `>PUSHW pathname`
* `>PUSHW statbuf`
* `>SYSCALL stat`
* ## RETURN VALUE
*\--------------------------------------
K.Stat			jsr PFT.CheckPath2
				bcs K.ChMod.RET4		go discard statbuf

				>PULLW K.Stat.Store.2+1

				inc pStack
				inc pStack

				jsr SHARED.Stat.I
				bcc K.Stat.Store

				rts
*/-------------------------------------
* # MKDir
* create a directory
* ## C
* `int mkdir(const char *pathname, int mode);`
* ## ASM
* **In:** 
* `>PUSHW pathname`
* `>PUSHW mode`
* `>SYSCALL mkdir`
* ## RETURN VALUE
* CC : success
* CS : error
* A = EC
*\-------------------------------------
K.MKDir			jsr PFT.CheckPath2
				bcs .9

*				lda (pStack)			TODO : mode
				
				lda #S.FI.T.DIR
				sta K.MLI.PARAMS+4
				stz K.MLI.PARAMS+5
				stz K.MLI.PARAMS+6
				lda #S.FI.ST.DIR
				jsr IO.MLI.CREATE
				
.9				>RET 4
*/--------------------------------------
* # MKFIFO
* return a pathname to a new FIFO
* ## C
* `int mkfifo( char *pathname, int mode );`
* ## ASM
* **In:** 
* `>PUSHW mode`
* `>LDYA pathname`
* `>SYSCALL mkfifo`
* ## RETURN VALUE
* CC = OK, CS = ERROR
* A = hFD
*\--------------------------------------
K.MKFIFO		>STYA ZPPtr1			pathname buffer
				>PULLW K.S.STAT+S.STAT.MODE
				>LDYAI 512
				jsr K.pipe
				bcs K.Stat.Store.RTS

				ldy #0
.1				lda DEV.FIFO,y
				sta (ZPPtr1),y
				iny
				cpy #9
				bne .1

				lda IO.hFD
				jsr MATH.AToHexAX
				sta (ZPPtr1),y
				txa
				iny
				sta (ZPPtr1),y
				iny
				lda #0
				sta (ZPPtr1),y
				bra K.MkNod.I
*/--------------------------------------
* # MkNod
* Create a special or ordinary file.
* (CDEV, BDEV, DSOCKS, SSOCK, PIPE)
* ## C
* `hFILE mknod(const char *pathname, int mode, hFD fd);`
* ## ASM
* **In:** 
* `>PUSHB fd`
* `>PUSHW mode`
* `>LDYA pathname`
* `>SYSCALL mknod`
* ## RETURN VALUE
* CC = OK, CS = ERROR
* A = hFILE
*\--------------------------------------
K.MkNod 		>STYA ZPPtr1			pathname
				>PULLW K.S.STAT+S.STAT.MODE
				>PULLB IO.hFD
			
K.MkNod.I		ldx #2
.1				lda Nod.Table.hPath-2,x
				beq .2
				inx
				cpx #K.Nod.MAX+2
				bne .1
				lda #E.OOH
*				sec
				rts
.2				stx .3+1				Store hFILE
				>LDYA ZPPtr1
				ldx #SYS.StrDup
				jsr K.SYSCALL2
				bcs .9
				txa
.3				ldx #$ff				SELF MODIFIED
				sta Nod.Table.hPath-2,x
				lda IO.hFD
				sta Nod.Table.hFD-2,x
				txa						hNODE
				clc
.9				rts
*/--------------------------------------
* # pipe
* ## C
* `hFD pipe(int size);`
* ## ASM
* `>LDYA size`
* `>SYSCALL pipe`
* ## RETURN VALUE
* CC = OK, CS = ERROR
* A = hFD
*\--------------------------------------
K.Pipe			sta .1+1				SIZE HI

				>SYSCALL2 NewStkObj
				bcs .99

				stx .8+1				save PIPE buffer

				ldx #2
				jsr IO.MkFD
				bcs .9

.1				lda #$ff				SELF MODIFIED
				ldy #S.FD.PIPE.Free+1
				sta (pFD),y

				ldy #S.FD.PIPE.Size+1
				sta (pFD),y
				ldy #S.FD.PIPE.hMem
.8				lda #$ff				Self Modified
				sta (pFD),y
				txa						hFD
*				clc
				rts
.9				pha						save error code
				lda .8+1				get back PIPE buffer

				>SYSCALL2 FreeStkObj

				pla
				sec
.99				
K.Pipe.RTS		rts
*--------------------------------------
SHARED.Stat.I	jsr SHARED.ClrStat
				
				>MLICALL MLIGETFILEINFO
				bcs K.Pipe.RTS

				lda K.MLI.PARAMS+S.FI.A
				jsr SHARED.Stat.Access2Mode

				ldx #SHARED.GFI.Cnt

.3				ldy SHARED.GFI.Src-1,x
				lda K.MLI.PARAMS,y
				ldy SHARED.Stat.Dst-1,x
				sta K.S.STAT,y
				dex
				bne .3
				
				>LDYAI K.MLI.PARAMS+S.FI.CDATE
				ldx #S.STAT.CTIME
				jsr SHARED.PTime2StatYAX

				>LDYAI K.MLI.PARAMS+S.FI.MDATE
				ldx #S.STAT.MTIME
				jmp SHARED.PTime2StatYAX
*--------------------------------------
SHARED.GFI2Stat

				clc
				rts
*--------------------------------------
SHARED.DirEnt2Stat
				jsr SHARED.ClrStat

				lda (ZPPtr3)
				and #$F0
				cmp #$D0					Directory ?
				bne .1
				
				lda /S.STAT.MODE.DIR
				sta K.S.STAT+S.STAT.MODE+1

.1				ldx #SHARED.DirEnt.Cnt

.2				ldy SHARED.DirEnt.Src-1,x
				lda (ZPPtr3),y
				ldy SHARED.Stat.Dst-1,x
				sta K.S.STAT,y
				dex
				bne .2
				
				ldy #$1E				ProDOS Access
				lda (ZPPtr3),y

				jsr SHARED.Stat.Access2Mode

				lda #$18				creation Date/time
				ldx #S.STAT.CTIME
				jsr SHARED.PTime2StatAX

				lda #$21				mod Date/time
				ldx #S.STAT.MTIME
				jmp SHARED.PTime2StatAX
*--------------------------------------
SHARED.ClrStat	ldx #S.STAT-1

.1				stz K.S.STAT,x
				dex
				bpl .1
				
				lda CORE.FSID
				sta K.S.STAT+S.STAT.FSID

				rts
*--------------------------------------
SHARED.Stat.Access2Mode
				and #S.FI.A.FULL
				cmp #S.FI.A.FULL
				beq .7
				tay
				ldx #S.STAT.MODE.RO+S.STAT.MODE.RG+S.STAT.MODE.RU+S.STAT.MODE.XO+S.STAT.MODE.XG+S.STAT.MODE.XU

				bit #S.FI.A.W
				beq .1
				txa
				ora #S.STAT.MODE.WO
				tax
				tya

.1				asl						D in C

				bpl .2					RN in N
				txa
				ora #S.STAT.MODE.WG
				tax
.2				txa
				bcc .8
				ora #S.STAT.MODE.WU
				.HS 2C					BIT ABS
.7				lda #S.STAT.MODE.FO+S.STAT.MODE.FG+S.STAT.MODE.FU  rwxrwxrwx
.8				sta K.S.STAT+S.STAT.MODE

				lda /S.STAT.MODE.RU				??????R??
				ora K.S.STAT+S.STAT.MODE+1		preserve DIR
				sta K.S.STAT+S.STAT.MODE+1

				rts
*--------------------------------------
SHARED.PTime2StatAX
				clc
				adc ZPPtr3
				tay
				lda ZPPtr3+1
				adc #0
SHARED.PTime2StatYAX
				>PUSHYA

				txa
				clc
				adc #K.S.STAT
				tay
				lda /K.S.STAT
				adc #0
				>PUSHYA
				>SYSCALL2 PTime2Time
				rts
*--------------------------------------
SHARED.GFI.Src	.DA #S.FI.T,#S.FI.AUXTYPE,#S.FI.AUXTYPE+1
				.DA #S.FI.UBLKS,#S.FI.UBLKS+1
SHARED.GFI.Cnt	.EQ *-SHARED.GFI.Src				
*--------------------------------------
SHARED.DirEnt.Src
				.HS 10.1f20.1314.151617
SHARED.DirEnt.Cnt	.EQ *-SHARED.DirEnt.Src
*--------------------------------------
SHARED.Stat.Dst	.DA #S.STAT.P.TYPE,#S.STAT.P.AUXTYPE,#S.STAT.P.AUXTYPE+1
				.DA #S.STAT.BLOCKS,#S.STAT.BLOCKS+1
				.DA #S.STAT.SIZE,#S.STAT.SIZE+1,#S.STAT.SIZE+2
*--------------------------------------
MAN
SAVE USR/SRC/SYS/KERNEL.S.STAT
LOAD USR/SRC/SYS/KERNEL.S
ASM
