]> skyeroc.xyz Git - dgamelaunch/commitdiff
Avoid calling lseek() on a descriptor managed by stdio.
authorJilles Tjoelker <jilles@stack.nl>
Fri, 23 Jan 2004 18:21:07 +0000 (18:21 +0000)
committerJilles Tjoelker <jilles@stack.nl>
Fri, 23 Jan 2004 18:21:07 +0000 (18:21 +0000)
This often leads to undefined behaviour.
Function: set_seek_offset_clrscr()

git-svn-id: svn://katsu.triplehelix.org/dgamelaunch/trunk@153 db0b04b0-f4d1-0310-9a6d-de3e77497b0e

ttyplay.c

index 1fc9d7fad457317e8135a0a6f783f563ec3daf94..b2ba4c6b70b52c5603e9ae82af605b281c753cd4 100644 (file)
--- a/ttyplay.c
+++ b/ttyplay.c
@@ -258,7 +258,7 @@ ttyplay (FILE * fp, double speed, ReadFunc read_func,
   /* for dtype's attempt to get the last clrscr and playback from there */
   if (offset)
     {
-      lseek (fileno (fp), offset, SEEK_SET);
+      fseek (fp, offset, SEEK_SET);
     }
 
   while (1)
@@ -293,10 +293,10 @@ set_seek_offset_clrscr (FILE * fp)
   int i;
   int bytesread;
 
-  lseek (fileno (fp), 0, SEEK_SET);
   fstat (fileno (fp), &mystat);
   buf = malloc (mystat.st_size);
-  bytesread = read (fileno (fp), buf, mystat.st_size);
+  fseek (fp, 0, SEEK_SET);
+  bytesread = fread (buf, 1, mystat.st_size, fp);
 
   /* one byte at at time sucks, but is a simple hack for the temp
    * being to avoid looking for wraparounds */
@@ -332,7 +332,7 @@ set_seek_offset_clrscr (FILE * fp)
   free (buf);
 
   /* now find last filepos that is less than seek offset */
-  lseek (fileno (fp), 0, SEEK_SET);
+  fseek (fp, 0, SEEK_SET);
   while (1)
     {
       char *buf;
@@ -343,9 +343,9 @@ set_seek_offset_clrscr (FILE * fp)
           break;
         }
 
-      if (lseek (fileno (fp), 0, SEEK_CUR) < raw_seek_offset)
+      if (ftell (fp) < raw_seek_offset)
         {
-          seek_offset_clrscr = lseek (fileno (fp), 0, SEEK_CUR);
+          seek_offset_clrscr = ftell (fp);
         }
 
       free (buf);