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