last remnant of old Signal() scheme
[unix-history] / usr / src / games / sail / sync.c
CommitLineData
15e30c53 1#ifndef lint
0f5fabce 2static char *sccsid = "@(#)sync.c 1.3 83/10/14";
15e30c53
EW
3#endif
4
5#include "externs.h"
6
7static char sync_buf[BUFSIZE];
8static char *sync_bufp = sync_buf;
9static char sync_lock[25];
10static char sync_file[25];
11static long sync_seek;
12static FILE *sync_fp;
13#define SF "/tmp/#sailsink.%d"
14#define LF "/tmp/#saillock.%d"
15
16/*VARARGS3*/
17makesignal(from, fmt, ship, a, b, c)
18struct ship *from;
19char *fmt;
20register struct ship *ship;
21{
22 char message[80];
23
24 if (ship == 0)
25 (void) sprintf(message, fmt, a, b, c);
26 else
27 (void) sprintf(message, fmt,
28 ship->shipname, colours(ship),
29 sterncolour(ship), a, b, c);
30 Write(W_SIGNAL, from, 1, (int)message, 0, 0, 0);
31}
32
33sync_exists(game)
34{
35 char buf[sizeof sync_file];
36
37 (void) sprintf(buf, SF, game);
38 return access(buf, 0) >= 0;
39}
40
41sync_open()
42{
43 (void) sprintf(sync_lock, LF, game);
44 (void) sprintf(sync_file, SF, game);
45 if (access(sync_file, 0) < 0) {
46 int omask;
47#ifdef SETUID
48 omask = umask(077);
49#else
50 omask = umask(011);
51#endif
52 sync_fp = fopen(sync_file, "w+");
53 (void) umask(omask);
54 } else
55 sync_fp = fopen(sync_file, "r+");
56 if (sync_fp == 0)
57 return -1;
58 sync_seek == 0;
59}
60
61sync_close(remove)
62char remove;
63{
64 (void) fclose(sync_fp);
65 if (remove)
66 (void) unlink(sync_file);
67}
68
69Write(type, ship, isstr, a, b, c, d)
70int type;
71struct ship *ship;
72char isstr;
73int a, b, c, d;
74{
75 if (isstr)
76 (void) sprintf(sync_bufp, "%d %d %d %s\n",
77 type, ship-SHIP(0), isstr, a);
78 else
79 (void) sprintf(sync_bufp, "%d %d %d %d %d %d %d\n",
80 type, ship-SHIP(0), isstr, a, b, c, d);
81 while (*sync_bufp++)
82 ;
83 sync_bufp--;
84 if (sync_bufp >= &sync_buf[sizeof sync_buf])
85 abort();
86 sync_update(type, ship, a, b, c, d);
87}
88
89Sync()
90{
91 int (*sig1)(), (*sig2)();
92 int type, shipnum, isstr, a, b, c, d;
93 char buf[60];
94 register char *p = buf;
95 int n;
96
97 sig1 = signal(SIGHUP, SIG_IGN);
98 sig2 = signal(SIGINT, SIG_IGN);
99 for (n = 0; link(sync_file, sync_lock) < 0 && n < 30; n++)
100 sleep(2);
101 (void) fseek(sync_fp, sync_seek, 0);
102 while (fscanf(sync_fp, "%d%d%d", &type, &shipnum, &isstr) != EOF) {
103 if (isstr) {
104 for (p = buf;;) {
105 switch (*p++ = getc(sync_fp)) {
106 case '\n':
107 p--;
108 case EOF:
109 break;
110 default:
111 continue;
112 }
113 break;
114 }
115 *p = 0;
116 for (p = buf; *p == ' '; p++)
117 ;
118 a = (int)p;
119 b = c = d = 0;
120 } else
121 (void) fscanf(sync_fp, "%d%d%d%d", &a, &b, &c, &d);
122 sync_update(type, SHIP(shipnum), a, b, c, d);
123 }
124 if (sync_bufp != sync_buf) {
125 (void) fseek(sync_fp, 0L, 2);
126 (void) fputs(sync_buf, sync_fp);
127 (void) fflush(sync_fp);
128 sync_bufp = sync_buf;
129 }
130 sync_seek = ftell(sync_fp);
131 (void) unlink(sync_lock);
132 (void) signal(SIGHUP, sig1);
133 (void) signal(SIGINT, sig2);
134}
135
136sync_update(type, ship, a, b, c, d)
137int type;
138register struct ship *ship;
139int a, b, c, d;
140{
141 switch (type) {
142 case W_DBP: {
143 register struct BP *p = &ship->file->DBP[a];
144 p->turnsent = b;
145 p->toship = SHIP(c);
146 p->mensent = d;
147 break;
148 }
149 case W_OBP: {
150 register struct BP *p = &ship->file->OBP[a];
151 p->turnsent = b;
152 p->toship = SHIP(c);
153 p->mensent = d;
154 break;
155 }
156 case W_FOUL: {
157 register struct snag *p = &ship->file->fouls[a];
158 p->turnfoul = b;
159 p->toship = SHIP(c);
160 break;
161 }
162 case W_GRAP: {
163 register struct snag *p = &ship->file->grapples[a];
164 p->turnfoul = b;
165 p->toship = SHIP(c);
166 break;
167 }
168 case W_SIGNAL:
0f5fabce
EW
169 if (isplayer) {
170 (void) putchar('\7');
171 Signal("%s (%c%c): %s", ship, a);
172 }
15e30c53
EW
173 break;
174 case W_CREW: {
175 register struct shipspecs *s = ship->specs;
176 s->crew1 = a;
177 s->crew2 = b;
178 s->crew3 = c;
179 break;
180 }
181 case W_CAPTAIN:
182 (void) strncpy(ship->file->captain, (char *)a,
183 sizeof ship->file->captain - 1);
184 ship->file->captain[sizeof ship->file->captain - 1] = 0;
185 break;
186 case W_CAPTURED:
187 ship->file->captured = SHIP(a);
188 break;
189 case W_CLASS:
190 ship->specs->class = a;
191 break;
192 case W_DRIFT:
193 ship->file->drift = a;
194 break;
195 case W_EXPLODE:
196 if ((ship->file->explode = a) == 2)
197 ship->file->dir = 0;
198 break;
199 case W_FS:
200 ship->file->FS = a;
201 break;
202 case W_GUNL: {
203 register struct shipspecs *s = ship->specs;
204 s->gunL = a;
205 s->carL = b;
206 break;
207 }
208 case W_GUNR: {
209 register struct shipspecs *s = ship->specs;
210 s->gunR = a;
211 s->carR = b;
212 break;
213 }
214 case W_HULL:
215 ship->specs->hull = a;
216 break;
217 case W_LAST:
218 (void) strncpy(ship->file->last, (char *)a,
219 sizeof ship->file->last - 1);
220 ship->file->last[sizeof ship->file->last - 1] = 0;
221 break;
222 case W_PCREW:
223 ship->file->pcrew = a;
224 break;
225 case W_POINTS:
226 ship->file->points = a;
227 break;
228 case W_QUAL:
229 ship->specs->qual = a;
230 break;
231 case W_RIGG: {
232 register struct shipspecs *s = ship->specs;
233 s->rig1 = a;
234 s->rig2 = b;
235 s->rig3 = c;
236 s->rig4 = d;
237 break;
238 }
239 case W_RIG1:
240 ship->specs->rig1 = a;
241 break;
242 case W_RIG2:
243 ship->specs->rig2 = a;
244 break;
245 case W_RIG3:
246 ship->specs->rig3 = a;
247 break;
248 case W_RIG4:
249 ship->specs->rig4 = a;
250 break;
251 case W_SHIPCOL:
252 ship->file->col = a;
253 break;
254 case W_SHIPDIR:
255 ship->file->dir = a;
256 break;
257 case W_SHIPROW:
258 ship->file->row = a;
259 break;
260 case W_SINK:
261 if ((ship->file->sink = a) == 2)
262 ship->file->dir = 0;
263 break;
264 case W_STRUCK:
265 ship->file->struck = a;
266 break;
267 case W_TA:
268 ship->specs->ta = a;
269 break;
270 case W_ALIVE:
271 alive = 1;
272 break;
273 case W_TURN:
274 turn = a;
275 break;
276 case W_WIND:
277 winddir = a;
278 windspeed = b;
279 break;
280 case W_BEGIN:
281 (void) strcpy(ship->file->captain, "begin");
282 people++;
283 break;
284 case W_END:
285 (void) strcpy(ship->file->captain, "");
286 people--;
287 break;
288 default:
289 fprintf(stderr, "sync_update: unknown type %d\n", type);
290 break;
291 }
292}