]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - sail/sync.c
1 /* $NetBSD: sync.c,v 1.20 2001/02/05 01:10:11 christos 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.20 2001/02/05 01:10:11 christos Exp $");
55 #include "pathnames.h"
59 static int sync_update(int, struct ship
*, const char *, long, long, long, long);
61 static const char SF
[] = _PATH_SYNC
;
62 static const char LF
[] = _PATH_LOCK
;
63 static char sync_buf
[BUFSIZE
];
64 static char *sync_bp
= sync_buf
;
65 static char sync_lock
[sizeof SF
];
66 static char sync_file
[sizeof LF
];
67 static long sync_seek
;
71 fmtship(char *buf
, size_t len
, const char *fmt
, struct ship
*ship
)
78 if (*fmt
== '$' && fmt
[1] == '$') {
79 size_t l
= snprintf(buf
, len
, "%s (%c%c)",
80 ship
->shipname
, colours(ship
), sterncolour(ship
));
96 makesignal(struct ship
*from
, const char *fmt
, struct ship
*ship
, ...)
103 fmtship(format
, sizeof(format
), fmt
, ship
);
104 vsprintf(message
, format
, ap
);
106 Writestr(W_SIGNAL
, from
, message
);
111 makemsg(struct ship
*from
, const char *fmt
, ...)
113 char message
[BUFSIZ
];
117 vsprintf(message
, fmt
, ap
);
119 Writestr(W_SIGNAL
, from
, message
);
123 sync_exists(int game
)
125 char buf
[sizeof sync_file
];
129 sprintf(buf
, SF
, game
);
132 if (stat(buf
, &s
) < 0) {
136 if (s
.st_mtime
< t
- 60*60*2) { /* 2 hours */
138 sprintf(buf
, LF
, game
);
154 sprintf(sync_lock
, LF
, game
);
155 sprintf(sync_file
, SF
, game
);
157 if (stat(sync_file
, &tmp
) < 0) {
158 mode_t omask
= umask(002);
159 sync_fp
= fopen(sync_file
, "w+");
162 sync_fp
= fopen(sync_file
, "r+");
171 sync_close(int remove
)
183 Write(int type
, struct ship
*ship
, long a
, long b
, long c
, long d
)
186 sprintf(sync_bp
, "%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 sprintf(sync_bp
, "%d %d 1 %s\n", type
, ship
->file
->index
, a
);
203 if (sync_bp
>= &sync_buf
[sizeof sync_buf
])
205 sync_update(type
, ship
, a
, 0, 0, 0, 0);
211 sig_t sighup
, sigint
;
213 int type
, shipnum
, isstr
;
219 sighup
= signal(SIGHUP
, SIG_IGN
);
220 sigint
= signal(SIGINT
, SIG_IGN
);
221 for (n
= TIMEOUT
; --n
>= 0;) {
223 if (flock(fileno(sync_fp
), LOCK_EX
|LOCK_NB
) >= 0)
225 if (errno
!= EWOULDBLOCK
)
229 if (link(sync_file
, sync_lock
) >= 0) {
241 fseek(sync_fp
, sync_seek
, SEEK_SET
);
243 switch (fscanf(sync_fp
, "%d%d%d", &type
, &shipnum
, &isstr
)) {
251 if (shipnum
< 0 || shipnum
>= cc
->vessels
)
253 if (isstr
!= 0 && isstr
!= 1)
258 switch (*p
++ = getc(sync_fp
)) {
264 if (p
>= buf
+ sizeof buf
)
271 for (p
= buf
; *p
== ' '; p
++)
276 if (fscanf(sync_fp
, "%ld%ld%ld%ld", &a
, &b
, &c
, &d
) != 4)
280 if (sync_update(type
, SHIP(shipnum
), astr
, a
, b
, c
, d
) < 0)
286 if (!erred
&& sync_bp
!= sync_buf
) {
287 fseek(sync_fp
, 0L, SEEK_END
);
288 fwrite(sync_buf
, sizeof *sync_buf
, sync_bp
- sync_buf
,
293 sync_seek
= ftell(sync_fp
);
295 flock(fileno(sync_fp
), LOCK_UN
);
301 signal(SIGHUP
, sighup
);
302 signal(SIGINT
, sigint
);
303 return erred
? -1 : 0;
307 sync_update(int type
, struct ship
*ship
, const char *astr
, long a
, long b
, long c
, long d
)
311 struct BP
*p
= &ship
->file
->DBP
[a
];
318 struct BP
*p
= &ship
->file
->OBP
[a
];
325 struct snag
*p
= &ship
->file
->foul
[a
];
326 if (SHIP(a
)->file
->dir
== 0)
328 if (p
->sn_count
++ == 0)
334 struct snag
*p
= &ship
->file
->grap
[a
];
335 if (SHIP(a
)->file
->dir
== 0)
337 if (p
->sn_count
++ == 0)
343 struct snag
*p
= &ship
->file
->foul
[a
];
344 if (p
->sn_count
> 0) {
346 ship
->file
->nfoul
-= p
->sn_count
;
356 struct snag
*p
= &ship
->file
->grap
[a
];
357 if (p
->sn_count
> 0) {
359 ship
->file
->ngrap
-= p
->sn_count
;
369 if (mode
== MODE_PLAYER
) {
371 Signal("$$: %s", ship
, astr
);
373 Signal("\7$$: %s", ship
, astr
);
377 struct shipspecs
*s
= ship
->specs
;
384 strncpy(ship
->file
->captain
, astr
,
385 sizeof ship
->file
->captain
- 1);
386 ship
->file
->captain
[sizeof ship
->file
->captain
- 1] = 0;
390 ship
->file
->captured
= 0;
392 ship
->file
->captured
= SHIP(a
);
395 ship
->specs
->class = a
;
398 ship
->file
->drift
= a
;
401 if ((ship
->file
->explode
= a
) == 2)
408 struct shipspecs
*s
= ship
->specs
;
414 struct shipspecs
*s
= ship
->specs
;
420 ship
->specs
->hull
= a
;
423 strncpy(ship
->file
->movebuf
, astr
,
424 sizeof ship
->file
->movebuf
- 1);
425 ship
->file
->movebuf
[sizeof ship
->file
->movebuf
- 1] = 0;
428 ship
->file
->pcrew
= a
;
431 ship
->file
->points
= a
;
434 ship
->specs
->qual
= a
;
437 struct shipspecs
*s
= ship
->specs
;
445 ship
->specs
->rig1
= a
;
448 ship
->specs
->rig2
= a
;
451 ship
->specs
->rig3
= a
;
454 ship
->specs
->rig4
= a
;
466 if ((ship
->file
->sink
= a
) == 2)
470 ship
->file
->struck
= a
;
486 strcpy(ship
->file
->captain
, "begin");
490 *ship
->file
->captain
= 0;
491 ship
->file
->points
= 0;
498 fprintf(stderr
, "sync_update: unknown type %d\r\n", type
);