From 677694425b53b0a15e6ea09cb7554ede3ff42c6d Mon Sep 17 00:00:00 2001 From: Peter Wu Date: Tue, 29 Jan 2013 00:04:31 +0100 Subject: entropy-watcher: Print difference --- entropy-watcher.c | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/entropy-watcher.c b/entropy-watcher.c index 2d6fe86..6dc1b89 100644 --- a/entropy-watcher.c +++ b/entropy-watcher.c @@ -1,7 +1,8 @@ /** - * Watch available kernel entropy. By default, it prints the available entrophy - * every second, but this can be changed with arg1 (in milliseconds). You can - * also press Enter to immediately print the current entrophy. + * Watch available kernel entropy. By default, it prints the available entropy + * and difference to the previously recorded entropy every second, but this can + * be changed with arg1 (in milliseconds). You can also press Enter to + * immediately print the current entropy. * * Author: Peter Wu * Date: 2013-01-28 @@ -21,6 +22,7 @@ int main(int argc, char **argv) { struct pollfd pfd; int fd; int print_interval_ms; + long int avail, prev = 0; print_interval_ms = 1000; if (argc > 1) { @@ -37,7 +39,12 @@ int main(int argc, char **argv) { pfd.events = POLLIN; while ((r = read(fd, buff, sizeof(buff))) > 0) { - write(STDOUT_FILENO, buff, r - 1); + /* remove possible newline and terminate string for sure */ + buff[r - 1] = 0; + avail = strtol(buff, NULL, 10); + printf("%li (%+li)", avail, avail - prev); + prev = avail; + poll(&pfd, 1, print_interval_ms); lseek(fd, 0, SEEK_SET); if (pfd.revents == POLLIN) { /* pressed enter? */ @@ -45,8 +52,7 @@ int main(int argc, char **argv) { if (r > 0 && buff[r - 1] == '\n') continue; } - buff[0] = '\n'; - write(STDOUT_FILENO, buff, 1); + putchar('\n'); } close(fd); -- cgit v1.2.1