Remove SOL body flags
[neverball] / share / common.c
index b0ca104..3818202 100644 (file)
@@ -20,7 +20,6 @@
 #include <stdlib.h>
 #include <stdio.h>
 #include <string.h>
-#include <errno.h>
 #include <time.h>
 #include <ctype.h>
 #include <stdarg.h>
 
 int read_line(char **dst, fs_file fin)
 {
-    char buffer[MAXSTR] = "";
-    int  buffer_size    = 0;
+    char buff[MAXSTR];
 
-    char *store      = NULL;
-    char *store_new  = NULL;
-    int   store_size = 0;
+    char *line, *new;
+    size_t len0, len1;
 
-    int seen_newline = 0;
+    line = NULL;
 
-    while (!seen_newline)
+    while (fs_gets(buff, sizeof (buff), fin))
     {
-        if (fs_gets(buffer, sizeof (buffer), fin) == NULL)
-        {
-            if (store_size > 0)
-                break;
-            else
-            {
-                *dst = NULL;
-                return 0;
-            }
-        }
-
-        buffer_size = strlen(buffer) + 1;
+        /* Append to data read so far. */
 
-        /* Erase trailing newline. */
-
-        if (buffer[buffer_size - 2] == '\n')
+        if (line)
         {
-            seen_newline = 1;
-            buffer[buffer_size - 2] = '\0';
-            buffer_size--;
+            new  = concat_string(line, buff, NULL);
+            free(line);
+            line = new;
         }
-
-        /* Allocate or reallocate space for the buffer. */
-
-        if ((store_new = (char *) realloc(store, store_size + buffer_size)))
+        else
         {
-            /* Avoid passing garbage to string functions. */
+            line = strdup(buff);
+        }
 
-            if (store == NULL)
-                store_new[0] = '\0';
+        /* Strip newline, if any. */
 
-            store       = store_new;
-            store_size += buffer_size;
+        len0 = strlen(line);
+        strip_newline(line);
+        len1 = strlen(line);
 
-            store_new = NULL;
-        }
-        else
+        if (len1 != len0)
         {
-            fprintf(stderr, "Failed to allocate memory.\n");
-
-            free(store);
-            *dst = NULL;
-            return 0;
+            /* We hit a newline, clean up and break. */
+            line = realloc(line, len1 + 1);
+            break;
         }
-
-        strncat(store, buffer, buffer_size);
     }
 
-    *dst = store;
-
-    return 1;
+    return (*dst = line) ? 1 : 0;
 }
 
 char *strip_newline(char *str)
@@ -281,7 +255,7 @@ char *base_name(const char *name, const char *suffix)
 
     base = path_last_sep(name);
 
-    strncpy(buf, base ? base + 1 : name, sizeof (buf));
+    strncpy(buf, base ? base + 1 : name, sizeof (buf) - 1);
 
     /* Remove the suffix. */