Signed-off-by: Silvano Cirujano Cuesta <[email protected]>
---
 tools/bg_setenv.c | 98 +++++++++++++++++++++++++++--------------------
 1 file changed, 56 insertions(+), 42 deletions(-)

diff --git a/tools/bg_setenv.c b/tools/bg_setenv.c
index 1023395..262ccde 100644
--- a/tools/bg_setenv.c
+++ b/tools/bg_setenv.c
@@ -568,6 +568,56 @@ static bool get_env(char *configfilepath, BG_ENVDATA *data)
        return result;
 }
 
+static int printenv_from_file(char *envfilepath) {
+       int success = 0;
+       BG_ENVDATA data;
+
+       success = get_env(envfilepath, &data);
+       if (success) {
+               dump_env(&data);
+               return 0;
+       } else {
+               fprintf(stderr, "Error reading environment file.\n");
+               return 1;
+       }
+}
+
+static int dumpenv_to_file(char *envfilepath) {
+       /* execute journal and write to file */
+       int result = 0;
+       BGENV env;
+       BG_ENVDATA data;
+
+       memset(&env, 0, sizeof(BGENV));
+       memset(&data, 0, sizeof(BG_ENVDATA));
+       env.data = &data;
+
+       update_environment(&env);
+       if (verbosity) {
+               dump_env(env.data);
+       }
+       FILE *of = fopen(envfilepath, "wb");
+       if (of) {
+               if (fwrite(&data, sizeof(BG_ENVDATA), 1, of) != 1) {
+                       fprintf(stderr,
+                               "Error writing to output file: %s\n",
+                               strerror(errno));
+                       result = errno;
+               }
+               if (fclose(of)) {
+                       fprintf(stderr, "Error closing output file.\n");
+                       result = errno;
+               };
+               fprintf(stdout, "Output written to %s.\n", envfilepath);
+       } else {
+               fprintf(stderr, "Error opening output file %s (%s).\n",
+                       envfilepath, strerror(errno));
+               result = 1;
+       }
+
+       return result;
+}
+
 int main(int argc, char **argv)
 {
        static struct argp argp_setenv = {options_setenv, parse_opt, NULL, doc};
@@ -607,51 +657,15 @@ int main(int argc, char **argv)
 
        /* arguments are parsed, journal is filled */
 
-       /* is output to file ? */
-       if (envfilepath && write_mode) {
-               /* execute journal and write to file */
-               BGENV env;
-               BG_ENVDATA data;
-
-               memset(&env, 0, sizeof(BGENV));
-               memset(&data, 0, sizeof(BG_ENVDATA));
-               env.data = &data;
-
-               update_environment(&env);
-               if (verbosity) {
-                       dump_env(env.data);
-               }
-               FILE *of = fopen(envfilepath, "wb");
-               if (of) {
-                       if (fwrite(&data, sizeof(BG_ENVDATA), 1, of) != 1) {
-                               fprintf(stderr,
-                                       "Error writing to output file: %s\n",
-                                       strerror(errno));
-                               result = errno;
-                       }
-                       if (fclose(of)) {
-                               fprintf(stderr, "Error closing output file.\n");
-                               result = errno;
-                       };
-                       fprintf(stdout, "Output written to %s.\n", envfilepath);
+       /* is output to file or input from file ? */
+       if (envfilepath) {
+               if (write_mode) {
+                       result = dumpenv_to_file(envfilepath);
                } else {
-                       fprintf(stderr, "Error opening output file %s (%s).\n",
-                               envfilepath, strerror(errno));
-                       result = 1;
+                       result = printenv_from_file(envfilepath);
                }
                free(envfilepath);
-
-               return 0;
-       } else if (envfilepath && !write_mode) {
-               BG_ENVDATA env;
-               if (!get_env(envfilepath, &env)) {
-                       fprintf(stderr, "Error reading environment file.\n");
-                       return 1;
-               }
-
-               dump_env(&env);
-
-               return 0;
+               return result;
        }
 
        /* not in file mode */
-- 
2.29.2

-- 
You received this message because you are subscribed to the Google Groups "EFI 
Boot Guard" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/efibootguard-dev/20201201123137.96569-6-silvano.cirujano-cuesta%40siemens.com.

Reply via email to