2 ; 2002-11-16, Ullrich von Bassewitz
3 ; 2013-12-23, Greg King
5 ; int read (int fd, void* buf, unsigned count);
11 .import SETLFS, OPEN, CHKIN, BASIN, CLRCH, BSOUT, READST
14 .importzp ptr1, ptr2, ptr3, tmp1, tmp2, tmp3
19 .include "filedes.inc"
22 ;--------------------------------------------------------------------------
23 ; initstdin: Open the stdin file descriptors for the keyboard
29 lda #STDIN_FILENO + LFN_OFFS
33 jmp OPEN ; Will always succeed
37 ;--------------------------------------------------------------------------
44 jsr rwcommon ; Pop params, check handle
45 bcs invalidfd ; Invalid handle
47 ; Check if the LFN is valid and the file is open for writing
49 adc #LFN_OFFS ; Carry is already clear
51 lda fdtab-LFN_OFFS,x; Get flags for this handle
53 and #LFN_READ ; File open for writing?
56 ; Check the EOF flag. If it is set, don't read anything
61 ; Remember the device number.
63 ldy unittab-LFN_OFFS,x
66 ; Valid lfn. Make it the input file
69 bcc @L3 ; Branch if ok
70 jmp __mappederrno ; Store into __oserror, map to errno, return -1
75 sta tmp1 ; Save the input byte
77 bne @L0_1 ; Not keyboard/screen-editor
78 cmp #$0D ; Is it a Carriage Return?
80 jsr BSOUT ; Yes, echo it (because editor didn't)
82 @L0_1: jsr READST ; Read the IEEE status
84 and #%10111111 ; Check anything but the EOI bit
85 bne devnotpresent ; Assume device not present
87 ; Store the byte just read
94 inc ptr2+1 ; *buf++ = A;
96 ; Increment the byte count
102 ; Get the status again and check the EOI bit
105 and #%01000000 ; Check for EOI
106 bne @L4 ; Jump if end of file reached
108 ; Decrement the count
114 beq done ; Branch always
116 ; Set the EOI flag and bail out
118 @L4: ldx tmp2 ; Get the handle
123 ; Read done, close the input channel
127 ; Clear _oserror and return the number of chars read
135 ; Error entry: Device not present
139 jmp __directerrno ; Sets _errno, clears _oserror, returns -1
141 ; Error entry: The given file descriptor is not valid or not open
145 jmp __directerrno ; Sets _errno, clears _oserror, returns -1
150 ;--------------------------------------------------------------------------