aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--miscutils/ts.c88
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
23int ts_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
24int 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}