netstar pushed a commit to branch master.

http://git.enlightenment.org/apps/evisum.git/commit/?id=18fa8063e1d72971721e43c391eb9800b23f6b19

commit 18fa8063e1d72971721e43c391eb9800b23f6b19
Author: Alastair Poole <[email protected]>
Date:   Sat Jun 20 00:48:15 2020 +0100

    machine: file_contents
---
 src/bin/main.c           |  1 -
 src/bin/system/machine.c | 28 ++++++++++------------------
 src/bin/ui/ui.c          |  2 ++
 3 files changed, 12 insertions(+), 19 deletions(-)

diff --git a/src/bin/main.c b/src/bin/main.c
index 8ed62e1..8e707d4 100644
--- a/src/bin/main.c
+++ b/src/bin/main.c
@@ -131,7 +131,6 @@ main(int argc, char **argv)
      {
         ecore_main_loop_begin();
         evisum_ui_shutdown(ui);
-        free(ui);
      }
 
    elm_shutdown();
diff --git a/src/bin/system/machine.c b/src/bin/system/machine.c
index 54ee47d..9a7c247 100644
--- a/src/bin/system/machine.c
+++ b/src/bin/system/machine.c
@@ -85,31 +85,23 @@ file_contents(const char *path)
 {
    FILE *f;
    char *buf, *tmp;
-   char byte[1];
-   size_t count, n, bytes = 0;
+   size_t n = 1, len = 0;
+   const size_t block = 4096;
 
    f = fopen(path, "r");
    if (!f) return NULL;
 
-   n = 1024;
+   buf = NULL;
 
-   buf = malloc(n * sizeof(byte) + 1);
-   if (!buf) return NULL;
-
-   while ((count = (fread(byte, sizeof(byte), 1, f))) > 0)
+   while ((!feof(f)) && (!ferror(f)))
      {
-        bytes += sizeof(byte);
-        if (bytes == (n * sizeof(byte)))
-          {
-             n *= 2;
-             tmp = realloc(buf, n * sizeof(byte) + 1);
-             if (!tmp) return NULL;
-             buf = tmp;
-          }
-        memcpy(&buf[bytes - sizeof(byte)], byte, sizeof(byte));
+        tmp = realloc(buf, ++n * (sizeof(char) * block) + 1);
+        if (!tmp) return NULL;
+        buf = tmp;
+        len += fread(buf + len, sizeof(char), block, f);
      }
 
-   if (!feof(f))
+   if (ferror(f))
      {
         free(buf);
         fclose(f);
@@ -117,7 +109,7 @@ file_contents(const char *path)
      }
    fclose(f);
 
-   buf[bytes] = 0;
+   buf[len] = 0;
 
    return buf;
 }
diff --git a/src/bin/ui/ui.c b/src/bin/ui/ui.c
index 7e2244a..ff397c9 100644
--- a/src/bin/ui/ui.c
+++ b/src/bin/ui/ui.c
@@ -1520,6 +1520,8 @@ evisum_ui_shutdown(Ui *ui)
      evisum_ui_item_cache_free(ui->cache);
 
    eina_lock_free(&_lock);
+
+   free(ui);
 }
 
 static void

-- 


Reply via email to