NEW
  AUTO 3,1
*--------------------------------------
* ZPPtr1 : UID
* TXTPTR : pName (Input)
* FORPNT : pPW (Output)
* ZPPtr3 : pDB
* USERNAME:PASSWORD:UID:GID:GECOS:HOME:SHELL\CR
* ....\CR
* \0
*--------------------------------------
PWDX.GetPWUID	lda PWD.hDB
				beq PWD.GetPW.9

				jsr K.GetmemPtr
				>STYA ZPPtr3

.1				lda (ZPPtr3)
				beq PWD.GetPW.99		EOF
				ldy #0
				jsr PWD.NextString		skip USERNAME
	
				jsr PWD.NextString		skip PASSWORD
	
				jsr PWD.GetByte			get UID
				cmp ZPPtr1+1
				beq PWD.GetPW.8

				jsr PWD.NextLine
				bra .1

PWD.GetPW.8		ldx FORPNT+1
				beq  .8					Dry run, no output
				jmp PWD.Record2PW

.8				clc						a = UID
				rts

PWD.GetPW.9		lda #E.IPWDDB
				.HS 2C					BIT ABS
	
PWD.GetPW.99	lda #E.IUSR
				sec
PWD.GetPW.RTS	rts
*--------------------------------------
PWDX.GetPWName	lda PWD.hDB
				beq PWD.GetPW.9

				jsr K.GetmemPtr
				>STYA ZPPtr3

				ldy #0
				jsr SHARED.TXTPTR.GetY
				beq PWD.GetPW.99

.1				lda (ZPPtr3)
				beq PWD.GetPW.99

				ldy #$ff
.2				iny
				jsr SHARED.TXTPTR.GetY
				beq .4

				cmp (ZPPtr3),y
				beq .2

.3				jsr PWD.NextLine
				bra .1

.4				lda (ZPPtr3),y
				cmp #':'
				bne .3
				
				ldx FORPNT+1
				beq .8

				jmp PWD.Record2PW

.8				iny
				jsr PWD.NextString		skip PASSWORD
				jmp PWD.GetByte			get UID
*--------------------------------------
* FORPNT : pPW (Output)
* ZPPtr3 : pDB
*--------------------------------------
PWDX.PutPW		lda PWD.hDB
				bne .1

				ldy #S.PW.UID
				jsr SHARED.TXTPTR.GetY
				bne PWD.GetPW.RTS		Must be ROOT user

				jsr PWD.GetRecordLen
				tay
				lda #0
				>STYA PWD.DBSize

				jsr K.getmem
				bcs PWD.GetPW.RTS
				>STYA ZPPtr3
				>STYA PWD.DBPtr
				stx PWD.hDB
				jmp .8

.1				jsr K.GetmemPtr
				>STYA PWD.DBPtr

				ldy #S.PW.PASSWD
				jsr SHARED.TXTPTR.GetY
				bne .12

				jsr PWD.DeleteRecord
				bcc .80

				rts

.12				jsr PWD.DeleteRecord
				bcc .11					user exits..
				jsr PWD.GetUID			new user, get UID
				bcs .99

				ldy #S.PW.UID
				sta CLRWRITEAUX
				sta (TXTPTR),y
				sta SETWRITEAUX

.11				jsr PWD.GetRecordLen

				clc
				adc PWD.DBSize
				sta PWD.DBSize
				bcc .2
				inc PWD.DBSize+1
.2				>LDYA PWD.DBPtr
				>STYA ZPPtr4
				>LDYA PWD.DBSize

				jsr K.Getmem
				bcs .99
				>STYA ZPPtr3
				>STYA PWD.DBPtr
				ldy #0
.3				lda (ZPPtr4),y
				sta (ZPPtr3),y
				beq .4
				iny
				bne .3
				inc ZPPtr4
				inc ZPPtr3
				bra .3
.4				tya
				clc
				adc ZPPtr3
				sta ZPPtr3
				bcc .5
				inc ZPPtr3+1
.5				lda PWD.hDB
				stx PWD.hDB
				jsr K.Freemem

.8				jsr PWD.StoreRecord
			
.80				clc	
.99				rts

.9				lda #E.IPWDDB
				sec
				rts	
*--------------------------------------
PWDX.GetGRGID

*--------------------------------------
PWDX.GetGRName

*--------------------------------------
PWDX.PutGR
*--------------------------------------
* PRIVATE
*--------------------------------------
PWD.GetUID		>LDYA PWD.DBPtr
				>STYA ZPPtr3
				stz .8+1
.1				lda (ZPPtr3)
				beq .8					End of DB
				ldy #0
				jsr	PWD.NextString		skip name
				jsr PWD.NextString		skip password
				jsr PWD.GetByte
				cmp .8+1
				bcc .4
				sta .8+1
.4				jsr PWD.NextLine
				bra .1
.8				lda #$ff				SELF MODIFIED
				inc
				beq .9
				clc
				rts
.9 				lda #E.IUSR
				sec
				rts
*--------------------------------------
PWD.DeleteRecord
				jsr PWD.FindRecord
				bcs .9
.1				iny
				lda (ZPPtr3),y
				cmp #C.CR
				bne .1
				sty .3+1
				tya
				sec
				adc ZPPtr3
				sta ZPPtr4
				lda ZPPtr3+1
				adc #0
				sta ZPPtr4+1
				ldy #$ff
.2				iny
				lda (ZPPtr4),y
				sta (ZPPtr3),y
				bne .2

				lda PWD.DBSize
				sec
