Merge pull request #44 from philburk/fix_11_off_t
authorPhil Burk <philburk@mobileer.com>
Wed, 25 Apr 2018 03:24:33 +0000 (20:24 -0700)
committerGitHub <noreply@github.com>
Wed, 25 Apr 2018 03:24:33 +0000 (20:24 -0700)
Replace off_t with file_offset_t

1  2 
csrc/pf_inner.c

diff --combined csrc/pf_inner.c
  **
  ***************************************************************/
  
- #ifndef AMIGA
- #include <sys/types.h>
- #else
- typedef long off_t;
- #endif
  #include "pf_all.h"
  
  #if defined(WIN32) && !defined(__MINGW32__)
@@@ -750,8 -744,8 +744,8 @@@ DBUGX(("After Branch: IP = 0x%x\n", Ins
  /* Calculate product sign: */
                  sg = ((cell_t)(ahi ^ bhi) < 0);
  /* Take absolute values and reduce to um* */
 -                if ((cell_t)ahi < 0) ahi = (ucell_t)(-ahi);
 -                if ((cell_t)bhi < 0) bhi = (ucell_t)(-bhi);
 +                if ((cell_t)ahi < 0) ahi = (ucell_t)(-(cell_t)ahi);
 +                if ((cell_t)bhi < 0) bhi = (ucell_t)(-(cell_t)bhi);
  
  /* Break into hi and lo 16 bit parts. */
                  alo = LOWER_HALF(ahi);
@@@ -1035,24 -1029,38 +1029,38 @@@ DBUG(("XX ah,m,l = 0x%8x,%8x,%8x - qh,
              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;
              {
-                 off_t endposition, offsetHi;
-                 off_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(off_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 */
+                 file_offset_t endposition = -1;
+                 file_offset_t original = sdTellFile( FileID );
+                 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;
  
  
          case ID_FILE_REPOSITION: /* ( ud fid -- ior ) */
              {
-                 off_t offset;
+                 file_offset_t offset;
+                 cell_t offsetHigh;
+                 cell_t offsetLow;
                  FileID = (FileStream *) TOS;
-                 offset = M_POP;
-                 /* Avoid compiler warnings on Mac. */
-                 offset = (sizeof(off_t) > sizeof(cell_t)) ? (offset << 8*sizeof(cell_t)) : 0 ;
-                 offset += M_POP;
+                 offsetHigh = M_POP;
+                 offsetLow = M_POP;
+                 /* We do not support double precision file offsets in pForth.
+                  * So check to make sure the high bits are not used.
+                  */
+                 if (offsetHigh != 0)
+                 {
+                     TOS = -3; /* TODO err num? */
+                     break;
+                 }
+                 offset = offsetLow;
                  TOS = sdSeekFile( FileID, offset, PF_SEEK_SET );
              }
              endcase;
  
          case ID_FILE_POSITION: /* ( fid -- ud ior ) */
              {
-                 off_t position;
-                 off_t offsetHi;
+                 file_offset_t position;
                  FileID = (FileStream *) TOS;
                  position = sdTellFile( FileID );
-                 M_PUSH(position);
-                 /* Just use a 0 if they are the same size. */
-                 offsetHi = (sizeof(off_t) > sizeof(cell_t)) ? (position >> (8*sizeof(cell_t))) : 0 ;
-                 M_PUSH(offsetHi);
-                 TOS = (position < 0) ? -4 : 0 ; /* !!! err num */
+                 if (position < 0)
+                 {
+                     M_PUSH(0); /* low */
+                     M_PUSH(0); /* high */
+                     TOS = -4;  /* TODO proper error number */
+                 }
+                 else
+                 {
+                     M_PUSH(position); /* low */
+                     /* We do not support double precision file offsets.*/
+                     M_PUSH(0); /* high */
+                     TOS = 0; /* OK */
+                 }
              }
              endcase;