2 ; Christian Groessler, Jun-2004
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
49 lda buflen ; line buffer active?
50 bne use_buf ; yes, get data from there
52 ; save user buffer address & length
53 ; update IOCB to point to line buffer
76 jsr CIOV ; read input data
78 cpy #EOFERR ; eof is treated specially
84 jmp __do_oserror ; update errno
87 lda ICBLL,x ; get # of bytes read
90 sta index ; fresh buffer
92 ; restore user buffer address & length
108 ; return bytes from line buffer
109 ; use buflen and index to access buffer
111 ; use dataptr as a temporary pointer
115 sbc index ; size of unread data in the buffer
118 lda ICBLL,x ; buf len lo
119 cmp cbs ; larger than buffer size?
121 bcs btsmall ; yes, adjust length
123 bl1: lda ICBLH,x ; get buf len hi
124 bne btsmall ; buffer too small: buffer contents < read size
130 lda ICBAL,x ; buffer address
132 lda ICBAH,x ; buffer address
136 pha ; remember for return value
148 pha ; save length to return at okdone
153 cmp buflen ; buffer used up?
157 sta buflen ; indicate empty line buffer
160 jmp okdone ; return to caller
169 index: .res 1 ; index into line buffer
170 buflen: .res 1 ; length of used part of buffer
171 cbs: .res 1 ; current buffer size: buflen - index
172 dataptr:.res 2 ; temp pointer to user buffer
173 copylen:.res 1 ; temp counter
177 linebuf:.res LINEBUF ; the line buffer
179 .endif ; .ifdef LINEBUF