From: Bastian Dehn Date: Fri, 14 Feb 2025 08:27:05 +0000 (+0100) Subject: change config read with size first X-Git-Tag: v1.0.35^2~1^2~6 X-Git-Url: https://gitweb.hhaalo.de/?a=commitdiff_plain;h=83b22bff6cb4ddc3ff626228e3cf454a25237d71;p=sane-kds-s2000w-net.git change config read with size first --- diff --git a/src/kds_s2000w_config.c b/src/kds_s2000w_config.c index 4c8a811..e793107 100644 --- a/src/kds_s2000w_config.c +++ b/src/kds_s2000w_config.c @@ -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; }