xref: /openbsd/games/sail/pl_1.c (revision a6445c1d)
1 /*	$OpenBSD: pl_1.c,v 1.10 2011/11/06 01:43:50 guenther Exp $	*/
2 /*	$NetBSD: pl_1.c,v 1.3 1995/04/22 10:37:07 cgd Exp $	*/
3 
4 /*
5  * Copyright (c) 1983, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 #include "player.h"
34 #include <sys/types.h>
35 #include <errno.h>
36 #include <sys/wait.h>
37 #include <stdlib.h>
38 #include <unistd.h>
39 
40 #ifndef __GNUC__
41 #define __attribute__(x)
42 #endif
43 
44 /*
45  * If we get here before a ship is chosen, then ms == 0 and
46  * we don't want to update the score file, or do any Write's either.
47  * We can assume the sync file is already created and may need
48  * to be removed.
49  * Of course, we don't do any more Sync()'s if we got here
50  * because of a Sync() failure.
51  */
52 void
53 leave(conditions)
54 	int conditions;
55 {
56 	(void) signal(SIGHUP, SIG_IGN);
57 	(void) signal(SIGINT, SIG_IGN);
58 	(void) signal(SIGQUIT, SIG_IGN);
59 	(void) signal(SIGALRM, SIG_IGN);
60 	(void) signal(SIGCHLD, SIG_DFL);
61 
62 	if (done_curses) {
63 		Msg("It looks like you've had it!");
64 		switch (conditions) {
65 		case LEAVE_QUIT:
66 			break;
67 		case LEAVE_CAPTURED:
68 			Msg("Your ship was captured.");
69 			break;
70 		case LEAVE_HURRICAN:
71 			Msg("Hurricane!  All ships destroyed.");
72 			break;
73 		case LEAVE_DRIVER:
74 			Msg("The driver died.");
75 			break;
76 		case LEAVE_SYNC:
77 			Msg("Synchronization error.");
78 			break;
79 		default:
80 			Msg("A funny thing happened (%d).", conditions);
81 		}
82 	} else {
83 		switch (conditions) {
84 		case LEAVE_QUIT:
85 			break;
86 		case LEAVE_DRIVER:
87 			printf("The driver died.\n");
88 			break;
89 		case LEAVE_FORK:
90 			perror("fork");
91 			break;
92 		case LEAVE_SYNC:
93 			printf("Synchronization error\n.");
94 			break;
95 		default:
96 			printf("A funny thing happened (%d).\n",
97 				conditions);
98 		}
99 	}
100 
101 	if (ms != 0) {
102 		logger(ms);
103 		if (conditions != LEAVE_SYNC) {
104 			makemsg(ms, "Captain %s relinquishing.",
105 				mf->captain);
106 			Write(W_END, ms, 0, 0, 0, 0);
107 			(void) Sync();
108 		}
109 	}
110 	sync_close(!hasdriver);
111 	sleep(5);
112 	cleanupscreen();
113 	exit(0);
114 }
115 
116 void
117 choke(n)
118 	int n __attribute__((unused));
119 {
120 	leave(LEAVE_QUIT);
121 }
122 
123 void
124 child(n)
125 	int n __attribute__((unused));
126 {
127 	int status;
128 	int pid;
129 	int save_errno = errno;
130 
131 	(void) signal(SIGCHLD, SIG_DFL);
132 	do {
133 		pid = waitpid((pid_t)-1, &status, WNOHANG);
134 		if (pid < 0 || (pid > 0 && !WIFSTOPPED(status)))
135 			hasdriver = 0;
136 	} while (pid > 0);
137 	(void) signal(SIGCHLD, child);
138 	errno = save_errno;
139 }
140