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