diff options
author | Todd C. Miller <millert@cvs.openbsd.org> | 2011-04-25 13:30:08 +0000 |
---|---|---|
committer | Todd C. Miller <millert@cvs.openbsd.org> | 2011-04-25 13:30:08 +0000 |
commit | 4645bbbc27a8727d2771088d40fa3b7405d1181c (patch) | |
tree | 712d356814146f1e0da823a97ef8ab3d87422f3a /games/worms/worms.c | |
parent | 4c06f62abd8308f288e7674df41e6e29887a7665 (diff) |
Fix "-d delay" option and convert to use strtonum()
OK martynas@ a while ago.
Diffstat (limited to 'games/worms/worms.c')
-rw-r--r-- | games/worms/worms.c | 21 |
1 files changed, 13 insertions, 8 deletions
diff --git a/games/worms/worms.c b/games/worms/worms.c index 131c005062f..30b3bd10758 100644 --- a/games/worms/worms.c +++ b/games/worms/worms.c @@ -1,4 +1,4 @@ -/* $OpenBSD: worms.c,v 1.19 2009/10/27 23:59:27 deraadt Exp $ */ +/* $OpenBSD: worms.c,v 1.20 2011/04/25 13:30:07 millert Exp $ */ /* * Copyright (c) 1980, 1993 @@ -177,10 +177,11 @@ main(int argc, char *argv[]) short *ip; int CO, LI, last, bottom, ch, length, number, trail; short **ref; - const char *field; + const char *field, *errstr; + struct timespec sleeptime; struct termios term; speed_t speed; - u_int delay = 0; + time_t delay = 0; /* set default delay based on terminal baud rate */ if (tcgetattr(STDOUT_FILENO, &term) == 0 && @@ -194,10 +195,9 @@ main(int argc, char *argv[]) while ((ch = getopt(argc, argv, "d:fhl:n:t")) != -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 'f': field = "WORM"; @@ -221,6 +221,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(); if (!(worm = calloc((size_t)number, sizeof(struct worm)))) nomem(); @@ -284,7 +289,7 @@ main(int argc, char *argv[]) endwin(); exit(0); } - if (delay) usleep(delay); + nanosleep(&sleeptime, NULL); for (n = 0, w = &worm[0]; n < number; n++, w++) { if ((x = w->xpos[h = w->head]) < 0) { mvaddch(y = w->ypos[h] = bottom, |