From: Lukáš Jiřiště Date: Wed, 9 Sep 2026 19:38:49 +0000 (+0200) Subject: Fix saving X-Git-Url: https://git.ljiriste.work/?a=commitdiff_plain;h=6b7a4d25bd636191602b154586b58d81137d32eb;p=FET_sim.git Fix saving 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. --- diff --git a/src/c_save.c b/src/c_save.c index 0aecd70..9451d9e 100644 --- a/src/c_save.c +++ b/src/c_save.c @@ -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) { diff --git a/src/main.c b/src/main.c index 2772e54..a9aab7d 100644 --- a/src/main.c +++ b/src/main.c @@ -3,6 +3,7 @@ #include #include #include +#include #include /* @@ -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)