mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: barebox@lists.infradead.org
Cc: Ahmad Fatoum <a.fatoum@pengutronix.de>
Subject: [PATCH 2/6] commands: time: refactor into new strjoin
Date: Wed, 26 Oct 2022 08:42:01 +0200	[thread overview]
Message-ID: <20221026064205.2360041-2-a.fatoum@pengutronix.de> (raw)
In-Reply-To: <20221026064205.2360041-1-a.fatoum@pengutronix.de>

time concatenates all its remaining arguments with a space in-between
and then passes that to the command executor. This can be useful
elsewhere as well, so factor it out into a new strjoin function.

No functional change.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 commands/time.c  | 11 +----------
 include/string.h |  2 ++
 lib/string.c     | 22 ++++++++++++++++++++++
 3 files changed, 25 insertions(+), 10 deletions(-)

diff --git a/commands/time.c b/commands/time.c
index 5b8933ea6553..336128f6a9be 100644
--- a/commands/time.c
+++ b/commands/time.c
@@ -12,26 +12,17 @@ static int do_time(int argc, char *argv[])
 	unsigned char *buf;
 	u64 start, end, diff64;
 	bool nanoseconds = false;
-	int len = 1; /* '\0' */
 
 	if (argc < 2)
 		return COMMAND_ERROR_USAGE;
 
-	for (i = 1; i < argc; i++)
-		len += strlen(argv[i]) + 1;
-
-	buf = xzalloc(len);
-
 	i = 1;
 	if (!strcmp(argv[i], "-n")) {
 		nanoseconds = true;
 		i++;
 	}
 
-	for (; i < argc; i++) {
-		strcat(buf, argv[i]);
-		strcat(buf, " ");
-	}
+	buf = strjoin(" ", &argv[i], argc - i);
 
 	start = get_time_ns();
 
diff --git a/include/string.h b/include/string.h
index d423bee6fba5..2cc727fd1d7a 100644
--- a/include/string.h
+++ b/include/string.h
@@ -17,4 +17,6 @@ void *__nokasan_default_memcpy(void * dest,const void *src,size_t count);
 
 char *parse_assignment(char *str);
 
+char *strjoin(const char *separator, char **array, size_t len);
+
 #endif /* __STRING_H */
diff --git a/lib/string.c b/lib/string.c
index 6389217d5b41..a500e8a3d1ba 100644
--- a/lib/string.c
+++ b/lib/string.c
@@ -938,3 +938,25 @@ char *parse_assignment(char *str)
 
 	return value;
 }
+
+char *strjoin(const char *separator, char **arr, size_t arrlen)
+{
+	size_t separatorlen;
+	int len = 1; /* '\0' */
+	char *buf;
+	int i;
+
+	separatorlen = strlen(separator);
+
+	for (i = 0; i < arrlen; i++)
+		len += strlen(arr[i]) + separatorlen;
+
+	buf = xzalloc(len);
+
+	for (i = 0; i < arrlen; i++) {
+		strcat(buf, arr[i]);
+		strcat(buf, separator);
+	}
+
+	return buf;
+}
-- 
2.30.2




  reply	other threads:[~2022-10-26  6:43 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-10-26  6:42 [PATCH 1/6] commands: add new uptime command Ahmad Fatoum
2022-10-26  6:42 ` Ahmad Fatoum [this message]
2022-10-26  6:42 ` [PATCH 3/6] string: reduce strjoin runtime, drop trailing separator Ahmad Fatoum
2022-10-27  6:56   ` Sascha Hauer
2022-10-27  7:24     ` Ahmad Fatoum
2022-10-27  7:33       ` Sascha Hauer
2022-10-27  7:53         ` Ahmad Fatoum
2022-10-26  6:42 ` [PATCH 4/6] test: self: add strjoin tests Ahmad Fatoum
2022-10-26  6:42 ` [PATCH 5/6] commands: drvinfo: support filtering by driver Ahmad Fatoum
2022-10-27  7:29   ` Sascha Hauer
2022-10-27  7:51     ` Ahmad Fatoum
2022-10-27  8:49       ` Sascha Hauer
2022-10-26  6:42 ` [PATCH 6/6] test: self: only include ramfs selftest when CONFIG_SELFTEST_FS_RAMFS=y Ahmad Fatoum
2022-10-27  7:11 ` [PATCH 1/6] commands: add new uptime command Sascha Hauer

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=20221026064205.2360041-2-a.fatoum@pengutronix.de \
    --to=a.fatoum@pengutronix.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox