diff options
author | Laurent Bercot <ska-skaware@skarnet.org> | 2020-06-21 21:09:18 +0000 |
---|---|---|
committer | Laurent Bercot <ska-skaware@skarnet.org> | 2020-06-21 21:09:18 +0000 |
commit | 2491528e5542153370122aa6f64424d30610562e (patch) | |
tree | 515184ba8658a1a5ef6811641b86d25279e5fcdc /src | |
parent | 13a02f2ccc602e8d038a0808b052f2bffc3f5408 (diff) | |
download | s6-portable-utils-2491528e5542153370122aa6f64424d30610562e.tar.gz s6-portable-utils-2491528e5542153370122aa6f64424d30610562e.tar.xz s6-portable-utils-2491528e5542153370122aa6f64424d30610562e.zip |
Add s6-tai64ndiff ; prepare for 2.2.3.0
Diffstat (limited to 'src')
-rw-r--r-- | src/skaembutils/deps-exe/s6-tai64ndiff | 1 | ||||
-rw-r--r-- | src/skaembutils/s6-tai64ndiff.c | 74 |
2 files changed, 75 insertions, 0 deletions
diff --git a/src/skaembutils/deps-exe/s6-tai64ndiff b/src/skaembutils/deps-exe/s6-tai64ndiff new file mode 100644 index 0000000..e7187fe --- /dev/null +++ b/src/skaembutils/deps-exe/s6-tai64ndiff @@ -0,0 +1 @@ +-lskarnet diff --git a/src/skaembutils/s6-tai64ndiff.c b/src/skaembutils/s6-tai64ndiff.c new file mode 100644 index 0000000..25abea3 --- /dev/null +++ b/src/skaembutils/s6-tai64ndiff.c @@ -0,0 +1,74 @@ +/* ISC license. */ + +#include <stdint.h> +#include <string.h> +#include <errno.h> + +#include <skalibs/uint32.h> +#include <skalibs/uint64.h> +#include <skalibs/buffer.h> +#include <skalibs/strerr2.h> +#include <skalibs/tai.h> +#include <skalibs/stralloc.h> +#include <skalibs/skamisc.h> + +int main (int argc, char const *const *argv) +{ + stralloc sa = STRALLOC_ZERO ; + tain_t prev ; + int defined = 0 ; + PROG = "s6-tai64ndiff" ; + + for (;;) + { + unsigned int p = 0 ; + char prefix[23] = "[ . ]" ; + int r = skagetln(buffer_0f1, &sa, '\n') ; + if (r == -1) + if (errno != EPIPE) + strerr_diefu1sys(111, "read from stdin") ; + else r = 1 ; + else if (!r) break ; + if (sa.len > TIMESTAMP) + { + tain_t cur ; + p = timestamp_scan(sa.s, &cur) ; + if (p) + { + if (defined) + { + tain_t diff ; + int64_t secs ; + size_t len ; + tain_sub(&diff, &cur, &prev) ; + secs = tai_sec(tain_secp(&diff)) ; + len = int64_fmt(0, secs) ; + if (len > 10) + { + char fmtn[9] ; + size_t m = 1 + (len < 20) ; + m += int64_fmt(prefix + m, secs) ; + prefix[m++] = '.' ; + uint320_fmt(fmtn, tain_nano(&diff), 9) ; + memcpy(prefix + m, fmtn, 22 - m) ; + } + else + { + int64_fmt(prefix + 11 - len, secs) ; + uint320_fmt(prefix + 12, tain_nano(&diff), 9) ; + } + } + prev = cur ; + defined = 1 ; + if (buffer_put(buffer_1, prefix, 23) < 23) + strerr_diefu1sys(111, "write to stdout") ; + } + else defined = 0 ; + } + else defined = 0 ; + if (buffer_put(buffer_1, sa.s + p, sa.len - p) < (ssize_t)(sa.len - p)) + strerr_diefu1sys(111, "write to stdout") ; + sa.len = 0 ; + } + return 0 ; +} |