2002-11-23 22:51:25 +00:00
|
|
|
;
|
|
|
|
; Ullrich von Bassewitz, 22.11.2002
|
|
|
|
;
|
2003-06-17 20:52:33 +00:00
|
|
|
; size_t __fastcall__ fwrite (const void* buf, size_t size, size_t count, FILE* file);
|
2002-11-23 22:51:25 +00:00
|
|
|
; /* Write to a file */
|
|
|
|
;
|
|
|
|
|
|
|
|
.export _fwrite
|
|
|
|
|
|
|
|
.import _write
|
|
|
|
.import pushax, incsp6, addysp, ldaxysp, pushwysp, return0
|
|
|
|
.import tosumulax, tosudivax
|
|
|
|
|
|
|
|
.importzp ptr1
|
|
|
|
|
|
|
|
.include "errno.inc"
|
|
|
|
.include "_file.inc"
|
|
|
|
|
|
|
|
|
|
|
|
; ------------------------------------------------------------------------
|
|
|
|
; Code
|
|
|
|
|
|
|
|
.proc _fwrite
|
|
|
|
|
2003-06-17 20:52:33 +00:00
|
|
|
; Save file and place it into ptr1
|
2002-11-23 22:51:25 +00:00
|
|
|
|
2003-06-17 20:52:33 +00:00
|
|
|
sta file
|
2002-11-23 22:51:25 +00:00
|
|
|
sta ptr1
|
2003-06-17 20:52:33 +00:00
|
|
|
stx file+1
|
2002-11-23 22:51:25 +00:00
|
|
|
stx ptr1+1
|
|
|
|
|
|
|
|
; Check if the file is open
|
|
|
|
|
2003-12-03 10:15:33 +00:00
|
|
|
ldy #_FILE::f_flags
|
2002-11-23 22:51:25 +00:00
|
|
|
lda (ptr1),y
|
|
|
|
and #_FOPEN ; Is the file open?
|
|
|
|
bne @L2 ; Branch if yes
|
|
|
|
|
|
|
|
; File not open
|
|
|
|
|
|
|
|
lda #EINVAL
|
2004-05-13 21:09:08 +00:00
|
|
|
jsr __seterrno
|
2002-11-23 22:51:25 +00:00
|
|
|
@L1: jsr incsp6
|
|
|
|
jmp return0
|
|
|
|
|
|
|
|
; Check if the stream is in an error state
|
|
|
|
|
2003-06-17 20:52:33 +00:00
|
|
|
@L2: lda (ptr1),y ; get file->f_flags again
|
2002-11-23 22:51:25 +00:00
|
|
|
and #_FERROR
|
|
|
|
bne @L1
|
|
|
|
|
|
|
|
; Build the stackframe for write()
|
|
|
|
|
2003-12-03 10:15:33 +00:00
|
|
|
ldy #_FILE::f_fd
|
2002-11-23 22:51:25 +00:00
|
|
|
lda (ptr1),y
|
|
|
|
ldx #$00
|
2003-06-17 20:52:33 +00:00
|
|
|
jsr pushax ; file->f_fd
|
2002-11-23 22:51:25 +00:00
|
|
|
|
|
|
|
ldy #9
|
|
|
|
jsr pushwysp ; buf
|
|
|
|
|
2003-06-17 20:52:33 +00:00
|
|
|
; Stack is now: buf/size/count/file->fd/buf
|
2002-11-23 22:51:25 +00:00
|
|
|
; Calculate the number of bytes to write: count * size
|
|
|
|
|
|
|
|
ldy #7
|
|
|
|
jsr pushwysp ; count
|
|
|
|
ldy #9
|
|
|
|
jsr ldaxysp ; Get size
|
|
|
|
jsr tosumulax ; count * size -> a/x
|
|
|
|
|
|
|
|
; Check if the number of bytes is zero. Don't call write in this case
|
|
|
|
|
|
|
|
cpx #0
|
|
|
|
bne @L3
|
|
|
|
cmp #0
|
|
|
|
bne @L3
|
|
|
|
|
|
|
|
; The number of bytes to write is zero, just return count
|
|
|
|
|
|
|
|
ldy #5
|
|
|
|
jsr ldaxysp ; Get count
|
|
|
|
ldy #10
|
|
|
|
jmp addysp ; Drop params, return
|
|
|
|
|
|
|
|
; Call write(). This will leave the original 3 params on the stack
|
|
|
|
|
2002-12-03 22:31:06 +00:00
|
|
|
@L3: jsr _write
|
2002-11-23 22:51:25 +00:00
|
|
|
|
|
|
|
; Check for errors in write
|
|
|
|
|
|
|
|
cpx #$FF
|
|
|
|
bne @L4
|
|
|
|
cmp #$FF
|
|
|
|
bne @L4
|
|
|
|
|
|
|
|
; Error in write. Set the stream error flag and bail out. _oserror and/or
|
|
|
|
; errno are already set by write().
|
|
|
|
|
2003-06-17 20:52:33 +00:00
|
|
|
lda file
|
2002-11-23 22:51:25 +00:00
|
|
|
sta ptr1
|
2003-06-17 20:52:33 +00:00
|
|
|
lda file+1
|
2002-11-23 22:51:25 +00:00
|
|
|
sta ptr1+1
|
2003-12-03 10:15:33 +00:00
|
|
|
ldy #_FILE::f_flags
|
2002-11-23 22:51:25 +00:00
|
|
|
lda (ptr1),y
|
|
|
|
ora #_FERROR
|
|
|
|
sta (ptr1),y
|
|
|
|
bne @L1 ; Return zero
|
|
|
|
|
|
|
|
; Write was ok. Return the number of items successfully written. Since we've
|
|
|
|
; checked for bytes == 0 above, size cannot be zero here, so the division is
|
|
|
|
; safe.
|
|
|
|
|
|
|
|
@L4: jsr pushax ; Push number of bytes written
|
|
|
|
ldy #5
|
|
|
|
jsr ldaxysp ; Get size
|
|
|
|
jsr tosudivax ; bytes / size -> a/x
|
|
|
|
jmp incsp6 ; Drop params, return
|
|
|
|
|
|
|
|
.endproc
|
|
|
|
|
|
|
|
; ------------------------------------------------------------------------
|
|
|
|
; Data
|
|
|
|
|
|
|
|
.bss
|
2003-06-17 20:52:33 +00:00
|
|
|
file: .res 2
|
2002-11-23 22:51:25 +00:00
|
|
|
|