xref: /original-bsd/games/sail/pl_1.c (revision 4887b8aa)
1 /*
2  * Copyright (c) 1983, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char sccsid[] = "@(#)pl_1.c	8.1 (Berkeley) 05/31/93";
10 #endif /* not lint */
11 
12 #include "player.h"
13 #include <sys/types.h>
14 #include <sys/wait.h>
15 
16 /*
17  * If we get here before a ship is chosen, then ms == 0 and
18  * we don't want to update the score file, or do any Write's either.
19  * We can assume the sync file is already created and may need
20  * to be removed.
21  * Of course, we don't do any more Sync()'s if we got here
22  * because of a Sync() failure.
23  */
24 leave(conditions)
25 int conditions;
26 {
27 	(void) signal(SIGHUP, SIG_IGN);
28 	(void) signal(SIGINT, SIG_IGN);
29 	(void) signal(SIGQUIT, SIG_IGN);
30 	(void) signal(SIGALRM, SIG_IGN);
31 	(void) signal(SIGCHLD, SIG_IGN);
32 
33 	if (done_curses) {
34 		Signal("It looks like you've had it!",
35 			(struct ship *)0);
36 		switch (conditions) {
37 		case LEAVE_QUIT:
38 			break;
39 		case LEAVE_CAPTURED:
40 			Signal("Your ship was captured.",
41 				(struct ship *)0);
42 			break;
43 		case LEAVE_HURRICAN:
44 			Signal("Hurricane!  All ships destroyed.",
45 				(struct ship *)0);
46 			break;
47 		case LEAVE_DRIVER:
48 			Signal("The driver died.", (struct ship *)0);
49 			break;
50 		case LEAVE_SYNC:
51 			Signal("Synchronization error.", (struct ship *)0);
52 			break;
53 		default:
54 			Signal("A funny thing happened (%d).",
55 				(struct ship *)0, conditions);
56 		}
57 	} else {
58 		switch (conditions) {
59 		case LEAVE_QUIT:
60 			break;
61 		case LEAVE_DRIVER:
62 			printf("The driver died.\n");
63 			break;
64 		case LEAVE_FORK:
65 			perror("fork");
66 			break;
67 		case LEAVE_SYNC:
68 			printf("Synchronization error\n.");
69 			break;
70 		default:
71 			printf("A funny thing happened (%d).\n",
72 				conditions);
73 		}
74 	}
75 
76 	if (ms != 0) {
77 		log(ms);
78 		if (conditions != LEAVE_SYNC) {
79 			makesignal(ms, "Captain %s relinquishing.",
80 				(struct ship *)0, mf->captain);
81 			Write(W_END, ms, 0, 0, 0, 0, 0);
82 			(void) Sync();
83 		}
84 	}
85 	sync_close(!hasdriver);
86 	cleanupscreen();
87 	exit(0);
88 }
89 
90 void
91 choke()
92 {
93 	leave(LEAVE_QUIT);
94 }
95 
96 void
97 child()
98 {
99 	union wait status;
100 	int pid;
101 
102 	(void) signal(SIGCHLD, SIG_IGN);
103 	do {
104 		pid = wait3((int *)&status, WNOHANG, (struct rusage *)0);
105 		if (pid < 0 || pid > 0 && !WIFSTOPPED(status))
106 			hasdriver = 0;
107 	} while (pid > 0);
108 	(void) signal(SIGCHLD, child);
109 }
110