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