[PATCH 1/3] scripts/kwbimage: Move configfile opening to the function that reads it

Sascha Hauer s.hauer at pengutronix.de
Thu Oct 15 01:18:54 PDT 2015


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 <s.hauer at pengutronix.de>
---
 scripts/kwbimage.c | 34 ++++++++++++++++++----------------
 1 file changed, 18 insertions(+), 16 deletions(-)

diff --git a/scripts/kwbimage.c b/scripts/kwbimage.c
index 5b8e738..16be2dd 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;
-- 
2.6.1




More information about the barebox mailing list