]> git.sur5r.net Git - cc65/blob - libsrc/common/fwrite.s
Removed (pretty inconsistently used) tab chars from source code base.
[cc65] / libsrc / common / fwrite.s
1 ;
2 ; Ullrich von Bassewitz, 22.11.2002
3 ;
4 ; size_t __fastcall__ fwrite (const void* buf, size_t size, size_t count, FILE* file);
5 ; /* Write to a file */
6 ;
7
8         .export         _fwrite
9
10         .import         _write
11         .import         pushax, incsp6, addysp, ldaxysp, pushwysp, return0
12         .import         tosumulax, tosudivax
13
14         .importzp       ptr1
15
16         .include        "errno.inc"
17         .include        "_file.inc"
18
19
20 ; ------------------------------------------------------------------------
21 ; Code
22                                                           
23 .proc   _fwrite
24
25 ; Save file and place it into ptr1
26
27         sta     file
28         sta     ptr1
29         stx     file+1
30         stx     ptr1+1
31
32 ; Check if the file is open
33
34         ldy     #_FILE::f_flags
35         lda     (ptr1),y
36         and     #_FOPEN                 ; Is the file open?
37         bne     @L2                     ; Branch if yes
38
39 ; File not open
40
41 @L1:    lda     #EBADF
42         jsr     __seterrno              ; Returns with A = 0
43         tax                             ; A = X = 0
44         jmp     incsp6
45
46 ; Check if the stream is in an error state
47
48 @L2:    lda     (ptr1),y                ; get file->f_flags again
49         and     #_FERROR
50         bne     @L1
51
52 ; Build the stackframe for write()
53
54         ldy     #_FILE::f_fd
55         lda     (ptr1),y
56         ldx     #$00
57         jsr     pushax                  ; file->f_fd
58
59         ldy     #9
60         jsr     pushwysp                ; buf
61
62 ; Stack is now: buf/size/count/file->fd/buf
63 ; Calculate the number of bytes to write: count * size
64
65         ldy     #7
66         jsr     pushwysp                ; count
67         ldy     #9
68         jsr     ldaxysp                 ; Get size
69         jsr     tosumulax               ; count * size -> a/x
70
71 ; Check if the number of bytes is zero. Don't call write in this case
72
73         cpx     #0
74         bne     @L3
75         cmp     #0
76         bne     @L3
77
78 ; The number of bytes to write is zero, just return count
79
80         ldy     #5
81         jsr     ldaxysp                 ; Get count
82         ldy     #10
83         jmp     addysp                  ; Drop params, return
84
85 ; Call write(). This will leave the original 3 params on the stack
86
87 @L3:    jsr     _write
88
89 ; Check for errors in write
90
91         cpx     #$FF
92         bne     @L4
93         cmp     #$FF
94         bne     @L4
95
96 ; Error in write. Set the stream error flag and bail out. errno is already
97 ; set by write().
98
99         lda     file
100         sta     ptr1
101         lda     file+1
102         sta     ptr1+1
103         ldy     #_FILE::f_flags
104         lda     (ptr1),y
105         ora     #_FERROR
106         sta     (ptr1),y
107         bne     @L1                     ; Return zero
108
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
111 ; safe.
112
113 @L4:    jsr     pushax                  ; Push number of bytes written
114         ldy     #5
115         jsr     ldaxysp                 ; Get size
116         jsr     tosudivax               ; bytes / size -> a/x
117         jmp     incsp6                  ; Drop params, return
118
119 .endproc
120
121 ; ------------------------------------------------------------------------
122 ; Data
123
124 .bss
125 file:   .res    2
126