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