worms.c revision 1.12
1/* $OpenBSD: worms.c,v 1.12 2002/05/31 03:40:01 pjanzen Exp $ */ 2 3/* 4 * Copyright (c) 1980, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 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. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by the University of 18 * California, Berkeley and its contributors. 19 * 4. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 */ 35 36#ifndef lint 37static char copyright[] = 38"@(#) Copyright (c) 1980, 1993\n\ 39 The Regents of the University of California. All rights reserved.\n"; 40#endif /* not lint */ 41 42#ifndef lint 43#if 0 44static char sccsid[] = "@(#)worms.c 8.1 (Berkeley) 5/31/93"; 45#else 46static char rcsid[] = "$OpenBSD: worms.c,v 1.12 2002/05/31 03:40:01 pjanzen Exp $"; 47#endif 48#endif /* not lint */ 49 50/* 51 * 52 * @@@ @@@ @@@@@@@@@@ @@@@@@@@@@@ @@@@@@@@@@@@ 53 * @@@ @@@ @@@@@@@@@@@@ @@@@@@@@@@@@ @@@@@@@@@@@@@ 54 * @@@ @@@ @@@@ @@@@ @@@@ @@@@ @@@ @@@@ 55 * @@@ @@ @@@ @@@ @@@ @@@ @@@ @@@ @@@ 56 * @@@ @@@@ @@@ @@@ @@@ @@@ @@@ @@@ @@@ 57 * @@@@ @@@@ @@@@ @@@ @@@ @@@ @@@ @@@ @@@ 58 * @@@@@@@@@@@@ @@@@ @@@@ @@@ @@@ @@@ @@@ 59 * @@@@ @@@@ @@@@@@@@@@@@ @@@ @@@ @@@ @@@ 60 * @@ @@ @@@@@@@@@@ @@@ @@@ @@@ @@@ 61 * 62 * Eric P. Scott 63 * Caltech High Energy Physics 64 * October, 1980 65 * 66 */ 67#include <sys/types.h> 68 69#include <curses.h> 70#include <err.h> 71#include <signal.h> 72#include <stdio.h> 73#include <stdlib.h> 74#include <unistd.h> 75 76static const struct options { 77 int nopts; 78 int opts[3]; 79} 80 normal[8] = { 81 { 3, { 7, 0, 1 } }, 82 { 3, { 0, 1, 2 } }, 83 { 3, { 1, 2, 3 } }, 84 { 3, { 2, 3, 4 } }, 85 { 3, { 3, 4, 5 } }, 86 { 3, { 4, 5, 6 } }, 87 { 3, { 5, 6, 7 } }, 88 { 3, { 6, 7, 0 } } 89}, upper[8] = { 90 { 1, { 1, 0, 0 } }, 91 { 2, { 1, 2, 0 } }, 92 { 0, { 0, 0, 0 } }, 93 { 0, { 0, 0, 0 } }, 94 { 0, { 0, 0, 0 } }, 95 { 2, { 4, 5, 0 } }, 96 { 1, { 5, 0, 0 } }, 97 { 2, { 1, 5, 0 } } 98}, 99 left[8] = { 100 { 0, { 0, 0, 0 } }, 101 { 0, { 0, 0, 0 } }, 102 { 0, { 0, 0, 0 } }, 103 { 2, { 2, 3, 0 } }, 104 { 1, { 3, 0, 0 } }, 105 { 2, { 3, 7, 0 } }, 106 { 1, { 7, 0, 0 } }, 107 { 2, { 7, 0, 0 } } 108}, 109 right[8] = { 110 { 1, { 7, 0, 0 } }, 111 { 2, { 3, 7, 0 } }, 112 { 1, { 3, 0, 0 } }, 113 { 2, { 3, 4, 0 } }, 114 { 0, { 0, 0, 0 } }, 115 { 0, { 0, 0, 0 } }, 116 { 0, { 0, 0, 0 } }, 117 { 2, { 6, 7, 0 } } 118}, 119 lower[8] = { 120 { 0, { 0, 0, 0 } }, 121 { 2, { 0, 1, 0 } }, 122 { 1, { 1, 0, 0 } }, 123 { 2, { 1, 5, 0 } }, 124 { 1, { 5, 0, 0 } }, 125 { 2, { 5, 6, 0 } }, 126 { 0, { 0, 0, 0 } }, 127 { 0, { 0, 0, 0 } } 128}, 129 upleft[8] = { 130 { 0, { 0, 0, 0 } }, 131 { 0, { 0, 0, 0 } }, 132 { 0, { 0, 0, 0 } }, 133 { 0, { 0, 0, 0 } }, 134 { 0, { 0, 0, 0 } }, 135 { 1, { 3, 0, 0 } }, 136 { 2, { 1, 3, 0 } }, 137 { 1, { 1, 0, 0 } } 138}, 139 upright[8] = { 140 { 2, { 3, 5, 0 } }, 141 { 1, { 3, 0, 0 } }, 142 { 0, { 0, 0, 0 } }, 143 { 0, { 0, 0, 0 } }, 144 { 0, { 0, 0, 0 } }, 145 { 0, { 0, 0, 0 } }, 146 { 0, { 0, 0, 0 } }, 147 { 1, { 5, 0, 0 } } 148}, 149 lowleft[8] = { 150 { 3, { 7, 0, 1 } }, 151 { 0, { 0, 0, 0 } }, 152 { 0, { 0, 0, 0 } }, 153 { 1, { 1, 0, 0 } }, 154 { 2, { 1, 7, 0 } }, 155 { 1, { 7, 0, 0 } }, 156 { 0, { 0, 0, 0 } }, 157 { 0, { 0, 0, 0 } } 158}, 159 lowright[8] = { 160 { 0, { 0, 0, 0 } }, 161 { 1, { 7, 0, 0 } }, 162 { 2, { 5, 7, 0 } }, 163 { 1, { 5, 0, 0 } }, 164 { 0, { 0, 0, 0 } }, 165 { 0, { 0, 0, 0 } }, 166 { 0, { 0, 0, 0 } }, 167 { 0, { 0, 0, 0 } } 168}; 169 170static const char flavor[] = { 171 'O', '*', '#', '$', '%', '0', '@', '~' 172}; 173static const short xinc[] = { 174 1, 1, 1, 0, -1, -1, -1, 0 175}, yinc[] = { 176 -1, 0, 1, 1, 1, 0, -1, -1 177}; 178static struct worm { 179 int orientation, head; 180 short *xpos, *ypos; 181} *worm; 182 183volatile sig_atomic_t sig_caught = 0; 184 185void nomem(void); 186void onsig(int); 187 188int 189main(argc, argv) 190 int argc; 191 char *argv[]; 192{ 193 int x, y, h, n; 194 struct worm *w; 195 const struct options *op; 196 short *ip; 197 int CO, LI, last, bottom, ch, length, number, trail; 198 short **ref; 199 const char *field; 200 u_int delay = 0; 201 202 length = 16; 203 number = 3; 204 trail = ' '; 205 field = NULL; 206 while ((ch = getopt(argc, argv, "d:fhl:n:t")) != -1) 207 switch(ch) { 208 case 'd': 209 if ((delay = (u_int)strtoul(optarg, (char **)NULL, 10)) < 1 210 || delay > 1000) 211 errx(1, "invalid delay (1-1000)"); 212 delay *= 1000; /* ms -> us */ 213 break; 214 case 'f': 215 field = "WORM"; 216 break; 217 case 'l': 218 if ((length = atoi(optarg)) < 2 || length > 1024) 219 errx(1, "invalid length (%d - %d).", 2, 1024); 220 break; 221 case 'n': 222 if ((number = atoi(optarg)) < 1 || number > 100) 223 errx(1, "invalid number of worms (%d - %d).", 224 1, 100); 225 break; 226 case 't': 227 trail = '.'; 228 break; 229 case '?': case 'h': 230 default: 231 (void)fprintf(stderr, 232 "usage: worms [-ft] [-d delay] [-l length] [-n number]\n"); 233 exit(1); 234 } 235 236 srandom(time((time_t *)NULL)); 237 if (!(worm = malloc((size_t)number * sizeof(struct worm)))) 238 nomem(); 239 initscr(); 240 curs_set(0); 241 CO = COLS; 242 LI = LINES; 243 last = CO - 1; 244 bottom = LI - 1; 245 if (!(ip = malloc((size_t)(LI * CO * sizeof(short)))) || 246 !(ref = malloc((size_t)(LI * sizeof(short *))))) { 247 endwin(); 248 nomem(); 249 } 250 for (n = 0; n < LI; ++n) { 251 ref[n] = ip; 252 ip += CO; 253 } 254 for (ip = ref[0], n = LI * CO; --n >= 0;) 255 *ip++ = 0; 256 for (n = number, w = &worm[0]; --n >= 0; w++) { 257 w->orientation = w->head = 0; 258 if (!(ip = malloc((size_t)(length * sizeof(short))))) { 259 endwin(); 260 nomem(); 261 } 262 w->xpos = ip; 263 for (x = length; --x >= 0;) 264 *ip++ = -1; 265 if (!(ip = malloc((size_t)(length * sizeof(short))))) { 266 endwin(); 267 nomem(); 268 } 269 w->ypos = ip; 270 for (y = length; --y >= 0;) 271 *ip++ = -1; 272 } 273 274 (void)signal(SIGHUP, onsig); 275 (void)signal(SIGINT, onsig); 276 (void)signal(SIGQUIT, onsig); 277 (void)signal(SIGSTOP, onsig); 278 (void)signal(SIGTSTP, onsig); 279 (void)signal(SIGTERM, onsig); 280 281 if (field) { 282 const char *p = field; 283 284 for (y = LI; --y >= 0;) { 285 for (x = CO; --x >= 0;) { 286 addch(*p++); 287 if (!*p) 288 p = field; 289 } 290 refresh(); 291 } 292 } 293 for (;;) { 294 refresh(); 295 if (sig_caught) { 296 endwin(); 297 exit(0); 298 } 299 if (delay) usleep(delay); 300 for (n = 0, w = &worm[0]; n < number; n++, w++) { 301 if ((x = w->xpos[h = w->head]) < 0) { 302 mvaddch(y = w->ypos[h] = bottom, 303 x = w->xpos[h] = 0, 304 flavor[n % sizeof(flavor)]); 305 ref[y][x]++; 306 } 307 else 308 y = w->ypos[h]; 309 if (++h == length) 310 h = 0; 311 if (w->xpos[w->head = h] >= 0) { 312 register int x1, y1; 313 314 x1 = w->xpos[h]; 315 y1 = w->ypos[h]; 316 if (--ref[y1][x1] == 0) 317 mvaddch(y1, x1, trail); 318 } 319 op = &(!x ? (!y ? upleft : (y == bottom ? lowleft : left)) : (x == last ? (!y ? upright : (y == bottom ? lowright : right)) : (!y ? upper : (y == bottom ? lower : normal))))[w->orientation]; 320 switch (op->nopts) { 321 case 0: 322 endwin(); 323 return(1); 324 case 1: 325 w->orientation = op->opts[0]; 326 break; 327 default: 328 w->orientation = 329 op->opts[(int)random() % op->nopts]; 330 } 331 mvaddch(y += yinc[w->orientation], 332 x += xinc[w->orientation], 333 flavor[n % sizeof(flavor)]); 334 ref[w->ypos[h] = y][w->xpos[h] = x]++; 335 } 336 } 337} 338 339void 340onsig(signo) 341 int signo; 342{ 343 sig_caught = 1; 344} 345 346void 347nomem() 348{ 349 errx(1, "not enough memory."); 350} 351