]> git.sur5r.net Git - cc65/blob - libsrc/cbm/write.s
Merge remote-tracking branch 'upstream/master'
[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         SETLFS, OPEN, CKOUT, BSOUT, READST, CLRCH
11         .import         rwcommon
12         .importzp       sp, ptr1, ptr2, ptr3
13
14         .include        "cbm.inc"
15         .include        "errno.inc"
16         .include        "fcntl.inc"
17         .include        "filedes.inc"
18
19
20 ;--------------------------------------------------------------------------
21 ; initstdout: Open the stdout and stderr file descriptors for the screen.
22
23 .segment        "INIT"
24
25 .proc   initstdout
26
27         lda     #LFN_WRITE
28         sta     fdtab+STDOUT_FILENO
29         sta     fdtab+STDERR_FILENO
30         lda     #CBMDEV_SCREEN
31         sta     unittab+STDOUT_FILENO
32         sta     unittab+STDERR_FILENO
33         lda     #STDOUT_FILENO + LFN_OFFS
34         jsr     @L1
35         lda     #STDERR_FILENO + LFN_OFFS
36 @L1:    ldx     #CBMDEV_SCREEN
37         ldy     #$FF
38         jsr     SETLFS
39         jmp     OPEN            ; Will always succeed
40
41 .endproc
42
43 ;--------------------------------------------------------------------------
44 ; _write
45
46 .code
47
48 .proc   _write
49
50         jsr     rwcommon        ; Pop params, check handle
51         bcs     invalidfd       ; Invalid handle
52
53 ; Check if the LFN is valid and the file is open for writing
54
55         adc     #LFN_OFFS       ; Carry is already clear
56         tax
57         lda     fdtab-LFN_OFFS,x; Get flags for this handle
58         and     #LFN_WRITE      ; File open for writing?
59         beq     invalidfd
60
61 ; Valid lfn. Make it the output file
62
63         jsr     CKOUT
64         bcc     @L2
65 @error: jmp     __mappederrno   ; Store into __oserror, map to errno, return -1
66
67 ; Output the next character from the buffer
68
69 @L0:    ldy     #0
70         lda     (ptr2),y
71         inc     ptr2
72         bne     @L1
73         inc     ptr2+1          ; A = *buf++;
74 @L1:    jsr     BSOUT
75
76 ; Check the status
77
78         pha
79         jsr     READST
80         lsr     a               ; Bit zero is write timeout
81         bne     devnotpresent2
82         pla
83         bcs     @L3
84
85 ; Count characters written
86
87         inc     ptr3
88         bne     @L2
89         inc     ptr3+1
90
91 ; Decrement count
92
93 @L2:    inc     ptr1
94         bne     @L0
95         inc     ptr1+1
96         bne     @L0
97
98 ; Wrote all chars or disk full. Close the output channel
99
100 @L3:    jsr     CLRCH
101
102 ; Clear _oserror and return the number of chars written
103
104         lda     #0
105         sta     __oserror
106         lda     ptr3
107         ldx     ptr3+1
108         rts
109
110 ; Error entry: Device not present
111
112 devnotpresent2:
113         pla
114 devnotpresent:
115         lda     #ENODEV
116         jmp     __directerrno   ; Sets _errno, clears _oserror, returns -1
117
118 ; Error entry: The given file descriptor is not valid or not open
119
120 invalidfd:
121         lda     #EBADF
122         jmp     __directerrno   ; Sets _errno, clears _oserror, returns -1
123
124 .endproc
125
126
127
128