getc: support for unknown file sizes
authorH. Peter Anvin <hpa@zytor.com>
Wed, 27 Feb 2008 03:57:26 +0000 (19:57 -0800)
committerH. Peter Anvin <hpa@zytor.com>
Wed, 27 Feb 2008 03:57:26 +0000 (19:57 -0800)
Handle unknown file sizes in getc

getc.inc

index eddba80..b36115c 100644 (file)
--- a/getc.inc
+++ b/getc.inc
@@ -23,12 +23,11 @@ MAX_GETC    equ (1 << MAX_GETC_LG2)
 bytes_per_getc_lg2     equ 16-MAX_GETC_LG2
 bytes_per_getc         equ (1 << bytes_per_getc_lg2)
 secs_per_getc  equ bytes_per_getc/SECTOR_SIZE
-MAX_UNGET      equ 5                   ; Max bytes that can be pushed back
+MAX_UNGET      equ 9                   ; Max bytes that can be pushed back
 
                struc getc_file
 gc_file                resw 1                  ; File pointer
 gc_bufbytes    resw 1                  ; Bytes left in buffer
-gc_bytes       resd 1                  ; Bytes left in file
 gc_bufdata     resw 1                  ; Pointer to data in buffer
 gc_unget_cnt   resb 1                  ; Character pushed back count
 gc_unget_buf   resb MAX_UNGET          ; Character pushed back buffer
@@ -73,7 +72,6 @@ openfd:
                mov [CurrentGetC],bx
 
                mov [bx+gc_file],si     ; File pointer
-               mov [bx+gc_bytes],eax   ; Bytes available
                xor ax,ax
                mov [bx+gc_bufbytes],ax         ; Buffer empty
                mov [bx+gc_unget_cnt],al        ; ungetc buffer empty
@@ -132,27 +130,22 @@ getc:
 
                mov [di+gc_bufdata],bx
                mov si,[di+gc_file]
-               mov ecx,[di+gc_bytes]
-               jecxz .empty
-               cmp ecx,bytes_per_getc
-               jna .sizeok
-               mov ecx,bytes_per_getc
-.sizeok:
-               mov [di+gc_bufbytes],cx
-               sub [di+gc_bytes],ecx
-               add cx,SECTOR_SIZE-1
-               shr cx,SECTOR_SHIFT
+               and si,si
+               mov [di+gc_bufbytes],si ; In case SI == 0
+               jz .empty
+               mov cx,bytes_per_getc >> SECTOR_SHIFT
                call getfssec
+               mov [di+gc_bufbytes],cx
                mov [di+gc_file],si
+               jcxz .empty
                popad
+               TRACER 'd'
                jmp .got_data
 
 .empty:
-               ; CX == 0 at this point; gc_bufbytes was clobbered
-               ; by the subtract; we need to restore it to zero so
-               ; we will continue to get EOF on any further attempts
-               ; to read the file.
-               mov [di+gc_bufbytes],cx
+               TRACER 'e'
+               ; [di+gc_bufbytes] is zero already, thus we will continue
+               ; to get EOF on any further attempts to read the file.
                popad
                xor al,al               ; Return a predictable zero
                stc