]> git.sur5r.net Git - cc65/blob - libsrc/cbm/write.s
7a1441fe005299a89b8dc68b921abe09e9503fd5
[cc65] / libsrc / cbm / write.s
1 ;
2 ; Ullrich von Bassewitz, 16.11.2002
3 ;
4 ; int write (int fd, const void* buf, unsigned count);
5 ;
6
7         .export         _write
8         .constructor    initstdout
9
10         .import         rwcommon
11         .import         __errno, __oserror
12         .importzp       sp, ptr1, ptr2, ptr3
13
14         .include        "errno.inc"
15         .include        "fcntl.inc"
16         .include        "cbm.inc"
17         .include        "filedes.inc"
18
19
20 ;--------------------------------------------------------------------------
21 ; initstdout: Open the stdout and stderr file descriptors for the screen.
22
23 .proc   initstdout
24
25         lda     #LFN_WRITE
26         sta     fdtab+STDOUT_FILENO
27         sta     fdtab+STDERR_FILENO
28         lda     #CBMDEV_SCREEN
29         sta     unittab+STDOUT_FILENO
30         sta     unittab+STDERR_FILENO
31         lda     #STDOUT_FILENO + LFN_OFFS
32         jsr     @L1
33         lda     #STDERR_FILENO + LFN_OFFS
34 @L1:    ldx     #CBMDEV_SCREEN
35         ldy     #$FF
36         jsr     SETLFS
37         jmp     OPEN            ; Will always succeed
38
39 .endproc
40
41 ;--------------------------------------------------------------------------
42 ; _write
43
44
45 .proc   _write
46
47         jsr     rwcommon        ; Pop params, check handle
48         bcs     invalidfd       ; Branch if handle not ok
49
50 ; Check if the LFN is valid and the file is open for writing
51
52         adc     #LFN_OFFS       ; Carry is already clear
53         tax
54         lda     fdtab-LFN_OFFS,x; Get flags for this handle
55         and     #LFN_WRITE      ; File open for writing?
56         beq     notopen
57
58 ; Valid lfn. Make it the output file
59
60         jsr     CKOUT
61         bcs     error
62         bcc     @L2
63
64 ; Output the next character from the buffer
65
66 @L0:    ldy     #0
67         lda     (ptr2),y
68         inc     ptr2
69         bne     @L1
70         inc     ptr2+1          ; A = *buf++;
71 @L1:    jsr     BSOUT
72         bcs     error           ; Bail out on errors
73
74 ; Count characters written
75
76         inc     ptr3
77         bne     @L2
78         inc     ptr3+1
79
80 ; Decrement count
81
82 @L2:    inc     ptr1
83         bne     @L0
84         inc     ptr1+1
85         bne     @L0
86
87 ; Wrote all chars, close the output channel
88
89         jsr     CLRCH
90
91 ; Return the number of chars written
92
93         lda     ptr3
94         ldx     ptr3+1
95         rts
96
97 ; Error entry, file descriptor is invalid
98
99 invalidfd:
100         lda     #EINVAL
101         sta     __errno
102         lda     #0
103         sta     __errno+1
104         beq     errout
105
106 ; Error entry, file is not open
107
108 notopen:
109         lda     #3              ; File not open
110         bne     error
111
112 ; Error entry, status not ok
113
114 error5: lda     #5              ; Device not present
115 error:  sta     __oserror
116 errout: lda     #$FF
117         tax                     ; Return -1
118         rts
119
120 .endproc
121
122
123
124