2 ; Ullrich von Bassewitz, 22.11.2002
4 ; size_t __fastcall__ fwrite (const void* buf, size_t size, size_t count, FILE* file);
5 ; /* Write to a file */
11 .import pushax, incsp6, addysp, ldaxysp, pushwysp, return0
12 .import tosumulax, tosudivax
20 ; ------------------------------------------------------------------------
25 ; Save file 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 file->f_flags again
54 ; Build the stackframe for write()
59 jsr pushax ; file->f_fd
64 ; Stack is now: buf/size/count/file->fd/buf
65 ; Calculate the number of bytes to write: 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 write in this case
80 ; The number of bytes to write is zero, just return count
83 jsr ldaxysp ; Get count
85 jmp addysp ; Drop params, return
87 ; Call write(). This will leave the original 3 params on the stack
91 ; Check for errors in write
98 ; Error in write. Set the stream error flag and bail out. _oserror and/or
99 ; errno are already set by write().
109 bne @L1 ; Return zero
111 ; Write was ok. Return the number of items successfully written. Since we've
112 ; checked for bytes == 0 above, size cannot be zero here, so the division is
115 @L4: jsr pushax ; Push number of bytes written
117 jsr ldaxysp ; Get size
118 jsr tosudivax ; bytes / size -> a/x
119 jmp incsp6 ; Drop params, return
123 ; ------------------------------------------------------------------------