summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPeter Wu <lekensteyn@gmail.com>2013-01-29 00:04:31 +0100
committerPeter Wu <lekensteyn@gmail.com>2013-01-29 00:04:31 +0100
commit677694425b53b0a15e6ea09cb7554ede3ff42c6d (patch)
tree63faf5d47980eed906f0ade43e162ee53fc6cd3a
parent48fd809d8c94bd5d1af885fa1dd79b3cc9fa9015 (diff)
downloadc-files-677694425b53b0a15e6ea09cb7554ede3ff42c6d.tar.gz
entropy-watcher: Print difference
-rw-r--r--entropy-watcher.c18
1 files 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 <lekensteyn@gmail.com>
* 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);