]> git.cameronkatri.com Git - bsdgames-darwin.git/blob - sail/sync.c
initial import of 386bsd-0.1 sources
[bsdgames-darwin.git] / sail / sync.c
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, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34 #ifndef lint
35 static char sccsid[] = "@(#)sync.c 5.6 (Berkeley) 6/1/90";
36 #endif /* not lint */
37
38 #include "externs.h"
39 #include <sys/file.h>
40 #include <sys/errno.h>
41
42 #define BUFSIZE 4096
43
44 static char sync_buf[BUFSIZE];
45 static char *sync_bp = sync_buf;
46 static char sync_lock[25];
47 static char sync_file[25];
48 static long sync_seek;
49 static FILE *sync_fp;
50 #define SF "/tmp/#sailsink.%d"
51 #define LF "/tmp/#saillock.%d"
52
53 /*VARARGS3*/
54 makesignal(from, fmt, ship, a, b, c)
55 struct ship *from;
56 char *fmt;
57 register struct ship *ship;
58 {
59 char message[80];
60
61 if (ship == 0)
62 (void) sprintf(message, fmt, a, b, c);
63 else
64 (void) sprintf(message, fmt,
65 ship->shipname, colours(ship),
66 sterncolour(ship), a, b, c);
67 Write(W_SIGNAL, from, 1, (int)message, 0, 0, 0);
68 }
69
70 #include <sys/types.h>
71 #include <sys/stat.h>
72 sync_exists(game)
73 {
74 char buf[sizeof sync_file];
75 struct stat s;
76 time_t t;
77
78 (void) sprintf(buf, SF, game);
79 (void) time(&t);
80 if (stat(buf, &s) < 0)
81 return 0;
82 if (s.st_mtime < t - 60*60*2) { /* 2 hours */
83 (void) unlink(buf);
84 (void) sprintf(buf, LF, game);
85 (void) unlink(buf);
86 return 0;
87 } else
88 return 1;
89 }
90
91 sync_open()
92 {
93 if (sync_fp != NULL)
94 (void) fclose(sync_fp);
95 (void) sprintf(sync_lock, LF, game);
96 (void) sprintf(sync_file, SF, game);
97 if (access(sync_file, 0) < 0) {
98 int omask = umask(issetuid ? 077 : 011);
99 sync_fp = fopen(sync_file, "w+");
100 (void) umask(omask);
101 } else
102 sync_fp = fopen(sync_file, "r+");
103 if (sync_fp == NULL)
104 return -1;
105 sync_seek = 0;
106 return 0;
107 }
108
109 sync_close(remove)
110 char remove;
111 {
112 if (sync_fp != 0)
113 (void) fclose(sync_fp);
114 if (remove)
115 (void) unlink(sync_file);
116 }
117
118 Write(type, ship, isstr, a, b, c, d)
119 int type;
120 struct ship *ship;
121 char isstr;
122 int a, b, c, d;
123 {
124 if (isstr)
125 (void) sprintf(sync_bp, "%d %d %d %s\n",
126 type, ship->file->index, isstr, a);
127 else
128 (void) sprintf(sync_bp, "%d %d %d %d %d %d %d\n",
129 type, ship->file->index, isstr, a, b, c, d);
130 while (*sync_bp++)
131 ;
132 sync_bp--;
133 if (sync_bp >= &sync_buf[sizeof sync_buf])
134 abort();
135 (void) sync_update(type, ship, a, b, c, d);
136 }
137
138 Sync()
139 {
140 sig_t sighup, sigint;
141 register n;
142 int type, shipnum, isstr, a, b, c, d;
143 char buf[80];
144 char erred = 0;
145 extern errno;
146
147 sighup = signal(SIGHUP, SIG_IGN);
148 sigint = signal(SIGINT, SIG_IGN);
149 for (n = TIMEOUT; --n >= 0;) {
150 #ifdef LOCK_EX
151 if (flock(fileno(sync_fp), LOCK_EX|LOCK_NB) >= 0)
152 break;
153 if (errno != EWOULDBLOCK)
154 return -1;
155 #else
156 if (link(sync_file, sync_lock) >= 0)
157 break;
158 if (errno != EEXIST)
159 return -1;
160 #endif
161 sleep(1);
162 }
163 if (n <= 0)
164 return -1;
165 (void) fseek(sync_fp, sync_seek, 0);
166 for (;;) {
167 switch (fscanf(sync_fp, "%d%d%d", &type, &shipnum, &isstr)) {
168 case 3:
169 break;
170 case EOF:
171 goto out;
172 default:
173 goto bad;
174 }
175 if (shipnum < 0 || shipnum >= cc->vessels)
176 goto bad;
177 if (isstr != 0 && isstr != 1)
178 goto bad;
179 if (isstr) {
180 register char *p;
181 for (p = buf;;) {
182 switch (*p++ = getc(sync_fp)) {
183 case '\n':
184 p--;
185 case EOF:
186 break;
187 default:
188 if (p >= buf + sizeof buf)
189 p--;
190 continue;
191 }
192 break;
193 }
194 *p = 0;
195 for (p = buf; *p == ' '; p++)
196 ;
197 a = (int)p;
198 b = c = d = 0;
199 } else
200 if (fscanf(sync_fp, "%d%d%d%d", &a, &b, &c, &d) != 4)
201 goto bad;
202 if (sync_update(type, SHIP(shipnum), a, b, c, d) < 0)
203 goto bad;
204 }
205 bad:
206 erred++;
207 out:
208 if (!erred && sync_bp != sync_buf) {
209 (void) fseek(sync_fp, 0L, 2);
210 (void) fwrite(sync_buf, sizeof *sync_buf, sync_bp - sync_buf,
211 sync_fp);
212 (void) fflush(sync_fp);
213 sync_bp = sync_buf;
214 }
215 sync_seek = ftell(sync_fp);
216 #ifdef LOCK_EX
217 (void) flock(fileno(sync_fp), LOCK_UN);
218 #else
219 (void) unlink(sync_lock);
220 #endif
221 (void) signal(SIGHUP, sighup);
222 (void) signal(SIGINT, sigint);
223 return erred ? -1 : 0;
224 }
225
226 sync_update(type, ship, a, b, c, d)
227 int type;
228 register struct ship *ship;
229 int a, b, c, d;
230 {
231 switch (type) {
232 case W_DBP: {
233 register struct BP *p = &ship->file->DBP[a];
234 p->turnsent = b;
235 p->toship = SHIP(c);
236 p->mensent = d;
237 break;
238 }
239 case W_OBP: {
240 register struct BP *p = &ship->file->OBP[a];
241 p->turnsent = b;
242 p->toship = SHIP(c);
243 p->mensent = d;
244 break;
245 }
246 case W_FOUL: {
247 register struct snag *p = &ship->file->foul[a];
248 if (SHIP(a)->file->dir == 0)
249 break;
250 if (p->sn_count++ == 0)
251 p->sn_turn = turn;
252 ship->file->nfoul++;
253 break;
254 }
255 case W_GRAP: {
256 register struct snag *p = &ship->file->grap[a];
257 if (SHIP(a)->file->dir == 0)
258 break;
259 if (p->sn_count++ == 0)
260 p->sn_turn = turn;
261 ship->file->ngrap++;
262 break;
263 }
264 case W_UNFOUL: {
265 register struct snag *p = &ship->file->foul[a];
266 if (p->sn_count > 0)
267 if (b) {
268 ship->file->nfoul -= p->sn_count;
269 p->sn_count = 0;
270 } else {
271 ship->file->nfoul--;
272 p->sn_count--;
273 }
274 break;
275 }
276 case W_UNGRAP: {
277 register struct snag *p = &ship->file->grap[a];
278 if (p->sn_count > 0)
279 if (b) {
280 ship->file->ngrap -= p->sn_count;
281 p->sn_count = 0;
282 } else {
283 ship->file->ngrap--;
284 p->sn_count--;
285 }
286 break;
287 }
288 case W_SIGNAL:
289 if (mode == MODE_PLAYER)
290 if (nobells)
291 Signal("%s (%c%c): %s", ship, a);
292 else
293 Signal("\7%s (%c%c): %s", ship, a);
294 break;
295 case W_CREW: {
296 register struct shipspecs *s = ship->specs;
297 s->crew1 = a;
298 s->crew2 = b;
299 s->crew3 = c;
300 break;
301 }
302 case W_CAPTAIN:
303 (void) strncpy(ship->file->captain, (char *)a,
304 sizeof ship->file->captain - 1);
305 ship->file->captain[sizeof ship->file->captain - 1] = 0;
306 break;
307 case W_CAPTURED:
308 if (a < 0)
309 ship->file->captured = 0;
310 else
311 ship->file->captured = SHIP(a);
312 break;
313 case W_CLASS:
314 ship->specs->class = a;
315 break;
316 case W_DRIFT:
317 ship->file->drift = a;
318 break;
319 case W_EXPLODE:
320 if ((ship->file->explode = a) == 2)
321 ship->file->dir = 0;
322 break;
323 case W_FS:
324 ship->file->FS = a;
325 break;
326 case W_GUNL: {
327 register struct shipspecs *s = ship->specs;
328 s->gunL = a;
329 s->carL = b;
330 break;
331 }
332 case W_GUNR: {
333 register struct shipspecs *s = ship->specs;
334 s->gunR = a;
335 s->carR = b;
336 break;
337 }
338 case W_HULL:
339 ship->specs->hull = a;
340 break;
341 case W_MOVE:
342 (void) strncpy(ship->file->movebuf, (char *)a,
343 sizeof ship->file->movebuf - 1);
344 ship->file->movebuf[sizeof ship->file->movebuf - 1] = 0;
345 break;
346 case W_PCREW:
347 ship->file->pcrew = a;
348 break;
349 case W_POINTS:
350 ship->file->points = a;
351 break;
352 case W_QUAL:
353 ship->specs->qual = a;
354 break;
355 case W_RIGG: {
356 register struct shipspecs *s = ship->specs;
357 s->rig1 = a;
358 s->rig2 = b;
359 s->rig3 = c;
360 s->rig4 = d;
361 break;
362 }
363 case W_RIG1:
364 ship->specs->rig1 = a;
365 break;
366 case W_RIG2:
367 ship->specs->rig2 = a;
368 break;
369 case W_RIG3:
370 ship->specs->rig3 = a;
371 break;
372 case W_RIG4:
373 ship->specs->rig4 = a;
374 break;
375 case W_COL:
376 ship->file->col = a;
377 break;
378 case W_DIR:
379 ship->file->dir = a;
380 break;
381 case W_ROW:
382 ship->file->row = a;
383 break;
384 case W_SINK:
385 if ((ship->file->sink = a) == 2)
386 ship->file->dir = 0;
387 break;
388 case W_STRUCK:
389 ship->file->struck = a;
390 break;
391 case W_TA:
392 ship->specs->ta = a;
393 break;
394 case W_ALIVE:
395 alive = 1;
396 break;
397 case W_TURN:
398 turn = a;
399 break;
400 case W_WIND:
401 winddir = a;
402 windspeed = b;
403 break;
404 case W_BEGIN:
405 (void) strcpy(ship->file->captain, "begin");
406 people++;
407 break;
408 case W_END:
409 *ship->file->captain = 0;
410 ship->file->points = 0;
411 people--;
412 break;
413 case W_DDEAD:
414 hasdriver = 0;
415 break;
416 default:
417 fprintf(stderr, "sync_update: unknown type %d\r\n", type);
418 return -1;
419 }
420 return 0;
421 }