]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - sail/sync.c
1 /* $NetBSD: sync.c,v 1.27 2009/03/14 18:41:21 dholland 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. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
35 static char sccsid
[] = "@(#)sync.c 8.2 (Berkeley) 4/28/95";
37 __RCSID("$NetBSD: sync.c,v 1.27 2009/03/14 18:41:21 dholland Exp $");
53 #include "pathnames.h"
57 static int sync_update(int, struct ship
*, const char *, long, long, long, long);
59 static const char SF
[] = _PATH_SYNC
;
60 static const char LF
[] = _PATH_LOCK
;
61 static char sync_buf
[BUFSIZE
];
62 static char *sync_bp
= sync_buf
;
63 static char sync_lock
[sizeof SF
];
64 static char sync_file
[sizeof LF
];
65 static long sync_seek
;
69 fmtship(char *buf
, size_t len
, const char *fmt
, struct ship
*ship
)
76 if (*fmt
== '$' && fmt
[1] == '$') {
77 size_t l
= snprintf(buf
, len
, "%s (%c%c)",
78 ship
->shipname
, colours(ship
), sterncolour(ship
));
94 makesignal(struct ship
*from
, const char *fmt
, struct ship
*ship
, ...)
101 fmtship(format
, sizeof(format
), fmt
, ship
);
102 vsnprintf(message
, sizeof(message
), format
, ap
);
104 Writestr(W_SIGNAL
, from
, message
);
109 makemsg(struct ship
*from
, const char *fmt
, ...)
111 char message
[BUFSIZ
];
115 vsnprintf(message
, sizeof(message
), fmt
, ap
);
117 Writestr(W_SIGNAL
, from
, message
);
121 sync_exists(int gamenum
)
123 char buf
[sizeof sync_file
];
127 snprintf(buf
, sizeof(buf
), SF
, gamenum
);
130 if (stat(buf
, &s
) < 0) {
134 if (s
.st_mtime
< t
- 60*60*2) { /* 2 hours */
136 snprintf(buf
, sizeof(buf
), LF
, gamenum
);
152 snprintf(sync_lock
, sizeof(sync_lock
), LF
, game
);
153 snprintf(sync_file
, sizeof(sync_file
), SF
, game
);
155 if (stat(sync_file
, &tmp
) < 0) {
156 mode_t omask
= umask(002);
157 sync_fp
= fopen(sync_file
, "w+");
160 sync_fp
= fopen(sync_file
, "r+");
169 sync_close(int doremove
)
181 Write(int type
, struct ship
*ship
, long a
, long b
, long c
, long d
)
183 size_t max
= sizeof(sync_buf
) - (sync_bp
- sync_buf
);
185 snprintf(sync_bp
, max
, "%d %d 0 %ld %ld %ld %ld\n",
186 type
, ship
->file
->index
, a
, b
, c
, d
);
190 if (sync_bp
>= &sync_buf
[sizeof sync_buf
])
192 sync_update(type
, ship
, NULL
, a
, b
, c
, d
);
196 Writestr(int type
, struct ship
*ship
, const char *a
)
198 size_t max
= sizeof(sync_buf
) - (sync_bp
- sync_buf
);
200 snprintf(sync_bp
, max
, "%d %d 1 %s\n", type
, ship
->file
->index
, a
);
204 if (sync_bp
>= &sync_buf
[sizeof sync_buf
])
206 sync_update(type
, ship
, a
, 0, 0, 0, 0);
212 sig_t sighup
, sigint
;
214 int type
, shipnum
, isstr
;
220 sighup
= signal(SIGHUP
, SIG_IGN
);
221 sigint
= signal(SIGINT
, SIG_IGN
);
222 for (n
= TIMEOUT
; --n
>= 0;) {
224 if (flock(fileno(sync_fp
), LOCK_EX
|LOCK_NB
) >= 0)
226 if (errno
!= EWOULDBLOCK
)
230 if (link(sync_file
, sync_lock
) >= 0) {
242 fseek(sync_fp
, sync_seek
, SEEK_SET
);
244 switch (fscanf(sync_fp
, "%d%d%d", &type
, &shipnum
, &isstr
)) {
252 if (shipnum
< 0 || shipnum
>= cc
->vessels
)
254 if (isstr
!= 0 && isstr
!= 1)
269 if (p
>= buf
+ sizeof buf
)
276 for (p
= buf
; *p
== ' '; p
++)
281 if (fscanf(sync_fp
, "%ld%ld%ld%ld", &a
, &b
, &c
, &d
) != 4)
285 if (sync_update(type
, SHIP(shipnum
), astr
, a
, b
, c
, d
) < 0)
291 if (!erred
&& sync_bp
!= sync_buf
) {
292 fseek(sync_fp
, 0L, SEEK_END
);
293 fwrite(sync_buf
, sizeof *sync_buf
, sync_bp
- sync_buf
,
298 sync_seek
= ftell(sync_fp
);
300 flock(fileno(sync_fp
), LOCK_UN
);
306 signal(SIGHUP
, sighup
);
307 signal(SIGINT
, sigint
);
308 return erred
? -1 : 0;
312 sync_update(int type
, struct ship
*ship
, const char *astr
, long a
, long b
, long c
, long d
)
316 struct BP
*p
= &ship
->file
->DBP
[a
];
323 struct BP
*p
= &ship
->file
->OBP
[a
];
330 struct snag
*p
= &ship
->file
->foul
[a
];
331 if (SHIP(a
)->file
->dir
== 0)
333 if (p
->sn_count
++ == 0)
339 struct snag
*p
= &ship
->file
->grap
[a
];
340 if (SHIP(a
)->file
->dir
== 0)
342 if (p
->sn_count
++ == 0)
348 struct snag
*p
= &ship
->file
->foul
[a
];
349 if (p
->sn_count
> 0) {
351 ship
->file
->nfoul
-= p
->sn_count
;
361 struct snag
*p
= &ship
->file
->grap
[a
];
362 if (p
->sn_count
> 0) {
364 ship
->file
->ngrap
-= p
->sn_count
;
374 if (mode
== MODE_PLAYER
) {
376 Signal("$$: %s", ship
, astr
);
378 Signal("\7$$: %s", ship
, astr
);
382 struct shipspecs
*s
= ship
->specs
;
389 strlcpy(ship
->file
->captain
, astr
,
390 sizeof ship
->file
->captain
);
394 ship
->file
->captured
= 0;
396 ship
->file
->captured
= SHIP(a
);
399 ship
->specs
->class = a
;
402 ship
->file
->drift
= a
;
405 if ((ship
->file
->explode
= a
) == 2)
412 struct shipspecs
*s
= ship
->specs
;
418 struct shipspecs
*s
= ship
->specs
;
424 ship
->specs
->hull
= a
;
427 strlcpy(ship
->file
->movebuf
, astr
,
428 sizeof ship
->file
->movebuf
);
431 ship
->file
->pcrew
= a
;
434 ship
->file
->points
= a
;
437 ship
->specs
->qual
= a
;
440 struct shipspecs
*s
= ship
->specs
;
448 ship
->specs
->rig1
= a
;
451 ship
->specs
->rig2
= a
;
454 ship
->specs
->rig3
= a
;
457 ship
->specs
->rig4
= a
;
469 if ((ship
->file
->sink
= a
) == 2)
473 ship
->file
->struck
= a
;
489 strcpy(ship
->file
->captain
, "begin");
493 *ship
->file
->captain
= 0;
494 ship
->file
->points
= 0;
501 fprintf(stderr
, "sync_update: unknown type %d\r\n", type
);