]> git.sur5r.net Git - cc65/blob - libsrc/atari/open.s
always use setcursor to update cursor settings
[cc65] / libsrc / atari / open.s
1 ;
2 ; Christian Groessler, May-2002
3 ;
4 ; int open(const char *name,int flags,...);
5 ;
6
7         .include "atari.inc"
8         .include "fcntl.inc"
9         .include "errno.inc"
10         .export _open
11         .import clriocb
12         .import fddecusage,newfd
13         .import findfreeiocb
14         .import __do_oserror,__seterrno,incsp4
15         .import ldaxysp,addysp
16         .import __oserror
17         .importzp tmp4,tmp2
18 .ifdef  UCASE_FILENAME
19         .importzp tmp3
20         .import ucase_fn
21 .endif
22
23 .proc   _open
24
25         cpy     #4              ; correct # of arguments (bytes)?
26         beq     parmok          ; parameter count ok
27         tya                     ; parm count < 4 shouldn't be needed to be checked
28         sec                     ;       (it generates a c compiler warning)
29         sbc     #4
30         tay
31         jsr     addysp          ; fix stack, throw away unused parameters
32
33 parmok: jsr     findfreeiocb
34         beq     iocbok          ; we found one
35
36         lda     #<EMFILE        ; "too many open files"
37         ldx     #>EMFILE
38 seterr: jsr     __seterrno
39         jsr     incsp4          ; clean up stack
40         lda     #$FF
41         tax
42         rts                     ; return -1
43
44         ; process the mode argument
45
46 iocbok: stx     tmp4
47         jsr     clriocb         ; init with zero
48         ldy     #1
49         jsr     ldaxysp         ; get mode
50         ;brk
51         ldx     tmp4
52         pha
53         and     #O_APPEND
54         beq     no_app
55         pla
56         and     #15
57         cmp     #O_RDONLY       ; DOS supports append with write-only only
58         beq     invret
59         cmp     #O_RDWR
60         beq     invret
61         lda     #OPNOT|APPEND
62         bne     set
63
64 .ifndef UCASE_FILENAME
65 invret: lda     #<EINVAL        ; file name is too long
66         ldx     #>EINVAL
67         jmp     seterr
68 .endif
69
70 no_app: pla
71         and     #15
72         cmp     #O_RDONLY
73         bne     l1
74         lda     #OPNIN
75 set:    sta     ICAX1,x
76         bne     cont
77
78 l1:     cmp     #O_WRONLY
79         bne     l2
80         lda     #OPNOT
81         bne     set
82
83 l2:     ; O_RDWR
84         lda     #OPNOT|OPNIN
85         bne     set
86
87         ; process the filename argument
88
89 cont:   ldy     #3
90         jsr     ldaxysp
91
92 .ifdef  UCASE_FILENAME
93
94         jsr     ucase_fn
95         bcc     ucok1
96 invret: lda     #<EINVAL        ; file name is too long
97         ldx     #>EINVAL
98         jmp     seterr
99 ucok1:
100
101 .endif  ; defined UCASE_FILENAME
102
103         ldy     tmp4
104
105         ;AX - points to filename
106         ;Y  - iocb to use, if open needed
107         jsr     newfd           ; maybe we don't need to open and can reuse an iocb
108                                 ; returns fd num to use in tmp2, all regs unchanged
109         bcs     doopen          ; C set: open needed
110         lda     #0              ; clears N flag
111         beq     finish
112
113 doopen: sta     ICBAL,y
114         txa
115         sta     ICBAH,y
116         ldx     tmp4
117         lda     #OPEN
118         sta     ICCOM,x
119         jsr     CIOV
120
121         ; clean up the stack
122
123 finish: php
124         txa
125         pha
126         tya
127         pha
128
129 .ifdef  UCASE_FILENAME
130         ldy     tmp3            ; get size
131         jsr     addysp          ; free used space on the stack
132 .endif  ; defined UCASE_FILENAME
133
134         jsr     incsp4          ; clean up stack
135
136         pla
137         tay
138         pla
139         tax
140         plp
141
142         bpl     ok
143         jsr     fddecusage      ; decrement usage counter of fd as open failed
144         jmp     __do_oserror
145
146 ok:     lda     tmp2            ; get fd
147         ldx     #0
148         stx     __oserror
149         rts
150
151 .endproc