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