[PATCH 3/5] cmd: Convert echo to use cmd_result
Sean Anderson
seanga2 at gmail.com
Mon Mar 1 00:47:16 CET 2021
This adds some complexity, since we need to know how big the arguments are
ahead of time instead of finding out when we print them.
Signed-off-by: Sean Anderson <seanga2 at gmail.com>
---
cmd/echo.c | 33 ++++++++++++++++++++++++++-------
1 file changed, 26 insertions(+), 7 deletions(-)
diff --git a/cmd/echo.c b/cmd/echo.c
index fda844ee9d..8f20e635ce 100644
--- a/cmd/echo.c
+++ b/cmd/echo.c
@@ -6,11 +6,16 @@
#include <common.h>
#include <command.h>
+#include <malloc.h>
+
+DECLARE_GLOBAL_DATA_PTR;
static int do_echo(struct cmd_tbl *cmdtp, int flag, int argc,
char *const argv[])
{
- int i = 1;
+ char *result;
+ int j, i = 1;
+ size_t result_size, arglens[CONFIG_SYS_MAXARGS];
bool space = false;
bool newline = true;
@@ -21,18 +26,32 @@ static int do_echo(struct cmd_tbl *cmdtp, int flag, int argc,
}
}
+ result_size = 1 + newline; /* \0 + \n */
+ result_size += argc - i - 1; /* spaces */
+ for (j = i; j < argc; ++j) {
+ arglens[j] = strlen(argv[j]);
+ result_size += arglens[j];
+ }
+
+ result = malloc(result_size);
+ if (!result)
+ return CMD_RET_FAILURE;
+ gd->cmd_result = result;
+
for (; i < argc; ++i) {
- if (space) {
- putc(' ');
- }
- puts(argv[i]);
+ if (space)
+ *result++ = ' ';
+
+ memcpy(result, argv[i], arglens[i]);
+ result += arglens[i];
space = true;
}
if (newline)
- putc('\n');
+ *result++ = '\n';
+ *result = '\0';
- return 0;
+ return CMD_RET_SUCCESS;
}
U_BOOT_CMD(
--
2.30.1
More information about the U-Boot
mailing list