]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - sail/sync.c
1 /* $NetBSD: sync.c,v 1.17 2001/01/04 03:21:17 jwise Exp $ */
4 * Copyright (c) 1983, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 #include <sys/cdefs.h>
39 static char sccsid
[] = "@(#)sync.c 8.2 (Berkeley) 4/28/95";
41 __RCSID("$NetBSD: sync.c,v 1.17 2001/01/04 03:21:17 jwise Exp $");
53 #include "pathnames.h"
57 void fmtship(char *, size_t, const char *, struct ship
*);
58 void makesignal(struct ship
*, const char *, struct ship
*, ...);
59 void makemsg(struct ship
*, const char *, ...);
63 void Write(int, struct ship
*, long, long, long, long);
64 void Writestr(int, struct ship
*, const char *);
66 static int sync_update(int, struct ship
*, const char *, long, long, long, long);
68 static const char SF
[] = _PATH_SYNC
;
69 static const char LF
[] = _PATH_LOCK
;
70 static char sync_buf
[BUFSIZE
];
71 static char *sync_bp
= sync_buf
;
72 static char sync_lock
[sizeof SF
];
73 static char sync_file
[sizeof LF
];
74 static long sync_seek
;
78 fmtship(char *buf
, size_t len
, const char *fmt
, struct ship
*ship
)
85 if (*fmt
== '$' && fmt
[1] == '$') {
86 size_t l
= snprintf(buf
, len
, "%s (%c%c)",
87 ship
->shipname
, colours(ship
), sterncolour(ship
));
103 makesignal(struct ship
*from
, const char *fmt
, struct ship
*ship
, ...)
105 char message
[BUFSIZ
];
110 fmtship(format
, sizeof(format
), fmt
, ship
);
111 vsprintf(message
, format
, ap
);
113 Writestr(W_SIGNAL
, from
, message
);
118 makemsg(struct ship
*from
, const char *fmt
, ...)
120 char message
[BUFSIZ
];
124 vsprintf(message
, fmt
, ap
);
126 Writestr(W_SIGNAL
, from
, message
);
130 sync_exists(int game
)
132 char buf
[sizeof sync_file
];
136 sprintf(buf
, SF
, game
);
139 if (stat(buf
, &s
) < 0) {
143 if (s
.st_mtime
< t
- 60*60*2) { /* 2 hours */
145 sprintf(buf
, LF
, game
);
161 sprintf(sync_lock
, LF
, game
);
162 sprintf(sync_file
, SF
, game
);
164 if (stat(sync_file
, &tmp
) < 0) {
165 mode_t omask
= umask(002);
166 sync_fp
= fopen(sync_file
, "w+");
169 sync_fp
= fopen(sync_file
, "r+");
178 sync_close(int remove
)
190 Write(int type
, struct ship
*ship
, long a
, long b
, long c
, long d
)
193 sprintf(sync_bp
, "%d %d 0 %ld %ld %ld %ld\n",
194 type
, ship
->file
->index
, a
, b
, c
, d
);
198 if (sync_bp
>= &sync_buf
[sizeof sync_buf
])
200 sync_update(type
, ship
, NULL
, a
, b
, c
, d
);
204 Writestr(int type
, struct ship
*ship
, const char *a
)
206 sprintf(sync_bp
, "%d %d 1 %s\n", type
, ship
->file
->index
, a
);
210 if (sync_bp
>= &sync_buf
[sizeof sync_buf
])
212 sync_update(type
, ship
, a
, 0, 0, 0, 0);
218 sig_t sighup
, sigint
;
220 int type
, shipnum
, isstr
;
226 sighup
= signal(SIGHUP
, SIG_IGN
);
227 sigint
= signal(SIGINT
, SIG_IGN
);
228 for (n
= TIMEOUT
; --n
>= 0;) {
230 if (flock(fileno(sync_fp
), LOCK_EX
|LOCK_NB
) >= 0)
232 if (errno
!= EWOULDBLOCK
)
236 if (link(sync_file
, sync_lock
) >= 0) {
248 fseek(sync_fp
, sync_seek
, SEEK_SET
);
250 switch (fscanf(sync_fp
, "%d%d%d", &type
, &shipnum
, &isstr
)) {
258 if (shipnum
< 0 || shipnum
>= cc
->vessels
)
260 if (isstr
!= 0 && isstr
!= 1)
265 switch (*p
++ = getc(sync_fp
)) {
271 if (p
>= buf
+ sizeof buf
)
278 for (p
= buf
; *p
== ' '; p
++)
283 if (fscanf(sync_fp
, "%ld%ld%ld%ld", &a
, &b
, &c
, &d
) != 4)
287 if (sync_update(type
, SHIP(shipnum
), astr
, a
, b
, c
, d
) < 0)
293 if (!erred
&& sync_bp
!= sync_buf
) {
294 fseek(sync_fp
, 0L, SEEK_END
);
295 fwrite(sync_buf
, sizeof *sync_buf
, sync_bp
- sync_buf
,
300 sync_seek
= ftell(sync_fp
);
302 flock(fileno(sync_fp
), LOCK_UN
);
308 signal(SIGHUP
, sighup
);
309 signal(SIGINT
, sigint
);
310 return erred
? -1 : 0;
314 sync_update(int type
, struct ship
*ship
, const char *astr
, long a
, long b
, long c
, long d
)
318 struct BP
*p
= &ship
->file
->DBP
[a
];
325 struct BP
*p
= &ship
->file
->OBP
[a
];
332 struct snag
*p
= &ship
->file
->foul
[a
];
333 if (SHIP(a
)->file
->dir
== 0)
335 if (p
->sn_count
++ == 0)
341 struct snag
*p
= &ship
->file
->grap
[a
];
342 if (SHIP(a
)->file
->dir
== 0)
344 if (p
->sn_count
++ == 0)
350 struct snag
*p
= &ship
->file
->foul
[a
];
351 if (p
->sn_count
> 0) {
353 ship
->file
->nfoul
-= p
->sn_count
;
363 struct snag
*p
= &ship
->file
->grap
[a
];
364 if (p
->sn_count
> 0) {
366 ship
->file
->ngrap
-= p
->sn_count
;
376 if (mode
== MODE_PLAYER
) {
378 Signal("$$: %s", ship
, astr
);
380 Signal("\7$$: %s", ship
, astr
);
384 struct shipspecs
*s
= ship
->specs
;
391 strncpy(ship
->file
->captain
, astr
,
392 sizeof ship
->file
->captain
- 1);
393 ship
->file
->captain
[sizeof ship
->file
->captain
- 1] = 0;
397 ship
->file
->captured
= 0;
399 ship
->file
->captured
= SHIP(a
);
402 ship
->specs
->class = a
;
405 ship
->file
->drift
= a
;
408 if ((ship
->file
->explode
= a
) == 2)
415 struct shipspecs
*s
= ship
->specs
;
421 struct shipspecs
*s
= ship
->specs
;
427 ship
->specs
->hull
= a
;
430 strncpy(ship
->file
->movebuf
, astr
,
431 sizeof ship
->file
->movebuf
- 1);
432 ship
->file
->movebuf
[sizeof ship
->file
->movebuf
- 1] = 0;
435 ship
->file
->pcrew
= a
;
438 ship
->file
->points
= a
;
441 ship
->specs
->qual
= a
;
444 struct shipspecs
*s
= ship
->specs
;
452 ship
->specs
->rig1
= a
;
455 ship
->specs
->rig2
= a
;
458 ship
->specs
->rig3
= a
;
461 ship
->specs
->rig4
= a
;
473 if ((ship
->file
->sink
= a
) == 2)
477 ship
->file
->struck
= a
;
493 strcpy(ship
->file
->captain
, "begin");
497 *ship
->file
->captain
= 0;
498 ship
->file
->points
= 0;
505 fprintf(stderr
, "sync_update: unknown type %d\r\n", type
);