.3				sbc #$ff				SELF MODIFIED
				sta PWD.DBSize
				bcs .8
				dec PWD.DBSize+1 
.8				clc
	
.9				rts
*--------------------------------------
PWD.FindRecord	>LDYA PWD.DBPtr
				>STYA ZPPtr3

.2				lda (ZPPtr3)
				beq .9					End of DB

				ldy #$ff
.3				iny
				jsr SHARED.TXTPTR.GetY
				beq .4
				cmp (ZPPtr3),y
				beq .3
.5				jsr PWD.NextLine
				bra .2
.4				lda (ZPPtr3),y
				cmp #':'
				bne .5

				clc
				rts
.9				lda #E.IUSR
				sec
				rts
*--------------------------------------
PWD.NextString	lda (ZPPtr3),y
				iny
				cmp #':'
				bne PWD.NextString
				rts
*--------------------------------------
PWD.NextLine	iny
				lda (ZPPtr3),y
				cmp #C.CR
				bne PWD.NextLine
				tya
				sec
				adc ZPPtr3
				sta ZPPtr3
				bcc .8
				inc ZPPtr3+1
.8				rts
*--------------------------------------
PWD.Record2PW	ldy #0

				ldx #0					NAME

.1				lda (ZPPtr3),y
				iny
				cmp #':'
				beq .2
				jsr SHARED.FORPNT.PutNext
				inx
				bra .1
.2				lda #0
				jsr SHARED.FORPNT.PutNext
				inx
				cpx #S.PW.PASSWD
				bne .2

.3				lda (ZPPtr3),y
				iny
				cmp #':'
				beq .4

				jsr SHARED.FORPNT.PutNext
				inx
				bra .3

.4				lda #0
				jsr SHARED.FORPNT.PutNext
				inx
				cpx #S.PW.UID
				bne .4

				jsr PWD.GetByte			UID
				jsr SHARED.FORPNT.PutNext

				jsr PWD.GetByte			GID
				jsr SHARED.FORPNT.PutNext

				ldx #S.PW.GECOS

.5				lda (ZPPtr3),y
				iny
				cmp #':'
				beq .6
				jsr SHARED.FORPNT.PutNext
				inx
				bra .5
.6				lda #0
				jsr SHARED.FORPNT.PutNext
				inx
				cpx #S.PW.DIR
				bne .6

.7				lda (ZPPtr3),y
				iny
				cmp #':'
				beq .8
				jsr SHARED.FORPNT.PutNext
				inx
				bra .7
.8				lda #0
				jsr SHARED.FORPNT.PutNext
				inx
				cpx #S.PW.SHELL
				bne .8

.9				lda (ZPPtr3),y
				iny
				cmp #C.CR
				beq .10
				jsr SHARED.FORPNT.PutNext
				inx
				bra .9
.10				lda #0
				jsr SHARED.FORPNT.PutNext

				clc
				rts
*--------------------------------------
PWD.GetByte		stz ZPPtr4

.1				lda (ZPPtr3),y
				iny
				cmp #':'
				beq .8
				and #$f
				pha
				lda ZPPtr4
				asl
				asl
				clc
				adc ZPPtr4
				asl
				sta ZPPtr4
				pla
				clc
				adc ZPPtr4
				sta ZPPtr4
				bra .1
.8				lda ZPPtr4
				clc
				rts
*--------------------------------------
PWD.StoreRecord	sec
				.HS 90					BCC
*--------------------------------------
PWD.GetRecordLen
				clc
				ldx #0
				ldy #S.PW.NAME-1

				jsr PWD.StoreString
				lda #':'
				jsr PWD.StoreChar
				ldy #S.PW.PASSWD-1

				jsr PWD.StoreString	

				lda #':'
				jsr PWD.StoreChar

				ldy #S.PW.UID
				jsr SHARED.TXTPTR.GetY
				jsr PWD.StoreByte

				lda #':'
				jsr PWD.StoreChar
				
				ldy #S.PW.GID
				jsr SHARED.TXTPTR.GetY
				jsr PWD.StoreByte
				lda #':'
				jsr PWD.StoreChar

				ldy #S.PW.GECOS-1

				jsr PWD.StoreString
				lda #':'
				jsr PWD.StoreChar

				ldy #S.PW.DIR-1

				jsr PWD.StoreString
				lda #':'
				jsr PWD.StoreChar

				ldy #S.PW.SHELL-1

				jsr PWD.StoreString
				lda #C.CR
				jsr PWD.StoreChar

				txa
PWD.GetRecordLen.RTS
				rts
*--------------------------------------
PWD.StoreString	iny
				jsr SHARED.TXTPTR.GetY
				beq PWD.GetRecordLen.RTS
				jsr PWD.StoreChar
				bra PWD.StoreString
*--------------------------------------
PWD.StoreByte	php
				phx
				jsr MATH.A2STR10NP
				plx
				plp
				ldy #0
.1				lda A2osX.NumStrBuf,y
				beq PWD.GetRecordLen.RTS
				iny
				jsr PWD.StoreChar
				bra .1
*--------------------------------------
PWD.StoreChar	inx
				bcc .9					Dry run, no store
				sta (ZPPtr3)
				inc ZPPtr3
				bne .9
				inc ZPPtr3+1
.9				rts
*--------------------------------------
MAN
SAVE USR/SRC/SYS/KERNEL.S.PWDX
LOAD USR/SRC/SYS/KERNEL.S
ASM
