]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - sail/sync.c
2 * Copyright (c) 1983 Regents of the University of California.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 /*static char sccsid[] = "from: @(#)sync.c 5.6 (Berkeley) 6/1/90";*/
36 static char rcsid
[] = "$Id: sync.c,v 1.2 1993/08/01 18:51:33 mycroft Exp $";
41 #include <sys/errno.h>
45 static char sync_buf
[BUFSIZE
];
46 static char *sync_bp
= sync_buf
;
47 static char sync_lock
[25];
48 static char sync_file
[25];
49 static long sync_seek
;
51 #define SF "/tmp/#sailsink.%d"
52 #define LF "/tmp/#saillock.%d"
55 makesignal(from
, fmt
, ship
, a
, b
, c
)
58 register struct ship
*ship
;
63 (void) sprintf(message
, fmt
, a
, b
, c
);
65 (void) sprintf(message
, fmt
,
66 ship
->shipname
, colours(ship
),
67 sterncolour(ship
), a
, b
, c
);
68 Write(W_SIGNAL
, from
, 1, (int)message
, 0, 0, 0);
71 #include <sys/types.h>
75 char buf
[sizeof sync_file
];
79 (void) sprintf(buf
, SF
, game
);
81 if (stat(buf
, &s
) < 0)
83 if (s
.st_mtime
< t
- 60*60*2) { /* 2 hours */
85 (void) sprintf(buf
, LF
, game
);
95 (void) fclose(sync_fp
);
96 (void) sprintf(sync_lock
, LF
, game
);
97 (void) sprintf(sync_file
, SF
, game
);
98 if (access(sync_file
, 0) < 0) {
99 int omask
= umask(issetuid
? 077 : 011);
100 sync_fp
= fopen(sync_file
, "w+");
103 sync_fp
= fopen(sync_file
, "r+");
114 (void) fclose(sync_fp
);
116 (void) unlink(sync_file
);
119 Write(type
, ship
, isstr
, a
, b
, c
, d
)
126 (void) sprintf(sync_bp
, "%d %d %d %s\n",
127 type
, ship
->file
->index
, isstr
, a
);
129 (void) sprintf(sync_bp
, "%d %d %d %d %d %d %d\n",
130 type
, ship
->file
->index
, isstr
, a
, b
, c
, d
);
134 if (sync_bp
>= &sync_buf
[sizeof sync_buf
])
136 (void) sync_update(type
, ship
, a
, b
, c
, d
);
141 sig_t sighup
, sigint
;
143 int type
, shipnum
, isstr
, a
, b
, c
, d
;
148 sighup
= signal(SIGHUP
, SIG_IGN
);
149 sigint
= signal(SIGINT
, SIG_IGN
);
150 for (n
= TIMEOUT
; --n
>= 0;) {
152 if (flock(fileno(sync_fp
), LOCK_EX
|LOCK_NB
) >= 0)
154 if (errno
!= EWOULDBLOCK
)
157 if (link(sync_file
, sync_lock
) >= 0)
166 (void) fseek(sync_fp
, sync_seek
, 0);
168 switch (fscanf(sync_fp
, "%d%d%d", &type
, &shipnum
, &isstr
)) {
176 if (shipnum
< 0 || shipnum
>= cc
->vessels
)
178 if (isstr
!= 0 && isstr
!= 1)
183 switch (*p
++ = getc(sync_fp
)) {
189 if (p
>= buf
+ sizeof buf
)
196 for (p
= buf
; *p
== ' '; p
++)
201 if (fscanf(sync_fp
, "%d%d%d%d", &a
, &b
, &c
, &d
) != 4)
203 if (sync_update(type
, SHIP(shipnum
), a
, b
, c
, d
) < 0)
209 if (!erred
&& sync_bp
!= sync_buf
) {
210 (void) fseek(sync_fp
, 0L, 2);
211 (void) fwrite(sync_buf
, sizeof *sync_buf
, sync_bp
- sync_buf
,
213 (void) fflush(sync_fp
);
216 sync_seek
= ftell(sync_fp
);
218 (void) flock(fileno(sync_fp
), LOCK_UN
);
220 (void) unlink(sync_lock
);
222 (void) signal(SIGHUP
, sighup
);
223 (void) signal(SIGINT
, sigint
);
224 return erred
? -1 : 0;
227 sync_update(type
, ship
, a
, b
, c
, d
)
229 register struct ship
*ship
;
234 register struct BP
*p
= &ship
->file
->DBP
[a
];
241 register struct BP
*p
= &ship
->file
->OBP
[a
];
248 register struct snag
*p
= &ship
->file
->foul
[a
];
249 if (SHIP(a
)->file
->dir
== 0)
251 if (p
->sn_count
++ == 0)
257 register struct snag
*p
= &ship
->file
->grap
[a
];
258 if (SHIP(a
)->file
->dir
== 0)
260 if (p
->sn_count
++ == 0)
266 register struct snag
*p
= &ship
->file
->foul
[a
];
269 ship
->file
->nfoul
-= p
->sn_count
;
278 register struct snag
*p
= &ship
->file
->grap
[a
];
281 ship
->file
->ngrap
-= p
->sn_count
;
290 if (mode
== MODE_PLAYER
)
292 Signal("%s (%c%c): %s", ship
, a
);
294 Signal("\7%s (%c%c): %s", ship
, a
);
297 register struct shipspecs
*s
= ship
->specs
;
304 (void) strncpy(ship
->file
->captain
, (char *)a
,
305 sizeof ship
->file
->captain
- 1);
306 ship
->file
->captain
[sizeof ship
->file
->captain
- 1] = 0;
310 ship
->file
->captured
= 0;
312 ship
->file
->captured
= SHIP(a
);
315 ship
->specs
->class = a
;
318 ship
->file
->drift
= a
;
321 if ((ship
->file
->explode
= a
) == 2)
328 register struct shipspecs
*s
= ship
->specs
;
334 register struct shipspecs
*s
= ship
->specs
;
340 ship
->specs
->hull
= a
;
343 (void) strncpy(ship
->file
->movebuf
, (char *)a
,
344 sizeof ship
->file
->movebuf
- 1);
345 ship
->file
->movebuf
[sizeof ship
->file
->movebuf
- 1] = 0;
348 ship
->file
->pcrew
= a
;
351 ship
->file
->points
= a
;
354 ship
->specs
->qual
= a
;
357 register struct shipspecs
*s
= ship
->specs
;
365 ship
->specs
->rig1
= a
;
368 ship
->specs
->rig2
= a
;
371 ship
->specs
->rig3
= a
;
374 ship
->specs
->rig4
= a
;
386 if ((ship
->file
->sink
= a
) == 2)
390 ship
->file
->struck
= a
;
406 (void) strcpy(ship
->file
->captain
, "begin");
410 *ship
->file
->captain
= 0;
411 ship
->file
->points
= 0;
418 fprintf(stderr
, "sync_update: unknown type %d\r\n", type
);