]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - sail/sync.c
1 /* $NetBSD: sync.c,v 1.4 1995/04/24 12:25:28 cgd 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
38 static char sccsid
[] = "@(#)sync.c 8.1 (Berkeley) 5/31/93";
40 static char rcsid
[] = "$NetBSD: sync.c,v 1.4 1995/04/24 12:25:28 cgd Exp $";
46 #include <sys/errno.h>
50 static char sync_buf
[BUFSIZE
];
51 static char *sync_bp
= sync_buf
;
52 static char sync_lock
[25];
53 static char sync_file
[25];
54 static long sync_seek
;
56 #define SF "/tmp/#sailsink.%d"
57 #define LF "/tmp/#saillock.%d"
60 makesignal(from
, fmt
, ship
, a
, b
, c
)
63 register struct ship
*ship
;
69 (void) sprintf(message
, fmt
, a
, b
, c
);
71 (void) sprintf(message
, fmt
,
72 ship
->shipname
, colours(ship
),
73 sterncolour(ship
), a
, b
, c
);
74 Write(W_SIGNAL
, from
, 1, (long)message
, 0, 0, 0);
77 #include <sys/types.h>
81 char buf
[sizeof sync_file
];
85 (void) sprintf(buf
, SF
, game
);
87 if (stat(buf
, &s
) < 0)
89 if (s
.st_mtime
< t
- 60*60*2) { /* 2 hours */
91 (void) sprintf(buf
, LF
, game
);
101 (void) fclose(sync_fp
);
102 (void) sprintf(sync_lock
, LF
, game
);
103 (void) sprintf(sync_file
, SF
, game
);
104 if (access(sync_file
, 0) < 0) {
105 int omask
= umask(issetuid
? 077 : 011);
106 sync_fp
= fopen(sync_file
, "w+");
109 sync_fp
= fopen(sync_file
, "r+");
120 (void) fclose(sync_fp
);
122 (void) unlink(sync_file
);
125 Write(type
, ship
, isstr
, a
, b
, c
, d
)
132 (void) sprintf(sync_bp
, "%d %d %d %s\n",
133 type
, ship
->file
->index
, isstr
, a
);
135 (void) sprintf(sync_bp
, "%d %d %d %d %d %d %d\n",
136 type
, ship
->file
->index
, isstr
, a
, b
, c
, d
);
140 if (sync_bp
>= &sync_buf
[sizeof sync_buf
])
142 (void) sync_update(type
, ship
, a
, b
, c
, d
);
147 sig_t sighup
, sigint
;
149 int type
, shipnum
, isstr
;
155 sighup
= signal(SIGHUP
, SIG_IGN
);
156 sigint
= signal(SIGINT
, SIG_IGN
);
157 for (n
= TIMEOUT
; --n
>= 0;) {
159 if (flock(fileno(sync_fp
), LOCK_EX
|LOCK_NB
) >= 0)
161 if (errno
!= EWOULDBLOCK
)
164 if (link(sync_file
, sync_lock
) >= 0)
173 (void) fseek(sync_fp
, sync_seek
, 0);
175 switch (fscanf(sync_fp
, "%d%d%d", &type
, &shipnum
, &isstr
)) {
183 if (shipnum
< 0 || shipnum
>= cc
->vessels
)
185 if (isstr
!= 0 && isstr
!= 1)
190 switch (*p
++ = getc(sync_fp
)) {
196 if (p
>= buf
+ sizeof buf
)
203 for (p
= buf
; *p
== ' '; p
++)
208 if (fscanf(sync_fp
, "%d%d%d%d", &a
, &b
, &c
, &d
) != 4)
210 if (sync_update(type
, SHIP(shipnum
), a
, b
, c
, d
) < 0)
216 if (!erred
&& sync_bp
!= sync_buf
) {
217 (void) fseek(sync_fp
, 0L, 2);
218 (void) fwrite(sync_buf
, sizeof *sync_buf
, sync_bp
- sync_buf
,
220 (void) fflush(sync_fp
);
223 sync_seek
= ftell(sync_fp
);
225 (void) flock(fileno(sync_fp
), LOCK_UN
);
227 (void) unlink(sync_lock
);
229 (void) signal(SIGHUP
, sighup
);
230 (void) signal(SIGINT
, sigint
);
231 return erred
? -1 : 0;
234 sync_update(type
, ship
, a
, b
, c
, d
)
236 register struct ship
*ship
;
241 register struct BP
*p
= &ship
->file
->DBP
[a
];
248 register struct BP
*p
= &ship
->file
->OBP
[a
];
255 register struct snag
*p
= &ship
->file
->foul
[a
];
256 if (SHIP(a
)->file
->dir
== 0)
258 if (p
->sn_count
++ == 0)
264 register struct snag
*p
= &ship
->file
->grap
[a
];
265 if (SHIP(a
)->file
->dir
== 0)
267 if (p
->sn_count
++ == 0)
273 register struct snag
*p
= &ship
->file
->foul
[a
];
276 ship
->file
->nfoul
-= p
->sn_count
;
285 register struct snag
*p
= &ship
->file
->grap
[a
];
288 ship
->file
->ngrap
-= p
->sn_count
;
297 if (mode
== MODE_PLAYER
)
299 Signal("%s (%c%c): %s", ship
, a
);
301 Signal("\7%s (%c%c): %s", ship
, a
);
304 register struct shipspecs
*s
= ship
->specs
;
311 (void) strncpy(ship
->file
->captain
, (char *)a
,
312 sizeof ship
->file
->captain
- 1);
313 ship
->file
->captain
[sizeof ship
->file
->captain
- 1] = 0;
317 ship
->file
->captured
= 0;
319 ship
->file
->captured
= SHIP(a
);
322 ship
->specs
->class = a
;
325 ship
->file
->drift
= a
;
328 if ((ship
->file
->explode
= a
) == 2)
335 register struct shipspecs
*s
= ship
->specs
;
341 register struct shipspecs
*s
= ship
->specs
;
347 ship
->specs
->hull
= a
;
350 (void) strncpy(ship
->file
->movebuf
, (char *)a
,
351 sizeof ship
->file
->movebuf
- 1);
352 ship
->file
->movebuf
[sizeof ship
->file
->movebuf
- 1] = 0;
355 ship
->file
->pcrew
= a
;
358 ship
->file
->points
= a
;
361 ship
->specs
->qual
= a
;
364 register struct shipspecs
*s
= ship
->specs
;
372 ship
->specs
->rig1
= a
;
375 ship
->specs
->rig2
= a
;
378 ship
->specs
->rig3
= a
;
381 ship
->specs
->rig4
= a
;
393 if ((ship
->file
->sink
= a
) == 2)
397 ship
->file
->struck
= a
;
413 (void) strcpy(ship
->file
->captain
, "begin");
417 *ship
->file
->captain
= 0;
418 ship
->file
->points
= 0;
425 fprintf(stderr
, "sync_update: unknown type %d\r\n", type
);