;
-; Christian Groessler, May-2000
+; Christian Groessler, Jan-2003
;
; int open(const char *name,int flags,...);
;
.include "atari.inc"
- .include "fmode.inc"
+ .include "fcntl.inc"
.include "errno.inc"
+ .include "fd.inc"
+
.export _open
+ .destructor closeallfiles, 17
+
+ .import _close
.import clriocb
.import fddecusage,newfd
.import findfreeiocb
- .import __do_oserror,__seterrno,incsp4
+ .import __do_oserror,incsp4
.import ldaxysp,addysp
.import __oserror
.importzp tmp4,tmp2
beq iocbok ; we found one
lda #<EMFILE ; "too many open files"
- ldx #>EMFILE
seterr: jsr __seterrno
jsr incsp4 ; clean up stack
lda #$FF
rts ; return -1
; process the mode argument
- ; @@@TODO: append not handled yet!
iocbok: stx tmp4
jsr clriocb ; init with zero
ldy #1
jsr ldaxysp ; get mode
ldx tmp4
+ pha
+ and #O_APPEND
+ beq no_app
+ pla
+ and #15
+ cmp #O_RDONLY ; DOS supports append with write-only only
+ beq invret
+ cmp #O_RDWR
+ beq invret
+ lda #OPNOT|APPEND
+ bne set
+
+.ifndef UCASE_FILENAME
+invret: lda #<EINVAL ; file name is too long
+ ldx #>EINVAL
+ jmp seterr
+.endif
+
+no_app: pla
+ and #15
cmp #O_RDONLY
bne l1
lda #OPNIN
jsr ucase_fn
bcc ucok1
- lda #<EINVAL ; file name is too long
+invret: lda #<EINVAL ; file name is too long
ldx #>EINVAL
jmp seterr
ucok1:
rts
.endproc
+
+
+; closeallfiles: Close all files opened by the program.
+
+.proc closeallfiles
+
+ lda #MAX_FD_INDEX-1
+loop: ldx #0
+ pha
+ jsr _close
+ pla
+ clc
+ sbc #0
+ bpl loop
+ rts
+
+.endproc