]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - sail/sync.c
1 /* $NetBSD: sync.c,v 1.7 1997/10/13 21:04:40 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.7 1997/10/13 21:04:40 christos Exp $");
54 #include <sys/types.h>
60 static char sync_buf
[BUFSIZE
];
61 static char *sync_bp
= sync_buf
;
62 static char sync_lock
[25];
63 static char sync_file
[25];
64 static long sync_seek
;
66 #define SF "/tmp/#sailsink.%d"
67 #define LF "/tmp/#saillock.%d"
70 fmtship(buf
, len
, fmt
, ship
)
81 if (*fmt
== '$' && fmt
[1] == '$') {
82 size_t l
= snprintf(buf
, len
, "%s (%c%c)",
83 ship
->shipname
, colours(ship
), sterncolour(ship
));
100 makesignal(struct ship
*from
, const char *fmt
, struct ship
*ship
, ...)
106 char message
[BUFSIZ
];
115 from
= va_arg(ap
, struct ship
*);
116 fmt
= va_arg(ap
, const char *);
117 ship
= va_arg(ap
, struct ship
*);
121 fmtship(format
, sizeof(format
), fmt
, ship
);
122 (void) vsprintf(message
, format
, ap
);
124 Write(W_SIGNAL
, from
, 1, (long)message
, 0, 0, 0);
129 makemsg(struct ship
*from
, const char *fmt
, ...)
135 char message
[BUFSIZ
];
142 from
= va_arg(ap
, struct ship
*);
143 fmt
= va_arg(ap
, const char *);
147 (void) vsprintf(message
, fmt
, ap
);
149 Write(W_SIGNAL
, from
, 1, (long)message
, 0, 0, 0);
154 char buf
[sizeof sync_file
];
158 (void) sprintf(buf
, SF
, game
);
160 if (stat(buf
, &s
) < 0)
162 if (s
.st_mtime
< t
- 60*60*2) { /* 2 hours */
164 (void) sprintf(buf
, LF
, game
);
175 (void) fclose(sync_fp
);
176 (void) sprintf(sync_lock
, LF
, game
);
177 (void) sprintf(sync_file
, SF
, game
);
178 if (access(sync_file
, 0) < 0) {
179 int omask
= umask(issetuid
? 077 : 011);
180 sync_fp
= fopen(sync_file
, "w+");
183 sync_fp
= fopen(sync_file
, "r+");
195 (void) fclose(sync_fp
);
197 (void) unlink(sync_file
);
201 Write(type
, ship
, isstr
, a
, b
, c
, d
)
209 (void) sprintf(sync_bp
, "%d %d %d %s\n",
210 type
, ship
->file
->index
, isstr
, (char *) a
);
212 (void) sprintf(sync_bp
, "%d %d %d %ld %ld %ld %ld\n",
213 type
, ship
->file
->index
, isstr
, a
, b
, c
, d
);
217 if (sync_bp
>= &sync_buf
[sizeof sync_buf
])
219 (void) sync_update(type
, ship
, a
, b
, c
, d
);
225 sig_t sighup
, sigint
;
227 int type
, shipnum
, isstr
;
233 sighup
= signal(SIGHUP
, SIG_IGN
);
234 sigint
= signal(SIGINT
, SIG_IGN
);
235 for (n
= TIMEOUT
; --n
>= 0;) {
237 if (flock(fileno(sync_fp
), LOCK_EX
|LOCK_NB
) >= 0)
239 if (errno
!= EWOULDBLOCK
)
242 if (link(sync_file
, sync_lock
) >= 0)
251 (void) fseek(sync_fp
, sync_seek
, 0);
253 switch (fscanf(sync_fp
, "%d%d%d", &type
, &shipnum
, &isstr
)) {
261 if (shipnum
< 0 || shipnum
>= cc
->vessels
)
263 if (isstr
!= 0 && isstr
!= 1)
268 switch (*p
++ = getc(sync_fp
)) {
274 if (p
>= buf
+ sizeof buf
)
281 for (p
= buf
; *p
== ' '; p
++)
286 if (fscanf(sync_fp
, "%ld%ld%ld%ld", &a
, &b
, &c
, &d
) != 4)
288 if (sync_update(type
, SHIP(shipnum
), a
, b
, c
, d
) < 0)
294 if (!erred
&& sync_bp
!= sync_buf
) {
295 (void) fseek(sync_fp
, 0L, 2);
296 (void) fwrite(sync_buf
, sizeof *sync_buf
, sync_bp
- sync_buf
,
298 (void) fflush(sync_fp
);
301 sync_seek
= ftell(sync_fp
);
303 (void) flock(fileno(sync_fp
), LOCK_UN
);
305 (void) unlink(sync_lock
);
307 (void) signal(SIGHUP
, sighup
);
308 (void) signal(SIGINT
, sigint
);
309 return erred
? -1 : 0;
313 sync_update(type
, ship
, a
, b
, c
, d
)
320 struct BP
*p
= &ship
->file
->DBP
[a
];
327 struct BP
*p
= &ship
->file
->OBP
[a
];
334 struct snag
*p
= &ship
->file
->foul
[a
];
335 if (SHIP(a
)->file
->dir
== 0)
337 if (p
->sn_count
++ == 0)
343 struct snag
*p
= &ship
->file
->grap
[a
];
344 if (SHIP(a
)->file
->dir
== 0)
346 if (p
->sn_count
++ == 0)
352 struct snag
*p
= &ship
->file
->foul
[a
];
355 ship
->file
->nfoul
-= p
->sn_count
;
364 struct snag
*p
= &ship
->file
->grap
[a
];
367 ship
->file
->ngrap
-= p
->sn_count
;
376 if (mode
== MODE_PLAYER
)
378 Signal("$$: %s", ship
, (char *) a
);
380 Signal("\7$$: %s", ship
, (char *) a
);
383 struct shipspecs
*s
= ship
->specs
;
390 (void) strncpy(ship
->file
->captain
, (char *)a
,
391 sizeof ship
->file
->captain
- 1);
392 ship
->file
->captain
[sizeof ship
->file
->captain
- 1] = 0;
396 ship
->file
->captured
= 0;
398 ship
->file
->captured
= SHIP(a
);
401 ship
->specs
->class = a
;
404 ship
->file
->drift
= a
;
407 if ((ship
->file
->explode
= a
) == 2)
414 struct shipspecs
*s
= ship
->specs
;
420 struct shipspecs
*s
= ship
->specs
;
426 ship
->specs
->hull
= a
;
429 (void) strncpy(ship
->file
->movebuf
, (char *)a
,
430 sizeof ship
->file
->movebuf
- 1);
431 ship
->file
->movebuf
[sizeof ship
->file
->movebuf
- 1] = 0;
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 (void) 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
);