aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--mongotimer.12
-rw-r--r--mongotimer.c28
2 files changed, 17 insertions, 13 deletions
diff --git a/mongotimer.1 b/mongotimer.1
index 7067e68..c99f09b 100644
--- a/mongotimer.1
+++ b/mongotimer.1
@@ -39,7 +39,7 @@ Exit with status 0 when the timer reaches 0.
If not specified, the timer will continue
into negative times but with a red font.
.SH "SEE ALSO"
-.BR mongoclock (1)
+.BR mongoclock (1),
.BR watch (1)
.SH AUTHORS
Mattias Andrée
diff --git a/mongotimer.c b/mongotimer.c
index 40ed234..a59e9c0 100644
--- a/mongotimer.c
+++ b/mongotimer.c
@@ -269,7 +269,7 @@ display_timer(int timerfd, int64_t time, int exit_on_zero)
x = winsize.ws_col;
for (h_div = 1, h_digits = 0; h / h_div; h_div *= 10, h_digits += 1);
width = 4 * DX + DC;
- if (time < 0)
+ if (time <= 0)
width += DM;
if (h_digits)
width += h_digits * DX + DC;
@@ -289,7 +289,7 @@ display_timer(int timerfd, int64_t time, int exit_on_zero)
}
i = 0;
- if (time < 0)
+ if (time <= 0)
digits[i++] = mongo_m;
if (h_digits) {
for (th = h; th; th /= 10)
@@ -309,7 +309,7 @@ display_timer(int timerfd, int64_t time, int exit_on_zero)
fprintf(stdout, "\033[39m\n");
pause();
continue;
- } else if (time >= 0) {
+ } else if (time > 0) {
print_time(digits, y, x);
} else {
fprintf(stdout, "\033[31m\n");
@@ -340,7 +340,7 @@ fail:
int
main(int argc, char *argv[])
{
- int timerfd = -1, old_flags = -1;
+ int timerfd = -1, old_flags = -1, tcset = 0;
int exit_on_zero = 0, old_sig = 0;
struct itimerspec itimerspec;
struct sigaction sigact;
@@ -401,10 +401,12 @@ main(int argc, char *argv[])
if (old_sig)
fcntl(STDIN_FILENO, F_SETSIG, 0);
- tcgetattr(STDIN_FILENO, &stty);
- saved_stty = stty;
- stty.c_lflag &= (tcflag_t)~(ECHO | ICANON);
- tcsetattr(STDIN_FILENO, TCSAFLUSH, &stty);
+ if (!tcgetattr(STDIN_FILENO, &stty)) {
+ saved_stty = stty;
+ stty.c_lflag &= (tcflag_t)~(ECHO | ICANON);
+ tcsetattr(STDIN_FILENO, TCSAFLUSH, &stty);
+ tcset = 1;
+ }
if (argc) {
for (s = argv[0]; *s; s++) {
@@ -446,10 +448,10 @@ fail:
fcntl(STDIN_FILENO, F_SETOWN_EX, &old_owner);
if (old_flags != -1)
fcntl(STDIN_FILENO, F_SETFL, old_flags);
- if (old_sig) {
+ if (old_sig)
fcntl(STDIN_FILENO, F_SETSIG, old_sig);
+ if (tcset)
tcsetattr(STDIN_FILENO, TCSAFLUSH, &saved_stty);
- }
if (timerfd >= 0)
close(timerfd);
return 1;
@@ -461,9 +463,11 @@ fail_usage:
fcntl(STDIN_FILENO, F_SETOWN_EX, &old_owner);
if (old_flags != -1)
fcntl(STDIN_FILENO, F_SETFL, old_flags);
- if (old_sig) {
+ if (old_sig)
fcntl(STDIN_FILENO, F_SETSIG, old_sig);
+ if (tcset)
tcsetattr(STDIN_FILENO, TCSAFLUSH, &saved_stty);
- }
+ if (timerfd >= 0)
+ close(timerfd);
usage();
}