-/* $NetBSD: worm.c,v 1.5 1995/04/22 07:56:23 cgd Exp $ */
+/* $NetBSD: worm.c,v 1.19 2000/05/08 07:56:06 mycroft Exp $ */
/*
* Copyright (c) 1980, 1993
* SUCH DAMAGE.
*/
+#include <sys/cdefs.h>
#ifndef lint
-static char copyright[] =
-"@(#) Copyright (c) 1980, 1993\n\
- The Regents of the University of California. 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
#if 0
static char sccsid[] = "@(#)worm.c 8.1 (Berkeley) 5/31/93";
#else
-static char rcsid[] = "$NetBSD: worm.c,v 1.5 1995/04/22 07:56:23 cgd Exp $";
+__RCSID("$NetBSD: worm.c,v 1.19 2000/05/08 07:56:06 mycroft Exp $");
#endif
#endif /* not lint */
#include <ctype.h>
#include <curses.h>
+#include <err.h>
#include <signal.h>
+#include <stdlib.h>
#include <termios.h>
+#include <unistd.h>
#define newlink() (struct body *) malloc(sizeof (struct body));
#define HEAD '@'
#define LENGTH 7
#define RUNLEN 8
#define CNTRL(p) (p-'A'+1)
-#ifndef baudrate
-# define baudrate() _tty.sg_ospeed
-#endif
WINDOW *tv;
WINDOW *stw;
int slow = 0;
int score = 0;
int start_len = LENGTH;
-char lastch;
+int lastch;
char outbuf[BUFSIZ];
-void leave(), wake(), suspend();
+void crash __P((void)) __attribute__((__noreturn__));
+void display __P((const struct body *, char));
+int main __P((int, char **));
+void leave __P((int)) __attribute__((__noreturn__));
+void life __P((void));
+void newpos __P((struct body *));
+void process __P((int));
+void prize __P((void));
+int rnd __P((int));
+void setup __P((void));
+void wake __P((int));
+int
main(argc, argv)
int argc;
char **argv;
{
- char ch;
+
+ /* Revoke setgid privileges */
+ setgid(getgid());
if (argc == 2)
start_len = atoi(argv[1]);
signal(SIGALRM, wake);
signal(SIGINT, leave);
signal(SIGQUIT, leave);
- signal(SIGTSTP, suspend); /* process control signal */
initscr();
crmode();
noecho();
+#ifdef KEY_LEFT
+ keypad(stdscr, TRUE);
+#endif
slow = (baudrate() <= 1200);
clear();
stw = newwin(1, COLS-1, 0, 0);
else
{
fflush(stdout);
- if (read(0, &ch, 1) >= 0)
- process(ch);
+ process(getch());
}
}
}
+void
life()
{
- register struct body *bp, *np;
- register int i;
+ struct body *bp, *np;
+ int i;
+ np = NULL;
head = newlink();
+ if (head == NULL)
+ err(1, NULL);
head->x = start_len+2;
head->y = 12;
head->next = NULL;
display(head, HEAD);
for (i = 0, bp = head; i < start_len; i++, bp = np) {
np = newlink();
+ if (np == NULL)
+ err(1, NULL);
np->next = bp;
bp->prev = np;
np->x = bp->x - 1;
tail->prev = NULL;
}
+void
display(pos, chr)
-struct body *pos;
-char chr;
+ const struct body *pos;
+ char chr;
{
wmove(tv, pos->y, pos->x);
waddch(tv, chr);
}
void
-leave()
+leave(dummy)
+ int dummy;
{
endwin();
+
+ if (dummy == 0){ /* called via crash() */
+ printf("\nWell, you ran into something and the game is over.\n");
+ printf("Your final score was %d\n\n", score);
+ }
exit(0);
}
void
-wake()
+wake(dummy)
+ int dummy __attribute__((__unused__));
{
signal(SIGALRM, wake);
fflush(stdout);
process(lastch);
}
+int
rnd(range)
+ int range;
{
return abs((rand()>>5)+(rand()>>5)) % range;
}
+void
newpos(bp)
-struct body * bp;
+ struct body * bp;
{
do {
bp->y = rnd(LINES-3)+ 2;
} while(winch(tv) != ' ');
}
+void
prize()
{
int value;
wrefresh(tv);
}
+void
process(ch)
-char ch;
+ int ch;
{
- register int x,y;
+ int x,y;
struct body *nh;
alarm(0);
y = head->y;
switch(ch)
{
- case 'h': x--; break;
- case 'j': y++; break;
- case 'k': y--; break;
- case 'l': x++; break;
+#ifdef KEY_LEFT
+ case KEY_LEFT:
+#endif
+ case 'h':
+ x--; break;
+
+#ifdef KEY_DOWN
+ case KEY_DOWN:
+#endif
+ case 'j':
+ y++; break;
+
+#ifdef KEY_UP
+ case KEY_UP:
+#endif
+ case 'k':
+ y--; break;
+
+#ifdef KEY_RIGHT
+ case KEY_RIGHT:
+#endif
+ case 'l':
+ x++; break;
+
case 'H': x--; running = RUNLEN; ch = tolower(ch); break;
case 'J': y++; running = RUNLEN/2; ch = tolower(ch); break;
case 'K': y--; running = RUNLEN/2; ch = tolower(ch); break;
case 'L': x++; running = RUNLEN; ch = tolower(ch); break;
case '\f': setup(); return;
- case CNTRL('Z'): suspend(); return;
- case CNTRL('C'): crash(); return;
- case CNTRL('D'): crash(); return;
+
+ case ERR:
+ case CNTRL('C'):
+ case CNTRL('D'):
+ crash();
+ return;
+
default: if (! running) alarm(1);
return;
}
}
else if(ch != ' ') crash();
nh = newlink();
+ if (nh == NULL)
+ err(1, NULL);
nh->next = NULL;
nh->prev = head;
head->next = nh;
display(nh, HEAD);
head = nh;
if (!(slow && running))
+ {
+ wmove(tv, head->y, head->x);
wrefresh(tv);
+ }
if (!running)
alarm(1);
}
+void
crash()
{
- sleep(2);
- clear();
- move(23, 0);
- refresh();
- printf("Well, you ran into something and the game is over.\n");
- printf("Your final score was %d\n", score);
- leave();
+ leave(0);
}
void
-suspend()
-{
- char *sh;
-
- move(LINES-1, 0);
- refresh();
- endwin();
- fflush(stdout);
- kill(getpid(), SIGTSTP);
- signal(SIGTSTP, suspend);
- crmode();
- noecho();
- setup();
-}
-
setup()
{
clear();