From e0489cd1a53be4d921447ccc0fbcfdc436fc0b0a Mon Sep 17 00:00:00 2001 From: Phil Burk Date: Mon, 23 Apr 2018 21:03:17 -0700 Subject: [PATCH] Make FILE-SIZE more consistent with FILE-POSITION. --- csrc/pf_inner.c | 35 ++++++++++++++++++++++++----------- csrc/pf_io.c | 2 +- 2 files changed, 25 insertions(+), 12 deletions(-) diff --git a/csrc/pf_inner.c b/csrc/pf_inner.c index 606dc5c..a157705 100644 --- a/csrc/pf_inner.c +++ b/csrc/pf_inner.c @@ -1029,25 +1029,38 @@ DBUG(("XX ah,m,l = 0x%8x,%8x,%8x - qh,l = 0x%8x,%8x\n", ah,am,al, qh,ql )); Scratch = M_POP; CharPtr = (char *) M_POP; Temp = sdReadFile( CharPtr, 1, Scratch, FileID ); + /* TODO check feof() or ferror() */ M_PUSH(Temp); TOS = 0; endcase; + /* TODO Why does this crash when passed an illegal FID? */ case ID_FILE_SIZE: /* ( fid -- ud ior ) */ /* Determine file size by seeking to end and returning position. */ FileID = (FileStream *) TOS; { - file_offset_t endposition, offsetHi; + file_offset_t endposition = -1; file_offset_t original = sdTellFile( FileID ); - sdSeekFile( FileID, 0, PF_SEEK_END ); - endposition = sdTellFile( FileID ); - M_PUSH(endposition); - /* Just use a 0 if they are the same size. */ - offsetHi = (sizeof(file_offset_t) > sizeof(cell_t)) - ? (endposition >> (8*sizeof(cell_t))) : 0 ; - M_PUSH(offsetHi); - sdSeekFile( FileID, original, PF_SEEK_SET ); - TOS = (original < 0) ? -4 : 0 ; /* !!! err num */ + if (original >= 0) + { + sdSeekFile( FileID, 0, PF_SEEK_END ); + endposition = sdTellFile( FileID ); + /* Restore original position. */ + sdSeekFile( FileID, original, PF_SEEK_SET ); + } + if (endposition < 0) + { + M_PUSH(0); /* low */ + M_PUSH(0); /* high */ + TOS = -4; /* TODO proper error number */ + } + else + { + M_PUSH(endposition); /* low */ + /* We do not support double precision file offsets.*/ + M_PUSH(0); /* high */ + TOS = 0; /* OK */ + } } endcase; @@ -1096,7 +1109,7 @@ DBUG(("XX ah,m,l = 0x%8x,%8x,%8x - qh,l = 0x%8x,%8x\n", ah,am,al, qh,ql )); M_PUSH(position); /* low */ /* We do not support double precision file offsets.*/ M_PUSH(0); /* high */ - TOS = 0; + TOS = 0; /* OK */ } } endcase; diff --git a/csrc/pf_io.c b/csrc/pf_io.c index ea6f4b5..da16a14 100644 --- a/csrc/pf_io.c +++ b/csrc/pf_io.c @@ -202,7 +202,7 @@ cell_t sdSeekFile( FileStream * Stream, file_offset_t Position, int32_t Mode ) TOUCH(Mode); return 0; } -cell_t sdTellFile( FileStream * Stream ) +file_offset_t sdTellFile( FileStream * Stream ) { UNIMPLEMENTED("sdTellFile"); TOUCH(Stream); -- 2.20.1