diff mbox series

[libubootenv,1/2] fw_printenv: dont hard-code configuration/environment files

Message ID 20220902104110.1351380-2-liu.ming50@gmail.com
State Changes Requested
Headers show
Series [libubootenv,1/2] fw_printenv: dont hard-code configuration/environment files | expand

Commit Message

Ming Liu Sept. 2, 2022, 10:41 a.m. UTC
From: Ming Liu <liu.ming50@gmail.com>

Avoid to hard-code configuration and environment files, allow them to
be configurable during build. This is usefull when some end users want
to distinguish these files at build time rather than run time with
command parameters.

Signed-off-by: Ming Liu <liu.ming50@gmail.com>
---
 CMakeLists.txt    |  8 ++++++++
 src/fw_printenv.c | 16 ++++++++++++----
 2 files changed, 20 insertions(+), 4 deletions(-)
diff mbox series

Patch

diff --git a/CMakeLists.txt b/CMakeLists.txt
index f855b73..94ea221 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -12,6 +12,14 @@  set(VERSION	"0.3.2")
 SET(SOVERSION "0")
 add_definitions(-DVERSION="${VERSION}")
 
+if(DEFAULT_CFG_FILE)
+    add_definitions(-DDEFAULT_CFG_FILE="${DEFAULT_CFG_FILE}")
+endif(DEFAULT_CFG_FILE)
+
+if(DEFAULT_ENV_FILE)
+    add_definitions(-DDEFAULT_ENV_FILE="${DEFAULT_ENV_FILE}")
+endif(DEFAULT_ENV_FILE)
+
 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=gnu99")
 
 #set(CMAKE_C_FLAGS_DEBUG "-g")
diff --git a/src/fw_printenv.c b/src/fw_printenv.c
index 86f721d..44d0380 100644
--- a/src/fw_printenv.c
+++ b/src/fw_printenv.c
@@ -20,6 +20,14 @@ 
 #define VERSION "0.1"
 #endif
 
+#ifndef DEFAULT_CFG_FILE
+#define DEFAULT_CFG_FILE "/etc/fw_env.config"
+#endif
+
+#ifndef DEFAULT_ENV_FILE
+#define DEFAULT_ENV_FILE "/etc/u-boot-initial-env"
+#endif
+
 #define PROGRAM_SET	"fw_setenv"
 
 static struct option long_options[] = {
@@ -39,8 +47,8 @@  static void usage(char *program, bool setprogram)
 			program);
 	fprintf(stdout,
 		" -h, --help                       : print this help\n"
-		" -c, --config <filename>          : configuration file (old fw_env.config)\n"
-		" -f, --defenv <filename>          : default environment if no one found\n"
+		" -c, --config <filename>          : configuration file (by default: " DEFAULT_CFG_FILE ")\n"
+		" -f, --defenv <filename>          : default environment if no one found (by default: " DEFAULT_ENV_FILE ")\n"
 		" -V, --version                    : print version and exit\n"
 	);
 	if (!setprogram)
@@ -127,7 +135,7 @@  int main (int argc, char **argv) {
 	}
 
 	if (!cfgfname)
-		cfgfname = "/etc/fw_env.config";
+		cfgfname = DEFAULT_CFG_FILE;
 
 	if ((ret = libuboot_read_config(ctx, cfgfname)) < 0) {
 		fprintf(stderr, "Configuration file wrong or corrupted\n");
@@ -135,7 +143,7 @@  int main (int argc, char **argv) {
 	}
 
 	if (!defenvfile)
-		defenvfile = "/etc/u-boot-initial-env";
+		defenvfile = DEFAULT_ENV_FILE;
 
 	if ((ret = libuboot_open(ctx)) < 0) {
 		fprintf(stderr, "Cannot read environment, using default\n");