Fix fs_gets to return NULL on encountering EOF with no data read
authorparasti <parasti@78b8d119-cf0a-0410-b17c-f493084dd1d7>
Tue, 16 Nov 2010 22:00:43 +0000 (22:00 +0000)
committerparasti <parasti@78b8d119-cf0a-0410-b17c-f493084dd1d7>
Tue, 16 Nov 2010 22:00:43 +0000 (22:00 +0000)
git-svn-id: https://s.snth.net/svn/neverball/trunk@3364 78b8d119-cf0a-0410-b17c-f493084dd1d7

share/fs_common.c

index 275c497..007cc5f 100644 (file)
@@ -126,30 +126,35 @@ char *fs_gets(char *dst, int count, fs_file fh)
     if (fs_eof(fh))
         return NULL;
 
-    while (count > 1 && (c = fs_getc(fh)) >= 0)
-    {
-        count--;
+    while (count > 1)
+        if ((c = fs_getc(fh)) >= 0)
+        {
+            count--;
 
-        *s = c;
+            *s = c;
 
-        /* Keep a newline and break. */
+            /* Keep a newline and break. */
 
-        if (*s == '\n')
-        {
-            s++;
-            break;
-        }
+            if (*s == '\n')
+            {
+                s++;
+                break;
+            }
 
-        /* Ignore carriage returns. */
+            /* Ignore carriage returns. */
 
-        if (*s == '\r')
-        {
-            count++;
-            s--;
-        }
+            if (*s == '\r')
+            {
+                count++;
+                s--;
+            }
 
-        s++;
-    }
+            s++;
+        }
+        else if (s == dst)
+            return NULL;
+        else
+            break;
 
     *s = '\0';