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
33 * @(#)sync.c 8.1 (Berkeley) 5/31/93
34 * $FreeBSD: src/games/sail/sync.c,v 1.9 1999/11/30 03:49:38 billf Exp $
35 * $DragonFly: src/games/sail/sync.c,v 1.4 2006/09/03 17:33:13 pavalos Exp $
40 #include <sys/errno.h>
41 #include <sys/types.h>
46 static char sync_buf[BUFSIZE];
47 static char *sync_bp = sync_buf;
48 static char sync_lock[25];
49 static char sync_file[25];
50 static long sync_seek;
52 #define SF "/tmp/#sailsink.%d"
53 #define LF "/tmp/#saillock.%d"
55 static int sync_update(int, struct ship *, const char *, long, long, long,
59 fmtship(char *buf, size_t len, const char *fmt, struct ship *ship)
66 if (*fmt == '$' && fmt[1] == '$') {
67 size_t l = snprintf(buf, len, "%s (%c%c)",
68 ship->shipname, colours(ship), sterncolour(ship));
83 makesignal(struct ship *from, const char *fmt, struct ship *ship, ...)
91 vsprintf(message, fmt, ap);
93 fmtship(format, sizeof(format), fmt, ship);
94 vsprintf(message, format, ap);
97 Writestr(W_SIGNAL, from, message);
101 sync_exists(int lgame)
103 char buf[sizeof sync_file];
107 sprintf(buf, SF, game);
109 if (stat(buf, &s) < 0)
111 if (s.st_mtime < t - 60*60*2) { /* 2 hours */
113 sprintf(buf, LF, lgame);
125 sprintf(sync_lock, LF, game);
126 sprintf(sync_file, SF, game);
127 if (access(sync_file, 0) < 0) {
128 int omask = umask(issetuid ? 077 : 011);
129 sync_fp = fopen(sync_file, "w+");
132 sync_fp = fopen(sync_file, "r+");
149 Write(int type, struct ship *ship, int a, int b, int c, int d)
151 sprintf(sync_bp, "%d %d 0 %d %d %d %d\n",
152 type, ship->file->index, a, b, c, d);
156 if (sync_bp >= &sync_buf[sizeof sync_buf])
158 sync_update(type, ship, NULL, a, b, c, d);
162 Writestr(int type, struct ship *ship, const char *a)
164 sprintf(sync_bp, "%d %d 1 %s\n", type, ship->file->index, a);
168 if (sync_bp >= &sync_buf[sizeof sync_buf])
170 sync_update(type, ship, a, 0, 0, 0, 0);
176 sig_t sighup, sigint;
178 int type, shipnum, isstr, a, b, c, d;
182 sighup = signal(SIGHUP, SIG_IGN);
183 sigint = signal(SIGINT, SIG_IGN);
184 for (n = TIMEOUT; --n >= 0;) {
186 if (flock(fileno(sync_fp), LOCK_EX|LOCK_NB) >= 0)
188 if (errno != EWOULDBLOCK)
191 if (link(sync_file, sync_lock) >= 0)
200 fseek(sync_fp, sync_seek, 0);
202 switch (fscanf(sync_fp, "%d%d%d", &type, &shipnum, &isstr)) {
210 if (shipnum < 0 || shipnum >= cc->vessels)
212 if (isstr != 0 && isstr != 1)
217 switch (*p++ = getc(sync_fp)) {
223 if (p >= buf + sizeof buf)
230 for (p = buf; *p == ' '; p++)
235 if (fscanf(sync_fp, "%d%d%d%d", &a, &b, &c, &d) != 4)
239 if (sync_update(type, SHIP(shipnum), astr, a, b, c, d) < 0)
245 if (!erred && sync_bp != sync_buf) {
246 fseek(sync_fp, 0L, SEEK_END);
247 fwrite(sync_buf, sizeof *sync_buf, sync_bp - sync_buf,
252 sync_seek = ftell(sync_fp);
254 flock(fileno(sync_fp), LOCK_UN);
258 signal(SIGHUP, sighup);
259 signal(SIGINT, sigint);
260 return erred ? -1 : 0;
264 sync_update(int type, struct ship *ship, const char *astr, long a, long b,
269 struct BP *p = &ship->file->DBP[a];
276 struct BP *p = &ship->file->OBP[a];
283 struct snag *p = &ship->file->foul[a];
284 if (SHIP(a)->file->dir == 0)
286 if (p->sn_count++ == 0)
292 struct snag *p = &ship->file->grap[a];
293 if (SHIP(a)->file->dir == 0)
295 if (p->sn_count++ == 0)
301 struct snag *p = &ship->file->foul[a];
302 if (p->sn_count > 0) {
304 ship->file->nfoul -= p->sn_count;
314 struct snag *p = &ship->file->grap[a];
315 if (p->sn_count > 0) {
317 ship->file->ngrap -= p->sn_count;
327 if (mode == MODE_PLAYER) {
329 Signal("%s (%c%c): %s", ship, astr);
331 Signal("\7%s (%c%c): %s", ship, astr);
335 struct shipspecs *s = ship->specs;
342 strncpy(ship->file->captain, astr,
343 sizeof ship->file->captain - 1);
344 ship->file->captain[sizeof ship->file->captain - 1] = 0;
348 ship->file->captured = 0;
350 ship->file->captured = SHIP(a);
353 ship->specs->class = a;
356 ship->file->drift = a;
359 if ((ship->file->explode = a) == 2)
366 struct shipspecs *s = ship->specs;
372 struct shipspecs *s = ship->specs;
378 ship->specs->hull = a;
381 strncpy(ship->file->movebuf, astr,
382 sizeof ship->file->movebuf - 1);
383 ship->file->movebuf[sizeof ship->file->movebuf - 1] = 0;
386 ship->file->pcrew = a;
389 ship->file->points = a;
392 ship->specs->qual = a;
395 struct shipspecs *s = ship->specs;
403 ship->specs->rig1 = a;
406 ship->specs->rig2 = a;
409 ship->specs->rig3 = a;
412 ship->specs->rig4 = a;
424 if ((ship->file->sink = a) == 2)
428 ship->file->struck = a;
444 strcpy(ship->file->captain, "begin");
448 *ship->file->captain = 0;
449 ship->file->points = 0;
456 fprintf(stderr, "sync_update: unknown type %d\r\n", type);