-/* $NetBSD: wump.c,v 1.4 1995/04/24 12:26:22 cgd Exp $ */
+/* $NetBSD: wump.c,v 1.9 1999/07/14 22:49:27 hubertf Exp $ */
/*
* Copyright (c) 1989, 1993
* SUCH DAMAGE.
*/
+#include <sys/cdefs.h>
#ifndef lint
-static char copyright[] =
-"@(#) Copyright (c) 1989, 1993\n\
- The Regents of the University of California. All rights reserved.\n";
+__COPYRIGHT("@(#) Copyright (c) 1989, 1993\n\
+ The Regents of the University of California. All rights reserved.\n");
#endif /* not lint */
#ifndef lint
#if 0
static char sccsid[] = "@(#)wump.c 8.1 (Berkeley) 5/31/93";
#else
-static char rcsid[] = "$NetBSD: wump.c,v 1.4 1995/04/24 12:26:22 cgd Exp $";
+__RCSID("$NetBSD: wump.c,v 1.9 1999/07/14 22:49:27 hubertf Exp $");
#endif
#endif /* not lint */
* would care to remember.
*/
+#include <err.h>
#include <sys/types.h>
#include <sys/file.h>
+#include <sys/wait.h>
#include <stdio.h>
+#include <stdlib.h>
#include <string.h>
+#include <unistd.h>
#include "pathnames.h"
/* some defines to spec out what our wumpus cave should look like */
char answer[20]; /* user input */
+int bats_nearby __P((void));
+void cave_init __P((void));
+void clear_things_in_cave __P((void));
+void display_room_stats __P((void));
+int getans __P((const char *));
+void initialize_things_in_cave __P((void));
+void instructions __P((void));
+int int_compare __P((const void *, const void *));
+void jump __P((int));
+void kill_wump __P((void));
+int main __P((int, char **));
+int move_to __P((const char *));
+void move_wump __P((void));
+void no_arrows __P((void));
+void pit_kill __P((void));
+int pit_nearby __P((void));
+void pit_survive __P((void));
+int shoot __P((char *));
+void shoot_self __P((void));
+int take_action __P((void));
+void usage __P((void)) __attribute__((__noreturn__));
+void wump_kill __P((void));
+int wump_nearby __P((void));
+
+int
main(argc, argv)
int argc;
char **argv;
{
- extern char *optarg;
int c;
#ifdef DEBUG
- while ((c = getopt(argc, argv, "a:b:hp:r:t:d")) != EOF)
+ while ((c = getopt(argc, argv, "a:b:hp:r:t:d")) != -1)
#else
- while ((c = getopt(argc, argv, "a:b:hp:r:t:")) != EOF)
+ while ((c = getopt(argc, argv, "a:b:hp:r:t:")) != -1)
#endif
switch (c) {
case 'a':
cave_init();
}
/* NOTREACHED */
+ return (0);
}
+void
display_room_stats()
{
- register int i;
+ int i;
/*
* Routine will explain what's going on with the current room, as well
(void)printf("and %d.\n", cave[player_loc].tunnel[link_num - 1]);
}
+int
take_action()
{
/*
return(0);
}
+int
move_to(room_number)
- char *room_number;
+ const char *room_number;
{
int i, just_moved_by_bats, next_room, tunnel_available;
wump_kill();
return(1);
}
- if (cave[next_room].has_a_pit)
+ if (cave[next_room].has_a_pit) {
if (random() % 12 < 2) {
pit_survive();
return(0);
pit_kill();
return(1);
}
+ }
if (cave[next_room].has_a_bat) {
(void)printf(
return(0);
}
+int
shoot(room_list)
char *room_list;
{
int chance, next, roomcnt;
int j, arrow_location, link, ok;
- char *p, *strtok();
+ char *p;
/*
* Implement shooting arrows. Arrows are shot by the player indicating
*/
arrow_location = player_loc;
for (roomcnt = 1;; ++roomcnt, room_list = NULL) {
- if (!(p = strtok(room_list, " \t\n")))
+ if (!(p = strtok(room_list, " \t\n"))) {
if (roomcnt == 1) {
(void)printf(
"The arrow falls to the ground at your feet!\n");
return(0);
} else
break;
+ }
if (roomcnt > 5) {
(void)printf(
"The arrow wavers in its flight and and can go no further!\n");
return(0);
}
+void
cave_init()
{
- register int i, j, k, link;
- int delta, int_compare();
- time_t time();
+ int i, j, k, link;
+ int delta;
/*
* This does most of the interesting work in this program actually!
#endif
}
+void
clear_things_in_cave()
{
- register int i;
+ int i;
/*
* remove bats and pits from the current cave in preparation for us
cave[i].has_a_bat = cave[i].has_a_pit = 0;
}
+void
initialize_things_in_cave()
{
- register int i, loc;
+ int i, loc;
/* place some bats, pits, the wumpus, and the player. */
for (i = 0; i < bat_num; ++i) {
(link_num / room_num < 0.4 ? wump_nearby() : 0) : 0));
}
+int
getans(prompt)
- char *prompt;
+ const char *prompt;
{
char buf[20];
/* NOTREACHED */
}
+int
bats_nearby()
{
- register int i;
+ int i;
/* check for bats in the immediate vicinity */
for (i = 0; i < link_num; ++i)
return(0);
}
+int
pit_nearby()
{
- register int i;
+ int i;
/* check for pits in the immediate vicinity */
for (i = 0; i < link_num; ++i)
return(0);
}
+int
wump_nearby()
{
- register int i, j;
+ int i, j;
/* check for a wumpus within TWO caves of where we are */
for (i = 0; i < link_num; ++i) {
return(0);
}
+void
move_wump()
{
wumpus_loc = cave[wumpus_loc].tunnel[random() % link_num];
}
+int
int_compare(a, b)
- int *a, *b;
+ const void *a, *b;
{
- return(*a < *b ? -1 : 1);
+ return(*(const int *)a < *(const int *)b ? -1 : 1);
}
+void
instructions()
{
- char buf[120], *p, *getenv();
+ const char *pager;
+ pid_t pid;
+ int status;
+ int fd;
/*
* read the instructions file, if needed, and show the user how to
return;
}
- if (!(p = getenv("PAGER")) ||
- strlen(p) > sizeof(buf) + strlen(_PATH_WUMPINFO) + 5)
- p = _PATH_PAGER;
-
- (void)sprintf(buf, "%s %s", p, _PATH_WUMPINFO);
- (void)system(buf);
+ if (!isatty(1))
+ pager = "cat";
+ else {
+ if (!(pager = getenv("PAGER")) || (*pager == 0))
+ pager = _PATH_PAGER;
+ }
+ switch (pid = fork()) {
+ case 0: /* child */
+ if ((fd = open(_PATH_WUMPINFO, O_RDONLY)) == -1)
+ err(1, "open %s", _PATH_WUMPINFO);
+ if (dup2(fd, 0) == -1)
+ err(1, "dup2");
+ (void)execl("/bin/sh", "sh", "-c", pager, NULL);
+ err(1, "exec sh -c %s", pager);
+ case -1:
+ err(1, "fork");
+ default:
+ (void)waitpid(pid, &status, 0);
+ break;
+ }
}
+void
usage()
{
(void)fprintf(stderr,
/* messages */
+void
wump_kill()
{
(void)printf(
passed out from the stench!\n");
}
+void
kill_wump()
{
(void)printf(
mightiest adventurer at a single whiff!!\n");
}
+void
no_arrows()
{
(void)printf(
you, and with a mighty *ROAR* eats you alive!\n");
}
+void
shoot_self()
{
(void)printf(
(*CHOMP*)\n");
}
+void
jump(where)
int where;
{
a very curious, warm sensation and find yourself in room %d!!\n", where);
}
+void
pit_kill()
{
(void)printf(
you can at least find out if Jules Verne was right...\n");
}
+void
pit_survive()
{
(void)printf(