1 /* $NetBSD: worms.c,v 1.19 2008/08/08 16:10:47 drochner Exp $ */
4 * Copyright (c) 1980, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
34 __COPYRIGHT("@(#) Copyright (c) 1980, 1993\
35 The Regents of the University of California. All rights reserved.");
40 static char sccsid
[] = "@(#)worms.c 8.1 (Berkeley) 5/31/93";
42 __RCSID("$NetBSD: worms.c,v 1.19 2008/08/08 16:10:47 drochner Exp $");
48 * @@@ @@@ @@@@@@@@@@ @@@@@@@@@@@ @@@@@@@@@@@@
49 * @@@ @@@ @@@@@@@@@@@@ @@@@@@@@@@@@ @@@@@@@@@@@@@
50 * @@@ @@@ @@@@ @@@@ @@@@ @@@@ @@@ @@@@
51 * @@@ @@ @@@ @@@ @@@ @@@ @@@ @@@ @@@
52 * @@@ @@@@ @@@ @@@ @@@ @@@ @@@ @@@ @@@
53 * @@@@ @@@@ @@@@ @@@ @@@ @@@ @@@ @@@ @@@
54 * @@@@@@@@@@@@ @@@@ @@@@ @@@ @@@ @@@ @@@
55 * @@@@ @@@@ @@@@@@@@@@@@ @@@ @@@ @@@ @@@
56 * @@ @@ @@@@@@@@@@ @@@ @@@ @@@ @@@
59 * Caltech High Energy Physics
63 #include <sys/types.h>
72 static const struct options
{
167 static const char flavor
[] = {
168 'O', '*', '#', '$', '%', '0', '@', '~'
170 static const short xinc
[] = {
171 1, 1, 1, 0, -1, -1, -1, 0
173 -1, 0, 1, 1, 1, 0, -1, -1
176 int orientation
, head
;
180 static volatile sig_atomic_t sig_caught
= 0;
182 int main(int, char **);
183 static void nomem(void) __dead
;
184 static void onsig(int);
193 const struct options
*op
;
195 int CO
, LI
, last
, bottom
, ch
, length
, number
, trail
;
199 unsigned int delay
= 0;
206 while ((ch
= getopt(argc
, argv
, "d:fl:n:t")) != -1)
209 if ((delay
= (unsigned int)strtoul(optarg
, (char **)NULL
, 10)) < 1 || delay
> 1000)
210 errx(1, "invalid delay (1-1000)");
211 delay
*= 1000; /* ms -> us */
217 if ((length
= atoi(optarg
)) < 2 || length
> 1024) {
218 errx(1, "invalid length (%d - %d).",
223 if ((number
= atoi(optarg
)) < 1) {
224 errx(1, "invalid number of worms.");
232 (void)fprintf(stderr
,
233 "usage: worms [-ft] [-d delay] [-l length] [-n number]\n");
237 if (!(worm
= malloc((size_t)number
*
238 sizeof(struct worm
))) || !(mp
= malloc((size_t)1024)))
241 errx(0, "couldn't initialize screen");
247 if (!(ip
= malloc((size_t)(LI
* CO
* sizeof(short)))))
249 if (!(ref
= malloc((size_t)(LI
* sizeof(short *)))))
251 for (n
= 0; n
< LI
; ++n
) {
255 for (ip
= ref
[0], n
= LI
* CO
; --n
>= 0;)
257 for (n
= number
, w
= &worm
[0]; --n
>= 0; w
++) {
258 w
->orientation
= w
->head
= 0;
259 if (!(ip
= malloc((size_t)(length
* sizeof(short)))))
262 for (x
= length
; --x
>= 0;)
264 if (!(ip
= malloc((size_t)(length
* sizeof(short)))))
267 for (y
= length
; --y
>= 0;)
271 (void)signal(SIGHUP
, onsig
);
272 (void)signal(SIGINT
, onsig
);
273 (void)signal(SIGQUIT
, onsig
);
274 (void)signal(SIGTSTP
, onsig
);
275 (void)signal(SIGTERM
, onsig
);
278 const char *p
= field
;
280 for (y
= LI
; --y
>= 0;) {
281 for (x
= CO
; --x
>= 0;) {
296 if (delay
% 1000000 != 0)
297 usleep(delay
% 1000000);
298 if (delay
>= 1000000)
299 sleep(delay
/ 1000000);
301 for (n
= 0, w
= &worm
[0]; n
< number
; n
++, w
++) {
302 if ((x
= w
->xpos
[h
= w
->head
]) < 0) {
303 mvaddch(y
= w
->ypos
[h
] = bottom
,
305 flavor
[n
% sizeof(flavor
)]);
312 if (w
->xpos
[w
->head
= h
] >= 0) {
317 if (--ref
[y1
][x1
] == 0) {
318 mvaddch(y1
, x1
, trail
);
321 op
= &(!x
? (!y
? upleft
: (y
== bottom
? lowleft
: left
)) : (x
== last
? (!y
? upright
: (y
== bottom
? lowright
: right
)) : (!y
? upper
: (y
== bottom
? lower
: normal
))))[w
->orientation
];
328 w
->orientation
= op
->opts
[0];
332 op
->opts
[(int)random() % op
->nopts
];
334 mvaddch(y
+= yinc
[w
->orientation
],
335 x
+= xinc
[w
->orientation
],
336 flavor
[n
% sizeof(flavor
)]);
337 ref
[w
->ypos
[h
] = y
][w
->xpos
[h
] = x
]++;
352 errx(1, "not enough memory.");