diff options
author | Stuart Henderson <sthen@cvs.openbsd.org> | 2012-05-27 10:09:34 +0000 |
---|---|---|
committer | Stuart Henderson <sthen@cvs.openbsd.org> | 2012-05-27 10:09:34 +0000 |
commit | c10a6683729b50f73f3636cd0f40192ac08effbf (patch) | |
tree | 239696b97d9d1377b3b55abe7140fc0cf872c260 | |
parent | 6b32bc23fbac9420b837244eea94ce24122bf25e (diff) |
Teach rain(6) to calculate terminal delays in the same way as worms(6),
rather than use a default which totally floods the network if run over ssh.
Discussed with matthieu@ martynas@, ok matthieu@
-rw-r--r-- | games/rain/rain.c | 28 |
1 files changed, 21 insertions, 7 deletions
diff --git a/games/rain/rain.c b/games/rain/rain.c index 86aac6f7cfe..348a908c22a 100644 --- a/games/rain/rain.c +++ b/games/rain/rain.c @@ -1,4 +1,4 @@ -/* $OpenBSD: rain.c,v 1.15 2009/10/27 23:59:26 deraadt Exp $ */ +/* $OpenBSD: rain.c,v 1.16 2012/05/27 10:09:33 sthen Exp $ */ /* * Copyright (c) 1980, 1993 @@ -40,6 +40,7 @@ #include <signal.h> #include <stdio.h> #include <stdlib.h> +#include <termios.h> #include <unistd.h> volatile sig_atomic_t sig_caught = 0; @@ -52,17 +53,25 @@ main(int argc, char *argv[]) { int x, y, j; long tcols, tlines; - u_int delay = 0; + const char *errstr; + struct termios term; + struct timespec sleeptime; + speed_t speed; + time_t delay = 0; int ch; int xpos[5], ypos[5]; + /* set default delay based on terminal baud rate */ + if (tcgetattr(STDOUT_FILENO, &term) == 0 && + (speed = cfgetospeed(&term)) > B9600) + delay = (speed / B9600) - 1; + while ((ch = getopt(argc, argv, "d:h")) != -1) switch(ch) { case 'd': - if ((delay = (u_int)strtoul(optarg,(char **)NULL,10)) < 1 - || delay > 1000) - errx(1, "invalid delay (1-1000)"); - delay *= 1000; /* ms -> us */ + delay = (time_t)strtonum(optarg, 0, 1000, &errstr); + if (errstr) + errx(1, "delay (0-1000) is %s: %s", errstr, optarg); break; case 'h': default: @@ -70,6 +79,11 @@ main(int argc, char *argv[]) exit(1); } + /* Convert delay from ms -> ns */ + sleeptime.tv_sec = 0; + sleeptime.tv_nsec = delay * 500000; + timespecadd(&sleeptime, &sleeptime, &sleeptime); + srandomdev(); initscr(); tcols = COLS - 4; @@ -121,7 +135,7 @@ main(int argc, char *argv[]) xpos[j] = x; ypos[j] = y; refresh(); - if (delay) usleep(delay); + nanosleep(&sleeptime, NULL); } } |