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
42 jsr __seterrno ; Returns with A = 0
46 ; Check if the stream is in an error state
48 @L2: lda (ptr1),y ; get file->f_flags again
52 ; Build the stackframe for write()
57 jsr pushax ; file->f_fd
62 ; Stack is now: buf/size/count/file->fd/buf
63 ; Calculate the number of bytes to write: count * size
68 jsr ldaxysp ; Get size
69 jsr tosumulax ; count * size -> a/x
71 ; Check if the number of bytes is zero. Don't call write in this case
78 ; The number of bytes to write is zero, just return count
81 jsr ldaxysp ; Get count
83 jmp addysp ; Drop params, return
85 ; Call write(). This will leave the original 3 params on the stack
89 ; Check for errors in write
96 ; Error in write. Set the stream error flag and bail out. errno is already
107 bne @L1 ; Return zero
109 ; Write was ok. Return the number of items successfully written. Since we've
110 ; checked for bytes == 0 above, size cannot be zero here, so the division is
113 @L4: jsr pushax ; Push number of bytes written
115 jsr ldaxysp ; Get size
116 jsr tosudivax ; bytes / size -> a/x
117 jmp incsp6 ; Drop params, return
121 ; ------------------------------------------------------------------------