Denys Vlasenko | 16df5e8 | 2019-03-26 11:02:31 +0100 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
Denys Vlasenko | f370a66 | 2019-03-26 11:12:15 +0100 | [diff] [blame] | 3 | * Copyright (C) 2019 Denys Vlasenko <vda.linux@googlemail.com> |
| 4 | * Licensed under GPLv2, see file LICENSE in this source tree. |
Denys Vlasenko | 16df5e8 | 2019-03-26 11:02:31 +0100 | [diff] [blame] | 5 | */ |
| 6 | //config:config TS |
| 7 | //config: bool "ts (450 bytes)" |
| 8 | //config: default y |
| 9 | |
| 10 | //applet:IF_TS(APPLET(ts, BB_DIR_USR_BIN, BB_SUID_DROP)) |
| 11 | |
| 12 | //kbuild:lib-$(CONFIG_TS) += ts.o |
| 13 | |
| 14 | //usage:#define ts_trivial_usage |
| 15 | //usage: "[-is] [STRFTIME]" |
Denys Vlasenko | 1849285 | 2021-06-13 03:12:09 +0200 | [diff] [blame] | 16 | //usage:#define ts_full_usage "\n\n" |
| 17 | //usage: "Pipe stdin to stdout, add timestamp to each line\n" |
| 18 | //usage: "\n -s Time since start" |
| 19 | //usage: "\n -i Time since previous line" |
Denys Vlasenko | 16df5e8 | 2019-03-26 11:02:31 +0100 | [diff] [blame] | 20 | |
| 21 | #include "libbb.h" |
| 22 | #include "common_bufsiz.h" |
Denys Vlasenko | 16df5e8 | 2019-03-26 11:02:31 +0100 | [diff] [blame] | 23 | |
| 24 | int ts_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denys Vlasenko | f370a66 | 2019-03-26 11:12:15 +0100 | [diff] [blame] | 25 | int ts_main(int argc UNUSED_PARAM, char **argv) |
Denys Vlasenko | 16df5e8 | 2019-03-26 11:02:31 +0100 | [diff] [blame] | 26 | { |
Denys Vlasenko | 973698d | 2019-03-26 11:44:48 +0100 | [diff] [blame] | 27 | struct timeval base; |
Denys Vlasenko | 16df5e8 | 2019-03-26 11:02:31 +0100 | [diff] [blame] | 28 | unsigned opt; |
| 29 | char *frac; |
| 30 | char *fmt_dt2str; |
| 31 | char *line; |
| 32 | |
| 33 | opt = getopt32(argv, "^" "is" "\0" "?1" /*max one arg*/); |
| 34 | if (opt) { |
| 35 | putenv((char*)"TZ=UTC0"); |
| 36 | tzset(); |
| 37 | } |
| 38 | /*argc -= optind;*/ |
| 39 | argv += optind; |
| 40 | fmt_dt2str = argv[0] ? argv[0] |
| 41 | : (char*)(opt ? "%b %d %H:%M:%S"+6 : "%b %d %H:%M:%S"); |
| 42 | frac = is_suffixed_with(fmt_dt2str, "%.S"); |
| 43 | if (!frac) |
| 44 | frac = is_suffixed_with(fmt_dt2str, "%.s"); |
| 45 | if (frac) { |
| 46 | frac++; |
Denys Vlasenko | 3395e2a | 2019-03-26 11:41:35 +0100 | [diff] [blame] | 47 | frac[0] = frac[1]; |
| 48 | frac[1] = '\0'; |
Denys Vlasenko | 16df5e8 | 2019-03-26 11:02:31 +0100 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | #define date_buf bb_common_bufsiz1 |
| 52 | setup_common_bufsiz(); |
Denys Vlasenko | 3c13da3 | 2020-12-30 23:48:01 +0100 | [diff] [blame] | 53 | xgettimeofday(&base); |
Denys Vlasenko | 16df5e8 | 2019-03-26 11:02:31 +0100 | [diff] [blame] | 54 | |
| 55 | while ((line = xmalloc_fgets(stdin)) != NULL) { |
Denys Vlasenko | 973698d | 2019-03-26 11:44:48 +0100 | [diff] [blame] | 56 | struct timeval ts; |
Denys Vlasenko | 16df5e8 | 2019-03-26 11:02:31 +0100 | [diff] [blame] | 57 | struct tm tm_time; |
| 58 | |
Denys Vlasenko | 3c13da3 | 2020-12-30 23:48:01 +0100 | [diff] [blame] | 59 | xgettimeofday(&ts); |
Denys Vlasenko | 16df5e8 | 2019-03-26 11:02:31 +0100 | [diff] [blame] | 60 | if (opt) { |
| 61 | /* -i and/or -s */ |
Denys Vlasenko | 973698d | 2019-03-26 11:44:48 +0100 | [diff] [blame] | 62 | struct timeval ts1 = ts1; |
Denys Vlasenko | 16df5e8 | 2019-03-26 11:02:31 +0100 | [diff] [blame] | 63 | if (opt & 1) /* -i */ |
| 64 | ts1 = ts; |
| 65 | //printf("%d %d\n", ts.tv_sec, base.tv_sec); |
| 66 | ts.tv_sec -= base.tv_sec; |
| 67 | //printf("%d %d\n", ts.tv_sec, base.tv_sec); |
Denys Vlasenko | 973698d | 2019-03-26 11:44:48 +0100 | [diff] [blame] | 68 | ts.tv_usec -= base.tv_usec; |
| 69 | if ((int32_t)(ts.tv_usec) < 0) { |
Denys Vlasenko | 16df5e8 | 2019-03-26 11:02:31 +0100 | [diff] [blame] | 70 | ts.tv_sec--; |
Denys Vlasenko | 973698d | 2019-03-26 11:44:48 +0100 | [diff] [blame] | 71 | ts.tv_usec += 1000*1000; |
Denys Vlasenko | 16df5e8 | 2019-03-26 11:02:31 +0100 | [diff] [blame] | 72 | } |
| 73 | if (opt & 1) /* -i */ |
| 74 | base = ts1; |
Denys Vlasenko | 16df5e8 | 2019-03-26 11:02:31 +0100 | [diff] [blame] | 75 | } |
Denys Vlasenko | fe78c9a | 2019-03-26 11:51:21 +0100 | [diff] [blame] | 76 | localtime_r(&ts.tv_sec, &tm_time); |
Denys Vlasenko | 16df5e8 | 2019-03-26 11:02:31 +0100 | [diff] [blame] | 77 | strftime(date_buf, COMMON_BUFSIZE, fmt_dt2str, &tm_time); |
| 78 | if (!frac) { |
| 79 | printf("%s %s", date_buf, line); |
| 80 | } else { |
Denys Vlasenko | 973698d | 2019-03-26 11:44:48 +0100 | [diff] [blame] | 81 | printf("%s.%06u %s", date_buf, (unsigned)ts.tv_usec, line); |
Denys Vlasenko | 16df5e8 | 2019-03-26 11:02:31 +0100 | [diff] [blame] | 82 | } |
| 83 | free(line); |
| 84 | } |
| 85 | |
| 86 | return EXIT_SUCCESS; |
| 87 | } |