All of lore.kernel.org
 help / color / mirror / Atom feed
From: Sebastian Block <basti@linux-source.de>
To: barebox@lists.infradead.org
Subject: [PATCH] commands: add lodvar - load variable from file content
Date: Sat, 06 Sep 2014 14:25:08 +0200	[thread overview]
Message-ID: <540AFD24.1000802@linux-source.de> (raw)

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@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@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

_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

             reply	other threads:[~2014-09-06 12:25 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-09-06 12:25 Sebastian Block [this message]
2014-09-06 12:37 ` [PATCH] commands: add lodvar - load variable from file content Alexander Aring
2014-09-06 13:03   ` Sebastian Block
2014-09-06 13:39     ` Alexander Aring

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=540AFD24.1000802@linux-source.de \
    --to=basti@linux-source.de \
    --cc=barebox@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.