2 ; Ullrich von Bassewitz, 22.11.2002
4 ; size_t __fastcall__ fread (void* buf, size_t size, size_t count, FILE* f);
5 ; /* Read from a file */
11 .import pushax, incsp6, addysp, ldaxysp, pushwysp, return0
12 .import tosumulax, tosudivax
20 ; ------------------------------------------------------------------------
25 ; Save f and place it into ptr1
32 ; Check if the file is open
36 and #_FOPEN ; Is the file open?
37 bne @L2 ; Branch if yes
48 ; Check if the stream is in an error state
50 @L2: lda (ptr1),y ; get f->f_flags again
54 ; Build the stackframe for read()
64 ; Stack is now: buf/size/count/f->fd/buf
65 ; Calculate the number of bytes to read: count * size
70 jsr ldaxysp ; Get size
71 jsr tosumulax ; count * size -> a/x
73 ; Check if the number of bytes is zero. Don't call read in this case
80 ; The number of bytes to read is zero, just return count
83 jsr ldaxysp ; Get count
85 jmp addysp ; Drop params, return
87 ; Call read(). This will leave the original 3 params on the stack
91 ; Check for errors in read
98 ; Error in read. Set the stream error flag and bail out. _oserror and/or
99 ; errno are already set by read().
111 bne @L1 ; Return zero
113 ; Read was ok, check for end of file.
115 @L5: cmp #0 ; Zero bytes read?
120 ; Zero bytes read. Set the EOF flag
123 bne @L4 ; Set flag and return zero
125 ; Return the number of items successfully read. Since we've checked for
126 ; bytes == 0 above, size cannot be zero here, so the division is safe.
128 @L6: jsr pushax ; Push number of bytes read
130 jsr ldaxysp ; Get size
131 jsr tosudivax ; bytes / size -> a/x
132 jmp incsp6 ; Drop params, return
136 ; ------------------------------------------------------------------------