diff options
| author | Denys Vlasenko <vda.linux@googlemail.com> | 2019-03-26 11:02:31 +0100 |
|---|---|---|
| committer | Denys Vlasenko <vda.linux@googlemail.com> | 2019-03-26 11:02:31 +0100 |
| commit | 16df5e8e6dccbcec52371fc569a3237e5854ba5b (patch) | |
| tree | 770cbb595b3c0ff47aa7b2abc1b334ef0b15d252 /miscutils | |
| parent | 5059653882dbd86e3bbf48389f9f81b0fac8cd0a (diff) | |
| download | busybox-w32-16df5e8e6dccbcec52371fc569a3237e5854ba5b.tar.gz busybox-w32-16df5e8e6dccbcec52371fc569a3237e5854ba5b.tar.bz2 busybox-w32-16df5e8e6dccbcec52371fc569a3237e5854ba5b.zip | |
ts: new applet
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat (limited to 'miscutils')
| -rw-r--r-- | miscutils/ts.c | 88 |
1 files changed, 88 insertions, 0 deletions
diff --git a/miscutils/ts.c b/miscutils/ts.c new file mode 100644 index 000000000..4a02341cc --- /dev/null +++ b/miscutils/ts.c | |||
| @@ -0,0 +1,88 @@ | |||
| 1 | /* vi: set sw=4 ts=4: */ | ||
| 2 | /* | ||
| 3 | * Copyright (C) 2006 Michael Opdenacker <michael@free-electrons.com> | ||
| 4 | * | ||
| 5 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. | ||
| 6 | */ | ||
| 7 | //config:config TS | ||
| 8 | //config: bool "ts (450 bytes)" | ||
| 9 | //config: default y | ||
| 10 | |||
| 11 | //applet:IF_TS(APPLET(ts, BB_DIR_USR_BIN, BB_SUID_DROP)) | ||
| 12 | |||
| 13 | //kbuild:lib-$(CONFIG_TS) += ts.o | ||
| 14 | |||
| 15 | //usage:#define ts_trivial_usage | ||
| 16 | //usage: "[-is] [STRFTIME]" | ||
| 17 | //usage:#define ts_full_usage "" | ||
| 18 | |||
| 19 | #include "libbb.h" | ||
| 20 | #include "common_bufsiz.h" | ||
| 21 | # include <sys/syscall.h> | ||
| 22 | |||
| 23 | int ts_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; | ||
| 24 | int ts_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) | ||
| 25 | { | ||
| 26 | struct timespec base; | ||
| 27 | unsigned opt; | ||
| 28 | char *frac; | ||
| 29 | char *fmt_dt2str; | ||
| 30 | char *line; | ||
| 31 | |||
| 32 | opt = getopt32(argv, "^" "is" "\0" "?1" /*max one arg*/); | ||
| 33 | if (opt) { | ||
| 34 | putenv((char*)"TZ=UTC0"); | ||
| 35 | tzset(); | ||
| 36 | } | ||
| 37 | /*argc -= optind;*/ | ||
| 38 | argv += optind; | ||
| 39 | fmt_dt2str = argv[0] ? argv[0] | ||
| 40 | : (char*)(opt ? "%b %d %H:%M:%S"+6 : "%b %d %H:%M:%S"); | ||
| 41 | frac = is_suffixed_with(fmt_dt2str, "%.S"); | ||
| 42 | if (!frac) | ||
| 43 | frac = is_suffixed_with(fmt_dt2str, "%.s"); | ||
| 44 | if (frac) { | ||
| 45 | frac++; | ||
| 46 | strcpy(frac, frac + 1); | ||
| 47 | } | ||
| 48 | |||
| 49 | #define date_buf bb_common_bufsiz1 | ||
| 50 | setup_common_bufsiz(); | ||
| 51 | syscall(__NR_clock_gettime, CLOCK_REALTIME, &base); | ||
| 52 | |||
| 53 | while ((line = xmalloc_fgets(stdin)) != NULL) { | ||
| 54 | struct timespec ts; | ||
| 55 | struct tm tm_time; | ||
| 56 | |||
| 57 | /* libc has incredibly messy way of doing this, | ||
| 58 | * typically requiring -lrt. We just skip all this mess | ||
| 59 | */ | ||
| 60 | syscall(__NR_clock_gettime, CLOCK_REALTIME, &ts); | ||
| 61 | if (opt) { | ||
| 62 | /* -i and/or -s */ | ||
| 63 | struct timespec ts1 = ts1; | ||
| 64 | if (opt & 1) /* -i */ | ||
| 65 | ts1 = ts; | ||
| 66 | //printf("%d %d\n", ts.tv_sec, base.tv_sec); | ||
| 67 | ts.tv_sec -= base.tv_sec; | ||
| 68 | //printf("%d %d\n", ts.tv_sec, base.tv_sec); | ||
| 69 | ts.tv_nsec -= base.tv_nsec; | ||
| 70 | if ((int32_t)(ts.tv_nsec) < 0) { | ||
| 71 | ts.tv_sec--; | ||
| 72 | ts.tv_nsec += 1000*1000*1000; | ||
| 73 | } | ||
| 74 | if (opt & 1) /* -i */ | ||
| 75 | base = ts1; | ||
| 76 | localtime_r(&ts.tv_sec, &tm_time); | ||
| 77 | } | ||
| 78 | strftime(date_buf, COMMON_BUFSIZE, fmt_dt2str, &tm_time); | ||
| 79 | if (!frac) { | ||
| 80 | printf("%s %s", date_buf, line); | ||
| 81 | } else { | ||
| 82 | printf("%s.%06u %s", date_buf, (unsigned)ts.tv_nsec / 1000u, line); | ||
| 83 | } | ||
| 84 | free(line); | ||
| 85 | } | ||
| 86 | |||
| 87 | return EXIT_SUCCESS; | ||
| 88 | } | ||
