]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - sail/sync.c
1 /* $NetBSD: sync.c,v 1.28 2009/03/14 19:35:13 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.28 2009/03/14 19:35:13 dholland Exp $");
53 #include "pathnames.h"
57 static int sync_update(int, struct ship
*, const char *,
58 long, long, long, long);
60 static const char SF
[] = _PATH_SYNC
;
61 static const char LF
[] = _PATH_LOCK
;
62 static char sync_buf
[BUFSIZE
];
63 static char *sync_bp
= sync_buf
;
64 static char sync_lock
[sizeof SF
];
65 static char sync_file
[sizeof LF
];
66 static long sync_seek
;
70 fmtship(char *buf
, size_t len
, const char *fmt
, struct ship
*ship
)
77 if (*fmt
== '$' && fmt
[1] == '$') {
78 size_t l
= snprintf(buf
, len
, "%s (%c%c)",
79 ship
->shipname
, colours(ship
), sterncolour(ship
));
95 makesignal(struct ship
*from
, const char *fmt
, struct ship
*ship
, ...)
102 fmtship(format
, sizeof(format
), fmt
, ship
);
103 vsnprintf(message
, sizeof(message
), format
, ap
);
105 Writestr(W_SIGNAL
, from
, message
);
110 makemsg(struct ship
*from
, const char *fmt
, ...)
112 char message
[BUFSIZ
];
116 vsnprintf(message
, sizeof(message
), fmt
, ap
);
118 Writestr(W_SIGNAL
, from
, message
);
122 sync_exists(int gamenum
)
124 char buf
[sizeof sync_file
];
128 snprintf(buf
, sizeof(buf
), SF
, gamenum
);
131 if (stat(buf
, &s
) < 0) {
135 if (s
.st_mtime
< t
- 60*60*2) { /* 2 hours */
137 snprintf(buf
, sizeof(buf
), LF
, gamenum
);
153 snprintf(sync_lock
, sizeof(sync_lock
), LF
, game
);
154 snprintf(sync_file
, sizeof(sync_file
), SF
, game
);
156 if (stat(sync_file
, &tmp
) < 0) {
157 mode_t omask
= umask(002);
158 sync_fp
= fopen(sync_file
, "w+");
161 sync_fp
= fopen(sync_file
, "r+");
170 sync_close(int doremove
)
182 Write(int type
, struct ship
*ship
, long a
, long b
, long c
, long d
)
184 size_t max
= sizeof(sync_buf
) - (sync_bp
- sync_buf
);
186 snprintf(sync_bp
, max
, "%d %d 0 %ld %ld %ld %ld\n",
187 type
, ship
->file
->index
, a
, b
, c
, d
);
191 if (sync_bp
>= &sync_buf
[sizeof sync_buf
])
193 sync_update(type
, ship
, NULL
, a
, b
, c
, d
);
197 Writestr(int type
, struct ship
*ship
, const char *a
)
199 size_t max
= sizeof(sync_buf
) - (sync_bp
- sync_buf
);
201 snprintf(sync_bp
, max
, "%d %d 1 %s\n", type
, ship
->file
->index
, a
);
205 if (sync_bp
>= &sync_buf
[sizeof sync_buf
])
207 sync_update(type
, ship
, a
, 0, 0, 0, 0);
213 sig_t sighup
, sigint
;
215 int type
, shipnum
, isstr
;
221 sighup
= signal(SIGHUP
, SIG_IGN
);
222 sigint
= signal(SIGINT
, SIG_IGN
);
223 for (n
= TIMEOUT
; --n
>= 0;) {
225 if (flock(fileno(sync_fp
), LOCK_EX
|LOCK_NB
) >= 0)
227 if (errno
!= EWOULDBLOCK
)
231 if (link(sync_file
, sync_lock
) >= 0) {
243 fseek(sync_fp
, sync_seek
, SEEK_SET
);
245 switch (fscanf(sync_fp
, "%d%d%d", &type
, &shipnum
, &isstr
)) {
253 if (shipnum
< 0 || shipnum
>= cc
->vessels
)
255 if (isstr
!= 0 && isstr
!= 1)
270 if (p
>= buf
+ sizeof buf
)
277 for (p
= buf
; *p
== ' '; p
++)
282 if (fscanf(sync_fp
, "%ld%ld%ld%ld", &a
, &b
, &c
, &d
)
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
,
315 long a
, long b
, long c
, long d
)
319 struct BP
*p
= &ship
->file
->DBP
[a
];
326 struct BP
*p
= &ship
->file
->OBP
[a
];
333 struct snag
*p
= &ship
->file
->foul
[a
];
334 if (SHIP(a
)->file
->dir
== 0)
336 if (p
->sn_count
++ == 0)
342 struct snag
*p
= &ship
->file
->grap
[a
];
343 if (SHIP(a
)->file
->dir
== 0)
345 if (p
->sn_count
++ == 0)
351 struct snag
*p
= &ship
->file
->foul
[a
];
352 if (p
->sn_count
> 0) {
354 ship
->file
->nfoul
-= p
->sn_count
;
364 struct snag
*p
= &ship
->file
->grap
[a
];
365 if (p
->sn_count
> 0) {
367 ship
->file
->ngrap
-= p
->sn_count
;
377 if (mode
== MODE_PLAYER
) {
379 Signal("$$: %s", ship
, astr
);
381 Signal("\7$$: %s", ship
, astr
);
385 struct shipspecs
*s
= ship
->specs
;
392 strlcpy(ship
->file
->captain
, astr
,
393 sizeof ship
->file
->captain
);
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 strlcpy(ship
->file
->movebuf
, astr
,
431 sizeof ship
->file
->movebuf
);
434 ship
->file
->pcrew
= a
;
437 ship
->file
->points
= a
;
440 ship
->specs
->qual
= a
;
443 struct shipspecs
*s
= ship
->specs
;
451 ship
->specs
->rig1
= a
;
454 ship
->specs
->rig2
= a
;
457 ship
->specs
->rig3
= a
;
460 ship
->specs
->rig4
= a
;
472 if ((ship
->file
->sink
= a
) == 2)
476 ship
->file
->struck
= a
;
492 strcpy(ship
->file
->captain
, "begin");
496 *ship
->file
->captain
= 0;
497 ship
->file
->points
= 0;
504 fprintf(stderr
, "sync_update: unknown type %d\r\n", type
);