env: Move env_set() to env.h
[oweals/u-boot.git] / tools / env / fw_env.c
index 77eac3d6c1736eabc97fe5eadb138fb2e08d90a4..f06252d9168af09a6e31e48f59570e3380c36f22 100644 (file)
@@ -1,16 +1,16 @@
+// SPDX-License-Identifier: GPL-2.0+
 /*
  * (C) Copyright 2000-2010
  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  *
  * (C) Copyright 2008
  * Guennadi Liakhovetski, DENX Software Engineering, lg@denx.de.
- *
- * SPDX-License-Identifier:    GPL-2.0+
  */
 
 #define _GNU_SOURCE
 
 #include <compiler.h>
+#include <env.h>
 #include <errno.h>
 #include <env_flags.h>
 #include <fcntl.h>
@@ -738,7 +738,8 @@ int fw_env_set(int argc, char *argv[], struct env_opts *opts)
 int fw_parse_script(char *fname, struct env_opts *opts)
 {
        FILE *fp;
-       char dump[1024];        /* Maximum line length in the file */
+       char *line = NULL;
+       size_t linesize = 0;
        char *name;
        char *val;
        int lineno = 0;
@@ -764,36 +765,34 @@ int fw_parse_script(char *fname, struct env_opts *opts)
                }
        }
 
-       while (fgets(dump, sizeof(dump), fp)) {
+       while ((len = getline(&line, &linesize, fp)) != -1) {
                lineno++;
-               len = strlen(dump);
 
                /*
-                * Read a whole line from the file. If the line is too long
-                * or is not terminated, reports an error and exit.
+                * Read a whole line from the file. If the line is not
+                * terminated, reports an error and exit.
                 */
-               if (dump[len - 1] != '\n') {
+               if (line[len - 1] != '\n') {
                        fprintf(stderr,
-                               "Line %d not corrected terminated or too long\n",
+                               "Line %d not correctly terminated\n",
                                lineno);
                        ret = -1;
                        break;
                }
 
                /* Drop ending line feed / carriage return */
-               dump[--len] = '\0';
-               if (len && dump[len - 1] == '\r')
-                       dump[--len] = '\0';
+               line[--len] = '\0';
+               if (len && line[len - 1] == '\r')
+                       line[--len] = '\0';
 
                /* Skip comment or empty lines */
-               if (len == 0 || dump[0] == '#')
+               if (len == 0 || line[0] == '#')
                        continue;
 
                /*
-                * Search for variable's name,
-                * remove leading whitespaces
+                * Search for variable's name remove leading whitespaces
                 */
-               name = skip_blanks(dump);
+               name = skip_blanks(line);
                if (!name)
                        continue;
 
@@ -830,6 +829,7 @@ int fw_parse_script(char *fname, struct env_opts *opts)
                }
 
        }
+       free(line);
 
        /* Close file if not stdin */
        if (strcmp(fname, "-") != 0)
@@ -843,10 +843,10 @@ int fw_parse_script(char *fname, struct env_opts *opts)
 }
 
 /**
- * environment_end() - compute offset of first byte right after environemnt
+ * environment_end() - compute offset of first byte right after environment
  * @dev - index of enviroment buffer
  * Return:
- *  device offset of first byte right after environemnt
+ *  device offset of first byte right after environment
  */
 off_t environment_end(int dev)
 {
@@ -1318,7 +1318,7 @@ static int flash_io_write(int fd_current)
                        rc = -1;
                }
 
-               if (target_temp) {
+               if (rc >= 0 && target_temp) {
                        int dir_fd;
 
                        dir_fd = open(dname, O_DIRECTORY | O_RDONLY);
@@ -1567,7 +1567,7 @@ int fw_env_open(struct env_opts *opts)
                free(addr0);
 
        if (addr1)
-               free(addr0);
+               free(addr1);
 
        return ret;
 }
@@ -1743,7 +1743,7 @@ static int parse_config(struct env_opts *opts)
 
                if (ENVSIZE(0) != ENVSIZE(1)) {
                        fprintf(stderr,
-                               "Redundant environments have unequal size");
+                               "Redundant environments have unequal size\n");
                        return -1;
                }
        }
@@ -1761,19 +1761,20 @@ static int get_config(char *fname)
        FILE *fp;
        int i = 0;
        int rc;
-       char dump[128];
+       char *line = NULL;
+       size_t linesize = 0;
        char *devname;
 
        fp = fopen(fname, "r");
        if (fp == NULL)
                return -1;
 
-       while (i < 2 && fgets(dump, sizeof(dump), fp)) {
-               /* Skip incomplete conversions and comment strings */
-               if (dump[0] == '#')
+       while (i < 2 && getline(&line, &linesize, fp) != -1) {
+               /* Skip comment strings */
+               if (line[0] == '#')
                        continue;
 
-               rc = sscanf(dump, "%ms %lli %lx %lx %lx",
+               rc = sscanf(line, "%ms %lli %lx %lx %lx",
                            &devname,
                            &DEVOFFSET(i),
                            &ENVSIZE(i), &DEVESIZE(i), &ENVSECTORS(i));
@@ -1789,6 +1790,7 @@ static int get_config(char *fname)
 
                i++;
        }
+       free(line);
        fclose(fp);
 
        have_redund_env = i - 1;