summaryrefslogtreecommitdiffstats
path: root/commands
diff options
context:
space:
mode:
authorAhmad Fatoum <a.fatoum@pengutronix.de>2024-03-04 19:59:31 +0100
committerSascha Hauer <s.hauer@pengutronix.de>2024-03-05 16:28:05 +0100
commita238ac807882e17702d7110952549727901f85a0 (patch)
tree97dfad2a97238af6488830ed0315bbf9280bca1b /commands
parent3893b00bda38e25977eb8d70ee3e54ddcb021e71 (diff)
downloadbarebox-a238ac807882e17702d7110952549727901f85a0.tar.gz
barebox-a238ac807882e17702d7110952549727901f85a0.tar.xz
commands: time: switch to using getopt for -n
Now that we have support for the stop-at-first-non-option scanning mode indicated by a leading `+', there is nothing preventing us from using getopt for the time command. This rids us of interleaving argument parsing with the command's processing, has the command behave as one would expect, e.g. if multiple -n arguments are specified and allows for easier future extension. Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de> Link: https://lore.barebox.org/20240304190038.3486881-47-a.fatoum@pengutronix.de Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
Diffstat (limited to 'commands')
-rw-r--r--commands/time.c28
1 files changed, 18 insertions, 10 deletions
diff --git a/commands/time.c b/commands/time.c
index 5b8933ea65..72647a3bb8 100644
--- a/commands/time.c
+++ b/commands/time.c
@@ -5,30 +5,38 @@
#include <clock.h>
#include <linux/math64.h>
#include <malloc.h>
+#include <getopt.h>
static int do_time(int argc, char *argv[])
{
- int i;
+ int i, opt;
unsigned char *buf;
u64 start, end, diff64;
bool nanoseconds = false;
int len = 1; /* '\0' */
- if (argc < 2)
+ while ((opt = getopt(argc, argv, "+n")) > 0) {
+ switch (opt) {
+ case 'n':
+ nanoseconds = true;
+ break;
+ default:
+ return COMMAND_ERROR_USAGE;
+ }
+ }
+
+ argv += optind;
+ argc -= optind;
+
+ if (argc < 1)
return COMMAND_ERROR_USAGE;
- for (i = 1; i < argc; i++)
+ for (i = 0; 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++) {
+ for (i = 0; i < argc; i++) {
strcat(buf, argv[i]);
strcat(buf, " ");
}