]> git.sur5r.net Git - cc65/blob - libsrc/atari/open.s
Removed (pretty inconsistently used) tab chars from source code base.
[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
95         jsr     ucase_fn
96         bcc     ucok1
97 invret: lda     #<EINVAL        ; file name is too long
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         tya                     ; put error code into A
145         jmp     __mappederrno
146
147 ok:     lda     tmp2            ; get fd
148         ldx     #0
149         stx     __oserror
150         rts
151
152 .endproc
153
154
155 ; closeallfiles: Close all files opened by the program.
156
157 .proc   closeallfiles
158
159         lda     #MAX_FD_INDEX-1
160 loop:   ldx     #0
161         pha
162         jsr     _close
163         pla
164         clc
165         sbc     #0
166         bpl     loop
167         rts
168
169 .endproc