diff options
author | Ahmad Fatoum <a.fatoum@pengutronix.de> | 2024-03-04 19:59:32 +0100 |
---|---|---|
committer | Sascha Hauer <s.hauer@pengutronix.de> | 2024-03-05 16:28:05 +0100 |
commit | 35a903414ddcf8cef180a29a8271f59e0b1623cd (patch) | |
tree | d5dabad11045673778554fdc4604a32712e1a0cc | |
parent | a238ac807882e17702d7110952549727901f85a0 (diff) | |
download | barebox-35a903414ddc.tar.gz barebox-35a903414ddc.tar.xz |
commands: time: reduce strjoin runtime, drop trailing space
time concatenates its arguments to pass it to run_command in a
suboptimal manner: The destination string is traversed from the
beginning on every iteration due to strcat and we have a left-over
separator at the end, while it's only needed in-between.
In preparation for factoring the code out as a library strjoin function,
fix these shortcomings.
Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
Link: https://lore.barebox.org/20240304190038.3486881-48-a.fatoum@pengutronix.de
Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
-rw-r--r-- | commands/time.c | 12 |
1 files changed, 7 insertions, 5 deletions
diff --git a/commands/time.c b/commands/time.c index 72647a3bb8..0a38db6184 100644 --- a/commands/time.c +++ b/commands/time.c @@ -10,7 +10,7 @@ static int do_time(int argc, char *argv[]) { int i, opt; - unsigned char *buf; + unsigned char *buf, *p; u64 start, end, diff64; bool nanoseconds = false; int len = 1; /* '\0' */ @@ -34,13 +34,15 @@ static int do_time(int argc, char *argv[]) for (i = 0; i < argc; i++) len += strlen(argv[i]) + 1; - buf = xzalloc(len); + p = buf = xmalloc(len); - for (i = 0; i < argc; i++) { - strcat(buf, argv[i]); - strcat(buf, " "); + for (i = 0; i < argc - 1; i++) { + p = stpcpy(p, argv[i]); + p = mempcpy(p, " ", strlen(" ")); } + stpcpy(p, argv[i]); + start = get_time_ns(); run_command(buf); |