worms.c revision 1.23
1/* $OpenBSD: worms.c,v 1.23 2015/11/21 05:29:42 deraadt 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. 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. 18 * 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 29 * SUCH DAMAGE. 30 */ 31 32/* 33 * 34 * @@@ @@@ @@@@@@@@@@ @@@@@@@@@@@ @@@@@@@@@@@@ 35 * @@@ @@@ @@@@@@@@@@@@ @@@@@@@@@@@@ @@@@@@@@@@@@@ 36 * @@@ @@@ @@@@ @@@@ @@@@ @@@@ @@@ @@@@ 37 * @@@ @@ @@@ @@@ @@@ @@@ @@@ @@@ @@@ 38 * @@@ @@@@ @@@ @@@ @@@ @@@ @@@ @@@ @@@ 39 * @@@@ @@@@ @@@@ @@@ @@@ @@@ @@@ @@@ @@@ 40 * @@@@@@@@@@@@ @@@@ @@@@ @@@ @@@ @@@ @@@ 41 * @@@@ @@@@ @@@@@@@@@@@@ @@@ @@@ @@@ @@@ 42 * @@ @@ @@@@@@@@@@ @@@ @@@ @@@ @@@ 43 * 44 * Eric P. Scott 45 * Caltech High Energy Physics 46 * October, 1980 47 * 48 */ 49#include <sys/types.h> 50 51#include <curses.h> 52#include <err.h> 53#include <signal.h> 54#include <stdio.h> 55#include <stdlib.h> 56#include <termios.h> 57#include <unistd.h> 58 59static const struct options { 60 int nopts; 61 int opts[3]; 62} 63 normal[8] = { 64 { 3, { 7, 0, 1 } }, 65 { 3, { 0, 1, 2 } }, 66 { 3, { 1, 2, 3 } }, 67 { 3, { 2, 3, 4 } }, 68 { 3, { 3, 4, 5 } }, 69 { 3, { 4, 5, 6 } }, 70 { 3, { 5, 6, 7 } }, 71 { 3, { 6, 7, 0 } } 72}, upper[8] = { 73 { 1, { 1, 0, 0 } }, 74 { 2, { 1, 2, 0 } }, 75 { 0, { 0, 0, 0 } }, 76 { 0, { 0, 0, 0 } }, 77 { 0, { 0, 0, 0 } }, 78 { 2, { 4, 5, 0 } }, 79 { 1, { 5, 0, 0 } }, 80 { 2, { 1, 5, 0 } } 81}, 82 left[8] = { 83 { 0, { 0, 0, 0 } }, 84 { 0, { 0, 0, 0 } }, 85 { 0, { 0, 0, 0 } }, 86 { 2, { 2, 3, 0 } }, 87 { 1, { 3, 0, 0 } }, 88 { 2, { 3, 7, 0 } }, 89 { 1, { 7, 0, 0 } }, 90 { 2, { 7, 0, 0 } } 91}, 92 right[8] = { 93 { 1, { 7, 0, 0 } }, 94 { 2, { 3, 7, 0 } }, 95 { 1, { 3, 0, 0 } }, 96 { 2, { 3, 4, 0 } }, 97 { 0, { 0, 0, 0 } }, 98 { 0, { 0, 0, 0 } }, 99 { 0, { 0, 0, 0 } }, 100 { 2, { 6, 7, 0 } } 101}, 102 lower[8] = { 103 { 0, { 0, 0, 0 } }, 104 { 2, { 0, 1, 0 } }, 105 { 1, { 1, 0, 0 } }, 106 { 2, { 1, 5, 0 } }, 107 { 1, { 5, 0, 0 } }, 108 { 2, { 5, 6, 0 } }, 109 { 0, { 0, 0, 0 } }, 110 { 0, { 0, 0, 0 } } 111}, 112 upleft[8] = { 113 { 0, { 0, 0, 0 } }, 114 { 0, { 0, 0, 0 } }, 115 { 0, { 0, 0, 0 } }, 116 { 0, { 0, 0, 0 } }, 117 { 0, { 0, 0, 0 } }, 118 { 1, { 3, 0, 0 } }, 119 { 2, { 1, 3, 0 } }, 120 { 1, { 1, 0, 0 } } 121}, 122 upright[8] = { 123 { 2, { 3, 5, 0 } }, 124 { 1, { 3, 0, 0 } }, 125 { 0, { 0, 0, 0 } }, 126 { 0, { 0, 0, 0 } }, 127 { 0, { 0, 0, 0 } }, 128 { 0, { 0, 0, 0 } }, 129 { 0, { 0, 0, 0 } }, 130 { 1, { 5, 0, 0 } } 131}, 132 lowleft[8] = { 133 { 3, { 7, 0, 1 } }, 134 { 0, { 0, 0, 0 } }, 135 { 0, { 0, 0, 0 } }, 136 { 1, { 1, 0, 0 } }, 137 { 2, { 1, 7, 0 } }, 138 { 1, { 7, 0, 0 } }, 139 { 0, { 0, 0, 0 } }, 140 { 0, { 0, 0, 0 } } 141}, 142 lowright[8] = { 143 { 0, { 0, 0, 0 } }, 144 { 1, { 7, 0, 0 } }, 145 { 2, { 5, 7, 0 } }, 146 { 1, { 5, 0, 0 } }, 147 { 0, { 0, 0, 0 } }, 148 { 0, { 0, 0, 0 } }, 149 { 0, { 0, 0, 0 } }, 150 { 0, { 0, 0, 0 } } 151}; 152 153static const char flavor[] = { 154 'O', '*', '#', '$', '%', '0', '@', '~' 155}; 156static const short xinc[] = { 157 1, 1, 1, 0, -1, -1, -1, 0 158}, yinc[] = { 159 -1, 0, 1, 1, 1, 0, -1, -1 160}; 161static struct worm { 162 int orientation, head; 163 short *xpos, *ypos; 164} *worm; 165 166volatile sig_atomic_t sig_caught = 0; 167 168void nomem(void); 169void onsig(int); 170 171int 172main(int argc, char *argv[]) 173{ 174 int x, y, h, n; 175 struct worm *w; 176 const struct options *op; 177 short *ip; 178 int CO, LI, last, bottom, ch, length, number, trail; 179 short **ref; 180 const char *field, *errstr; 181 struct timespec sleeptime; 182 struct termios term; 183 speed_t speed; 184 time_t delay = 0; 185 186 if (pledge("stdio rpath tty", NULL) == -1) 187 err(1, "pledge"); 188 189 /* set default delay based on terminal baud rate */ 190 if (tcgetattr(STDOUT_FILENO, &term) == 0 && 191 (speed = cfgetospeed(&term)) > B9600) 192 delay = (speed / B9600) - 1; 193 194 length = 16; 195 number = 3; 196 trail = ' '; 197 field = NULL; 198 while ((ch = getopt(argc, argv, "d:fhl:n:t")) != -1) 199 switch(ch) { 200 case 'd': 201 delay = (time_t)strtonum(optarg, 0, 1000, &errstr); 202 if (errstr) 203 errx(1, "delay (0-1000) is %s: %s", errstr, optarg); 204 break; 205 case 'f': 206 field = "WORM"; 207 break; 208 case 'l': 209 if ((length = atoi(optarg)) < 2 || length > 1024) 210 errx(1, "invalid length (%d - %d).", 2, 1024); 211 break; 212 case 'n': 213 if ((number = atoi(optarg)) < 1 || number > 100) 214 errx(1, "invalid number of worms (%d - %d).", 215 1, 100); 216 break; 217 case 't': 218 trail = '.'; 219 break; 220 case '?': case 'h': 221 default: 222 (void)fprintf(stderr, 223 "usage: worms [-ft] [-d delay] [-l length] [-n number]\n"); 224 exit(1); 225 } 226 227 /* Convert delay from ms -> ns */ 228 sleeptime.tv_sec = 0; 229 sleeptime.tv_nsec = delay * 500000; 230 timespecadd(&sleeptime, &sleeptime, &sleeptime); 231 232 if (!(worm = calloc(number, sizeof(struct worm)))) 233 nomem(); 234 initscr(); 235 curs_set(0); 236 CO = COLS; 237 LI = LINES; 238 last = CO - 1; 239 bottom = LI - 1; 240 if (!(ip = reallocarray(NULL, LI, CO * sizeof(short))) || 241 !(ref = calloc(LI, sizeof(short *)))) { 242 endwin(); 243 nomem(); 244 } 245 for (n = 0; n < LI; ++n) { 246 ref[n] = ip; 247 ip += CO; 248 } 249 for (ip = ref[0], n = LI * CO; --n >= 0;) 250 *ip++ = 0; 251 for (n = number, w = &worm[0]; --n >= 0; w++) { 252 w->orientation = w->head = 0; 253 if (!(ip = calloc(length, sizeof(short)))) { 254 endwin(); 255 nomem(); 256 } 257 w->xpos = ip; 258 for (x = length; --x >= 0;) 259 *ip++ = -1; 260 if (!(ip = calloc(length, sizeof(short)))) { 261 endwin(); 262 nomem(); 263 } 264 w->ypos = ip; 265 for (y = length; --y >= 0;) 266 *ip++ = -1; 267 } 268 269 (void)signal(SIGHUP, onsig); 270 (void)signal(SIGINT, onsig); 271 (void)signal(SIGQUIT, onsig); 272 (void)signal(SIGSTOP, onsig); 273 (void)signal(SIGTSTP, onsig); 274 (void)signal(SIGTERM, onsig); 275 276 if (field) { 277 const char *p = field; 278 279 for (y = LI; --y >= 0;) { 280 for (x = CO; --x >= 0;) { 281 addch(*p++); 282 if (!*p) 283 p = field; 284 } 285 refresh(); 286 } 287 } 288 for (;;) { 289 refresh(); 290 if (sig_caught) { 291 endwin(); 292 exit(0); 293 } 294 nanosleep(&sleeptime, NULL); 295 for (n = 0, w = &worm[0]; n < number; n++, w++) { 296 if ((x = w->xpos[h = w->head]) < 0) { 297 mvaddch(y = w->ypos[h] = bottom, 298 x = w->xpos[h] = 0, 299 flavor[n % sizeof(flavor)]); 300 ref[y][x]++; 301 } 302 else 303 y = w->ypos[h]; 304 if (++h == length) 305 h = 0; 306 if (w->xpos[w->head = h] >= 0) { 307 int x1, y1; 308 309 x1 = w->xpos[h]; 310 y1 = w->ypos[h]; 311 if (--ref[y1][x1] == 0) 312 mvaddch(y1, x1, trail); 313 } 314 op = &(!x ? (!y ? upleft : (y == bottom ? lowleft : left)) : (x == last ? (!y ? upright : (y == bottom ? lowright : right)) : (!y ? upper : (y == bottom ? lower : normal))))[w->orientation]; 315 switch (op->nopts) { 316 case 0: 317 endwin(); 318 return(1); 319 case 1: 320 w->orientation = op->opts[0]; 321 break; 322 default: 323 w->orientation = 324 op->opts[arc4random_uniform(op->nopts)]; 325 } 326 mvaddch(y += yinc[w->orientation], 327 x += xinc[w->orientation], 328 flavor[n % sizeof(flavor)]); 329 ref[w->ypos[h] = y][w->xpos[h] = x]++; 330 } 331 } 332} 333 334void 335onsig(int signo) 336{ 337 sig_caught = 1; 338} 339 340void 341nomem(void) 342{ 343 errx(1, "not enough memory."); 344} 345