Source-Changes-HG archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
[src/trunk]: src/games/worms When worms default delay was changed from 0, to ...
details: https://anonhg.NetBSD.org/src/rev/9b98c99e4458
branches: trunk
changeset: 374267:9b98c99e4458
user: kre <kre%NetBSD.org@localhost>
date: Sat Apr 15 15:21:56 2023 +0000
description:
When worms default delay was changed from 0, to 20ms (Oct 2020)
it lost the ability to run flat out (no sleeps) - which while not
very useful for actually observing the behaviour of various Nematoda,
can be useful when 'worms' is being used to generate cpu heat - it
turns out to be a simple tool to make all of the app itself, the
xterm it runs in (when running under X) and the X server, all more
or less continuously busy - a trivial perpetual CPU load generator.
Changing that was not a simple matter of just allowing -d 0..1000
rather than -d 1..1000 which had always been the limits on -d, as
previously, simply by excluding 0, common error checking wasn't
essential. -djunk would return 0 which was invalid, and so an error
- that it was invalid because 0 < 1, rather than because "junk" is not
a number wasn't material.
Now we need some slightly more elaborate error checking code for the
-d value, and if we're going to do that, may as well do the same for
the -l and -n options, which also take numeric values. That is, it
will no longer be possible to say:
worms -n "5 worms"
Just the number is allowed (but -d now allows a "ms" or "us" suffix).
While here, place a reasonable upper limit (depending upon the
screen size, and worm length) upon the number of worms, so they
have somewhere to go, rather than just on top of each other.
exit(1) from the errx() rather than 0, in the case that curses
initscr() fails (doesn't seem right that it should appear as a
successful run of worms, if it never managed to start).
A couple more minor 80 column violations are handled, without
further penalty, in this update (one simply vanishes, the other
is wrapped).
Note that this sounds like a lot, but it is really all just minor
internal bookkeeping updates - the only real advertised user visible
change is the ability to use -d0 which just restores that which was
the default (though it could never be set via the option) until Oct 2020
- so really all that's happening is replacing minor functionality lost
then, while leaving the change of the default delay that that change
made, untouched.
No man page update required (that never excluded -d0).
diffstat:
games/worms/worms.c | 81 +++++++++++++++++++++++++++++++++++++++++++---------
1 files changed, 67 insertions(+), 14 deletions(-)
diffs (154 lines):
diff -r a3e8e5e0eb2c -r 9b98c99e4458 games/worms/worms.c
--- a/games/worms/worms.c Sat Apr 15 13:40:23 2023 +0000
+++ b/games/worms/worms.c Sat Apr 15 15:21:56 2023 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: worms.c,v 1.25 2023/04/15 13:40:23 kre Exp $ */
+/* $NetBSD: worms.c,v 1.26 2023/04/15 15:21:56 kre Exp $ */
/*
* Copyright (c) 1980, 1993
@@ -39,7 +39,7 @@
#if 0
static char sccsid[] = "@(#)worms.c 8.1 (Berkeley) 5/31/93";
#else
-__RCSID("$NetBSD: worms.c,v 1.25 2023/04/15 13:40:23 kre Exp $");
+__RCSID("$NetBSD: worms.c,v 1.26 2023/04/15 15:21:56 kre Exp $");
#endif
#endif /* not lint */
@@ -62,11 +62,14 @@ static char sccsid[] = "@(#)worms.c 8.1
*/
#include <sys/types.h>
+#include <ctype.h>
#include <curses.h>
#include <err.h>
+#include <limits.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
+#include <strings.h>
#include <unistd.h>
static const struct options {
@@ -193,7 +196,9 @@ main(int argc, char *argv[])
int CO, LI, last, bottom, ch, length, number, trail;
short **ref;
const char *field;
+ char *ep;
unsigned int delay = 20000;
+ unsigned long ul;
length = 16;
number = 3;
@@ -202,23 +207,54 @@ main(int argc, char *argv[])
while ((ch = getopt(argc, argv, "d:fl:n:t")) != -1)
switch(ch) {
case 'd':
- if ((delay = (unsigned int)strtoul(optarg, NULL, 10)) < 1 || delay > 1000)
- errx(1, "invalid delay (1-1000)");
- delay *= 1000; /* ms -> us */
+ ul = strtoul(optarg, &ep, 10);
+ if (ep != optarg) {
+ while (isspace(*(unsigned char *)ep))
+ ep++;
+ }
+ if (ep == optarg ||
+ (*ep != '\0' &&
+ ( ep[1] == '\0' ? (*ep != 'm' && *ep != 'u') :
+ ( strcasecmp(ep, "ms") != 0 &&
+ strcasecmp(ep, "us") != 0 )) )) {
+ errx(1, "-d: invalid delay (%s)", optarg);
+ }
+ /*
+ * if ul >= INT_MAX/1000 we don't need the *1000,
+ * as even without that it will exceed the limit
+ * just below and be treated as an error.
+ * (This does assume >=32 bit int, but so does POSIX)
+ */
+ if (*ep != 'u' && ul < INT_MAX / 1000)
+ ul *= 1000; /* ms -> us */
+ if (ul > 1000*1000) {
+ errx(1,
+ "-d: delay (%s) out of rannge [0 - 1000]",
+ optarg);
+ }
+ delay = (unsigned int)ul;
break;
case 'f':
field = "WORM";
break;
case 'l':
- if ((length = atoi(optarg)) < 2 || length > 1024) {
- errx(1, "invalid length (%d - %d).",
- 2, 1024);
+ ul = strtoul(optarg, &ep, 10);
+ if (ep == optarg || *ep != '\0' ||
+ ul < 2 || ul > 1024) {
+ errx(1, "-l: invalid length (%s) [%d - %d].",
+ optarg, 2, 1024);
}
+ length = (int)ul;
break;
case 'n':
- if ((number = atoi(optarg)) < 1) {
- errx(1, "invalid number of worms.");
+ ul = strtoul(optarg, &ep, 10);
+ if (ep == optarg || *ep != '\0' ||
+ ul < 1 || ul > INT_MAX / 10 ) {
+ errx(1, "-n: invalid number of worms (%s).",
+ optarg);
}
+ /* upper bound is further limited later */
+ number = (int)ul;
break;
case 't':
trail = '.';
@@ -226,17 +262,32 @@ main(int argc, char *argv[])
case '?':
default:
(void)fprintf(stderr,
- "usage: worms [-ft] [-d delay] [-l length] [-n number]\n");
+ "usage: worms [-ft] [-d delay] [-l length]"
+ " [-n number]\n");
exit(1);
}
- if (!(worm = calloc((size_t)number, sizeof(struct worm))))
- nomem();
if (!initscr())
- errx(0, "couldn't initialize screen");
+ errx(1, "couldn't initialize screen");
curs_set(0);
CO = COLS;
LI = LINES;
+
+ if (CO > 4*length || LI < 4*length) {
+ ul = (unsigned long)LI / 2;
+ ul *= (unsigned long)CO / length;
+ } else {
+ ul = (unsigned long)CO / 2;
+ ul *= (unsigned long)LI / length;
+ }
+
+ if ((unsigned long)(unsigned)number > ul) {
+ endwin();
+ errx(1, "-n: too many worms (%d) max: %lu", number, ul);
+ }
+ if (!(worm = calloc((size_t)number, sizeof(struct worm))))
+ nomem();
+
last = CO - 1;
bottom = LI - 1;
if (!(ip = malloc((size_t)(LI * CO * sizeof(short)))))
@@ -351,8 +402,10 @@ onsig(int signo __unused)
sig_caught = 1;
}
+/* This is never called before curses is initialised */
static void
nomem(void)
{
+ endwin();
errx(1, "not enough memory.");
}
Home |
Main Index |
Thread Index |
Old Index