From mboxrd@z Thu Jan 1 00:00:00 1970 Return-path: Received: from mail.high-consulting.de ([62.245.247.194]) by bombadil.infradead.org with esmtp (Exim 4.87 #1 (Red Hat Linux)) id 1dsTHH-0002TQ-MW for barebox@lists.infradead.org; Thu, 14 Sep 2017 12:29:57 +0000 From: gp@high-consulting.de Date: Thu, 14 Sep 2017 14:29:12 +0200 Message-Id: <1505392152-7965-1-git-send-email-gp@high-consulting.de> List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "barebox" Errors-To: barebox-bounces+u.kleine-koenig=pengutronix.de@lists.infradead.org Subject: [PATCH v1 1/2] commands: writef write variable contents to file To: barebox@lists.infradead.org Cc: Gerd Pauli From: Gerd Pauli Signed-off-by: Gerd Pauli --- commands/Kconfig | 10 ++++++++ commands/Makefile | 1 + commands/writef.c | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 79 insertions(+) create mode 100644 commands/writef.c diff --git a/commands/Kconfig b/commands/Kconfig index ae2dc4b..89b3103 100644 --- a/commands/Kconfig +++ b/commands/Kconfig @@ -1082,6 +1082,16 @@ 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 + + Writes a line from VARiable into a FILE. + 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..1cd17b1 --- /dev/null +++ b/commands/writef.c @@ -0,0 +1,68 @@ +/* + * writef.c - Write Content of Variable to File + * + * Copyright (c) 2017 Gerd Pauli , HighConsulting GmbH & Co. KG + * + * 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 +#include +#include +#include +#include +#include +#include +#include + +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") +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