2 ; Christian Groessler, Jul-2005
4 ; int __fastcall__ read(int fd,void *buf,int count)
8 .import __rwsetup,__do_oserror,__inviocb,__oserror
11 _read: jsr __rwsetup ; do common setup for read and write
12 beq done ; if size 0, it's a no-op
13 cpx #$FF ; invalid iocb?
17 ; E: should be always at IOCB #0
18 ; fixme: what happens when user closes and reopens stdin?
19 cpx #0 ; E: handler (line oriented keyboard input)?
23 lda #GETCHR ; iocb command code
27 cpy #EOFERR ; eof is treated specially
29 jmp __do_oserror ; update errno
31 done: lda ICBLL,x ; buf len lo
33 lda ICBLH,x ; get buf len hi
36 sta __oserror ; clear system dependend error code
48 .segment "EXTZP" : zeropage
50 index: .res 1 ; index into line buffer
51 buflen: .res 1 ; length of used part of buffer
52 cbs: .res 1 ; current buffer size: buflen - index
53 dataptr:.res 2 ; temp pointer to user buffer
54 copylen:.res 1 ; temp counter
58 linebuf:.res LINEBUF ; the line buffer
63 lda buflen ; line buffer active?
64 bne use_buf ; yes, get data from there
66 ; save user buffer address & length
67 ; update IOCB to point to line buffer
90 jsr CIOV ; read input data
92 cpy #EOFERR ; eof is treated specially
98 jmp __do_oserror ; update errno
101 lda ICBLL,x ; get # of bytes read
104 sta index ; fresh buffer
106 ; restore user buffer address & length
122 ; return bytes from line buffer
123 ; use buflen and index to access buffer
125 ; use dataptr as a temporary pointer
129 sbc index ; size of unread data in the buffer
132 lda ICBLL,x ; buf len lo
133 cmp cbs ; larger than buffer size?
135 bcs btsmall ; yes, adjust length
137 bl1: lda ICBLH,x ; get buf len hi
138 bne btsmall ; buffer too small: buffer contents < read size
144 lda ICBAL,x ; buffer address
146 lda ICBAH,x ; buffer address
150 pha ; remember for return value
162 pha ; save length to return at okdone
167 cmp buflen ; buffer used up?
171 sta buflen ; indicate empty line buffer
174 jmp okdone ; return to caller
181 .endif ; .ifdef LINEBUF