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>
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"
53 static int sync_update(int, struct ship
*, long, long, long, long);
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 Write(W_SIGNAL
, from
, 1, (long)message
, 0, 0, 0);
97 #include <sys/types.h>
100 sync_exists(int lgame
)
102 char buf
[sizeof sync_file
];
106 sprintf(buf
, SF
, game
);
108 if (stat(buf
, &s
) < 0)
110 if (s
.st_mtime
< t
- 60*60*2) { /* 2 hours */
112 sprintf(buf
, LF
, lgame
);
124 sprintf(sync_lock
, LF
, game
);
125 sprintf(sync_file
, SF
, game
);
126 if (access(sync_file
, 0) < 0) {
127 int omask
= umask(issetuid
? 077 : 011);
128 sync_fp
= fopen(sync_file
, "w+");
131 sync_fp
= fopen(sync_file
, "r+");
148 Write(int type
, struct ship
*ship
, char isstr
, int a
, int b
, int c
, int d
)
151 sprintf(sync_bp
, "%d %d %d %s\n",
152 type
, ship
->file
->index
, isstr
, (char *)a
);
154 sprintf(sync_bp
, "%d %d %d %d %d %d %d\n",
155 type
, ship
->file
->index
, isstr
, a
, b
, c
, d
);
159 if (sync_bp
>= &sync_buf
[sizeof sync_buf
])
161 sync_update(type
, ship
, a
, b
, c
, d
);
167 sig_t sighup
, sigint
;
169 int type
, shipnum
, isstr
, a
, b
, c
, d
;
172 sighup
= signal(SIGHUP
, SIG_IGN
);
173 sigint
= signal(SIGINT
, SIG_IGN
);
174 for (n
= TIMEOUT
; --n
>= 0;) {
176 if (flock(fileno(sync_fp
), LOCK_EX
|LOCK_NB
) >= 0)
178 if (errno
!= EWOULDBLOCK
)
181 if (link(sync_file
, sync_lock
) >= 0)
190 fseek(sync_fp
, sync_seek
, 0);
192 switch (fscanf(sync_fp
, "%d%d%d", &type
, &shipnum
, &isstr
)) {
200 if (shipnum
< 0 || shipnum
>= cc
->vessels
)
202 if (isstr
!= 0 && isstr
!= 1)
207 switch (*p
++ = getc(sync_fp
)) {
213 if (p
>= buf
+ sizeof buf
)
220 for (p
= buf
; *p
== ' '; p
++)
225 if (fscanf(sync_fp
, "%d%d%d%d", &a
, &b
, &c
, &d
) != 4)
227 if (sync_update(type
, SHIP(shipnum
), a
, b
, c
, d
) < 0)
233 if (!erred
&& sync_bp
!= sync_buf
) {
234 fseek(sync_fp
, 0L, 2);
235 fwrite(sync_buf
, sizeof *sync_buf
, sync_bp
- sync_buf
,
240 sync_seek
= ftell(sync_fp
);
242 flock(fileno(sync_fp
), LOCK_UN
);
246 signal(SIGHUP
, sighup
);
247 signal(SIGINT
, sigint
);
248 return erred
? -1 : 0;
252 sync_update(int type
, struct ship
*ship
, long a
, long b
, long c
, long d
)
256 struct BP
*p
= &ship
->file
->DBP
[a
];
263 struct BP
*p
= &ship
->file
->OBP
[a
];
270 struct snag
*p
= &ship
->file
->foul
[a
];
271 if (SHIP(a
)->file
->dir
== 0)
273 if (p
->sn_count
++ == 0)
279 struct snag
*p
= &ship
->file
->grap
[a
];
280 if (SHIP(a
)->file
->dir
== 0)
282 if (p
->sn_count
++ == 0)
288 struct snag
*p
= &ship
->file
->foul
[a
];
289 if (p
->sn_count
> 0) {
291 ship
->file
->nfoul
-= p
->sn_count
;
301 struct snag
*p
= &ship
->file
->grap
[a
];
302 if (p
->sn_count
> 0) {
304 ship
->file
->ngrap
-= p
->sn_count
;
314 if (mode
== MODE_PLAYER
) {
316 Signal("%s (%c%c): %s", ship
, a
);
318 Signal("\7%s (%c%c): %s", ship
, a
);
322 struct shipspecs
*s
= ship
->specs
;
329 strncpy(ship
->file
->captain
, (char *)a
,
330 sizeof ship
->file
->captain
- 1);
331 ship
->file
->captain
[sizeof ship
->file
->captain
- 1] = 0;
335 ship
->file
->captured
= 0;
337 ship
->file
->captured
= SHIP(a
);
340 ship
->specs
->class = a
;
343 ship
->file
->drift
= a
;
346 if ((ship
->file
->explode
= a
) == 2)
353 struct shipspecs
*s
= ship
->specs
;
359 struct shipspecs
*s
= ship
->specs
;
365 ship
->specs
->hull
= a
;
368 strncpy(ship
->file
->movebuf
, (char *)a
,
369 sizeof ship
->file
->movebuf
- 1);
370 ship
->file
->movebuf
[sizeof ship
->file
->movebuf
- 1] = 0;
373 ship
->file
->pcrew
= a
;
376 ship
->file
->points
= a
;
379 ship
->specs
->qual
= a
;
382 struct shipspecs
*s
= ship
->specs
;
390 ship
->specs
->rig1
= a
;
393 ship
->specs
->rig2
= a
;
396 ship
->specs
->rig3
= a
;
399 ship
->specs
->rig4
= a
;
411 if ((ship
->file
->sink
= a
) == 2)
415 ship
->file
->struck
= a
;
431 strcpy(ship
->file
->captain
, "begin");
435 *ship
->file
->captain
= 0;
436 ship
->file
->points
= 0;
443 fprintf(stderr
, "sync_update: unknown type %d\r\n", type
);