]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - sail/sync.c
1 /* $NetBSD: sync.c,v 1.23 2004/09/07 13:20:39 jrf 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.23 2004/09/07 13:20:39 jrf 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 vsprintf(message
, format
, ap
);
104 Writestr(W_SIGNAL
, from
, message
);
109 makemsg(struct ship
*from
, const char *fmt
, ...)
111 char message
[BUFSIZ
];
115 vsprintf(message
, fmt
, ap
);
117 Writestr(W_SIGNAL
, from
, message
);
121 sync_exists(int game
)
123 char buf
[sizeof sync_file
];
127 sprintf(buf
, SF
, game
);
130 if (stat(buf
, &s
) < 0) {
134 if (s
.st_mtime
< t
- 60*60*2) { /* 2 hours */
136 sprintf(buf
, LF
, game
);
152 sprintf(sync_lock
, LF
, game
);
153 sprintf(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 remove
)
181 Write(int type
, struct ship
*ship
, long a
, long b
, long c
, long d
)
184 sprintf(sync_bp
, "%d %d 0 %ld %ld %ld %ld\n",
185 type
, ship
->file
->index
, a
, b
, c
, d
);
189 if (sync_bp
>= &sync_buf
[sizeof sync_buf
])
191 sync_update(type
, ship
, NULL
, a
, b
, c
, d
);
195 Writestr(int type
, struct ship
*ship
, const char *a
)
197 sprintf(sync_bp
, "%d %d 1 %s\n", type
, ship
->file
->index
, a
);
201 if (sync_bp
>= &sync_buf
[sizeof sync_buf
])
203 sync_update(type
, ship
, a
, 0, 0, 0, 0);
209 sig_t sighup
, sigint
;
211 int type
, shipnum
, isstr
;
217 sighup
= signal(SIGHUP
, SIG_IGN
);
218 sigint
= signal(SIGINT
, SIG_IGN
);
219 for (n
= TIMEOUT
; --n
>= 0;) {
221 if (flock(fileno(sync_fp
), LOCK_EX
|LOCK_NB
) >= 0)
223 if (errno
!= EWOULDBLOCK
)
227 if (link(sync_file
, sync_lock
) >= 0) {
239 fseek(sync_fp
, sync_seek
, SEEK_SET
);
241 switch (fscanf(sync_fp
, "%d%d%d", &type
, &shipnum
, &isstr
)) {
249 if (shipnum
< 0 || shipnum
>= cc
->vessels
)
251 if (isstr
!= 0 && isstr
!= 1)
256 switch (*p
++ = getc(sync_fp
)) {
262 if (p
>= buf
+ sizeof buf
)
269 for (p
= buf
; *p
== ' '; p
++)
274 if (fscanf(sync_fp
, "%ld%ld%ld%ld", &a
, &b
, &c
, &d
) != 4)
278 if (sync_update(type
, SHIP(shipnum
), astr
, a
, b
, c
, d
) < 0)
284 if (!erred
&& sync_bp
!= sync_buf
) {
285 fseek(sync_fp
, 0L, SEEK_END
);
286 fwrite(sync_buf
, sizeof *sync_buf
, sync_bp
- sync_buf
,
291 sync_seek
= ftell(sync_fp
);
293 flock(fileno(sync_fp
), LOCK_UN
);
299 signal(SIGHUP
, sighup
);
300 signal(SIGINT
, sigint
);
301 return erred
? -1 : 0;
305 sync_update(int type
, struct ship
*ship
, const char *astr
, long a
, long b
, long c
, long d
)
309 struct BP
*p
= &ship
->file
->DBP
[a
];
316 struct BP
*p
= &ship
->file
->OBP
[a
];
323 struct snag
*p
= &ship
->file
->foul
[a
];
324 if (SHIP(a
)->file
->dir
== 0)
326 if (p
->sn_count
++ == 0)
332 struct snag
*p
= &ship
->file
->grap
[a
];
333 if (SHIP(a
)->file
->dir
== 0)
335 if (p
->sn_count
++ == 0)
341 struct snag
*p
= &ship
->file
->foul
[a
];
342 if (p
->sn_count
> 0) {
344 ship
->file
->nfoul
-= p
->sn_count
;
354 struct snag
*p
= &ship
->file
->grap
[a
];
355 if (p
->sn_count
> 0) {
357 ship
->file
->ngrap
-= p
->sn_count
;
367 if (mode
== MODE_PLAYER
) {
369 Signal("$$: %s", ship
, astr
);
371 Signal("\7$$: %s", ship
, astr
);
375 struct shipspecs
*s
= ship
->specs
;
382 strlcpy(ship
->file
->captain
, astr
,
383 sizeof ship
->file
->captain
);
387 ship
->file
->captured
= 0;
389 ship
->file
->captured
= SHIP(a
);
392 ship
->specs
->class = a
;
395 ship
->file
->drift
= a
;
398 if ((ship
->file
->explode
= a
) == 2)
405 struct shipspecs
*s
= ship
->specs
;
411 struct shipspecs
*s
= ship
->specs
;
417 ship
->specs
->hull
= a
;
420 strlcpy(ship
->file
->movebuf
, astr
,
421 sizeof ship
->file
->movebuf
);
424 ship
->file
->pcrew
= a
;
427 ship
->file
->points
= a
;
430 ship
->specs
->qual
= a
;
433 struct shipspecs
*s
= ship
->specs
;
441 ship
->specs
->rig1
= a
;
444 ship
->specs
->rig2
= a
;
447 ship
->specs
->rig3
= a
;
450 ship
->specs
->rig4
= a
;
462 if ((ship
->file
->sink
= a
) == 2)
466 ship
->file
->struck
= a
;
482 strcpy(ship
->file
->captain
, "begin");
486 *ship
->file
->captain
= 0;
487 ship
->file
->points
= 0;
494 fprintf(stderr
, "sync_update: unknown type %d\r\n", type
);