aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2024-01-01 22:22:15 +0100
committerDenys Vlasenko <vda.linux@googlemail.com>2024-01-01 22:24:42 +0100
commit1cac2585217f830d29f7e9d2a1226d55c80886b6 (patch)
treefb40d7a0720871587ab806cb9b36c2da6626777f
parent01e80ff9ebaf42f2fb9b4ddddc75d37bc9a403aa (diff)
downloadbusybox-w32-1cac2585217f830d29f7e9d2a1226d55c80886b6.tar.gz
busybox-w32-1cac2585217f830d29f7e9d2a1226d55c80886b6.tar.bz2
busybox-w32-1cac2585217f830d29f7e9d2a1226d55c80886b6.zip
time: implement %% and \escapes in -f FMT
function old new delta time_main 1217 1316 +99 Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r--miscutils/time.c39
-rwxr-xr-xtestsuite/time.tests37
2 files changed, 55 insertions, 21 deletions
diff --git a/miscutils/time.c b/miscutils/time.c
index 4b1b043c3..77d35a832 100644
--- a/miscutils/time.c
+++ b/miscutils/time.c
@@ -226,28 +226,19 @@ static void summarize(const char *fmt, char **command, resource_t *resp)
226 } 226 }
227 227
228 switch (*fmt) { 228 switch (*fmt) {
229#ifdef NOT_NEEDED
230 /* Handle literal char */
231 /* Usually we optimize for size, but there is a limit
232 * for everything. With this we do a lot of 1-byte writes */
233 default:
234 bb_putchar(*fmt);
235 break;
236#endif
237
238 case '%': 229 case '%':
239 switch (*++fmt) { 230 switch (*++fmt) {
240#ifdef NOT_NEEDED_YET
241 /* Our format strings do not have these */
242 /* and we do not take format str from user */
243 default: 231 default:
244 bb_putchar('%'); 232 /* Unknown %<char> is printed as "?<char>" */
233 bb_putchar('?');
234 if (!*fmt) {
235 /* Trailing -f '...%' prints "...?" but NOT newline */
236 goto ret;
237 }
245 /*FALLTHROUGH*/ 238 /*FALLTHROUGH*/
246 case '%': 239 case '%':
247 if (!*fmt) goto ret;
248 bb_putchar(*fmt); 240 bb_putchar(*fmt);
249 break; 241 break;
250#endif
251 case 'C': /* The command that got timed. */ 242 case 'C': /* The command that got timed. */
252 printargv(command); 243 printargv(command);
253 break; 244 break;
@@ -381,14 +372,21 @@ static void summarize(const char *fmt, char **command, resource_t *resp)
381 } 372 }
382 break; 373 break;
383 374
384#ifdef NOT_NEEDED_YET 375 default: /* *fmt is '\': format escape */
385 case '\\': /* Format escape. */
386 switch (*++fmt) { 376 switch (*++fmt) {
387 default: 377 default:
378 /* Unknown \<char> is printed as "?\<char>" */
379 bb_putchar('?');
388 bb_putchar('\\'); 380 bb_putchar('\\');
381 if (!*fmt) {
382 /* Trailing -f '...\': GNU time 1.9 prints
383 * "...?\COMMAND" (it's probably a bug).
384 */
385 puts(command[0]);
386 goto ret;
387 }
389 /*FALLTHROUGH*/ 388 /*FALLTHROUGH*/
390 case '\\': 389 case '\\':
391 if (!*fmt) goto ret;
392 bb_putchar(*fmt); 390 bb_putchar(*fmt);
393 break; 391 break;
394 case 't': 392 case 't':
@@ -399,12 +397,11 @@ static void summarize(const char *fmt, char **command, resource_t *resp)
399 break; 397 break;
400 } 398 }
401 break; 399 break;
402#endif
403 } 400 }
404 ++fmt; 401 ++fmt;
405 } 402 }
406 /* ret: */
407 bb_putchar('\n'); 403 bb_putchar('\n');
404 ret: ;
408} 405}
409 406
410/* Run command CMD and return statistics on it. 407/* Run command CMD and return statistics on it.
@@ -438,7 +435,7 @@ int time_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
438int time_main(int argc UNUSED_PARAM, char **argv) 435int time_main(int argc UNUSED_PARAM, char **argv)
439{ 436{
440 resource_t res; 437 resource_t res;
441 /* $TIME has lowest prio (-v,-p,-f FMT overrride it) */ 438 /* $TIME has lowest prio (-v,-p,-f FMT override it) */
442 const char *output_format = getenv("TIME") ? : default_format; 439 const char *output_format = getenv("TIME") ? : default_format;
443 char *output_filename; 440 char *output_filename;
444 int output_fd; 441 int output_fd;
diff --git a/testsuite/time.tests b/testsuite/time.tests
new file mode 100755
index 000000000..4e31b3868
--- /dev/null
+++ b/testsuite/time.tests
@@ -0,0 +1,37 @@
1#!/bin/sh
2
3# Copyright 2024 by Denys Vlasenko <vda.linux@googlemail.com>
4# Licensed under GPLv2, see file LICENSE in this source tree.
5
6. ./testing.sh
7
8# testing "description" "arguments" "result" "infile" "stdin"
9
10testing "time -f trailing backslash" \
11 "time -f 'abc\' sleep 0 2>&1" \
12 'abc?\sleep\n' '' ''
13# ^^^^^^^^^^^^^^ this is what GNU time version 1.9 prints
14
15testing "time -f trailing percent" \
16 "time -f 'abc%' sleep 0 2>&1" \
17 'abc?' '' ''
18
19testing "time -f undefined backslash" \
20 "time -f 'abc\^def' sleep 0 2>&1" \
21 'abc?\^def\n' '' ''
22
23testing "time -f undefined percent" \
24 "time -f 'abc%^def' sleep 0 2>&1" \
25 'abc?^def\n' '' ''
26
27testing "time -f backslash tab and newline" \
28 "time -f 'abc\ndef\txyz' sleep 0 2>&1" \
29 'abc
30def xyz
31' '' ''
32
33testing "time -f percent percent" \
34 "time -f 'abc%%def' sleep 0 2>&1" \
35 'abc%def\n' '' ''
36
37exit $FAILCOUNT