[PATCH] commands: add lodvar - load variable from file content
Sebastian Block
basti at linux-source.de
Sat Sep 6 05:25:08 PDT 2014
This adds a command to load a variable from file content.
It is a work-a-round for "var=$(cat file)".
Signed-off-by: Sebastian Block <basti at linux-source.de>
---
commands/Kconfig | 7 +++++
commands/Makefile | 1 +
commands/loadvar.c | 90 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 98 insertions(+)
create mode 100644 commands/loadvar.c
diff --git a/commands/Kconfig b/commands/Kconfig
index 3a49baf..c6b4e03 100644
--- a/commands/Kconfig
+++ b/commands/Kconfig
@@ -752,6 +752,13 @@ config CMD_SAVEENV
/dev/env0. Note that envfs can only handle files, directories are being
skipped silently.
+config CMD_LOADVAR
+ tristate
+ prompt "loadvar"
+ help
+ Load variables from file contents.
+ Usage: loadvar VAR FILE
+
# end Environment commands
endmenu
diff --git a/commands/Makefile b/commands/Makefile
index 52b6137..6553154 100644
--- a/commands/Makefile
+++ b/commands/Makefile
@@ -103,3 +103,4 @@ obj-$(CONFIG_CMD_LSPCI) += lspci.o
obj-$(CONFIG_CMD_IMD) += imd.o
obj-$(CONFIG_CMD_HWCLOCK) += hwclock.o
obj-$(CONFIG_CMD_USBGADGET) += usbgadget.o
+obj-$(CONFIG_CMD_LOADVAR) += loadvar.o
diff --git a/commands/loadvar.c b/commands/loadvar.c
new file mode 100644
index 0000000..f0b38d2
--- /dev/null
+++ b/commands/loadvar.c
@@ -0,0 +1,90 @@
+/*
+ * Copyright (c) 2014 Sebastian Block <basti at linux-source.de>
+ *
+ * See file CREDITS for list of people who contributed to this
+ * project.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ */
+
+#include <common.h>
+#include <command.h>
+#include <fs.h>
+#include <fcntl.h>
+#include <linux/ctype.h>
+#include <linux/stat.h>
+#include <errno.h>
+#include <xfuncs.h>
+#include <malloc.h>
+#include <environment.h>
+
+static int do_loadvar(int argc, char *argv[])
+{
+ int ret;
+ int fd;
+ char *buf;
+ int err = 0;
+ struct stat s;
+
+ if (argc < 3) {
+ perror("loadvar");
+ return 1;
+ }
+
+ ret = stat(argv[2], &s);
+ if (ret) {
+ perror("loadvar - could not stat file");
+ return 1;
+ }
+
+ buf = xmalloc(s.st_size + 1);
+
+ fd = open(argv[2], O_RDONLY);
+ if (fd < 0) {
+ err = 1;
+ printf("could not open %s: %s\n", argv[2], errno_str());
+ goto out;
+ }
+
+ ret = read(fd, buf, s.st_size);
+ if (ret < 0) {
+ err = 1;
+ printf("could not read %s: %s\n", argv[2], errno_str());
+ goto out;
+ }
+ close(fd);
+
+ buf[s.st_size] = '\0'; /* add string termination */
+
+ ret = setenv(argv[1], buf);
+ if (ret) {
+ printf("could not set var %s with value %s\n", argv[1], buf);
+ err = 1;
+ goto out;
+ }
+out:
+ free(buf);
+
+ return err;
+}
+
+BAREBOX_CMD_HELP_START(loadvar)
+BAREBOX_CMD_HELP_TEXT("Load content of FILE into VAR.")
+BAREBOX_CMD_HELP_END
+
+BAREBOX_CMD_START(loadvar)
+ .cmd = do_loadvar,
+ BAREBOX_CMD_DESC("load file content to var")
+ BAREBOX_CMD_OPTS("VAR FILE")
+ BAREBOX_CMD_GROUP(CMD_GRP_ENV)
+ BAREBOX_CMD_HELP(cmd_loadvar_help)
+BAREBOX_CMD_END
+
--
1.9.1
More information about the barebox
mailing list