;--------------------------------------------------------------------------
; _close
-
+
.proc _close
; Check if we have a valid handle
ldx unittab,y
jsr closecmdchannel ; Close the disk command channel
pla ; Get the error code from the disk
- jmp __mappederrno ; Set _oserror and _errno, returns 0/-1
+ jmp __mappederrno ; Set _oserror and _errno, return 0/-1
; Error entry: The given file descriptor is not valid or not open
invalidfd:
lda #EBADF
- jmp __directerrno ; Sets _errno, clears _oserror, returns -1
+ jmp __directerrno ; Set _errno, clear _oserror, return -1
.endproc
.import SETLFS, OPEN, CHKIN, BASIN, CLRCH, READST
.import rwcommon
.import popax
- .import __oserror
.importzp ptr1, ptr2, ptr3, tmp1, tmp2, tmp3
- .include "fcntl.inc"
.include "cbm.inc"
+ .include "errno.inc"
+ .include "fcntl.inc"
.include "filedes.inc"
;--------------------------------------------------------------------------
; initstdin: Open the stdin file descriptors for the keyboard
-
+
.segment "INIT"
.proc initstdin
.proc _read
jsr rwcommon ; Pop params, check handle
- bcs errout ; Invalid handle, errno already set
+ bcs invalidfd ; Invalid handle
; Check if the LFN is valid and the file is open for writing
tax
lda fdtab-LFN_OFFS,x; Get flags for this handle
and #LFN_READ ; File open for writing?
- beq notopen
+ beq invalidfd
; Check the EOF flag. If it is set, don't read anything
; Valid lfn. Make it the input file
jsr CHKIN
- bcs error
-
-; Go looping...
-
- bcc @L3 ; Branch always
+ bcc @L3 ; Branch if ok
+ jmp __mappederrno ; Store into __oserror, map to errno, return -1
; Read the next byte
jsr READST ; Read the IEEE status
sta tmp3 ; Save it
and #%10111111 ; Check anything but the EOI bit
- bne error5 ; Assume device not present
+ bne devnotpresent ; Assume device not present
; Store the byte just read
done: jsr CLRCH
-; Return the number of chars read
+; Clear _oserror and return the number of chars read
-eof: lda ptr3
+eof: lda #0
+ sta __oserror
+ lda ptr3
ldx ptr3+1
rts
-; Error entry, file is not open
+; Error entry: Device not present
-notopen:
- lda #3 ; File not open
- bne error
+devnotpresent:
+ lda #ENODEV
+ jmp __directerrno ; Sets _errno, clears _oserror, returns -1
-; Error entry, status not ok
+; Error entry: The given file descriptor is not valid or not open
-error5: lda #5 ; Device not present
-error: sta __oserror
-errout: lda #$FF
- tax ; Return -1
- rts
+invalidfd:
+ lda #EBADF
+ jmp __directerrno ; Sets _errno, clears _oserror, returns -1
.endproc
jsr popax ; Get the handle
cpx #$01
- bcs invhandle
- cmp #MAX_FDS
- bcs invhandle
+ bcs @L9
+ cmp #MAX_FDS ; Set carry if fd too large
sta tmp2
- rts ; Return with carry clear
-
-invhandle:
- lda #EINVAL
- sta __errno
- lda #0
- sta __errno+1
- rts ; Return with carry set
+@L9: rts ; Return with result in carry
.endproc
.import SETLFS, OPEN, CKOUT, BSOUT, CLRCH
.import rwcommon
- .import __oserror
.importzp sp, ptr1, ptr2, ptr3
- .include "fcntl.inc"
.include "cbm.inc"
+ .include "errno.inc"
+ .include "fcntl.inc"
.include "filedes.inc"
;--------------------------------------------------------------------------
; _write
-
+
.code
.proc _write
jsr rwcommon ; Pop params, check handle
- bcs errout ; Invalid handle, errno already set
+ bcs invalidfd ; Invalid handle
; Check if the LFN is valid and the file is open for writing
tax
lda fdtab-LFN_OFFS,x; Get flags for this handle
and #LFN_WRITE ; File open for writing?
- beq notopen
+ beq invalidfd
; Valid lfn. Make it the output file
jsr CKOUT
- bcs error
bcc @L2
+@error: jmp __mappederrno ; Store into __oserror, map to errno, return -1
; Output the next character from the buffer
bne @L1
inc ptr2+1 ; A = *buf++;
@L1: jsr BSOUT
- bcs error ; Bail out on errors
+ bcs @error ; Bail out on errors
; Count characters written
jsr CLRCH
-; Return the number of chars written
+; Clear _oserror and return the number of chars written
+ lda #0
+ sta __oserror
lda ptr3
ldx ptr3+1
rts
-; Error entry, file is not open
+; Error entry: Device not present
-notopen:
- lda #3 ; File not open
- bne error
+devnotpresent:
+ lda #ENODEV
+ jmp __directerrno ; Sets _errno, clears _oserror, returns -1
-; Error entry, status not ok
+; Error entry: The given file descriptor is not valid or not open
-error5: lda #5 ; Device not present
-error: sta __oserror
-errout: lda #$FF
- tax ; Return -1
- rts
+invalidfd:
+ lda #EBADF
+ jmp __directerrno ; Sets _errno, clears _oserror, returns -1
.endproc