mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH v1] writef: added command to write content of variable to file
@ 2017-10-06  8:23 Gerd Pauli
  2017-10-16  8:38 ` Sascha Hauer
  0 siblings, 1 reply; 2+ messages in thread
From: Gerd Pauli @ 2017-10-06  8:23 UTC (permalink / raw)
  To: barebox; +Cc: Gerd Pauli

writef: added command to write content of variable to file.
        Adds a newline at the emd of the string.

Usage: writef VAR FILE

Signed-off-by: Gerd Pauli <gp@high-consulting.de>
---
 commands/Kconfig  | 11 +++++++++
 commands/Makefile |  1 +
 commands/writef.c | 71 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 83 insertions(+)
 create mode 100644 commands/writef.c

diff --git a/commands/Kconfig b/commands/Kconfig
index ae2dc4b..af2b215 100644
--- a/commands/Kconfig
+++ b/commands/Kconfig
@@ -1082,6 +1082,17 @@ config CMD_READF
 	  whitespaces are removed, nonvisible characters are stripped. Input is
 	  limited to 1024 characters.
 
+config CMD_WRITEF
+        tristate
+        prompt "writef"
+        help
+          Write variable into file
+
+          Usage: writef VAR FILE
+
+          Write Content of VARiable to FILE. A Newline is added
+          at the end of the String.
+
 config CMD_SLEEP
 	tristate
 	prompt "sleep"
diff --git a/commands/Makefile b/commands/Makefile
index 37486dc..16c1768 100644
--- a/commands/Makefile
+++ b/commands/Makefile
@@ -103,6 +103,7 @@ obj-$(CONFIG_CMD_BOOT)		+= boot.o
 obj-$(CONFIG_CMD_DEVINFO)	+= devinfo.o
 obj-$(CONFIG_CMD_DRVINFO)	+= drvinfo.o
 obj-$(CONFIG_CMD_READF)		+= readf.o
+obj-$(CONFIG_CMD_WRITEF)	+= writef.o
 obj-$(CONFIG_CMD_MENUTREE)	+= menutree.o
 obj-$(CONFIG_CMD_2048)		+= 2048.o
 obj-$(CONFIG_CMD_REGULATOR)	+= regulator.o
diff --git a/commands/writef.c b/commands/writef.c
new file mode 100644
index 0000000..dea18b0
--- /dev/null
+++ b/commands/writef.c
@@ -0,0 +1,71 @@
+/* -*- Mode:C; c-file-style:"linux"; -*- */
+
+/*
+ * writef.c - Write Content of Variable to File
+ *
+ * Copyright (c) 2017 Gerd Pauli <gp@high-consulting.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 <libfile.h>
+#include <malloc.h>
+#include <linux/stat.h>
+#include <linux/ctype.h>
+#include <environment.h>
+
+static int do_writef(int argc, char *argv[])
+{
+	const char *val;
+	char *variable, *filename;
+	size_t size;
+	void *buf;
+	int ret;
+
+	if (argc != 3)
+		return COMMAND_ERROR_USAGE;
+
+	variable = argv[1];
+	filename = argv[2];
+
+	val = getenv(variable);
+	if (val == NULL)
+		return COMMAND_ERROR;
+
+	size = strlen(val);
+	size++;
+
+	buf = xmalloc(size+1);
+	sprintf(buf, "%s\n", val);
+
+	ret = write_file(filename, buf, size);
+	free(buf);
+	return ret;
+}
+
+BAREBOX_CMD_HELP_START(writef)
+BAREBOX_CMD_HELP_TEXT("Write Content of VARiable to FILE. A Newline is added")
+BAREBOX_CMD_HELP_TEXT("at the end of the String.")
+BAREBOX_CMD_HELP_END
+
+BAREBOX_CMD_START(writef)
+.cmd		= do_writef,
+	BAREBOX_CMD_DESC("write variable into file")
+	BAREBOX_CMD_OPTS("VAR FILE")
+	BAREBOX_CMD_GROUP(CMD_GRP_SCRIPT)
+	BAREBOX_CMD_HELP(cmd_writef_help)
+	BAREBOX_CMD_END
-- 
1.9.1


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

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH v1] writef: added command to write content of variable to file
  2017-10-06  8:23 [PATCH v1] writef: added command to write content of variable to file Gerd Pauli
@ 2017-10-16  8:38 ` Sascha Hauer
  0 siblings, 0 replies; 2+ messages in thread
From: Sascha Hauer @ 2017-10-16  8:38 UTC (permalink / raw)
  To: Gerd Pauli; +Cc: barebox

On Fri, Oct 06, 2017 at 10:23:16AM +0200, Gerd Pauli wrote:
> writef: added command to write content of variable to file.
>         Adds a newline at the emd of the string.
> 
> Usage: writef VAR FILE

Do we need this command at all? It could be replaced with

echo -o FILE $foo

no?

Sascha



-- 
Pengutronix e.K.                           |                             |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

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

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2017-10-16  8:38 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-10-06  8:23 [PATCH v1] writef: added command to write content of variable to file Gerd Pauli
2017-10-16  8:38 ` Sascha Hauer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox