]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - sail/sync.c
1 /* $NetBSD: sync.c,v 1.13 1999/09/09 17:30:20 jsm 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.13 1999/09/09 17:30:20 jsm Exp $");
54 #include <sys/types.h>
61 static char sync_buf
[BUFSIZE
];
62 static char *sync_bp
= sync_buf
;
63 static char sync_lock
[25];
64 static char sync_file
[25];
65 static long sync_seek
;
67 #define SF "/tmp/#sailsink.%d"
68 #define LF "/tmp/#saillock.%d"
71 fmtship(buf
, len
, fmt
, ship
)
82 if (*fmt
== '$' && fmt
[1] == '$') {
83 size_t l
= snprintf(buf
, len
, "%s (%c%c)",
84 ship
->shipname
, colours(ship
), sterncolour(ship
));
101 makesignal(struct ship
*from
, const char *fmt
, struct ship
*ship
, ...)
107 char message
[BUFSIZ
];
116 from
= va_arg(ap
, struct ship
*);
117 fmt
= va_arg(ap
, const char *);
118 ship
= va_arg(ap
, struct ship
*);
122 fmtship(format
, sizeof(format
), fmt
, ship
);
123 (void) vsprintf(message
, format
, ap
);
125 Writestr(W_SIGNAL
, from
, message
);
130 makemsg(struct ship
*from
, const char *fmt
, ...)
136 char message
[BUFSIZ
];
143 from
= va_arg(ap
, struct ship
*);
144 fmt
= va_arg(ap
, const char *);
148 (void) vsprintf(message
, fmt
, ap
);
150 Writestr(W_SIGNAL
, from
, message
);
157 char buf
[sizeof sync_file
];
161 (void) sprintf(buf
, SF
, game
);
163 if (stat(buf
, &s
) < 0)
165 if (s
.st_mtime
< t
- 60*60*2) { /* 2 hours */
167 (void) sprintf(buf
, LF
, game
);
178 (void) fclose(sync_fp
);
179 (void) sprintf(sync_lock
, LF
, game
);
180 (void) sprintf(sync_file
, SF
, game
);
181 if (access(sync_file
, 0) < 0) {
182 int omask
= umask(issetuid
? 077 : 011);
183 sync_fp
= fopen(sync_file
, "w+");
186 sync_fp
= fopen(sync_file
, "r+");
198 (void) fclose(sync_fp
);
200 (void) unlink(sync_file
);
204 Write(type
, ship
, a
, b
, c
, d
)
210 (void) sprintf(sync_bp
, "%d %d 0 %ld %ld %ld %ld\n",
211 type
, ship
->file
->index
, a
, b
, c
, d
);
215 if (sync_bp
>= &sync_buf
[sizeof sync_buf
])
217 (void) sync_update(type
, ship
, NULL
, a
, b
, c
, d
);
221 Writestr(type
, ship
, a
)
227 (void) sprintf(sync_bp
, "%d %d 1 %s\n",
228 type
, ship
->file
->index
, a
);
232 if (sync_bp
>= &sync_buf
[sizeof sync_buf
])
234 (void) sync_update(type
, ship
, a
, 0, 0, 0, 0);
240 sig_t sighup
, sigint
;
242 int type
, shipnum
, isstr
;
248 sighup
= signal(SIGHUP
, SIG_IGN
);
249 sigint
= signal(SIGINT
, SIG_IGN
);
250 for (n
= TIMEOUT
; --n
>= 0;) {
252 if (flock(fileno(sync_fp
), LOCK_EX
|LOCK_NB
) >= 0)
254 if (errno
!= EWOULDBLOCK
)
257 if (link(sync_file
, sync_lock
) >= 0)
266 (void) fseek(sync_fp
, sync_seek
, SEEK_SET
);
268 switch (fscanf(sync_fp
, "%d%d%d", &type
, &shipnum
, &isstr
)) {
276 if (shipnum
< 0 || shipnum
>= cc
->vessels
)
278 if (isstr
!= 0 && isstr
!= 1)
283 switch (*p
++ = getc(sync_fp
)) {
289 if (p
>= buf
+ sizeof buf
)
296 for (p
= buf
; *p
== ' '; p
++)
301 if (fscanf(sync_fp
, "%ld%ld%ld%ld", &a
, &b
, &c
, &d
) != 4)
305 if (sync_update(type
, SHIP(shipnum
), astr
, a
, b
, c
, d
) < 0)
311 if (!erred
&& sync_bp
!= sync_buf
) {
312 (void) fseek(sync_fp
, 0L, SEEK_END
);
313 (void) fwrite(sync_buf
, sizeof *sync_buf
, sync_bp
- sync_buf
,
315 (void) fflush(sync_fp
);
318 sync_seek
= ftell(sync_fp
);
320 (void) flock(fileno(sync_fp
), LOCK_UN
);
322 (void) unlink(sync_lock
);
324 (void) signal(SIGHUP
, sighup
);
325 (void) signal(SIGINT
, sigint
);
326 return erred
? -1 : 0;
330 sync_update(type
, ship
, astr
, a
, b
, c
, d
)
338 struct BP
*p
= &ship
->file
->DBP
[a
];
345 struct BP
*p
= &ship
->file
->OBP
[a
];
352 struct snag
*p
= &ship
->file
->foul
[a
];
353 if (SHIP(a
)->file
->dir
== 0)
355 if (p
->sn_count
++ == 0)
361 struct snag
*p
= &ship
->file
->grap
[a
];
362 if (SHIP(a
)->file
->dir
== 0)
364 if (p
->sn_count
++ == 0)
370 struct snag
*p
= &ship
->file
->foul
[a
];
371 if (p
->sn_count
> 0) {
373 ship
->file
->nfoul
-= p
->sn_count
;
383 struct snag
*p
= &ship
->file
->grap
[a
];
384 if (p
->sn_count
> 0) {
386 ship
->file
->ngrap
-= p
->sn_count
;
396 if (mode
== MODE_PLAYER
) {
398 Signal("$$: %s", ship
, astr
);
400 Signal("\7$$: %s", ship
, astr
);
404 struct shipspecs
*s
= ship
->specs
;
411 (void) strncpy(ship
->file
->captain
, astr
,
412 sizeof ship
->file
->captain
- 1);
413 ship
->file
->captain
[sizeof ship
->file
->captain
- 1] = 0;
417 ship
->file
->captured
= 0;
419 ship
->file
->captured
= SHIP(a
);
422 ship
->specs
->class = a
;
425 ship
->file
->drift
= a
;
428 if ((ship
->file
->explode
= a
) == 2)
435 struct shipspecs
*s
= ship
->specs
;
441 struct shipspecs
*s
= ship
->specs
;
447 ship
->specs
->hull
= a
;
450 (void) strncpy(ship
->file
->movebuf
, astr
,
451 sizeof ship
->file
->movebuf
- 1);
452 ship
->file
->movebuf
[sizeof ship
->file
->movebuf
- 1] = 0;
455 ship
->file
->pcrew
= a
;
458 ship
->file
->points
= a
;
461 ship
->specs
->qual
= a
;
464 struct shipspecs
*s
= ship
->specs
;
472 ship
->specs
->rig1
= a
;
475 ship
->specs
->rig2
= a
;
478 ship
->specs
->rig3
= a
;
481 ship
->specs
->rig4
= a
;
493 if ((ship
->file
->sink
= a
) == 2)
497 ship
->file
->struck
= a
;
513 (void) strcpy(ship
->file
->captain
, "begin");
517 *ship
->file
->captain
= 0;
518 ship
->file
->points
= 0;
525 fprintf(stderr
, "sync_update: unknown type %d\r\n", type
);