From 119f75f23a92b5171978a6691fd10e12247628ea Mon Sep 17 00:00:00 2001 From: Sascha Hauer Date: Thu, 15 Oct 2015 09:54:48 +0200 Subject: scripts/kwbimage: Move configfile opening to the function that reads it The configfile is read in image_create_config_parse(), so move opening the file to there aswell and pass the configfile as filename instead of FILE *. Signed-off-by: Sascha Hauer --- scripts/kwbimage.c | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/scripts/kwbimage.c b/scripts/kwbimage.c index 5b8e73892c..16be2dda1b 100644 --- a/scripts/kwbimage.c +++ b/scripts/kwbimage.c @@ -1098,12 +1098,20 @@ static int image_create_config_parse_oneline(char *line, * elements 'image_cfg', and return the number of configuration * elements in 'cfgn'. */ -static int image_create_config_parse(FILE *fcfg, +static int image_create_config_parse(const char *input, struct image_cfg_element *image_cfg, int *cfgn) { int ret; int cfgi = 0; + FILE *fcfg; + + fcfg = fopen(input, "r"); + if (!fcfg) { + fprintf(stderr, "Could not open input file %s\n", + input); + return -1; + } /* Parse the configuration file */ while (!feof(fcfg)) { @@ -1128,18 +1136,22 @@ static int image_create_config_parse(FILE *fcfg, ret = image_create_config_parse_oneline(line, &image_cfg[cfgi]); if (ret) - return ret; + goto out; cfgi++; if (cfgi >= IMAGE_CFG_ELEMENT_MAX) { fprintf(stderr, "Too many configuration elements in .cfg file\n"); - return -1; + ret = -1; + goto out; } } + ret = 0; *cfgn = cfgi; - return 0; +out: + fclose(fcfg); + return ret; } static int image_override_payload(struct image_cfg_element *image_cfg, @@ -1316,34 +1328,24 @@ static int image_create(const char *input, const char *output, int verbose) { struct image_cfg_element *image_cfg; - FILE *fcfg, *outputimg; + FILE *outputimg; void *image = NULL; int version; size_t imagesz; int cfgn; int ret; - fcfg = fopen(input, "r"); - if (!fcfg) { - fprintf(stderr, "Could not open input file %s\n", - input); - return -1; - } - image_cfg = malloc(IMAGE_CFG_ELEMENT_MAX * sizeof(struct image_cfg_element)); if (!image_cfg) { fprintf(stderr, "Cannot allocate memory\n"); - fclose(fcfg); return -1; } memset(image_cfg, 0, IMAGE_CFG_ELEMENT_MAX * sizeof(struct image_cfg_element)); - rewind(fcfg); - ret = image_create_config_parse(fcfg, image_cfg, &cfgn); - fclose(fcfg); + ret = image_create_config_parse(input, image_cfg, &cfgn); if (ret) { free(image_cfg); return -1; -- cgit v1.2.3