]> git.sur5r.net Git - cc65/blob - libsrc/atari/open.s
2188257cb17d8f404d1131516cbaadf220d5cce0
[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, 5
14
15         .import _close
16         .import clriocb
17         .import fddecusage,newfd
18         .import findfreeiocb
19         .import 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     __directerrno
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         jmp     seterr
69 .endif
70
71 no_app: pla
72         and     #15
73         cmp     #O_RDONLY
74         bne     l1
75         lda     #OPNIN
76 set:    sta     ICAX1,x
77         bne     cont
78
79 l1:     cmp     #O_WRONLY
80         bne     l2
81         lda     #OPNOT
82         bne     set
83
84 l2:     ; O_RDWR
85         lda     #OPNOT|OPNIN
86         bne     set
87
88         ; process the filename argument
89
90 cont:   ldy     #3
91         jsr     ldaxysp
92
93 .ifdef  UCASE_FILENAME
94 .ifdef  DEFAULT_DEVICE
95         ldy     #$80
96         sty     tmp2            ; set flag for ucase_fn
97 .endif
98         jsr     ucase_fn
99         bcc     ucok1
100 invret: lda     #<EINVAL        ; file name is too long
101         jmp     seterr
102 ucok1:
103
104 .endif  ; defined UCASE_FILENAME
105
106         ldy     tmp4
107
108         ;AX - points to filename
109         ;Y  - iocb to use, if open needed
110         jsr     newfd           ; maybe we don't need to open and can reuse an iocb
111                                 ; returns fd num to use in tmp2, all regs unchanged
112         bcs     doopen          ; C set: open needed
113         lda     #0              ; clears N flag
114         beq     finish
115
116 doopen: sta     ICBAL,y
117         txa
118         sta     ICBAH,y
119         ldx     tmp4
120         lda     #OPEN
121         sta     ICCOM,x
122         jsr     CIOV
123
124         ; clean up the stack
125
126 finish: php
127         txa
128         pha
129         tya
130         pha
131
132 .ifdef  UCASE_FILENAME
133         ldy     tmp3            ; get size
134         jsr     addysp          ; free used space on the stack
135 .endif  ; defined UCASE_FILENAME
136
137         jsr     incsp4          ; clean up stack
138
139         pla
140         tay
141         pla
142         tax
143         plp
144
145         bpl     ok
146         sty     tmp3            ; remember error code
147         lda     #CLOSE
148         sta     ICCOM,x
149         jsr     CIOV            ; close IOCB again since open failed
150         jsr     fddecusage      ; and decrement usage counter of fd
151         lda     tmp3            ; put error code into A
152         jmp     __mappederrno
153
154 ok:     lda     tmp2            ; get fd
155         ldx     #0
156         stx     __oserror
157         rts
158
159 .endproc
160
161
162 ; closeallfiles: Close all files opened by the program.
163
164 .proc   closeallfiles
165
166         lda     #MAX_FD_INDEX-1
167 loop:   ldx     #0
168         pha
169         jsr     _close
170         pla
171         clc
172         sbc     #0
173         bpl     loop
174         rts
175
176 .endproc