2019-03-26 15:32:31 +05:30
|
|
|
/* vi: set sw=4 ts=4: */
|
|
|
|
/*
|
2019-03-26 15:42:15 +05:30
|
|
|
* Copyright (C) 2019 Denys Vlasenko <vda.linux@googlemail.com>
|
|
|
|
* Licensed under GPLv2, see file LICENSE in this source tree.
|
2019-03-26 15:32:31 +05:30
|
|
|
*/
|
|
|
|
//config:config TS
|
|
|
|
//config: bool "ts (450 bytes)"
|
|
|
|
//config: default y
|
|
|
|
|
|
|
|
//applet:IF_TS(APPLET(ts, BB_DIR_USR_BIN, BB_SUID_DROP))
|
|
|
|
|
|
|
|
//kbuild:lib-$(CONFIG_TS) += ts.o
|
|
|
|
|
|
|
|
//usage:#define ts_trivial_usage
|
|
|
|
//usage: "[-is] [STRFTIME]"
|
2021-06-13 06:42:09 +05:30
|
|
|
//usage:#define ts_full_usage "\n\n"
|
|
|
|
//usage: "Pipe stdin to stdout, add timestamp to each line\n"
|
|
|
|
//usage: "\n -s Time since start"
|
|
|
|
//usage: "\n -i Time since previous line"
|
2019-03-26 15:32:31 +05:30
|
|
|
|
|
|
|
#include "libbb.h"
|
|
|
|
#include "common_bufsiz.h"
|
|
|
|
|
|
|
|
int ts_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
2019-03-26 15:42:15 +05:30
|
|
|
int ts_main(int argc UNUSED_PARAM, char **argv)
|
2019-03-26 15:32:31 +05:30
|
|
|
{
|
2019-03-26 16:14:48 +05:30
|
|
|
struct timeval base;
|
2019-03-26 15:32:31 +05:30
|
|
|
unsigned opt;
|
|
|
|
char *frac;
|
|
|
|
char *fmt_dt2str;
|
|
|
|
char *line;
|
|
|
|
|
|
|
|
opt = getopt32(argv, "^" "is" "\0" "?1" /*max one arg*/);
|
|
|
|
if (opt) {
|
|
|
|
putenv((char*)"TZ=UTC0");
|
|
|
|
tzset();
|
|
|
|
}
|
|
|
|
/*argc -= optind;*/
|
|
|
|
argv += optind;
|
|
|
|
fmt_dt2str = argv[0] ? argv[0]
|
|
|
|
: (char*)(opt ? "%b %d %H:%M:%S"+6 : "%b %d %H:%M:%S");
|
|
|
|
frac = is_suffixed_with(fmt_dt2str, "%.S");
|
|
|
|
if (!frac)
|
|
|
|
frac = is_suffixed_with(fmt_dt2str, "%.s");
|
|
|
|
if (frac) {
|
|
|
|
frac++;
|
2019-03-26 16:11:35 +05:30
|
|
|
frac[0] = frac[1];
|
|
|
|
frac[1] = '\0';
|
2019-03-26 15:32:31 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
#define date_buf bb_common_bufsiz1
|
|
|
|
setup_common_bufsiz();
|
2020-12-31 04:18:01 +05:30
|
|
|
xgettimeofday(&base);
|
2019-03-26 15:32:31 +05:30
|
|
|
|
|
|
|
while ((line = xmalloc_fgets(stdin)) != NULL) {
|
2019-03-26 16:14:48 +05:30
|
|
|
struct timeval ts;
|
2019-03-26 15:32:31 +05:30
|
|
|
struct tm tm_time;
|
|
|
|
|
2020-12-31 04:18:01 +05:30
|
|
|
xgettimeofday(&ts);
|
2019-03-26 15:32:31 +05:30
|
|
|
if (opt) {
|
|
|
|
/* -i and/or -s */
|
2019-03-26 16:14:48 +05:30
|
|
|
struct timeval ts1 = ts1;
|
2019-03-26 15:32:31 +05:30
|
|
|
if (opt & 1) /* -i */
|
|
|
|
ts1 = ts;
|
|
|
|
//printf("%d %d\n", ts.tv_sec, base.tv_sec);
|
|
|
|
ts.tv_sec -= base.tv_sec;
|
|
|
|
//printf("%d %d\n", ts.tv_sec, base.tv_sec);
|
2019-03-26 16:14:48 +05:30
|
|
|
ts.tv_usec -= base.tv_usec;
|
|
|
|
if ((int32_t)(ts.tv_usec) < 0) {
|
2019-03-26 15:32:31 +05:30
|
|
|
ts.tv_sec--;
|
2019-03-26 16:14:48 +05:30
|
|
|
ts.tv_usec += 1000*1000;
|
2019-03-26 15:32:31 +05:30
|
|
|
}
|
|
|
|
if (opt & 1) /* -i */
|
|
|
|
base = ts1;
|
|
|
|
}
|
2019-03-26 16:21:21 +05:30
|
|
|
localtime_r(&ts.tv_sec, &tm_time);
|
2019-03-26 15:32:31 +05:30
|
|
|
strftime(date_buf, COMMON_BUFSIZE, fmt_dt2str, &tm_time);
|
|
|
|
if (!frac) {
|
|
|
|
printf("%s %s", date_buf, line);
|
|
|
|
} else {
|
2019-03-26 16:14:48 +05:30
|
|
|
printf("%s.%06u %s", date_buf, (unsigned)ts.tv_usec, line);
|
2019-03-26 15:32:31 +05:30
|
|
|
}
|
|
|
|
free(line);
|
|
|
|
}
|
|
|
|
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|