1*e544d2b8Sdholland /* $NetBSD: pl_1.c,v 1.21 2009/03/14 22:52:52 dholland Exp $ */
2982615d7Scgd
361f28255Scgd /*
4982615d7Scgd * Copyright (c) 1983, 1993
5982615d7Scgd * The Regents of the University of California. All rights reserved.
661f28255Scgd *
761f28255Scgd * Redistribution and use in source and binary forms, with or without
861f28255Scgd * modification, are permitted provided that the following conditions
961f28255Scgd * are met:
1061f28255Scgd * 1. Redistributions of source code must retain the above copyright
1161f28255Scgd * notice, this list of conditions and the following disclaimer.
1261f28255Scgd * 2. Redistributions in binary form must reproduce the above copyright
1361f28255Scgd * notice, this list of conditions and the following disclaimer in the
1461f28255Scgd * documentation and/or other materials provided with the distribution.
15e5aeb4eaSagc * 3. Neither the name of the University nor the names of its contributors
1661f28255Scgd * may be used to endorse or promote products derived from this software
1761f28255Scgd * without specific prior written permission.
1861f28255Scgd *
1961f28255Scgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2061f28255Scgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2161f28255Scgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2261f28255Scgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2361f28255Scgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2461f28255Scgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2561f28255Scgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2661f28255Scgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2761f28255Scgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2861f28255Scgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2961f28255Scgd * SUCH DAMAGE.
3061f28255Scgd */
3161f28255Scgd
32bf72b656Schristos #include <sys/cdefs.h>
3361f28255Scgd #ifndef lint
34982615d7Scgd #if 0
35982615d7Scgd static char sccsid[] = "@(#)pl_1.c 8.1 (Berkeley) 5/31/93";
36982615d7Scgd #else
37*e544d2b8Sdholland __RCSID("$NetBSD: pl_1.c,v 1.21 2009/03/14 22:52:52 dholland Exp $");
38982615d7Scgd #endif
3961f28255Scgd #endif /* not lint */
4061f28255Scgd
4161f28255Scgd #include <sys/types.h>
4261f28255Scgd #include <sys/wait.h>
4361a39e0bSjwise #include <signal.h>
44aac077fcSjwise #include <stdio.h>
45bcc8ec99Scgd #include <stdlib.h>
465175902cSchristos #include <unistd.h>
479baf035bSjwise #include "extern.h"
48aac077fcSjwise #include "player.h"
4961f28255Scgd
5061f28255Scgd /*
5161f28255Scgd * If we get here before a ship is chosen, then ms == 0 and
5261f28255Scgd * we don't want to update the score file, or do any Write's either.
5361f28255Scgd * We can assume the sync file is already created and may need
5461f28255Scgd * to be removed.
5561f28255Scgd * Of course, we don't do any more Sync()'s if we got here
5661f28255Scgd * because of a Sync() failure.
5761f28255Scgd */
58bf72b656Schristos void
leave(int conditions)59dfea9f08Sjwise leave(int conditions)
6061f28255Scgd {
61dfea9f08Sjwise signal(SIGHUP, SIG_IGN);
62dfea9f08Sjwise signal(SIGINT, SIG_IGN);
63dfea9f08Sjwise signal(SIGQUIT, SIG_IGN);
64dfea9f08Sjwise signal(SIGALRM, SIG_IGN);
65dfea9f08Sjwise signal(SIGCHLD, SIG_IGN);
6661f28255Scgd
6761f28255Scgd if (done_curses) {
68bf72b656Schristos Msg("It looks like you've had it!");
6961f28255Scgd switch (conditions) {
7061f28255Scgd case LEAVE_QUIT:
7161f28255Scgd break;
7261f28255Scgd case LEAVE_CAPTURED:
73bf72b656Schristos Msg("Your ship was captured.");
7461f28255Scgd break;
7561f28255Scgd case LEAVE_HURRICAN:
76bf72b656Schristos Msg("Hurricane! All ships destroyed.");
7761f28255Scgd break;
7861f28255Scgd case LEAVE_DRIVER:
79bf72b656Schristos Msg("The driver died.");
8061f28255Scgd break;
8161f28255Scgd case LEAVE_SYNC:
82bf72b656Schristos Msg("Synchronization error.");
8361f28255Scgd break;
8461f28255Scgd default:
85bf72b656Schristos Msg("A funny thing happened (%d).", conditions);
8661f28255Scgd }
8761f28255Scgd } else {
8861f28255Scgd switch (conditions) {
8961f28255Scgd case LEAVE_QUIT:
9061f28255Scgd break;
9161f28255Scgd case LEAVE_DRIVER:
9261f28255Scgd printf("The driver died.\n");
9361f28255Scgd break;
9461f28255Scgd case LEAVE_FORK:
9561f28255Scgd break;
9661f28255Scgd case LEAVE_SYNC:
9761f28255Scgd printf("Synchronization error\n.");
9861f28255Scgd break;
9961f28255Scgd default:
10061f28255Scgd printf("A funny thing happened (%d).\n",
10161f28255Scgd conditions);
10261f28255Scgd }
10361f28255Scgd }
10461f28255Scgd
10561f28255Scgd if (ms != 0) {
106bf72b656Schristos logger(ms);
10761f28255Scgd if (conditions != LEAVE_SYNC) {
1085175902cSchristos makemsg(ms, "Captain %s relinquishing.",
1095175902cSchristos mf->captain);
110*e544d2b8Sdholland send_end(ms);
111dfea9f08Sjwise Sync();
11261f28255Scgd }
11361f28255Scgd }
11461f28255Scgd sync_close(!hasdriver);
1155175902cSchristos sleep(5);
11661f28255Scgd cleanupscreen();
11761f28255Scgd exit(0);
11861f28255Scgd }
11961f28255Scgd
120bf72b656Schristos /*ARGSUSED*/
12161f28255Scgd void
choke(int n __unused)1228b0f9554Sperry choke(int n __unused)
12361f28255Scgd {
12461f28255Scgd leave(LEAVE_QUIT);
12561f28255Scgd }
12661f28255Scgd
127bf72b656Schristos /*ARGSUSED*/
12861f28255Scgd void
child(int n __unused)1298b0f9554Sperry child(int n __unused)
13061f28255Scgd {
131a5f914a2Sjsm int status;
13261f28255Scgd int pid;
13361f28255Scgd
134dfea9f08Sjwise signal(SIGCHLD, SIG_IGN);
13561f28255Scgd do {
136a5f914a2Sjsm pid = wait3(&status, WNOHANG, (struct rusage *)0);
137bf72b656Schristos if (pid < 0 || (pid > 0 && !WIFSTOPPED(status)))
13861f28255Scgd hasdriver = 0;
13961f28255Scgd } while (pid > 0);
140dfea9f08Sjwise signal(SIGCHLD, child);
14161f28255Scgd }
142