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@xxxxxxxxxxxxxx> --- v1 -> v2: - new patch --- commands/time.c | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/commands/time.c b/commands/time.c index 5b8933ea6553..72647a3bb876 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, " "); } -- 2.39.2