X-Git-Url: https://git.cameronkatri.com/bsdgames-darwin.git/blobdiff_plain/9b85680687e77d758ed4d5f002375aa6f88dc803..a802fa34d624ed55dc2ad33475fb1087c6763d05:/worms/worms.c diff --git a/worms/worms.c b/worms/worms.c index 370c2e0e..b0731afd 100644 --- a/worms/worms.c +++ b/worms/worms.c @@ -1,6 +1,8 @@ +/* $NetBSD: worms.c,v 1.9 1997/10/12 02:18:25 lukem Exp $ */ + /* - * Copyright (c) 1980 Regents of the University of California. - * All rights reserved. + * Copyright (c) 1980, 1993 + * The Regents of the University of California. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -31,14 +33,18 @@ * SUCH DAMAGE. */ +#include #ifndef lint -char copyright[] = -"@(#) Copyright (c) 1980 Regents of the University of California.\n\ - All rights reserved.\n"; +__COPYRIGHT("@(#) Copyright (c) 1980, 1993\n\ + The Regents of the University of California. All rights reserved.\n"); #endif /* not lint */ #ifndef lint -static char sccsid[] = "@(#)worms.c 5.9 (Berkeley) 2/28/91"; +#if 0 +static char sccsid[] = "@(#)worms.c 8.1 (Berkeley) 5/31/93"; +#else +__RCSID("$NetBSD: worms.c,v 1.9 1997/10/12 02:18:25 lukem Exp $"); +#endif #endif /* not lint */ /* @@ -59,14 +65,13 @@ static char sccsid[] = "@(#)worms.c 5.9 (Berkeley) 2/28/91"; * */ #include +#include + +#include #include #include -#ifdef USG -#include -#else -#include -#endif -#include +#include +#include static struct options { int nopts; @@ -165,8 +170,6 @@ static struct options { #define cursor(c, r) tputs(tgoto(CM, c, r), 1, fputchar) char *tcp; -int fputchar(); - static char flavor[] = { 'O', '*', '#', '$', '%', '0', '@', '~' }; @@ -180,37 +183,43 @@ static struct worm { short *xpos, *ypos; } *worm; -char *tgetstr(), *tgoto(); +void fputchar __P((int)); +int main __P((int, char **)); +void nomem __P((void)); +void onsig __P((int)); +int tgetent __P((char *, char *)); +int tgetflag __P((char *)); +int tgetnum __P((char *)); +char *tgetstr __P((char *, char **)); +char *tgoto __P((char *, int, int)); +int tputs __P((char *, int, void (*)(int))); +int main(argc, argv) int argc; - char **argv; + char *argv[]; { - extern int optind; - extern short ospeed; - extern char *optarg, *UP; - register int x, y, h, n; - register struct worm *w; - register struct options *op; - register short *ip; - register char *term; + extern char *UP; + int x, y, h, n; + struct worm *w; + struct options *op; + short *ip; + char *term; int CO, IN, LI, last, bottom, ch, length, number, trail, Wrap; - void onsig(); short **ref; char *AL, *BC, *CM, *EI, *HO, *IC, *IM, *IP, *SR; char *field, tcb[100], *mp; - long random(); -#ifdef USG - struct termio sg; -#else - struct sgttyb sg; + struct termios ti; +#ifdef TIOCGWINSZ + struct winsize ws; #endif + mp = NULL; length = 16; number = 3; trail = ' '; field = NULL; - while ((ch = getopt(argc, argv, "fl:n:t")) != EOF) + while ((ch = getopt(argc, argv, "fl:n:t")) != -1) switch(ch) { case 'f': field = "WORM"; @@ -236,7 +245,7 @@ main(argc, argv) case '?': default: (void)fprintf(stderr, - "usage: worms [-ft] [-length #] [-number #]\n"); + "usage: worms [-ft] [-l length] [-n number]\n"); exit(1); } @@ -244,8 +253,8 @@ main(argc, argv) (void)fprintf(stderr, "worms: no TERM environment variable.\n"); exit(1); } - if (!(worm = (struct worm *)malloc((u_int)number * - sizeof(struct worm))) || !(mp = malloc((u_int)1024))) + if (!(worm = malloc((size_t)number * + sizeof(struct worm))) || !(mp = malloc((size_t)1024))) nomem(); if (tgetent(mp, term) <= 0) { (void)fprintf(stderr, "worms: %s: unknown terminal type.\n", @@ -260,31 +269,34 @@ main(argc, argv) } AL = tgetstr("al", &tcp); BC = tgetflag("bs") ? "\b" : tgetstr("bc", &tcp); - if ((CO = tgetnum("co")) <= 0) - CO = 80; - last = CO - 1; EI = tgetstr("ei", &tcp); HO = tgetstr("ho", &tcp); IC = tgetstr("ic", &tcp); IM = tgetstr("im", &tcp); IN = tgetflag("in"); IP = tgetstr("ip", &tcp); - if ((LI = tgetnum("li")) <= 0) - LI = 24; - bottom = LI - 1; SR = tgetstr("sr", &tcp); UP = tgetstr("up", &tcp); -#ifdef USG - ioctl(1, TCGETA, &sg); - ospeed = sg.c_cflag&CBAUD; -#else - gtty(1, &sg); - ospeed = sg.sg_ospeed; +#ifdef TIOCGWINSZ + if (ioctl(fileno(stdout), TIOCGWINSZ, &ws) != -1 && + ws.ws_col && ws.ws_row) { + CO = ws.ws_col; + LI = ws.ws_row; + } else #endif + { + if ((CO = tgetnum("co")) <= 0) + CO = 80; + if ((LI = tgetnum("li")) <= 0) + LI = 24; + } + last = CO - 1; + bottom = LI - 1; + tcgetattr(fileno(stdout), &ti); Wrap = tgetflag("am"); - if (!(ip = (short *)malloc((u_int)(LI * CO * sizeof(short))))) + if (!(ip = malloc((size_t)(LI * CO * sizeof(short))))) nomem(); - if (!(ref = (short **)malloc((u_int)(LI * sizeof(short *))))) + if (!(ref = malloc((size_t)(LI * sizeof(short *))))) nomem(); for (n = 0; n < LI; ++n) { ref[n] = ip; @@ -296,12 +308,12 @@ main(argc, argv) ref[bottom][last] = 1; for (n = number, w = &worm[0]; --n >= 0; w++) { w->orientation = w->head = 0; - if (!(ip = (short *)malloc((u_int)(length * sizeof(short))))) + if (!(ip = malloc((size_t)(length * sizeof(short))))) nomem(); w->xpos = ip; for (x = length; --x >= 0;) *ip++ = -1; - if (!(ip = (short *)malloc((u_int)(length * sizeof(short))))) + if (!(ip = malloc((size_t)(length * sizeof(short))))) nomem(); w->ypos = ip; for (y = length; --y >= 0;) @@ -318,7 +330,7 @@ main(argc, argv) tputs(tgetstr("ti", &tcp), 1, fputchar); tputs(tgetstr("cl", &tcp), 1, fputchar); if (field) { - register char *p = field; + char *p = field; for (y = bottom; --y >= 0;) { for (x = CO; --x >= 0;) { @@ -394,7 +406,7 @@ main(argc, argv) if (++h == length) h = 0; if (w->xpos[w->head = h] >= 0) { - register int x1, y1; + int x1, y1; x1 = w->xpos[h]; y1 = w->ypos[h]; @@ -409,7 +421,7 @@ main(argc, argv) case 0: (void)fflush(stdout); abort(); - return; + return(1); case 1: w->orientation = op->opts[0]; break; @@ -427,19 +439,22 @@ main(argc, argv) } void -onsig() +onsig(signo) + int signo; { tputs(tgetstr("cl", &tcp), 1, fputchar); tputs(tgetstr("te", &tcp), 1, fputchar); exit(0); } +void fputchar(c) - char c; + int c; { - putchar(c); + (void)putchar(c); } +void nomem() { (void)fprintf(stderr, "worms: not enough memory.\n");