2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
36 static char sccsid[] = "@(#)sync.c 8.1 (Berkeley) 5/31/93";
38 static const char rcsid[] =
39 "$FreeBSD: src/games/sail/sync.c,v 1.9 1999/11/30 03:49:38 billf Exp $";
44 #include <sys/errno.h>
48 static char sync_buf[BUFSIZE];
49 static char *sync_bp = sync_buf;
50 static char sync_lock[25];
51 static char sync_file[25];
52 static long sync_seek;
54 #define SF "/tmp/#sailsink.%d"
55 #define LF "/tmp/#saillock.%d"
58 makesignal(from, fmt, ship, a, b, c)
66 (void) sprintf(message, fmt, a, b, c);
68 (void) sprintf(message, fmt,
69 ship->shipname, colours(ship),
70 sterncolour(ship), a, b, c);
71 Write(W_SIGNAL, from, 1, (long)message, 0, 0, 0);
74 #include <sys/types.h>
79 char buf[sizeof sync_file];
83 (void) sprintf(buf, SF, game);
85 if (stat(buf, &s) < 0)
87 if (s.st_mtime < t - 60*60*2) { /* 2 hours */
89 (void) sprintf(buf, LF, game);
99 (void) fclose(sync_fp);
100 (void) sprintf(sync_lock, LF, game);
101 (void) sprintf(sync_file, SF, game);
102 if (access(sync_file, 0) < 0) {
103 int omask = umask(issetuid ? 077 : 011);
104 sync_fp = fopen(sync_file, "w+");
107 sync_fp = fopen(sync_file, "r+");
118 (void) fclose(sync_fp);
120 (void) unlink(sync_file);
123 Write(type, ship, isstr, a, b, c, d)
130 (void) sprintf(sync_bp, "%d %d %d %s\n",
131 type, ship->file->index, isstr, (char *)a);
133 (void) sprintf(sync_bp, "%d %d %d %d %d %d %d\n",
134 type, ship->file->index, isstr, a, b, c, d);
138 if (sync_bp >= &sync_buf[sizeof sync_buf])
140 (void) sync_update(type, ship, a, b, c, d);
145 sig_t sighup, sigint;
147 int type, shipnum, isstr, a, b, c, d;
152 sighup = signal(SIGHUP, SIG_IGN);
153 sigint = signal(SIGINT, SIG_IGN);
154 for (n = TIMEOUT; --n >= 0;) {
156 if (flock(fileno(sync_fp), LOCK_EX|LOCK_NB) >= 0)
158 if (errno != EWOULDBLOCK)
161 if (link(sync_file, sync_lock) >= 0)
170 (void) fseek(sync_fp, sync_seek, 0);
172 switch (fscanf(sync_fp, "%d%d%d", &type, &shipnum, &isstr)) {
180 if (shipnum < 0 || shipnum >= cc->vessels)
182 if (isstr != 0 && isstr != 1)
187 switch (*p++ = getc(sync_fp)) {
193 if (p >= buf + sizeof buf)
200 for (p = buf; *p == ' '; p++)
205 if (fscanf(sync_fp, "%d%d%d%d", &a, &b, &c, &d) != 4)
207 if (sync_update(type, SHIP(shipnum), a, b, c, d) < 0)
213 if (!erred && sync_bp != sync_buf) {
214 (void) fseek(sync_fp, 0L, 2);
215 (void) fwrite(sync_buf, sizeof *sync_buf, sync_bp - sync_buf,
217 (void) fflush(sync_fp);
220 sync_seek = ftell(sync_fp);
222 (void) flock(fileno(sync_fp), LOCK_UN);
224 (void) unlink(sync_lock);
226 (void) signal(SIGHUP, sighup);
227 (void) signal(SIGINT, sigint);
228 return erred ? -1 : 0;
231 sync_update(type, ship, a, b, c, d)
238 struct BP *p = &ship->file->DBP[a];
245 struct BP *p = &ship->file->OBP[a];
252 struct snag *p = &ship->file->foul[a];
253 if (SHIP(a)->file->dir == 0)
255 if (p->sn_count++ == 0)
261 struct snag *p = &ship->file->grap[a];
262 if (SHIP(a)->file->dir == 0)
264 if (p->sn_count++ == 0)
270 struct snag *p = &ship->file->foul[a];
271 if (p->sn_count > 0) {
273 ship->file->nfoul -= p->sn_count;
283 struct snag *p = &ship->file->grap[a];
284 if (p->sn_count > 0) {
286 ship->file->ngrap -= p->sn_count;
296 if (mode == MODE_PLAYER) {
298 Signal("%s (%c%c): %s", ship, a);
300 Signal("\7%s (%c%c): %s", ship, a);
304 struct shipspecs *s = ship->specs;
311 (void) strncpy(ship->file->captain, (char *)a,
312 sizeof ship->file->captain - 1);
313 ship->file->captain[sizeof ship->file->captain - 1] = 0;
317 ship->file->captured = 0;
319 ship->file->captured = SHIP(a);
322 ship->specs->class = a;
325 ship->file->drift = a;
328 if ((ship->file->explode = a) == 2)
335 struct shipspecs *s = ship->specs;
341 struct shipspecs *s = ship->specs;
347 ship->specs->hull = a;
350 (void) strncpy(ship->file->movebuf, (char *)a,
351 sizeof ship->file->movebuf - 1);
352 ship->file->movebuf[sizeof ship->file->movebuf - 1] = 0;
355 ship->file->pcrew = a;
358 ship->file->points = a;
361 ship->specs->qual = a;
364 struct shipspecs *s = ship->specs;
372 ship->specs->rig1 = a;
375 ship->specs->rig2 = a;
378 ship->specs->rig3 = a;
381 ship->specs->rig4 = a;
393 if ((ship->file->sink = a) == 2)
397 ship->file->struck = a;
413 (void) strcpy(ship->file->captain, "begin");
417 *ship->file->captain = 0;
418 ship->file->points = 0;
425 fprintf(stderr, "sync_update: unknown type %d\r\n", type);