Home | History | Annotate | Line # | Download | only in huntd
driver.c revision 1.34
      1  1.34  dholland /*	$NetBSD: driver.c,v 1.34 2014/03/30 00:26:58 dholland Exp $	*/
      2   1.1       mrg /*
      3   1.9       wiz  * Copyright (c) 1983-2003, Regents of the University of California.
      4   1.9       wiz  * All rights reserved.
      5   1.9       wiz  *
      6   1.9       wiz  * Redistribution and use in source and binary forms, with or without
      7   1.9       wiz  * modification, are permitted provided that the following conditions are
      8   1.9       wiz  * met:
      9   1.9       wiz  *
     10   1.9       wiz  * + Redistributions of source code must retain the above copyright
     11   1.9       wiz  *   notice, this list of conditions and the following disclaimer.
     12   1.9       wiz  * + Redistributions in binary form must reproduce the above copyright
     13   1.9       wiz  *   notice, this list of conditions and the following disclaimer in the
     14   1.9       wiz  *   documentation and/or other materials provided with the distribution.
     15   1.9       wiz  * + Neither the name of the University of California, San Francisco nor
     16   1.9       wiz  *   the names of its contributors may be used to endorse or promote
     17   1.9       wiz  *   products derived from this software without specific prior written
     18   1.9       wiz  *   permission.
     19   1.9       wiz  *
     20   1.9       wiz  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
     21   1.9       wiz  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22   1.9       wiz  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
     23   1.9       wiz  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     24   1.9       wiz  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     25   1.9       wiz  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     26   1.9       wiz  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27   1.9       wiz  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28   1.9       wiz  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29   1.9       wiz  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     30   1.9       wiz  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31   1.1       mrg  */
     32   1.1       mrg 
     33   1.2     lukem #include <sys/cdefs.h>
     34   1.2     lukem #ifndef lint
     35  1.34  dholland __RCSID("$NetBSD: driver.c,v 1.34 2014/03/30 00:26:58 dholland Exp $");
     36   1.2     lukem #endif /* not lint */
     37   1.2     lukem 
     38  1.33  dholland #include <sys/types.h>
     39  1.33  dholland #include <sys/socket.h>
     40  1.17  dholland #include <sys/stat.h>
     41  1.17  dholland #include <sys/time.h>
     42  1.17  dholland #include <err.h>
     43  1.17  dholland #include <errno.h>
     44  1.17  dholland #include <signal.h>
     45  1.17  dholland #include <stdlib.h>
     46  1.17  dholland #include <unistd.h>
     47  1.31  dholland 
     48  1.31  dholland #include "hunt.h"
     49  1.31  dholland #include "pathnames.h"
     50  1.17  dholland 
     51  1.34  dholland /*
     52  1.34  dholland  * There are three listening sockets in this daemon:
     53  1.34  dholland  *    - a datagram socket that listens on a well known port
     54  1.34  dholland  *    - a stream socket for general player connections
     55  1.34  dholland  *    - a second stream socket that prints the stats/scores
     56  1.34  dholland  *
     57  1.34  dholland  * These are (now) named as follows:
     58  1.34  dholland  *    - contact (contactsock, contactaddr, etc.)
     59  1.34  dholland  *    - hunt (huntsock, huntaddr, etc.)
     60  1.34  dholland  *    - stat (statsock, stataddr, etc.)
     61  1.34  dholland  *
     62  1.34  dholland  * Until being cleaned up in 2014 the code used an assortment of
     63  1.34  dholland  * inconsistent and confusing names to refer to the pieces of these:
     64  1.34  dholland  *
     65  1.34  dholland  *    test_port -> contactaddr
     66  1.34  dholland  *    Test_port -> contactport
     67  1.34  dholland  *    Test_socket -> contactsock
     68  1.34  dholland  *
     69  1.34  dholland  *    sock_port -> huntport
     70  1.34  dholland  *    Socket -> huntsock
     71  1.34  dholland  *
     72  1.34  dholland  *    Daemon -> both stataddr and huntaddr
     73  1.34  dholland  *    stat_port -> statport
     74  1.34  dholland  *    status -> statsock
     75  1.34  dholland  *
     76  1.34  dholland  * It isn't clear to me what purpose contactsocket is supposed to
     77  1.34  dholland  * serve; maybe it was supposed to avoid asking inetd to support
     78  1.34  dholland  * tcp/wait sockets? Anyway, we can't really change the protocol at
     79  1.34  dholland  * this point. (To complicate matters, contactsocket doesn't exist if
     80  1.34  dholland  * using AF_UNIX sockets; you just connect to the game socket.)
     81  1.34  dholland  *
     82  1.34  dholland  * When using internet sockets:
     83  1.34  dholland  *    - the contact socket listens on INADDR_ANY using the game port
     84  1.34  dholland  *      (either specified or the default: CONTACT_PORT, currently
     85  1.34  dholland  *      spelled TEST_PORT)
     86  1.34  dholland  *    - the hunt socket listens on INADDR_ANY using whatever port
     87  1.34  dholland  *    - the stat socket listens on INADDR_ANY using whatever port
     88  1.34  dholland  *
     89  1.34  dholland  * When using AF_UNIX sockets:
     90  1.34  dholland  *    - the contact socket isn't used
     91  1.34  dholland  *    - the hunt socket listens on the game socket (either a specified path
     92  1.34  dholland  *      or /tmp/hunt)
     93  1.34  dholland  *    - the stat socket listens on its own socket (huntsocket's path +
     94  1.34  dholland  *      ".stats")
     95  1.34  dholland  */
     96  1.34  dholland 
     97  1.28  dholland #ifdef INTERNET
     98  1.34  dholland static struct sockaddr_in huntaddr;
     99  1.34  dholland static struct sockaddr_in stataddr;
    100  1.34  dholland static uint16_t contactport = TEST_PORT;
    101  1.34  dholland static uint16_t	huntport;		/* port # of tcp listen socket */
    102  1.34  dholland static uint16_t	statport;		/* port # of statistics tcp socket */
    103  1.34  dholland static int contactsock;			/* socket to answer datagrams */
    104  1.34  dholland #define STATSOCKADDR_SIZE	(sizeof statsockaddr)
    105  1.28  dholland #else
    106  1.34  dholland static struct sockaddr_un huntaddr;
    107  1.34  dholland static struct sockaddr_un stataddr;
    108  1.34  dholland static const char huntsockpath[] = PATH_HUNTSOCKET;
    109  1.34  dholland static const char statsockpath[] = PATH_STATSOCKET;
    110  1.34  dholland #define STATSOCKADDR_SIZE	(sizeof statsockaddr - 1)
    111  1.28  dholland #endif
    112  1.28  dholland 
    113  1.34  dholland int huntsock;				/* main socket */
    114  1.34  dholland static int statsock;			/* stat socket */
    115  1.34  dholland static socklen_t huntaddrlen;
    116  1.34  dholland static socklen_t stataddrlen;
    117  1.17  dholland 
    118  1.17  dholland #ifdef INTERNET
    119  1.24  dholland static bool inetd_spawned;		/* invoked via inetd */
    120  1.24  dholland static bool standard_port = true;	/* true if listening on standard port */
    121  1.17  dholland #endif
    122  1.17  dholland 
    123  1.29  dholland #ifdef VOLCANO
    124  1.29  dholland static int volcano = 0;			/* Explosion size */
    125  1.29  dholland #endif
    126  1.29  dholland 
    127  1.17  dholland static void clear_scores(void);
    128  1.24  dholland static bool havechar(PLAYER *, int);
    129  1.17  dholland static void init(void);
    130  1.17  dholland static void makeboots(void);
    131  1.17  dholland static void send_stats(void);
    132  1.24  dholland static void zap(PLAYER *, bool, int);
    133   1.2     lukem 
    134   1.1       mrg 
    135   1.1       mrg /*
    136   1.1       mrg  * main:
    137   1.1       mrg  *	The main program.
    138   1.1       mrg  */
    139   1.2     lukem int
    140  1.26  dholland main(int ac, char **av)
    141   1.1       mrg {
    142  1.17  dholland 	PLAYER *pp;
    143  1.17  dholland #ifdef INTERNET
    144  1.17  dholland 	u_short msg;
    145  1.22  christos 	short reply;
    146  1.17  dholland 	socklen_t namelen;
    147  1.17  dholland 	SOCKET test;
    148  1.17  dholland #endif
    149  1.24  dholland 	static bool first = true;
    150  1.24  dholland 	static bool server = false;
    151  1.17  dholland 	int c, i;
    152  1.17  dholland 	const int linger = 90 * 1000;
    153   1.1       mrg 
    154   1.2     lukem 	while ((c = getopt(ac, av, "sp:")) != -1) {
    155   1.1       mrg 		switch (c) {
    156   1.1       mrg 		  case 's':
    157  1.24  dholland 			server = true;
    158   1.1       mrg 			break;
    159  1.17  dholland #ifdef INTERNET
    160   1.1       mrg 		  case 'p':
    161  1.24  dholland 			standard_port = false;
    162  1.34  dholland 			contactport = atoi(optarg);
    163   1.1       mrg 			break;
    164  1.17  dholland #endif
    165   1.1       mrg 		  default:
    166   1.1       mrg erred:
    167   1.1       mrg 			fprintf(stderr, "Usage: %s [-s] [-p port]\n", av[0]);
    168   1.1       mrg 			exit(1);
    169   1.1       mrg 		}
    170   1.1       mrg 	}
    171   1.1       mrg 	if (optind < ac)
    172   1.1       mrg 		goto erred;
    173   1.1       mrg 
    174   1.1       mrg 	init();
    175   1.1       mrg 
    176   1.1       mrg 
    177   1.1       mrg again:
    178   1.1       mrg 	do {
    179   1.1       mrg 		errno = 0;
    180   1.8   mycroft 		while (poll(fdset, 3+MAXPL+MAXMON, INFTIM) < 0)
    181   1.1       mrg 		{
    182   1.1       mrg 			if (errno != EINTR)
    183  1.32  dholland 				complain(LOG_WARNING, "poll");
    184   1.1       mrg 			errno = 0;
    185   1.1       mrg 		}
    186  1.17  dholland #ifdef INTERNET
    187   1.8   mycroft 		if (fdset[2].revents & POLLIN) {
    188  1.34  dholland 			namelen = sizeof(test);
    189  1.34  dholland 			(void) recvfrom(contactsock, &msg, sizeof msg,
    190   1.1       mrg 				0, (struct sockaddr *) &test, &namelen);
    191   1.1       mrg 			switch (ntohs(msg)) {
    192   1.1       mrg 			  case C_MESSAGE:
    193   1.1       mrg 				if (Nplayer <= 0)
    194   1.1       mrg 					break;
    195   1.1       mrg 				reply = htons((u_short) Nplayer);
    196  1.34  dholland 				(void) sendto(contactsock, &reply,
    197   1.1       mrg 					sizeof reply, 0,
    198  1.34  dholland 					(struct sockaddr *) &test, sizeof(test));
    199   1.1       mrg 				break;
    200   1.1       mrg 			  case C_SCORES:
    201  1.34  dholland 				reply = htons(statport);
    202  1.34  dholland 				(void) sendto(contactsock, &reply,
    203   1.1       mrg 					sizeof reply, 0,
    204  1.34  dholland 					(struct sockaddr *) &test, sizeof(test));
    205   1.1       mrg 				break;
    206   1.1       mrg 			  case C_PLAYER:
    207   1.1       mrg 			  case C_MONITOR:
    208   1.1       mrg 				if (msg == C_MONITOR && Nplayer <= 0)
    209   1.1       mrg 					break;
    210  1.34  dholland 				reply = htons(huntport);
    211  1.34  dholland 				(void) sendto(contactsock, &reply,
    212   1.1       mrg 					sizeof reply, 0,
    213  1.34  dholland 					(struct sockaddr *) &test, sizeof(test));
    214   1.1       mrg 				break;
    215   1.1       mrg 			}
    216   1.1       mrg 		}
    217  1.17  dholland #endif
    218   1.8   mycroft 		{
    219   1.8   mycroft 			for (pp = Player, i = 0; pp < End_player; pp++, i++)
    220   1.8   mycroft 				if (havechar(pp, i + 3)) {
    221   1.1       mrg 					execute(pp);
    222   1.1       mrg 					pp->p_nexec++;
    223   1.1       mrg 				}
    224  1.17  dholland #ifdef MONITOR
    225   1.8   mycroft 			for (pp = Monitor, i = 0; pp < End_monitor; pp++, i++)
    226   1.8   mycroft 				if (havechar(pp, i + MAXPL + 3)) {
    227   1.1       mrg 					mon_execute(pp);
    228   1.1       mrg 					pp->p_nexec++;
    229   1.1       mrg 				}
    230  1.17  dholland #endif
    231   1.1       mrg 			moveshots();
    232   1.8   mycroft 			for (pp = Player, i = 0; pp < End_player; )
    233   1.1       mrg 				if (pp->p_death[0] != '\0')
    234  1.24  dholland 					zap(pp, true, i + 3);
    235   1.1       mrg 				else
    236   1.8   mycroft 					pp++, i++;
    237  1.17  dholland #ifdef MONITOR
    238   1.8   mycroft 			for (pp = Monitor, i = 0; pp < End_monitor; )
    239   1.1       mrg 				if (pp->p_death[0] != '\0')
    240  1.24  dholland 					zap(pp, false, i + MAXPL + 3);
    241   1.1       mrg 				else
    242   1.8   mycroft 					pp++, i++;
    243  1.17  dholland #endif
    244   1.1       mrg 		}
    245   1.8   mycroft 		if (fdset[0].revents & POLLIN)
    246   1.1       mrg 			if (answer()) {
    247  1.27  dholland 				if (first) {
    248  1.27  dholland 					/* announce start of game? */
    249  1.27  dholland 				}
    250  1.24  dholland 				first = false;
    251   1.1       mrg 			}
    252   1.8   mycroft 		if (fdset[1].revents & POLLIN)
    253   1.1       mrg 			send_stats();
    254   1.8   mycroft 		for (pp = Player, i = 0; pp < End_player; pp++, i++) {
    255   1.8   mycroft 			if (fdset[i + 3].revents & POLLIN)
    256   1.1       mrg 				sendcom(pp, READY, pp->p_nexec);
    257   1.1       mrg 			pp->p_nexec = 0;
    258   1.1       mrg 			(void) fflush(pp->p_output);
    259   1.1       mrg 		}
    260  1.17  dholland #ifdef MONITOR
    261   1.8   mycroft 		for (pp = Monitor, i = 0; pp < End_monitor; pp++, i++) {
    262   1.8   mycroft 			if (fdset[i + MAXPL + 3].revents & POLLIN)
    263   1.1       mrg 				sendcom(pp, READY, pp->p_nexec);
    264   1.1       mrg 			pp->p_nexec = 0;
    265   1.1       mrg 			(void) fflush(pp->p_output);
    266   1.1       mrg 		}
    267  1.17  dholland #endif
    268   1.1       mrg 	} while (Nplayer > 0);
    269   1.1       mrg 
    270   1.8   mycroft 	if (poll(fdset, 3+MAXPL+MAXMON, linger) > 0) {
    271   1.1       mrg 		goto again;
    272   1.1       mrg 	}
    273   1.1       mrg 	if (server) {
    274   1.1       mrg 		clear_scores();
    275   1.1       mrg 		makemaze();
    276   1.1       mrg 		clearwalls();
    277  1.17  dholland #ifdef BOOTS
    278   1.1       mrg 		makeboots();
    279  1.17  dholland #endif
    280  1.24  dholland 		first = true;
    281   1.1       mrg 		goto again;
    282   1.1       mrg 	}
    283   1.1       mrg 
    284  1.17  dholland #ifdef MONITOR
    285   1.8   mycroft 	for (pp = Monitor, i = 0; pp < End_monitor; i++)
    286  1.24  dholland 		zap(pp, false, i + MAXPL + 3);
    287  1.17  dholland #endif
    288   1.1       mrg 	cleanup(0);
    289   1.2     lukem 	/* NOTREACHED */
    290   1.2     lukem 	return(0);
    291   1.1       mrg }
    292   1.1       mrg 
    293   1.1       mrg /*
    294   1.1       mrg  * init:
    295   1.1       mrg  *	Initialize the global parameters.
    296   1.1       mrg  */
    297   1.2     lukem static void
    298  1.16  dholland init(void)
    299   1.1       mrg {
    300  1.17  dholland 	int i;
    301  1.17  dholland #ifdef INTERNET
    302  1.34  dholland 	struct sockaddr_in contactaddr;
    303  1.17  dholland 	int msg;
    304  1.17  dholland 	socklen_t len;
    305  1.17  dholland #endif
    306   1.1       mrg 
    307  1.17  dholland #ifndef DEBUG
    308  1.17  dholland #ifdef TIOCNOTTY
    309   1.1       mrg 	(void) ioctl(fileno(stdout), TIOCNOTTY, NULL);
    310  1.17  dholland #endif
    311   1.1       mrg 	(void) setpgrp(getpid(), getpid());
    312   1.1       mrg 	(void) signal(SIGHUP, SIG_IGN);
    313   1.1       mrg 	(void) signal(SIGINT, SIG_IGN);
    314   1.1       mrg 	(void) signal(SIGQUIT, SIG_IGN);
    315   1.1       mrg 	(void) signal(SIGTERM, cleanup);
    316  1.17  dholland #endif
    317   1.1       mrg 
    318   1.4       mrg 	(void) chdir("/var/tmp");	/* just in case it core dumps */
    319   1.1       mrg 	(void) umask(0);		/* No privacy at all! */
    320   1.1       mrg 	(void) signal(SIGPIPE, SIG_IGN);
    321   1.1       mrg 
    322  1.17  dholland #ifdef LOG
    323   1.7     lukem 	openlog("huntd", LOG_PID, LOG_DAEMON);
    324  1.17  dholland #endif
    325   1.1       mrg 
    326   1.1       mrg 	/*
    327   1.1       mrg 	 * Initialize statistics socket
    328   1.1       mrg 	 */
    329  1.17  dholland #ifdef INTERNET
    330  1.34  dholland 	stataddr.sin_family = AF_INET;
    331  1.34  dholland 	stataddr.sin_addr.s_addr = INADDR_ANY;
    332  1.34  dholland 	stataddr.sin_port = 0;
    333  1.34  dholland 	stataddrlen = sizeof(stataddr);
    334  1.17  dholland #else
    335  1.34  dholland 	stataddr.sun_family = AF_UNIX;
    336  1.34  dholland 	(void) strcpy(stataddr.sun_path, statsockpath);
    337  1.34  dholland 	stataddrlen = SUN_LEN(&stataddr);
    338  1.17  dholland #endif
    339   1.1       mrg 
    340  1.34  dholland 	statsock = socket(SOCK_FAMILY, SOCK_STREAM, 0);
    341  1.34  dholland 	if (bind(statsock, (struct sockaddr *) &stataddr, stataddrlen) < 0) {
    342   1.1       mrg 		if (errno == EADDRINUSE)
    343   1.1       mrg 			exit(0);
    344   1.1       mrg 		else {
    345  1.32  dholland 			complain(LOG_ERR, "bind");
    346   1.1       mrg 			cleanup(1);
    347   1.1       mrg 		}
    348   1.1       mrg 	}
    349  1.34  dholland 	(void) listen(statsock, 5);
    350   1.1       mrg 
    351  1.17  dholland #ifdef INTERNET
    352  1.34  dholland 	len = sizeof(stataddr);
    353  1.34  dholland 	if (getsockname(statsock, (struct sockaddr *) &stataddr, &len) < 0)  {
    354  1.32  dholland 		complain(LOG_ERR, "getsockname");
    355   1.1       mrg 		exit(1);
    356   1.1       mrg 	}
    357  1.34  dholland 	statport = ntohs(stataddr.sin_port);
    358  1.17  dholland #endif
    359   1.1       mrg 
    360   1.1       mrg 	/*
    361   1.1       mrg 	 * Initialize main socket
    362   1.1       mrg 	 */
    363  1.17  dholland #ifdef INTERNET
    364  1.34  dholland 	huntaddr.sin_family = AF_INET;
    365  1.34  dholland 	huntaddr.sin_addr.s_addr = INADDR_ANY;
    366  1.34  dholland 	huntaddr.sin_port = 0;
    367  1.34  dholland 	huntaddrlen = sizeof(huntaddr);
    368  1.17  dholland #else
    369  1.34  dholland 	huntaddr.sun_family = AF_UNIX;
    370  1.34  dholland 	(void) strcpy(huntaddr.sun_path, huntsockpath);
    371  1.34  dholland 	huntaddrlen = SUN_LEN(&huntaddr);
    372  1.17  dholland #endif
    373   1.1       mrg 
    374  1.34  dholland 	huntsock = socket(SOCK_FAMILY, SOCK_STREAM, 0);
    375  1.17  dholland #if defined(INTERNET)
    376   1.1       mrg 	msg = 1;
    377  1.34  dholland 	if (setsockopt(huntsock, SOL_SOCKET, SO_USELOOPBACK, &msg, sizeof msg)<0)
    378  1.32  dholland 		complain(LOG_WARNING, "setsockopt loopback");
    379  1.17  dholland #endif
    380  1.34  dholland 	if (bind(huntsock, (struct sockaddr *) &huntaddr, huntaddrlen) < 0) {
    381   1.1       mrg 		if (errno == EADDRINUSE)
    382   1.1       mrg 			exit(0);
    383   1.1       mrg 		else {
    384  1.32  dholland 			complain(LOG_ERR, "bind");
    385   1.1       mrg 			cleanup(1);
    386   1.1       mrg 		}
    387   1.1       mrg 	}
    388  1.34  dholland 	(void) listen(huntsock, 5);
    389   1.1       mrg 
    390  1.17  dholland #ifdef INTERNET
    391  1.34  dholland 	len = sizeof(huntaddr);
    392  1.34  dholland 	if (getsockname(huntsock, (struct sockaddr *) &huntaddr, &len) < 0)  {
    393  1.32  dholland 		complain(LOG_ERR, "getsockname");
    394   1.1       mrg 		exit(1);
    395   1.1       mrg 	}
    396  1.34  dholland 	huntport = ntohs(huntaddr.sin_port);
    397  1.17  dholland #endif
    398   1.1       mrg 
    399   1.1       mrg 	/*
    400  1.12      elad 	 * Initialize minimal poll mask
    401   1.1       mrg 	 */
    402  1.34  dholland 	fdset[0].fd = huntsock;
    403   1.8   mycroft 	fdset[0].events = POLLIN;
    404  1.34  dholland 	fdset[1].fd = statsock;
    405   1.8   mycroft 	fdset[1].events = POLLIN;
    406   1.1       mrg 
    407  1.17  dholland #ifdef INTERNET
    408  1.34  dholland 	len = sizeof(contactaddr);
    409  1.34  dholland 	if (getsockname(STDIN_FILENO, (struct sockaddr *) &contactaddr, &len) >= 0
    410  1.34  dholland 	&& contactaddr.sin_family == AF_INET) {
    411  1.24  dholland 		inetd_spawned = true;
    412  1.34  dholland 		contactsock = STDIN_FILENO;
    413  1.34  dholland 		if (ntohs(contactaddr.sin_port) != contactport) {
    414  1.24  dholland 			standard_port = false;
    415  1.34  dholland 			contactport = ntohs(contactaddr.sin_port);
    416   1.1       mrg 		}
    417   1.1       mrg 	} else {
    418  1.34  dholland 		contactaddr = huntaddr;
    419  1.34  dholland 		contactaddr.sin_port = htons(contactport);
    420   1.1       mrg 
    421  1.34  dholland 		contactsock = socket(AF_INET, SOCK_DGRAM, 0);
    422  1.34  dholland 		if (bind(contactsock, (struct sockaddr *) &contactaddr,
    423  1.34  dholland 		    sizeof(contactaddr)) < 0) {
    424  1.32  dholland 			complain(LOG_ERR, "bind");
    425   1.1       mrg 			exit(1);
    426   1.1       mrg 		}
    427  1.34  dholland 		(void) listen(contactsock, 5);
    428   1.1       mrg 	}
    429   1.1       mrg 
    430  1.34  dholland 	fdset[2].fd = contactsock;
    431   1.8   mycroft 	fdset[2].events = POLLIN;
    432  1.17  dholland #else
    433   1.8   mycroft 	fdset[2].fd = -1;
    434  1.17  dholland #endif
    435   1.1       mrg 
    436  1.23  dholland 	srandom(time(NULL));
    437   1.1       mrg 	makemaze();
    438  1.17  dholland #ifdef BOOTS
    439   1.1       mrg 	makeboots();
    440  1.17  dholland #endif
    441   1.1       mrg 
    442   1.1       mrg 	for (i = 0; i < NASCII; i++)
    443  1.24  dholland 		See_over[i] = true;
    444  1.24  dholland 	See_over[DOOR] = false;
    445  1.24  dholland 	See_over[WALL1] = false;
    446  1.24  dholland 	See_over[WALL2] = false;
    447  1.24  dholland 	See_over[WALL3] = false;
    448  1.17  dholland #ifdef REFLECT
    449  1.24  dholland 	See_over[WALL4] = false;
    450  1.24  dholland 	See_over[WALL5] = false;
    451  1.17  dholland #endif
    452   1.1       mrg 
    453   1.1       mrg }
    454   1.1       mrg 
    455  1.17  dholland #ifdef BOOTS
    456   1.1       mrg /*
    457   1.1       mrg  * makeboots:
    458   1.1       mrg  *	Put the boots in the maze
    459   1.1       mrg  */
    460   1.2     lukem static void
    461  1.16  dholland makeboots(void)
    462   1.1       mrg {
    463  1.17  dholland 	int x, y;
    464  1.17  dholland 	PLAYER *pp;
    465   1.1       mrg 
    466   1.1       mrg 	do {
    467   1.1       mrg 		x = rand_num(WIDTH - 1) + 1;
    468   1.1       mrg 		y = rand_num(HEIGHT - 1) + 1;
    469   1.1       mrg 	} while (Maze[y][x] != SPACE);
    470   1.1       mrg 	Maze[y][x] = BOOT_PAIR;
    471   1.1       mrg 	for (pp = Boot; pp < &Boot[NBOOTS]; pp++)
    472   1.1       mrg 		pp->p_flying = -1;
    473   1.1       mrg }
    474  1.17  dholland #endif
    475   1.1       mrg 
    476   1.1       mrg 
    477   1.1       mrg /*
    478   1.1       mrg  * checkdam:
    479   1.1       mrg  *	Check the damage to the given player, and see if s/he is killed
    480   1.1       mrg  */
    481   1.2     lukem void
    482  1.16  dholland checkdam(PLAYER *ouch, PLAYER *gotcha, IDENT *credit, int amt,
    483  1.16  dholland 	 char this_shot_type)
    484   1.1       mrg {
    485  1.17  dholland 	const char *cp;
    486   1.1       mrg 
    487   1.1       mrg 	if (ouch->p_death[0] != '\0')
    488   1.1       mrg 		return;
    489  1.17  dholland #ifdef BOOTS
    490  1.13  dholland 	if (this_shot_type == SLIME)
    491   1.1       mrg 		switch (ouch->p_nboots) {
    492   1.1       mrg 		  default:
    493   1.1       mrg 			break;
    494   1.1       mrg 		  case 1:
    495   1.1       mrg 			amt = (amt + 1) / 2;
    496   1.1       mrg 			break;
    497   1.1       mrg 		  case 2:
    498   1.1       mrg 			if (gotcha != NULL)
    499   1.1       mrg 				message(gotcha, "He has boots on!");
    500   1.1       mrg 			return;
    501   1.1       mrg 		}
    502  1.17  dholland #endif
    503   1.1       mrg 	ouch->p_damage += amt;
    504   1.1       mrg 	if (ouch->p_damage <= ouch->p_damcap) {
    505  1.14  dholland 		(void) snprintf(Buf, sizeof(Buf), "%2d", ouch->p_damage);
    506   1.1       mrg 		cgoto(ouch, STAT_DAM_ROW, STAT_VALUE_COL);
    507   1.1       mrg 		outstr(ouch, Buf, 2);
    508   1.1       mrg 		return;
    509   1.1       mrg 	}
    510   1.1       mrg 
    511   1.1       mrg 	/* Someone DIED */
    512  1.13  dholland 	switch (this_shot_type) {
    513   1.1       mrg 	  default:
    514   1.1       mrg 		cp = "Killed";
    515   1.1       mrg 		break;
    516  1.17  dholland #ifdef FLY
    517   1.1       mrg 	  case FALL:
    518   1.1       mrg 		cp = "Killed on impact";
    519   1.1       mrg 		break;
    520  1.17  dholland #endif
    521   1.1       mrg 	  case KNIFE:
    522   1.1       mrg 		cp = "Stabbed to death";
    523   1.1       mrg 		ouch->p_ammo = 0;		/* No exploding */
    524   1.1       mrg 		break;
    525   1.1       mrg 	  case SHOT:
    526   1.1       mrg 		cp = "Shot to death";
    527   1.1       mrg 		break;
    528   1.1       mrg 	  case GRENADE:
    529   1.1       mrg 	  case SATCHEL:
    530   1.1       mrg 	  case BOMB:
    531   1.1       mrg 		cp = "Bombed";
    532   1.1       mrg 		break;
    533   1.1       mrg 	  case MINE:
    534   1.1       mrg 	  case GMINE:
    535   1.1       mrg 		cp = "Blown apart";
    536   1.1       mrg 		break;
    537  1.17  dholland #ifdef	OOZE
    538   1.1       mrg 	  case SLIME:
    539   1.1       mrg 		cp = "Slimed";
    540   1.1       mrg 		if (credit != NULL)
    541   1.1       mrg 			credit->i_slime++;
    542   1.1       mrg 		break;
    543  1.17  dholland #endif
    544  1.17  dholland #ifdef	VOLCANO
    545   1.1       mrg 	  case LAVA:
    546   1.1       mrg 		cp = "Baked";
    547   1.1       mrg 		break;
    548  1.17  dholland #endif
    549  1.17  dholland #ifdef DRONE
    550   1.1       mrg 	  case DSHOT:
    551   1.1       mrg 		cp = "Eliminated";
    552   1.1       mrg 		break;
    553  1.17  dholland #endif
    554   1.1       mrg 	}
    555   1.1       mrg 	if (credit == NULL) {
    556  1.14  dholland 		(void) snprintf(ouch->p_death, sizeof(ouch->p_death),
    557  1.14  dholland 			"| %s by %s |", cp,
    558  1.13  dholland 			(this_shot_type == MINE || this_shot_type == GMINE) ?
    559   1.1       mrg 			"a mine" : "act of God");
    560   1.1       mrg 		return;
    561   1.1       mrg 	}
    562   1.1       mrg 
    563  1.14  dholland 	(void) snprintf(ouch->p_death, sizeof(ouch->p_death),
    564  1.14  dholland 		"| %s by %s |", cp, credit->i_name);
    565   1.1       mrg 
    566   1.1       mrg 	if (ouch == gotcha) {		/* No use killing yourself */
    567   1.1       mrg 		credit->i_kills--;
    568   1.1       mrg 		credit->i_bkills++;
    569   1.1       mrg 	}
    570   1.1       mrg 	else if (ouch->p_ident->i_team == ' '
    571   1.1       mrg 	|| ouch->p_ident->i_team != credit->i_team) {
    572   1.1       mrg 		credit->i_kills++;
    573   1.1       mrg 		credit->i_gkills++;
    574   1.1       mrg 	}
    575   1.1       mrg 	else {
    576   1.1       mrg 		credit->i_kills--;
    577   1.1       mrg 		credit->i_bkills++;
    578   1.1       mrg 	}
    579   1.1       mrg 	credit->i_score = credit->i_kills / (double) credit->i_entries;
    580   1.1       mrg 	ouch->p_ident->i_deaths++;
    581   1.1       mrg 	if (ouch->p_nchar == 0)
    582   1.1       mrg 		ouch->p_ident->i_stillb++;
    583   1.1       mrg 	if (gotcha == NULL)
    584   1.1       mrg 		return;
    585   1.1       mrg 	gotcha->p_damcap += STABDAM;
    586   1.1       mrg 	gotcha->p_damage -= STABDAM;
    587   1.1       mrg 	if (gotcha->p_damage < 0)
    588   1.1       mrg 		gotcha->p_damage = 0;
    589  1.14  dholland 	(void) snprintf(Buf, sizeof(Buf), "%2d/%2d", gotcha->p_damage,
    590  1.14  dholland 			gotcha->p_damcap);
    591   1.1       mrg 	cgoto(gotcha, STAT_DAM_ROW, STAT_VALUE_COL);
    592   1.1       mrg 	outstr(gotcha, Buf, 5);
    593  1.14  dholland 	(void) snprintf(Buf, sizeof(Buf), "%3d",
    594  1.14  dholland 			(gotcha->p_damcap - MAXDAM) / 2);
    595   1.1       mrg 	cgoto(gotcha, STAT_KILL_ROW, STAT_VALUE_COL);
    596   1.1       mrg 	outstr(gotcha, Buf, 3);
    597  1.14  dholland 	(void) snprintf(Buf, sizeof(Buf), "%5.2f", gotcha->p_ident->i_score);
    598   1.1       mrg 	for (ouch = Player; ouch < End_player; ouch++) {
    599   1.1       mrg 		cgoto(ouch, STAT_PLAY_ROW + 1 + (gotcha - Player),
    600   1.1       mrg 			STAT_NAME_COL);
    601   1.1       mrg 		outstr(ouch, Buf, 5);
    602   1.1       mrg 	}
    603  1.17  dholland #ifdef MONITOR
    604   1.1       mrg 	for (ouch = Monitor; ouch < End_monitor; ouch++) {
    605   1.1       mrg 		cgoto(ouch, STAT_PLAY_ROW + 1 + (gotcha - Player),
    606   1.1       mrg 			STAT_NAME_COL);
    607   1.1       mrg 		outstr(ouch, Buf, 5);
    608   1.1       mrg 	}
    609  1.17  dholland #endif
    610   1.1       mrg }
    611   1.1       mrg 
    612   1.1       mrg /*
    613   1.1       mrg  * zap:
    614   1.1       mrg  *	Kill off a player and take him out of the game.
    615   1.1       mrg  */
    616   1.2     lukem static void
    617  1.24  dholland zap(PLAYER *pp, bool was_player, int i)
    618   1.1       mrg {
    619  1.17  dholland 	int n, len;
    620  1.17  dholland 	BULLET *bp;
    621  1.17  dholland 	PLAYER *np;
    622  1.17  dholland 	int x, y;
    623   1.1       mrg 
    624   1.1       mrg 	if (was_player) {
    625   1.1       mrg 		if (pp->p_undershot)
    626   1.1       mrg 			fixshots(pp->p_y, pp->p_x, pp->p_over);
    627  1.24  dholland 		drawplayer(pp, false);
    628   1.1       mrg 		Nplayer--;
    629   1.1       mrg 	}
    630   1.1       mrg 
    631   1.1       mrg 	len = strlen(pp->p_death);	/* Display the cause of death */
    632   1.1       mrg 	x = (WIDTH - len) / 2;
    633   1.1       mrg 	cgoto(pp, HEIGHT / 2, x);
    634   1.1       mrg 	outstr(pp, pp->p_death, len);
    635   1.8   mycroft 	for (n = 1; n < len; n++)
    636   1.8   mycroft 		pp->p_death[n] = '-';
    637   1.1       mrg 	pp->p_death[0] = '+';
    638   1.1       mrg 	pp->p_death[len - 1] = '+';
    639   1.1       mrg 	cgoto(pp, HEIGHT / 2 - 1, x);
    640   1.1       mrg 	outstr(pp, pp->p_death, len);
    641   1.1       mrg 	cgoto(pp, HEIGHT / 2 + 1, x);
    642   1.1       mrg 	outstr(pp, pp->p_death, len);
    643   1.1       mrg 	cgoto(pp, HEIGHT, 0);
    644   1.1       mrg 
    645  1.17  dholland #ifdef MONITOR
    646   1.1       mrg 	if (was_player) {
    647  1.17  dholland #endif
    648   1.1       mrg 		for (bp = Bullets; bp != NULL; bp = bp->b_next) {
    649   1.1       mrg 			if (bp->b_owner == pp)
    650   1.1       mrg 				bp->b_owner = NULL;
    651   1.1       mrg 			if (bp->b_x == pp->p_x && bp->b_y == pp->p_y)
    652   1.1       mrg 				bp->b_over = SPACE;
    653   1.1       mrg 		}
    654   1.1       mrg 
    655   1.8   mycroft 		n = rand_num(pp->p_ammo);
    656   1.1       mrg 		x = rand_num(pp->p_ammo);
    657   1.8   mycroft 		if (x > n)
    658   1.8   mycroft 			n = x;
    659   1.1       mrg 		if (pp->p_ammo == 0)
    660   1.1       mrg 			x = 0;
    661   1.8   mycroft 		else if (n == pp->p_ammo - 1) {
    662   1.1       mrg 			x = pp->p_ammo;
    663   1.1       mrg 			len = SLIME;
    664   1.1       mrg 		}
    665   1.1       mrg 		else {
    666   1.1       mrg 			for (x = MAXBOMB - 1; x > 0; x--)
    667   1.8   mycroft 				if (n >= shot_req[x])
    668   1.1       mrg 					break;
    669   1.1       mrg 			for (y = MAXSLIME - 1; y > 0; y--)
    670   1.8   mycroft 				if (n >= slime_req[y])
    671   1.1       mrg 					break;
    672   1.1       mrg 			if (y >= 0 && slime_req[y] > shot_req[x]) {
    673   1.1       mrg 				x = slime_req[y];
    674   1.1       mrg 				len = SLIME;
    675   1.1       mrg 			}
    676   1.1       mrg 			else if (x != 0) {
    677   1.1       mrg 				len = shot_type[x];
    678   1.1       mrg 				x = shot_req[x];
    679   1.1       mrg 			}
    680   1.1       mrg 		}
    681   1.1       mrg 		if (x > 0) {
    682   1.1       mrg 			(void) add_shot(len, pp->p_y, pp->p_x, pp->p_face, x,
    683  1.24  dholland 				NULL, true, SPACE);
    684  1.14  dholland 			(void) snprintf(Buf, sizeof(Buf), "%s detonated.",
    685   1.1       mrg 				pp->p_ident->i_name);
    686   1.1       mrg 			for (np = Player; np < End_player; np++)
    687   1.1       mrg 				message(np, Buf);
    688  1.17  dholland #ifdef MONITOR
    689   1.1       mrg 			for (np = Monitor; np < End_monitor; np++)
    690   1.1       mrg 				message(np, Buf);
    691  1.17  dholland #endif
    692  1.17  dholland #ifdef BOOTS
    693   1.1       mrg 			while (pp->p_nboots-- > 0) {
    694   1.1       mrg 				for (np = Boot; np < &Boot[NBOOTS]; np++)
    695   1.1       mrg 					if (np->p_flying < 0)
    696   1.1       mrg 						break;
    697   1.1       mrg 				if (np >= &Boot[NBOOTS])
    698   1.2     lukem 					err(1, "Too many boots");
    699  1.24  dholland 				np->p_undershot = false;
    700   1.1       mrg 				np->p_x = pp->p_x;
    701   1.1       mrg 				np->p_y = pp->p_y;
    702   1.1       mrg 				np->p_flying = rand_num(20);
    703   1.1       mrg 				np->p_flyx = 2 * rand_num(6) - 5;
    704   1.1       mrg 				np->p_flyy = 2 * rand_num(6) - 5;
    705   1.1       mrg 				np->p_over = SPACE;
    706   1.1       mrg 				np->p_face = BOOT;
    707   1.1       mrg 				showexpl(np->p_y, np->p_x, BOOT);
    708   1.1       mrg 			}
    709  1.17  dholland #endif
    710   1.1       mrg 		}
    711  1.17  dholland #ifdef BOOTS
    712   1.1       mrg 		else if (pp->p_nboots > 0) {
    713   1.1       mrg 			if (pp->p_nboots == 2)
    714   1.1       mrg 				Maze[pp->p_y][pp->p_x] = BOOT_PAIR;
    715   1.1       mrg 			else
    716   1.1       mrg 				Maze[pp->p_y][pp->p_x] = BOOT;
    717   1.1       mrg 			if (pp->p_undershot)
    718   1.1       mrg 				fixshots(pp->p_y, pp->p_x,
    719   1.1       mrg 					Maze[pp->p_y][pp->p_x]);
    720   1.1       mrg 		}
    721  1.17  dholland #endif
    722   1.1       mrg 
    723  1.17  dholland #ifdef VOLCANO
    724   1.1       mrg 		volcano += pp->p_ammo - x;
    725   1.1       mrg 		if (rand_num(100) < volcano / 50) {
    726   1.1       mrg 			do {
    727   1.1       mrg 				x = rand_num(WIDTH / 2) + WIDTH / 4;
    728   1.1       mrg 				y = rand_num(HEIGHT / 2) + HEIGHT / 4;
    729   1.1       mrg 			} while (Maze[y][x] != SPACE);
    730   1.1       mrg 			(void) add_shot(LAVA, y, x, LEFTS, volcano,
    731  1.24  dholland 				NULL, true, SPACE);
    732   1.1       mrg 			for (np = Player; np < End_player; np++)
    733   1.1       mrg 				message(np, "Volcano eruption.");
    734   1.1       mrg 			volcano = 0;
    735   1.1       mrg 		}
    736  1.17  dholland #endif
    737   1.1       mrg 
    738  1.17  dholland #ifdef DRONE
    739   1.1       mrg 		if (rand_num(100) < 2) {
    740   1.1       mrg 			do {
    741   1.1       mrg 				x = rand_num(WIDTH / 2) + WIDTH / 4;
    742   1.1       mrg 				y = rand_num(HEIGHT / 2) + HEIGHT / 4;
    743   1.1       mrg 			} while (Maze[y][x] != SPACE);
    744   1.1       mrg 			add_shot(DSHOT, y, x, rand_dir(),
    745   1.1       mrg 				shot_req[MINDSHOT +
    746   1.1       mrg 				rand_num(MAXBOMB - MINDSHOT)],
    747  1.24  dholland 				NULL, false, SPACE);
    748   1.1       mrg 		}
    749  1.17  dholland #endif
    750   1.1       mrg 
    751   1.1       mrg 		sendcom(pp, ENDWIN);
    752   1.1       mrg 		(void) putc(' ', pp->p_output);
    753   1.1       mrg 		(void) fclose(pp->p_output);
    754   1.1       mrg 
    755   1.1       mrg 		End_player--;
    756   1.1       mrg 		if (pp != End_player) {
    757   1.1       mrg 			memcpy(pp, End_player, sizeof (PLAYER));
    758   1.8   mycroft 			fdset[i] = fdset[End_player - Player + 3];
    759   1.8   mycroft 			fdset[End_player - Player + 3].fd = -1;
    760  1.14  dholland 			(void) snprintf(Buf, sizeof(Buf), "%5.2f%c%-10.10s %c",
    761   1.1       mrg 				pp->p_ident->i_score, stat_char(pp),
    762   1.1       mrg 				pp->p_ident->i_name, pp->p_ident->i_team);
    763   1.8   mycroft 			n = STAT_PLAY_ROW + 1 + (pp - Player);
    764   1.1       mrg 			for (np = Player; np < End_player; np++) {
    765   1.8   mycroft 				cgoto(np, n, STAT_NAME_COL);
    766   1.1       mrg 				outstr(np, Buf, STAT_NAME_LEN);
    767   1.1       mrg 			}
    768  1.17  dholland #ifdef MONITOR
    769   1.1       mrg 			for (np = Monitor; np < End_monitor; np++) {
    770   1.8   mycroft 				cgoto(np, n, STAT_NAME_COL);
    771   1.1       mrg 				outstr(np, Buf, STAT_NAME_LEN);
    772   1.1       mrg 			}
    773  1.17  dholland #endif
    774   1.8   mycroft 		} else
    775   1.8   mycroft 			fdset[i].fd = -1;
    776   1.1       mrg 
    777   1.1       mrg 		/* Erase the last player */
    778   1.8   mycroft 		n = STAT_PLAY_ROW + 1 + Nplayer;
    779   1.1       mrg 		for (np = Player; np < End_player; np++) {
    780   1.8   mycroft 			cgoto(np, n, STAT_NAME_COL);
    781   1.1       mrg 			ce(np);
    782   1.1       mrg 		}
    783  1.17  dholland #ifdef MONITOR
    784   1.1       mrg 		for (np = Monitor; np < End_monitor; np++) {
    785   1.8   mycroft 			cgoto(np, n, STAT_NAME_COL);
    786   1.1       mrg 			ce(np);
    787   1.1       mrg 		}
    788   1.1       mrg 	}
    789   1.1       mrg 	else {
    790   1.1       mrg 		sendcom(pp, ENDWIN);
    791   1.1       mrg 		(void) putc(LAST_PLAYER, pp->p_output);
    792   1.1       mrg 		(void) fclose(pp->p_output);
    793   1.1       mrg 
    794   1.1       mrg 		End_monitor--;
    795   1.1       mrg 		if (pp != End_monitor) {
    796   1.1       mrg 			memcpy(pp, End_monitor, sizeof (PLAYER));
    797   1.8   mycroft 			fdset[i] = fdset[End_monitor - Monitor + MAXPL + 3];
    798   1.8   mycroft 			fdset[End_monitor - Monitor + MAXPL + 3].fd = -1;
    799  1.14  dholland 			(void) snprintf(Buf, sizeof(Buf), "%5.5s %-10.10s %c",
    800  1.14  dholland 				" ",
    801   1.1       mrg 				pp->p_ident->i_name, pp->p_ident->i_team);
    802   1.8   mycroft 			n = STAT_MON_ROW + 1 + (pp - Player);
    803   1.1       mrg 			for (np = Player; np < End_player; np++) {
    804   1.8   mycroft 				cgoto(np, n, STAT_NAME_COL);
    805   1.1       mrg 				outstr(np, Buf, STAT_NAME_LEN);
    806   1.1       mrg 			}
    807   1.1       mrg 			for (np = Monitor; np < End_monitor; np++) {
    808   1.8   mycroft 				cgoto(np, n, STAT_NAME_COL);
    809   1.1       mrg 				outstr(np, Buf, STAT_NAME_LEN);
    810   1.1       mrg 			}
    811   1.8   mycroft 		} else
    812   1.8   mycroft 			fdset[i].fd = -1;
    813   1.1       mrg 
    814   1.1       mrg 		/* Erase the last monitor */
    815   1.8   mycroft 		n = STAT_MON_ROW + 1 + (End_monitor - Monitor);
    816   1.1       mrg 		for (np = Player; np < End_player; np++) {
    817   1.8   mycroft 			cgoto(np, n, STAT_NAME_COL);
    818   1.1       mrg 			ce(np);
    819   1.1       mrg 		}
    820   1.1       mrg 		for (np = Monitor; np < End_monitor; np++) {
    821   1.8   mycroft 			cgoto(np, n, STAT_NAME_COL);
    822   1.1       mrg 			ce(np);
    823   1.1       mrg 		}
    824   1.1       mrg 	}
    825  1.17  dholland #endif
    826   1.1       mrg }
    827   1.1       mrg 
    828   1.1       mrg /*
    829   1.1       mrg  * rand_num:
    830   1.1       mrg  *	Return a random number in a given range.
    831   1.1       mrg  */
    832   1.2     lukem int
    833  1.16  dholland rand_num(int range)
    834   1.1       mrg {
    835  1.23  dholland 	return (range == 0 ? 0 : random() % range);
    836   1.1       mrg }
    837   1.1       mrg 
    838   1.1       mrg /*
    839   1.1       mrg  * havechar:
    840   1.1       mrg  *	Check to see if we have any characters in the input queue; if
    841  1.24  dholland  *	we do, read them, stash them away, and return true; else return
    842  1.24  dholland  *	false.
    843   1.1       mrg  */
    844  1.24  dholland static bool
    845  1.16  dholland havechar(PLAYER *pp, int i)
    846   1.1       mrg {
    847   1.1       mrg 
    848   1.1       mrg 	if (pp->p_ncount < pp->p_nchar)
    849  1.24  dholland 		return true;
    850   1.8   mycroft 	if (!(fdset[i].revents & POLLIN))
    851  1.24  dholland 		return false;
    852   1.1       mrg check_again:
    853  1.30  dholland 	pp->p_nchar = read(pp->p_fd, pp->p_cbuf, sizeof pp->p_cbuf);
    854  1.30  dholland 	if (pp->p_nchar < 0 && errno == EINTR) {
    855  1.30  dholland 		goto check_again;
    856  1.30  dholland 	} else if (pp->p_nchar <= 0) {
    857   1.1       mrg 		if (errno == EINTR)
    858   1.1       mrg 		pp->p_cbuf[0] = 'q';
    859   1.1       mrg 	}
    860   1.1       mrg 	pp->p_ncount = 0;
    861  1.24  dholland 	return true;
    862   1.1       mrg }
    863   1.1       mrg 
    864   1.1       mrg /*
    865   1.1       mrg  * cleanup:
    866   1.1       mrg  *	Exit with the given value, cleaning up any droppings lying around
    867   1.1       mrg  */
    868  1.19  dholland void
    869  1.30  dholland cleanup(int exitval)
    870   1.1       mrg {
    871  1.17  dholland 	PLAYER *pp;
    872   1.1       mrg 
    873   1.1       mrg 	for (pp = Player; pp < End_player; pp++) {
    874   1.1       mrg 		cgoto(pp, HEIGHT, 0);
    875   1.1       mrg 		sendcom(pp, ENDWIN);
    876   1.1       mrg 		(void) putc(LAST_PLAYER, pp->p_output);
    877   1.1       mrg 		(void) fclose(pp->p_output);
    878   1.1       mrg 	}
    879  1.17  dholland #ifdef MONITOR
    880   1.1       mrg 	for (pp = Monitor; pp < End_monitor; pp++) {
    881   1.1       mrg 		cgoto(pp, HEIGHT, 0);
    882   1.1       mrg 		sendcom(pp, ENDWIN);
    883   1.1       mrg 		(void) putc(LAST_PLAYER, pp->p_output);
    884   1.1       mrg 		(void) fclose(pp->p_output);
    885   1.1       mrg 	}
    886  1.17  dholland #endif
    887  1.34  dholland 	(void) close(huntsock);
    888  1.17  dholland #ifdef AF_UNIX_HACK
    889  1.34  dholland 	(void) unlink(huntsockpath);
    890  1.17  dholland #endif
    891   1.1       mrg 
    892  1.30  dholland 	exit(exitval);
    893   1.1       mrg }
    894   1.1       mrg 
    895   1.1       mrg /*
    896   1.1       mrg  * send_stats:
    897   1.1       mrg  *	Print stats to requestor
    898   1.1       mrg  */
    899   1.2     lukem static void
    900  1.16  dholland send_stats(void)
    901   1.1       mrg {
    902  1.17  dholland 	IDENT *ip;
    903  1.17  dholland 	FILE *fp;
    904  1.17  dholland 	int s;
    905  1.17  dholland 	SOCKET sockstruct;
    906  1.17  dholland 	socklen_t socklen;
    907   1.1       mrg 
    908   1.1       mrg 	/*
    909   1.1       mrg 	 * Get the output stream ready
    910   1.1       mrg 	 */
    911  1.17  dholland #ifdef INTERNET
    912   1.1       mrg 	socklen = sizeof sockstruct;
    913  1.17  dholland #else
    914   1.1       mrg 	socklen = sizeof sockstruct - 1;
    915  1.17  dholland #endif
    916  1.34  dholland 	s = accept(statsock, (struct sockaddr *) &sockstruct, &socklen);
    917   1.1       mrg 	if (s < 0) {
    918   1.1       mrg 		if (errno == EINTR)
    919   1.1       mrg 			return;
    920  1.32  dholland 		complain(LOG_WARNING, "accept");
    921   1.1       mrg 		return;
    922   1.1       mrg 	}
    923   1.1       mrg 	fp = fdopen(s, "w");
    924   1.1       mrg 	if (fp == NULL) {
    925  1.32  dholland 		complain(LOG_WARNING, "fdopen");
    926   1.1       mrg 		(void) close(s);
    927   1.1       mrg 		return;
    928   1.1       mrg 	}
    929   1.1       mrg 
    930   1.1       mrg 	/*
    931   1.1       mrg 	 * Send output to requestor
    932   1.1       mrg 	 */
    933   1.1       mrg 	fputs("Name\t\tScore\tDucked\tAbsorb\tFaced\tShot\tRobbed\tMissed\tSlimeK\n", fp);
    934   1.1       mrg 	for (ip = Scores; ip != NULL; ip = ip->i_next) {
    935   1.1       mrg 		fprintf(fp, "%s\t", ip->i_name);
    936   1.1       mrg 		if (strlen(ip->i_name) < 8)
    937   1.1       mrg 			putc('\t', fp);
    938   1.1       mrg 		fprintf(fp, "%.2f\t%d\t%d\t%d\t%d\t%d\t%d\t%d\n",
    939   1.1       mrg 			ip->i_score, ip->i_ducked, ip->i_absorbed,
    940   1.1       mrg 			ip->i_faced, ip->i_shot, ip->i_robbed,
    941   1.1       mrg 			ip->i_missed, ip->i_slime);
    942   1.1       mrg 	}
    943   1.1       mrg 	fputs("\n\nName\t\tEnemy\tFriend\tDeaths\tStill\tSaved\n", fp);
    944   1.1       mrg 	for (ip = Scores; ip != NULL; ip = ip->i_next) {
    945   1.1       mrg 		if (ip->i_team == ' ') {
    946   1.1       mrg 			fprintf(fp, "%s\t", ip->i_name);
    947   1.1       mrg 			if (strlen(ip->i_name) < 8)
    948   1.1       mrg 				putc('\t', fp);
    949   1.1       mrg 		}
    950   1.1       mrg 		else {
    951   1.1       mrg 			fprintf(fp, "%s[%c]\t", ip->i_name, ip->i_team);
    952   1.1       mrg 			if (strlen(ip->i_name) + 3 < 8)
    953   1.1       mrg 				putc('\t', fp);
    954   1.1       mrg 		}
    955   1.1       mrg 		fprintf(fp, "%d\t%d\t%d\t%d\t%d\n",
    956   1.1       mrg 			ip->i_gkills, ip->i_bkills, ip->i_deaths,
    957   1.1       mrg 			ip->i_stillb, ip->i_saved);
    958   1.1       mrg 	}
    959   1.1       mrg 
    960   1.1       mrg 	(void) fclose(fp);
    961   1.1       mrg }
    962   1.1       mrg 
    963   1.1       mrg /*
    964   1.1       mrg  * clear_scores:
    965   1.1       mrg  *	Clear out the scores so the next session start clean
    966   1.1       mrg  */
    967   1.2     lukem static void
    968  1.16  dholland clear_scores(void)
    969   1.1       mrg {
    970  1.17  dholland 	IDENT *ip, *nextip;
    971   1.1       mrg 
    972   1.1       mrg 	for (ip = Scores; ip != NULL; ip = nextip) {
    973   1.1       mrg 		nextip = ip->i_next;
    974  1.15  dholland 		(void) free(ip);
    975   1.1       mrg 	}
    976   1.1       mrg 	Scores = NULL;
    977   1.1       mrg }
    978