X-Git-Url: https://git.cameronkatri.com/bsdgames-darwin.git/blobdiff_plain/00c616df1fcdefe33be74ec3f41f39b0c3852e5c..82159d673f616b12554c28d5ac54f515fec21e91:/hunt/huntd/driver.c diff --git a/hunt/huntd/driver.c b/hunt/huntd/driver.c index 2f8bdff2..ff5411d9 100644 --- a/hunt/huntd/driver.c +++ b/hunt/huntd/driver.c @@ -1,55 +1,81 @@ -/* $NetBSD: driver.c,v 1.3 1997/10/11 08:13:47 lukem Exp $ */ +/* $NetBSD: driver.c,v 1.22 2013/10/19 17:23:08 christos Exp $ */ /* - * Hunt - * Copyright (c) 1985 Conrad C. Huang, Gregory S. Couch, Kenneth C.R.C. Arnold - * San Francisco, California + * Copyright (c) 1983-2003, 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 are + * met: + * + * + Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + Neither the name of the University of California, San Francisco nor + * the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A + * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include #ifndef lint -__RCSID("$NetBSD: driver.c,v 1.3 1997/10/11 08:13:47 lukem Exp $"); +__RCSID("$NetBSD: driver.c,v 1.22 2013/10/19 17:23:08 christos Exp $"); #endif /* not lint */ -# include -# include -# include -# include -# include -# include -# include -# include -# include "hunt.h" - -# ifndef pdp11 -# define RN (((Seed = Seed * 11109 + 13849) >> 16) & 0xffff) -# else -# define RN ((Seed = Seed * 11109 + 13849) & 0x7fff) -# endif - -int Seed = 0; - - -SOCKET Daemon; -char *First_arg; /* pointer to argv[0] */ -char *Last_arg; /* pointer to end of argv/environ */ -# ifdef INTERNET -int Test_socket; /* test socket to answer datagrams */ -FLAG inetd_spawned; /* invoked via inetd */ -FLAG standard_port = TRUE; /* true if listening on standard port */ -u_short sock_port; /* port # of tcp listen socket */ -u_short stat_port; /* port # of statistics tcp socket */ -# define DAEMON_SIZE (sizeof Daemon) -# else -# define DAEMON_SIZE (sizeof Daemon - 1) -# endif - -static void clear_scores __P((void)); -static int havechar __P((PLAYER *)); -static void init __P((void)); - int main __P((int, char *[], char *[])); -static void makeboots __P((void)); -static void send_stats __P((void)); -static void zap __P((PLAYER *, FLAG)); +#include +#include +#include +#include +#include +#include +#include +#include +#include"hunt.h" + +#ifndef pdp11 +#define RN (((Seed = Seed * 11109 + 13849) >> 16) & 0xffff) +#else +#define RN ((Seed = Seed * 11109 + 13849) & 0x7fff) +#endif + +static int Seed = 0; + + +static SOCKET Daemon; +static char *First_arg; /* pointer to argv[0] */ +static char *Last_arg; /* pointer to end of argv/environ */ + +#ifdef INTERNET +static int Test_socket; /* test socket to answer datagrams */ +static FLAG inetd_spawned; /* invoked via inetd */ +static FLAG standard_port = TRUE; /* true if listening on standard port */ +static u_short sock_port; /* port # of tcp listen socket */ +static u_short stat_port; /* port # of statistics tcp socket */ +#define DAEMON_SIZE (sizeof Daemon) +#else +#define DAEMON_SIZE (sizeof Daemon - 1) +#endif + +static void clear_scores(void); +static int havechar(PLAYER *, int); +static void init(void); +int main(int, char *[], char *[]); +static void makeboots(void); +static void send_stats(void); +static void zap(PLAYER *, FLAG, int); /* @@ -57,25 +83,19 @@ static void zap __P((PLAYER *, FLAG)); * The main program. */ int -main(ac, av, ep) - int ac; - char **av, **ep; +main(int ac, char **av, char **ep) { - PLAYER *pp; - int had_char; -# ifdef INTERNET - u_short msg; - short port_num, reply; - int namelen; - SOCKET test; -# endif - static fd_set read_fds; - static FLAG first = TRUE; - static FLAG server = FALSE; - extern int optind; - extern char *optarg; - int c; - static struct timeval linger = { 90, 0 }; + PLAYER *pp; +#ifdef INTERNET + u_short msg; + short reply; + socklen_t namelen; + SOCKET test; +#endif + static FLAG first = TRUE; + static FLAG server = FALSE; + int c, i; + const int linger = 90 * 1000; First_arg = av[0]; if (ep == NULL || *ep == NULL) @@ -89,12 +109,12 @@ main(ac, av, ep) case 's': server = TRUE; break; -# ifdef INTERNET +#ifdef INTERNET case 'p': standard_port = FALSE; Test_port = atoi(optarg); break; -# endif +#endif default: erred: fprintf(stderr, "Usage: %s [-s] [-p port]\n", av[0]); @@ -109,37 +129,34 @@ erred: again: do { - read_fds = Fds_mask; errno = 0; - while (select(Num_fds, &read_fds, NULL, NULL, NULL) < 0) + while (poll(fdset, 3+MAXPL+MAXMON, INFTIM) < 0) { if (errno != EINTR) -# ifdef LOG - syslog(LOG_WARNING, "select: %m"); -# else - warn("select"); -# endif +#ifdef LOG + syslog(LOG_WARNING, "poll: %m"); +#else + warn("poll"); +#endif errno = 0; } - Have_inp = read_fds; -# ifdef INTERNET - if (FD_ISSET(Test_socket, &read_fds)) { +#ifdef INTERNET + if (fdset[2].revents & POLLIN) { namelen = DAEMON_SIZE; - port_num = htons(sock_port); - (void) recvfrom(Test_socket, (char *) &msg, sizeof msg, + (void) recvfrom(Test_socket, &msg, sizeof msg, 0, (struct sockaddr *) &test, &namelen); switch (ntohs(msg)) { case C_MESSAGE: if (Nplayer <= 0) break; reply = htons((u_short) Nplayer); - (void) sendto(Test_socket, (char *) &reply, + (void) sendto(Test_socket, &reply, sizeof reply, 0, (struct sockaddr *) &test, DAEMON_SIZE); break; case C_SCORES: reply = htons(stat_port); - (void) sendto(Test_socket, (char *) &reply, + (void) sendto(Test_socket, &reply, sizeof reply, 0, (struct sockaddr *) &test, DAEMON_SIZE); break; @@ -148,90 +165,84 @@ again: if (msg == C_MONITOR && Nplayer <= 0) break; reply = htons(sock_port); - (void) sendto(Test_socket, (char *) &reply, + (void) sendto(Test_socket, &reply, sizeof reply, 0, (struct sockaddr *) &test, DAEMON_SIZE); break; } } -# endif - for (;;) { - had_char = FALSE; - for (pp = Player; pp < End_player; pp++) - if (havechar(pp)) { +#endif + { + for (pp = Player, i = 0; pp < End_player; pp++, i++) + if (havechar(pp, i + 3)) { execute(pp); pp->p_nexec++; - had_char++; } -# ifdef MONITOR - for (pp = Monitor; pp < End_monitor; pp++) - if (havechar(pp)) { +#ifdef MONITOR + for (pp = Monitor, i = 0; pp < End_monitor; pp++, i++) + if (havechar(pp, i + MAXPL + 3)) { mon_execute(pp); pp->p_nexec++; - had_char++; } -# endif - if (!had_char) - break; +#endif moveshots(); - for (pp = Player; pp < End_player; ) + for (pp = Player, i = 0; pp < End_player; ) if (pp->p_death[0] != '\0') - zap(pp, TRUE); + zap(pp, TRUE, i + 3); else - pp++; -# ifdef MONITOR - for (pp = Monitor; pp < End_monitor; ) + pp++, i++; +#ifdef MONITOR + for (pp = Monitor, i = 0; pp < End_monitor; ) if (pp->p_death[0] != '\0') - zap(pp, FALSE); + zap(pp, FALSE, i + MAXPL + 3); else - pp++; -# endif + pp++, i++; +#endif } - if (FD_ISSET(Socket, &read_fds)) + if (fdset[0].revents & POLLIN) if (answer()) { -# ifdef INTERNET +#ifdef INTERNET if (first && standard_port) faketalk(); -# endif +#endif first = FALSE; } - if (FD_ISSET(Status, &read_fds)) + if (fdset[1].revents & POLLIN) send_stats(); - for (pp = Player; pp < End_player; pp++) { - if (FD_ISSET(pp->p_fd, &read_fds)) + for (pp = Player, i = 0; pp < End_player; pp++, i++) { + if (fdset[i + 3].revents & POLLIN) sendcom(pp, READY, pp->p_nexec); pp->p_nexec = 0; (void) fflush(pp->p_output); } -# ifdef MONITOR - for (pp = Monitor; pp < End_monitor; pp++) { - if (FD_ISSET(pp->p_fd, &read_fds)) +#ifdef MONITOR + for (pp = Monitor, i = 0; pp < End_monitor; pp++, i++) { + if (fdset[i + MAXPL + 3].revents & POLLIN) sendcom(pp, READY, pp->p_nexec); pp->p_nexec = 0; (void) fflush(pp->p_output); } -# endif +#endif } while (Nplayer > 0); - read_fds = Fds_mask; - if (select(Num_fds, &read_fds, NULL, NULL, &linger) > 0) { + if (poll(fdset, 3+MAXPL+MAXMON, linger) > 0) { goto again; } if (server) { clear_scores(); makemaze(); clearwalls(); -# ifdef BOOTS +#ifdef BOOTS makeboots(); -# endif +#endif first = TRUE; goto again; } -# ifdef MONITOR - for (pp = Monitor; pp < End_monitor; ) - zap(pp, FALSE); -# endif +#ifdef MONITOR + for (pp = Monitor, i = 0; pp < End_monitor; i++) + zap(pp, FALSE, i + MAXPL + 3); +#endif cleanup(0); /* NOTREACHED */ return(0); @@ -242,136 +253,132 @@ again: * Initialize the global parameters. */ static void -init() +init(void) { - int i; -# ifdef INTERNET - SOCKET test_port; - int msg; - int len; -# endif - -# ifndef DEBUG -# ifdef TIOCNOTTY + int i; +#ifdef INTERNET + SOCKET test_port; + int msg; + socklen_t len; +#endif + +#ifndef DEBUG +#ifdef TIOCNOTTY (void) ioctl(fileno(stdout), TIOCNOTTY, NULL); -# endif +#endif (void) setpgrp(getpid(), getpid()); (void) signal(SIGHUP, SIG_IGN); (void) signal(SIGINT, SIG_IGN); (void) signal(SIGQUIT, SIG_IGN); (void) signal(SIGTERM, cleanup); -# endif +#endif - (void) chdir("/usr/tmp"); /* just in case it core dumps */ + (void) chdir("/var/tmp"); /* just in case it core dumps */ (void) umask(0); /* No privacy at all! */ (void) signal(SIGPIPE, SIG_IGN); -# ifdef LOG -# ifdef SYSLOG_43 - openlog("HUNT", LOG_PID, LOG_DAEMON); -# endif -# ifdef SYSLOG_42 - openlog("HUNT", LOG_PID); -# endif -# endif +#ifdef LOG + openlog("huntd", LOG_PID, LOG_DAEMON); +#endif /* * Initialize statistics socket */ -# ifdef INTERNET +#ifdef INTERNET Daemon.sin_family = SOCK_FAMILY; Daemon.sin_addr.s_addr = INADDR_ANY; Daemon.sin_port = 0; -# else +#else Daemon.sun_family = SOCK_FAMILY; (void) strcpy(Daemon.sun_path, Stat_name); -# endif +#endif Status = socket(SOCK_FAMILY, SOCK_STREAM, 0); if (bind(Status, (struct sockaddr *) &Daemon, DAEMON_SIZE) < 0) { if (errno == EADDRINUSE) exit(0); else { -# ifdef LOG +#ifdef LOG syslog(LOG_ERR, "bind: %m"); -# else +#else warn("bind"); -# endif +#endif cleanup(1); } } (void) listen(Status, 5); -# ifdef INTERNET +#ifdef INTERNET len = sizeof (SOCKET); if (getsockname(Status, (struct sockaddr *) &Daemon, &len) < 0) { -# ifdef LOG +#ifdef LOG syslog(LOG_ERR, "getsockname: %m"); -# else +#else warn("getsockname"); -# endif +#endif exit(1); } stat_port = ntohs(Daemon.sin_port); -# endif +#endif /* * Initialize main socket */ -# ifdef INTERNET +#ifdef INTERNET Daemon.sin_family = SOCK_FAMILY; Daemon.sin_addr.s_addr = INADDR_ANY; Daemon.sin_port = 0; -# else +#else Daemon.sun_family = SOCK_FAMILY; (void) strcpy(Daemon.sun_path, Sock_name); -# endif +#endif Socket = socket(SOCK_FAMILY, SOCK_STREAM, 0); -# if defined(INTERNET) +#if defined(INTERNET) msg = 1; if (setsockopt(Socket, SOL_SOCKET, SO_USELOOPBACK, &msg, sizeof msg)<0) -# ifdef LOG +#ifdef LOG syslog(LOG_WARNING, "setsockopt loopback %m"); -# else +#else warn("setsockopt loopback"); -# endif -# endif +#endif +#endif if (bind(Socket, (struct sockaddr *) &Daemon, DAEMON_SIZE) < 0) { if (errno == EADDRINUSE) exit(0); else { -# ifdef LOG +#ifdef LOG syslog(LOG_ERR, "bind: %m"); -# else +#else warn("bind"); -# endif +#endif cleanup(1); } } (void) listen(Socket, 5); -# ifdef INTERNET +#ifdef INTERNET len = sizeof (SOCKET); if (getsockname(Socket, (struct sockaddr *) &Daemon, &len) < 0) { -# ifdef LOG +#ifdef LOG syslog(LOG_ERR, "getsockname: %m"); -# else +#else warn("getsockname"); -# endif +#endif exit(1); } sock_port = ntohs(Daemon.sin_port); -# endif +#endif /* - * Initialize minimal select mask + * Initialize minimal poll mask */ - FD_SET(Socket, &Fds_mask); - FD_SET(Status, &Fds_mask); - Num_fds = ((Socket > Status) ? Socket : Status) + 1; + fdset[0].fd = Socket; + fdset[0].events = POLLIN; + fdset[1].fd = Status; + fdset[1].events = POLLIN; -# ifdef INTERNET +#ifdef INTERNET len = sizeof (SOCKET); if (getsockname(0, (struct sockaddr *) &test_port, &len) >= 0 && test_port.sin_family == AF_INET) { @@ -388,26 +395,27 @@ init() Test_socket = socket(SOCK_FAMILY, SOCK_DGRAM, 0); if (bind(Test_socket, (struct sockaddr *) &test_port, DAEMON_SIZE) < 0) { -# ifdef LOG +#ifdef LOG syslog(LOG_ERR, "bind: %m"); -# else +#else warn("bind"); -# endif +#endif exit(1); } (void) listen(Test_socket, 5); } - FD_SET(Test_socket, &Fds_mask); - if (Test_socket + 1 > Num_fds) - Num_fds = Test_socket + 1; -# endif + fdset[2].fd = Test_socket; + fdset[2].events = POLLIN; +#else + fdset[2].fd = -1; +#endif - Seed = getpid() + time((time_t *) NULL); + Seed = getpid() + time(NULL); makemaze(); -# ifdef BOOTS +#ifdef BOOTS makeboots(); -# endif +#endif for (i = 0; i < NASCII; i++) See_over[i] = TRUE; @@ -415,23 +423,23 @@ init() See_over[WALL1] = FALSE; See_over[WALL2] = FALSE; See_over[WALL3] = FALSE; -# ifdef REFLECT +#ifdef REFLECT See_over[WALL4] = FALSE; See_over[WALL5] = FALSE; -# endif +#endif } -# ifdef BOOTS +#ifdef BOOTS /* * makeboots: * Put the boots in the maze */ static void -makeboots() +makeboots(void) { - int x, y; - PLAYER *pp; + int x, y; + PLAYER *pp; do { x = rand_num(WIDTH - 1) + 1; @@ -441,7 +449,7 @@ makeboots() for (pp = Boot; pp < &Boot[NBOOTS]; pp++) pp->p_flying = -1; } -# endif +#endif /* @@ -449,18 +457,15 @@ makeboots() * Check the damage to the given player, and see if s/he is killed */ void -checkdam(ouch, gotcha, credit, amt, shot_type) - PLAYER *ouch, *gotcha; - IDENT *credit; - int amt; - char shot_type; +checkdam(PLAYER *ouch, PLAYER *gotcha, IDENT *credit, int amt, + char this_shot_type) { - char *cp; + const char *cp; if (ouch->p_death[0] != '\0') return; -# ifdef BOOTS - if (shot_type == SLIME) +#ifdef BOOTS + if (this_shot_type == SLIME) switch (ouch->p_nboots) { default: break; @@ -472,25 +477,25 @@ checkdam(ouch, gotcha, credit, amt, shot_type) message(gotcha, "He has boots on!"); return; } -# endif +#endif ouch->p_damage += amt; if (ouch->p_damage <= ouch->p_damcap) { - (void) sprintf(Buf, "%2d", ouch->p_damage); + (void) snprintf(Buf, sizeof(Buf), "%2d", ouch->p_damage); cgoto(ouch, STAT_DAM_ROW, STAT_VALUE_COL); outstr(ouch, Buf, 2); return; } /* Someone DIED */ - switch (shot_type) { + switch (this_shot_type) { default: cp = "Killed"; break; -# ifdef FLY +#ifdef FLY case FALL: cp = "Killed on impact"; break; -# endif +#endif case KNIFE: cp = "Stabbed to death"; ouch->p_ammo = 0; /* No exploding */ @@ -507,32 +512,34 @@ checkdam(ouch, gotcha, credit, amt, shot_type) case GMINE: cp = "Blown apart"; break; -# ifdef OOZE +#ifdef OOZE case SLIME: cp = "Slimed"; if (credit != NULL) credit->i_slime++; break; -# endif -# ifdef VOLCANO +#endif +#ifdef VOLCANO case LAVA: cp = "Baked"; break; -# endif -# ifdef DRONE +#endif +#ifdef DRONE case DSHOT: cp = "Eliminated"; break; -# endif +#endif } if (credit == NULL) { - (void) sprintf(ouch->p_death, "| %s by %s |", cp, - (shot_type == MINE || shot_type == GMINE) ? + (void) snprintf(ouch->p_death, sizeof(ouch->p_death), + "| %s by %s |", cp, + (this_shot_type == MINE || this_shot_type == GMINE) ? "a mine" : "act of God"); return; } - (void) sprintf(ouch->p_death, "| %s by %s |", cp, credit->i_name); + (void) snprintf(ouch->p_death, sizeof(ouch->p_death), + "| %s by %s |", cp, credit->i_name); if (ouch == gotcha) { /* No use killing yourself */ credit->i_kills--; @@ -557,25 +564,27 @@ checkdam(ouch, gotcha, credit, amt, shot_type) gotcha->p_damage -= STABDAM; if (gotcha->p_damage < 0) gotcha->p_damage = 0; - (void) sprintf(Buf, "%2d/%2d", gotcha->p_damage, gotcha->p_damcap); + (void) snprintf(Buf, sizeof(Buf), "%2d/%2d", gotcha->p_damage, + gotcha->p_damcap); cgoto(gotcha, STAT_DAM_ROW, STAT_VALUE_COL); outstr(gotcha, Buf, 5); - (void) sprintf(Buf, "%3d", (gotcha->p_damcap - MAXDAM) / 2); + (void) snprintf(Buf, sizeof(Buf), "%3d", + (gotcha->p_damcap - MAXDAM) / 2); cgoto(gotcha, STAT_KILL_ROW, STAT_VALUE_COL); outstr(gotcha, Buf, 3); - (void) sprintf(Buf, "%5.2f", gotcha->p_ident->i_score); + (void) snprintf(Buf, sizeof(Buf), "%5.2f", gotcha->p_ident->i_score); for (ouch = Player; ouch < End_player; ouch++) { cgoto(ouch, STAT_PLAY_ROW + 1 + (gotcha - Player), STAT_NAME_COL); outstr(ouch, Buf, 5); } -# ifdef MONITOR +#ifdef MONITOR for (ouch = Monitor; ouch < End_monitor; ouch++) { cgoto(ouch, STAT_PLAY_ROW + 1 + (gotcha - Player), STAT_NAME_COL); outstr(ouch, Buf, 5); } -# endif +#endif } /* @@ -583,15 +592,12 @@ checkdam(ouch, gotcha, credit, amt, shot_type) * Kill off a player and take him out of the game. */ static void -zap(pp, was_player) - PLAYER *pp; - FLAG was_player; +zap(PLAYER *pp, FLAG was_player, int i) { - int i, len; - BULLET *bp; - PLAYER *np; - int x, y; - int savefd; + int n, len; + BULLET *bp; + PLAYER *np; + int x, y; if (was_player) { if (pp->p_undershot) @@ -604,8 +610,8 @@ zap(pp, was_player) x = (WIDTH - len) / 2; cgoto(pp, HEIGHT / 2, x); outstr(pp, pp->p_death, len); - for (i = 1; i < len; i++) - pp->p_death[i] = '-'; + for (n = 1; n < len; n++) + pp->p_death[n] = '-'; pp->p_death[0] = '+'; pp->p_death[len - 1] = '+'; cgoto(pp, HEIGHT / 2 - 1, x); @@ -614,11 +620,9 @@ zap(pp, was_player) outstr(pp, pp->p_death, len); cgoto(pp, HEIGHT, 0); - savefd = pp->p_fd; - -# ifdef MONITOR +#ifdef MONITOR if (was_player) { -# endif +#endif for (bp = Bullets; bp != NULL; bp = bp->b_next) { if (bp->b_owner == pp) bp->b_owner = NULL; @@ -626,22 +630,22 @@ zap(pp, was_player) bp->b_over = SPACE; } - i = rand_num(pp->p_ammo); + n = rand_num(pp->p_ammo); x = rand_num(pp->p_ammo); - if (x > i) - i = x; + if (x > n) + n = x; if (pp->p_ammo == 0) x = 0; - else if (i == pp->p_ammo - 1) { + else if (n == pp->p_ammo - 1) { x = pp->p_ammo; len = SLIME; } else { for (x = MAXBOMB - 1; x > 0; x--) - if (i >= shot_req[x]) + if (n >= shot_req[x]) break; for (y = MAXSLIME - 1; y > 0; y--) - if (i >= slime_req[y]) + if (n >= slime_req[y]) break; if (y >= 0 && slime_req[y] > shot_req[x]) { x = slime_req[y]; @@ -654,16 +658,16 @@ zap(pp, was_player) } if (x > 0) { (void) add_shot(len, pp->p_y, pp->p_x, pp->p_face, x, - (PLAYER *) NULL, TRUE, SPACE); - (void) sprintf(Buf, "%s detonated.", + NULL, TRUE, SPACE); + (void) snprintf(Buf, sizeof(Buf), "%s detonated.", pp->p_ident->i_name); for (np = Player; np < End_player; np++) message(np, Buf); -# ifdef MONITOR +#ifdef MONITOR for (np = Monitor; np < End_monitor; np++) message(np, Buf); -# endif -# ifdef BOOTS +#endif +#ifdef BOOTS while (pp->p_nboots-- > 0) { for (np = Boot; np < &Boot[NBOOTS]; np++) if (np->p_flying < 0) @@ -680,9 +684,9 @@ zap(pp, was_player) np->p_face = BOOT; showexpl(np->p_y, np->p_x, BOOT); } -# endif +#endif } -# ifdef BOOTS +#ifdef BOOTS else if (pp->p_nboots > 0) { if (pp->p_nboots == 2) Maze[pp->p_y][pp->p_x] = BOOT_PAIR; @@ -692,9 +696,9 @@ zap(pp, was_player) fixshots(pp->p_y, pp->p_x, Maze[pp->p_y][pp->p_x]); } -# endif +#endif -# ifdef VOLCANO +#ifdef VOLCANO volcano += pp->p_ammo - x; if (rand_num(100) < volcano / 50) { do { @@ -702,14 +706,14 @@ zap(pp, was_player) y = rand_num(HEIGHT / 2) + HEIGHT / 4; } while (Maze[y][x] != SPACE); (void) add_shot(LAVA, y, x, LEFTS, volcano, - (PLAYER *) NULL, TRUE, SPACE); + NULL, TRUE, SPACE); for (np = Player; np < End_player; np++) message(np, "Volcano eruption."); volcano = 0; } -# endif +#endif -# ifdef DRONE +#ifdef DRONE if (rand_num(100) < 2) { do { x = rand_num(WIDTH / 2) + WIDTH / 4; @@ -718,9 +722,9 @@ zap(pp, was_player) add_shot(DSHOT, y, x, rand_dir(), shot_req[MINDSHOT + rand_num(MAXBOMB - MINDSHOT)], - (PLAYER *) NULL, FALSE, SPACE); + NULL, FALSE, SPACE); } -# endif +#endif sendcom(pp, ENDWIN); (void) putc(' ', pp->p_output); @@ -729,31 +733,34 @@ zap(pp, was_player) End_player--; if (pp != End_player) { memcpy(pp, End_player, sizeof (PLAYER)); - (void) sprintf(Buf, "%5.2f%c%-10.10s %c", + fdset[i] = fdset[End_player - Player + 3]; + fdset[End_player - Player + 3].fd = -1; + (void) snprintf(Buf, sizeof(Buf), "%5.2f%c%-10.10s %c", pp->p_ident->i_score, stat_char(pp), pp->p_ident->i_name, pp->p_ident->i_team); - i = STAT_PLAY_ROW + 1 + (pp - Player); + n = STAT_PLAY_ROW + 1 + (pp - Player); for (np = Player; np < End_player; np++) { - cgoto(np, i, STAT_NAME_COL); + cgoto(np, n, STAT_NAME_COL); outstr(np, Buf, STAT_NAME_LEN); } -# ifdef MONITOR +#ifdef MONITOR for (np = Monitor; np < End_monitor; np++) { - cgoto(np, i, STAT_NAME_COL); + cgoto(np, n, STAT_NAME_COL); outstr(np, Buf, STAT_NAME_LEN); } -# endif - } +#endif + } else + fdset[i].fd = -1; /* Erase the last player */ - i = STAT_PLAY_ROW + 1 + Nplayer; + n = STAT_PLAY_ROW + 1 + Nplayer; for (np = Player; np < End_player; np++) { - cgoto(np, i, STAT_NAME_COL); + cgoto(np, n, STAT_NAME_COL); ce(np); } -# ifdef MONITOR +#ifdef MONITOR for (np = Monitor; np < End_monitor; np++) { - cgoto(np, i, STAT_NAME_COL); + cgoto(np, n, STAT_NAME_COL); ce(np); } } @@ -765,50 +772,35 @@ zap(pp, was_player) End_monitor--; if (pp != End_monitor) { memcpy(pp, End_monitor, sizeof (PLAYER)); - (void) sprintf(Buf, "%5.5s %-10.10s %c", " ", + fdset[i] = fdset[End_monitor - Monitor + MAXPL + 3]; + fdset[End_monitor - Monitor + MAXPL + 3].fd = -1; + (void) snprintf(Buf, sizeof(Buf), "%5.5s %-10.10s %c", + " ", pp->p_ident->i_name, pp->p_ident->i_team); - i = STAT_MON_ROW + 1 + (pp - Player); + n = STAT_MON_ROW + 1 + (pp - Player); for (np = Player; np < End_player; np++) { - cgoto(np, i, STAT_NAME_COL); + cgoto(np, n, STAT_NAME_COL); outstr(np, Buf, STAT_NAME_LEN); } for (np = Monitor; np < End_monitor; np++) { - cgoto(np, i, STAT_NAME_COL); + cgoto(np, n, STAT_NAME_COL); outstr(np, Buf, STAT_NAME_LEN); } - } + } else + fdset[i].fd = -1; /* Erase the last monitor */ - i = STAT_MON_ROW + 1 + (End_monitor - Monitor); + n = STAT_MON_ROW + 1 + (End_monitor - Monitor); for (np = Player; np < End_player; np++) { - cgoto(np, i, STAT_NAME_COL); + cgoto(np, n, STAT_NAME_COL); ce(np); } for (np = Monitor; np < End_monitor; np++) { - cgoto(np, i, STAT_NAME_COL); + cgoto(np, n, STAT_NAME_COL); ce(np); } - - } -# endif - - FD_CLR(savefd, &Fds_mask); - if (Num_fds == savefd + 1) { - Num_fds = Socket; -# ifdef INTERNET - if (Test_socket > Socket) - Num_fds = Test_socket; -# endif - for (np = Player; np < End_player; np++) - if (np->p_fd > Num_fds) - Num_fds = np->p_fd; -# ifdef MONITOR - for (np = Monitor; np < End_monitor; np++) - if (np->p_fd > Num_fds) - Num_fds = np->p_fd; -# endif - Num_fds++; } +#endif } /* @@ -816,8 +808,7 @@ zap(pp, was_player) * Return a random number in a given range. */ int -rand_num(range) - int range; +rand_num(int range) { return (range == 0 ? 0 : RN % range); } @@ -829,15 +820,13 @@ rand_num(range) * FALSE. */ static int -havechar(pp) - PLAYER *pp; +havechar(PLAYER *pp, int i) { if (pp->p_ncount < pp->p_nchar) return TRUE; - if (!FD_ISSET(pp->p_fd, &Have_inp)) + if (!(fdset[i].revents & POLLIN)) return FALSE; - FD_CLR(pp->p_fd, &Have_inp); check_again: errno = 0; if ((pp->p_nchar = read(pp->p_fd, pp->p_cbuf, sizeof pp->p_cbuf)) <= 0) @@ -854,11 +843,10 @@ check_again: * cleanup: * Exit with the given value, cleaning up any droppings lying around */ -SIGNAL_TYPE -cleanup(eval) - int eval; +void +cleanup(int eval) { - PLAYER *pp; + PLAYER *pp; for (pp = Player; pp < End_player; pp++) { cgoto(pp, HEIGHT, 0); @@ -866,18 +854,18 @@ cleanup(eval) (void) putc(LAST_PLAYER, pp->p_output); (void) fclose(pp->p_output); } -# ifdef MONITOR +#ifdef MONITOR for (pp = Monitor; pp < End_monitor; pp++) { cgoto(pp, HEIGHT, 0); sendcom(pp, ENDWIN); (void) putc(LAST_PLAYER, pp->p_output); (void) fclose(pp->p_output); } -# endif +#endif (void) close(Socket); -# ifdef AF_UNIX_HACK +#ifdef AF_UNIX_HACK (void) unlink(Sock_name); -# endif +#endif exit(eval); } @@ -887,40 +875,40 @@ cleanup(eval) * Print stats to requestor */ static void -send_stats() +send_stats(void) { - IDENT *ip; - FILE *fp; - int s; - SOCKET sockstruct; - int socklen; + IDENT *ip; + FILE *fp; + int s; + SOCKET sockstruct; + socklen_t socklen; /* * Get the output stream ready */ -# ifdef INTERNET +#ifdef INTERNET socklen = sizeof sockstruct; -# else +#else socklen = sizeof sockstruct - 1; -# endif +#endif s = accept(Status, (struct sockaddr *) &sockstruct, &socklen); if (s < 0) { if (errno == EINTR) return; -# ifdef LOG - syslog(LOG_ERR, "accept: %m"); -# else +#ifdef LOG + syslog(LOG_WARNING, "accept: %m"); +#else warn("accept"); -# endif +#endif return; } fp = fdopen(s, "w"); if (fp == NULL) { -# ifdef LOG - syslog(LOG_ERR, "fdopen: %m"); -# else +#ifdef LOG + syslog(LOG_WARNING, "fdopen: %m"); +#else warn("fdopen"); -# endif +#endif (void) close(s); return; } @@ -963,13 +951,13 @@ send_stats() * Clear out the scores so the next session start clean */ static void -clear_scores() +clear_scores(void) { - IDENT *ip, *nextip; + IDENT *ip, *nextip; for (ip = Scores; ip != NULL; ip = nextip) { nextip = ip->i_next; - (void) free((char *) ip); + (void) free(ip); } Scores = NULL; }