]> gitweb.hhaalo.de Git - sane-kds-s2000w-net.git/commitdiff
change config read with size first
authorBastian Dehn <hhaalo@arcor.de>
Fri, 14 Feb 2025 08:27:05 +0000 (09:27 +0100)
committerBastian Dehn <hhaalo@arcor.de>
Fri, 14 Feb 2025 08:31:05 +0000 (09:31 +0100)
src/kds_s2000w_config.c

index 4c8a8118a2eee78cd3943c1c97935384fd543c03..e7931075de52fc2c25ac579f494a478d11b55313 100644 (file)
@@ -18,28 +18,20 @@ void _kds_s2000w_config_copy_string_to_config_value(cfg_t* cfg, const char* key,
 char* kds_s2000w_config_read(const char* filename)
 {
        FILE* config_file = fopen(filename, "r");
-       char* buf = malloc(sizeof(char));
-       char** mem = malloc(sizeof(char*));
-       size_t* sizeloc = malloc(sizeof(size_t));
-       FILE* config_stream = open_memstream(mem, sizeloc);
+       size_t config_size = 0;
 
-       while (fread(buf, sizeof(char), 1, config_file) > 0) {
-               fwrite(buf, sizeof(char), 1, config_stream);
+       char buf = 0;
+       while (fread(&buf, sizeof(char), 1, config_file) > 0) {
+               config_size++;
        }
+       fseek(config_file, 0, SEEK_SET);
+
+       char* config_string = malloc(sizeof(char) * config_size + 1);
+       fread(config_string, config_size, 1, config_file);
+       config_string[config_size] = '\0';
 
        fclose(config_file);
        config_file = NULL;
-       fclose(config_stream);
-       config_stream = NULL;
-
-       char* config_string = *mem;
-
-       free(buf);
-       buf = NULL;
-       free(sizeloc);
-       sizeloc = NULL;
-       free(mem);
-       mem = NULL;
 
        return config_string;
 }