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 $
34 #include <sys/errno.h>
35 #include <sys/types.h>
41 static int sync_update(int, struct ship *, const char *,
42 long, long, long, long);
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;
50 #define SF "/tmp/#sailsink.%d"
51 #define LF "/tmp/#saillock.%d"
55 fmtship(char *buf, size_t len, const char *fmt, struct ship *ship)
62 if (*fmt == '$' && fmt[1] == '$') {
63 size_t l = snprintf(buf, len, "%s (%c%c)",
64 ship->shipname, colours(ship), sterncolour(ship));
79 makesignal(struct ship *from, const char *fmt, struct ship *ship, ...)
87 vsprintf(message, fmt, ap);
89 fmtship(format, sizeof(format), fmt, ship);
90 vsprintf(message, format, ap);
93 Writestr(W_SIGNAL, from, message);
97 sync_exists(int lgame)
99 char buf[sizeof sync_file];
103 sprintf(buf, SF, game);
105 if (stat(buf, &s) < 0)
107 if (s.st_mtime < t - 60*60*2) { /* 2 hours */
109 sprintf(buf, LF, lgame);
121 sprintf(sync_lock, LF, game);
122 sprintf(sync_file, SF, game);
123 if (access(sync_file, 0) < 0) {
124 int omask = umask(issetuid ? 077 : 011);
125 sync_fp = fopen(sync_file, "w+");
128 sync_fp = fopen(sync_file, "r+");
145 Write(int type, struct ship *ship, int a, int b, int c, int d)
147 sprintf(sync_bp, "%d %d 0 %d %d %d %d\n",
148 type, ship->file->index, a, b, c, d);
152 if (sync_bp >= &sync_buf[sizeof sync_buf])
154 sync_update(type, ship, NULL, a, b, c, d);
158 Writestr(int type, struct ship *ship, const char *a)
160 sprintf(sync_bp, "%d %d 1 %s\n", type, ship->file->index, a);
164 if (sync_bp >= &sync_buf[sizeof sync_buf])
166 sync_update(type, ship, a, 0, 0, 0, 0);
172 sig_t sighup, sigint;
174 int type, shipnum, isstr, a, b, c, d;
178 sighup = signal(SIGHUP, SIG_IGN);
179 sigint = signal(SIGINT, SIG_IGN);
180 for (n = TIMEOUT; --n >= 0;) {
182 if (flock(fileno(sync_fp), LOCK_EX|LOCK_NB) >= 0)
184 if (errno != EWOULDBLOCK)
187 if (link(sync_file, sync_lock) >= 0)
196 fseek(sync_fp, sync_seek, SEEK_SET);
198 switch (fscanf(sync_fp, "%d%d%d", &type, &shipnum, &isstr)) {
206 if (shipnum < 0 || shipnum >= cc->vessels)
208 if (isstr != 0 && isstr != 1)
213 switch (*p++ = getc(sync_fp)) {
219 if (p >= buf + sizeof buf)
226 for (p = buf; *p == ' '; p++)
231 if (fscanf(sync_fp, "%d%d%d%d", &a, &b, &c, &d) != 4)
235 if (sync_update(type, SHIP(shipnum), astr, a, b, c, d) < 0)
241 if (!erred && sync_bp != sync_buf) {
242 fseek(sync_fp, 0L, SEEK_END);
243 fwrite(sync_buf, sizeof *sync_buf, sync_bp - sync_buf,
248 sync_seek = ftell(sync_fp);
250 flock(fileno(sync_fp), LOCK_UN);
254 signal(SIGHUP, sighup);
255 signal(SIGINT, sigint);
256 return erred ? -1 : 0;
260 sync_update(int type, struct ship *ship, const char *astr, long a, long b,
265 struct BP *p = &ship->file->DBP[a];
272 struct BP *p = &ship->file->OBP[a];
279 struct snag *p = &ship->file->foul[a];
280 if (SHIP(a)->file->dir == 0)
282 if (p->sn_count++ == 0)
288 struct snag *p = &ship->file->grap[a];
289 if (SHIP(a)->file->dir == 0)
291 if (p->sn_count++ == 0)
297 struct snag *p = &ship->file->foul[a];
298 if (p->sn_count > 0) {
300 ship->file->nfoul -= p->sn_count;
310 struct snag *p = &ship->file->grap[a];
311 if (p->sn_count > 0) {
313 ship->file->ngrap -= p->sn_count;
323 if (mode == MODE_PLAYER) {
325 Signal("%s (%c%c): %s", ship, astr);
327 Signal("\7%s (%c%c): %s", ship, astr);
331 struct shipspecs *s = ship->specs;
338 strncpy(ship->file->captain, astr,
339 sizeof ship->file->captain - 1);
340 ship->file->captain[sizeof ship->file->captain - 1] = 0;
344 ship->file->captured = 0;
346 ship->file->captured = SHIP(a);
349 ship->specs->class = a;
352 ship->file->drift = a;
355 if ((ship->file->explode = a) == 2)
362 struct shipspecs *s = ship->specs;
368 struct shipspecs *s = ship->specs;
374 ship->specs->hull = a;
377 strncpy(ship->file->movebuf, astr,
378 sizeof ship->file->movebuf - 1);
379 ship->file->movebuf[sizeof ship->file->movebuf - 1] = 0;
382 ship->file->pcrew = a;
385 ship->file->points = a;
388 ship->specs->qual = a;
391 struct shipspecs *s = ship->specs;
399 ship->specs->rig1 = a;
402 ship->specs->rig2 = a;
405 ship->specs->rig3 = a;
408 ship->specs->rig4 = a;
420 if ((ship->file->sink = a) == 2)
424 ship->file->struck = a;
440 strcpy(ship->file->captain, "begin");
444 *ship->file->captain = 0;
445 ship->file->points = 0;
452 fprintf(stderr, "sync_update: unknown type %d\r\n", type);