]> www.ljiriste.work Git - FET_sim.git/commitdiff
Fix saving
authorLukáš Jiřiště <redacted>
Wed, 9 Sep 2026 19:38:49 +0000 (21:38 +0200)
committerLukáš Jiřiště <redacted>
Wed, 9 Sep 2026 19:38:49 +0000 (21:38 +0200)
There were 3 problems:
I forgot about mode when creating a new file.
I did not read docs about write and assumed 0 is returned on success.
I have written the boolean return values in a way I am not happy with.

The first 2 problems are now solved, the second will need a refactor.

src/c_save.c
src/main.c

index 0aecd70528c1afc3d392c79e770d221cd1a06dd0..9451d9e15f642f7113d98ffa7666da3114567ebe 100644 (file)
@@ -51,6 +51,7 @@ static int    write_save(int fd, t_save *buffer, size_t byte_count)
 {
        size_t  saves_count;
        size_t  i;
+       int             res;
 
        saves_count = byte_count / sizeof(*buffer);
        i = 0;
@@ -59,7 +60,8 @@ static int    write_save(int fd, t_save *buffer, size_t byte_count)
                buffer[i] = to_big_endian(buffer[i]);
                ++i;
        }
-       return (write(fd, buffer, byte_count));
+       res = write(fd, buffer, byte_count);
+       return (res < 0 || (size_t)res != byte_count);
 }
 
 static int     write_header(int fd)
@@ -68,6 +70,7 @@ static int    write_header(int fd)
        t_save  version_copy;
 
        res = write(fd, g_signature, sizeof(g_signature));
+       res = (res < 0 || (size_t)res != sizeof(g_signature));
        version_copy = g_version;
        res = res || write_save(fd, &version_copy, sizeof(g_version));
        return (res);
@@ -135,6 +138,7 @@ static int  write_vec(int fd, const t_vec *vec, t_el_write write_el)
 
        val = to_big_endian(vec->size);
        res = write(fd, &val, sizeof(val));
+       res = (res < 0 || (size_t)res != sizeof(val));
        i = 0;
        while (i < vec->size)
        {
index 2772e541e273eb922a988cc0648ccb26fbd6046d..a9aab7d3beaa4ae855e09ef9cd9af7f350ef65df 100644 (file)
@@ -3,6 +3,7 @@
 #include <stdlib.h>
 #include <unistd.h>
 #include <fcntl.h>
+#include <sys/stat.h>
 #include <ncurses.h>
 
 /*
@@ -133,9 +134,9 @@ int has_correct_argc(t_input input)
                return (0);
        if (c == load && argc != 1)
                return (0);
-       if (c == save || argc != 1)
+       if (c == save && argc != 1)
                return (0);
-       if (c == openf || argc != 1)
+       if (c == openf && argc != 1)
                return (0);
        if (c == setnode && argc != 2)
                return (0);
@@ -215,7 +216,7 @@ int parse_arg(t_input *input, const char *str, size_t i)
                else if (c == save)
                {
                        input->argv[i].type = file_descriptor;
-                       input->argv[i].val.fd = open(str, O_WRONLY);
+                       input->argv[i].val.fd = open(str, O_CREAT | O_WRONLY | O_EXCL, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
                        res = (input->argv[i].val.fd >= 0);
                }
                else if (c == openf)