+/* $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
* SUCH DAMAGE.
*/
+#include <sys/cdefs.h>
#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 */
/*
*
*/
#include <sys/types.h>
+#include <sys/ioctl.h>
+
+#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
-#ifdef USG
-#include <termio.h>
-#else
-#include <sgtty.h>
-#endif
-#include <signal.h>
+#include <termios.h>
+#include <unistd.h>
static struct options {
int nopts;
#define cursor(c, r) tputs(tgoto(CM, c, r), 1, fputchar)
char *tcp;
-int fputchar();
-
static char flavor[] = {
'O', '*', '#', '$', '%', '0', '@', '~'
};
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";
case '?':
default:
(void)fprintf(stderr,
- "usage: worms [-ft] [-length #] [-number #]\n");
+ "usage: worms [-ft] [-l length] [-n number]\n");
exit(1);
}
(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",
}
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;
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;)
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;) {
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];
case 0:
(void)fflush(stdout);
abort();
- return;
+ return(1);
case 1:
w->orientation = op->opts[0];
break;
}
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");