]> git.sur5r.net Git - cc65/blobdiff - libsrc/cbm/write.s
Added two useful opcode mnemonic aliases.
[cc65] / libsrc / cbm / write.s
index 7a1441fe005299a89b8dc68b921abe09e9503fd5..fdf7cfbc18fa354b72284ebbebad77407361123f 100644 (file)
@@ -7,19 +7,21 @@
         .export         _write
         .constructor    initstdout
 
+        .import         SETLFS, OPEN, CKOUT, BSOUT, READST, CLRCH
         .import         rwcommon
-        .import         __errno, __oserror
         .importzp       sp, ptr1, ptr2, ptr3
 
+        .include        "cbm.inc"
         .include        "errno.inc"
         .include        "fcntl.inc"
-        .include        "cbm.inc"
         .include        "filedes.inc"
 
 
 ;--------------------------------------------------------------------------
 ; initstdout: Open the stdout and stderr file descriptors for the screen.
 
+.segment        "INIT"
+
 .proc   initstdout
 
         lda     #LFN_WRITE
 ;--------------------------------------------------------------------------
 ; _write
 
+.code
 
 .proc   _write
 
         jsr     rwcommon        ; Pop params, check handle
-        bcs     invalidfd       ; Branch if handle not ok
+        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
+
+; Check the status
+
+        pha
+        jsr     READST
+        lsr     a               ; Bit zero is write timeout
+        bne     devnotpresent2
+        pla
+        bcs     @L3
 
 ; Count characters written
 
         inc     ptr1+1
         bne     @L0
 
-; Wrote all chars, close the output channel
+; Wrote all chars or disk full. Close the output channel
 
-        jsr     CLRCH
+@L3:    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 descriptor is invalid
+; Error entry: Device not present
 
-invalidfd:
-        lda     #EINVAL
-        sta     __errno
-        lda     #0
-        sta     __errno+1
-        beq     errout
-
-; Error entry, file is not open
+devnotpresent2:
+        pla
+devnotpresent:
+        lda     #ENODEV
+        jmp     __directerrno   ; Sets _errno, clears _oserror, returns -1
 
-notopen:
-        lda     #3              ; File not open
-        bne     error
+; Error entry: The given file descriptor is not valid or not open
 
-; Error entry, status not ok
-
-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