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. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#)sync.c 8.1 (Berkeley) 5/31/93
30 * $FreeBSD: src/games/sail/sync.c,v 1.9 1999/11/30 03:49:38 billf Exp $
31 * $DragonFly: src/games/sail/sync.c,v 1.4 2006/09/03 17:33:13 pavalos Exp $
35 #include <sys/errno.h>
36 #include <sys/types.h>
42 static int sync_update(int, struct ship *, const char *,
43 long, long, long, long);
45 static char sync_buf[BUFSIZE];
46 static char *sync_bp = sync_buf;
47 static char sync_lock[25];
48 static char sync_file[25];
49 static long sync_seek;
51 #define SF "/tmp/#sailsink.%d"
52 #define LF "/tmp/#saillock.%d"
56 fmtship(char *buf, size_t len, const char *fmt, struct ship *ship)
63 if (*fmt == '$' && fmt[1] == '$') {
64 size_t l = snprintf(buf, len, "%s (%c%c)",
65 ship->shipname, colours(ship), sterncolour(ship));
80 makesignal(struct ship *from, const char *fmt, struct ship *ship, ...)
88 vsprintf(message, fmt, ap);
90 fmtship(format, sizeof(format), fmt, ship);
91 vsprintf(message, format, ap);
94 Writestr(W_SIGNAL, from, message);
98 sync_exists(int lgame)
100 char buf[sizeof sync_file];
104 sprintf(buf, SF, game);
106 if (stat(buf, &s) < 0)
108 if (s.st_mtime < t - 60*60*2) { /* 2 hours */
110 sprintf(buf, LF, lgame);
122 sprintf(sync_lock, LF, game);
123 sprintf(sync_file, SF, game);
124 if (access(sync_file, 0) < 0) {
125 int omask = umask(issetuid ? 077 : 011);
126 sync_fp = fopen(sync_file, "w+");
129 sync_fp = fopen(sync_file, "r+");
146 Write(int type, struct ship *ship, int a, int b, int c, int d)
148 sprintf(sync_bp, "%d %d 0 %d %d %d %d\n",
149 type, ship->file->index, a, b, c, d);
153 if (sync_bp >= &sync_buf[sizeof sync_buf])
155 sync_update(type, ship, NULL, a, b, c, d);
159 Writestr(int type, struct ship *ship, const char *a)
161 sprintf(sync_bp, "%d %d 1 %s\n", type, ship->file->index, a);
165 if (sync_bp >= &sync_buf[sizeof sync_buf])
167 sync_update(type, ship, a, 0, 0, 0, 0);
173 sig_t sighup, sigint;
175 int type, shipnum, isstr, a, b, c, d;
179 sighup = signal(SIGHUP, SIG_IGN);
180 sigint = signal(SIGINT, SIG_IGN);
181 for (n = TIMEOUT; --n >= 0;) {
183 if (flock(fileno(sync_fp), LOCK_EX|LOCK_NB) >= 0)
185 if (errno != EWOULDBLOCK)
188 if (link(sync_file, sync_lock) >= 0)
197 fseek(sync_fp, sync_seek, SEEK_SET);
199 switch (fscanf(sync_fp, "%d%d%d", &type, &shipnum, &isstr)) {
207 if (shipnum < 0 || shipnum >= cc->vessels)
209 if (isstr != 0 && isstr != 1)
214 switch (*p++ = getc(sync_fp)) {
220 if (p >= buf + sizeof buf)
227 for (p = buf; *p == ' '; p++)
232 if (fscanf(sync_fp, "%d%d%d%d", &a, &b, &c, &d) != 4)
236 if (sync_update(type, SHIP(shipnum), astr, a, b, c, d) < 0)
242 if (!erred && sync_bp != sync_buf) {
243 fseek(sync_fp, 0L, SEEK_END);
244 fwrite(sync_buf, sizeof *sync_buf, sync_bp - sync_buf,
249 sync_seek = ftell(sync_fp);
251 flock(fileno(sync_fp), LOCK_UN);
255 signal(SIGHUP, sighup);
256 signal(SIGINT, sigint);
257 return erred ? -1 : 0;
261 sync_update(int type, struct ship *ship, const char *astr, long a, long b,
266 struct BP *p = &ship->file->DBP[a];
273 struct BP *p = &ship->file->OBP[a];
280 struct snag *p = &ship->file->foul[a];
281 if (SHIP(a)->file->dir == 0)
283 if (p->sn_count++ == 0)
289 struct snag *p = &ship->file->grap[a];
290 if (SHIP(a)->file->dir == 0)
292 if (p->sn_count++ == 0)
298 struct snag *p = &ship->file->foul[a];
299 if (p->sn_count > 0) {
301 ship->file->nfoul -= p->sn_count;
311 struct snag *p = &ship->file->grap[a];
312 if (p->sn_count > 0) {
314 ship->file->ngrap -= p->sn_count;
324 if (mode == MODE_PLAYER) {
326 Signal("%s (%c%c): %s", ship, astr);
328 Signal("\7%s (%c%c): %s", ship, astr);
332 struct shipspecs *s = ship->specs;
339 strncpy(ship->file->captain, astr,
340 sizeof ship->file->captain - 1);
341 ship->file->captain[sizeof ship->file->captain - 1] = 0;
345 ship->file->captured = 0;
347 ship->file->captured = SHIP(a);
350 ship->specs->class = a;
353 ship->file->drift = a;
356 if ((ship->file->explode = a) == 2)
363 struct shipspecs *s = ship->specs;
369 struct shipspecs *s = ship->specs;
375 ship->specs->hull = a;
378 strncpy(ship->file->movebuf, astr,
379 sizeof ship->file->movebuf - 1);
380 ship->file->movebuf[sizeof ship->file->movebuf - 1] = 0;
383 ship->file->pcrew = a;
386 ship->file->points = a;
389 ship->specs->qual = a;
392 struct shipspecs *s = ship->specs;
400 ship->specs->rig1 = a;
403 ship->specs->rig2 = a;
406 ship->specs->rig3 = a;
409 ship->specs->rig4 = a;
421 if ((ship->file->sink = a) == 2)
425 ship->file->struck = a;
441 strcpy(ship->file->captain, "begin");
445 *ship->file->captain = 0;
446 ship->file->points = 0;
453 fprintf(stderr, "sync_update: unknown type %d\r\n", type);