]> git.cameronkatri.com Git - bsdgames-darwin.git/blob - sail/sync.c
Use the symbolic names `SEEK_SET' and `O_RDONLY' where appropriate in
[bsdgames-darwin.git] / sail / sync.c
1 /* $NetBSD: sync.c,v 1.12 1999/09/08 21:57:20 jsm Exp $ */
2
3 /*
4 * Copyright (c) 1983, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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.
22 *
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
33 * SUCH DAMAGE.
34 */
35
36 #include <sys/cdefs.h>
37 #ifndef lint
38 #if 0
39 static char sccsid[] = "@(#)sync.c 8.2 (Berkeley) 4/28/95";
40 #else
41 __RCSID("$NetBSD: sync.c,v 1.12 1999/09/08 21:57:20 jsm Exp $");
42 #endif
43 #endif /* not lint */
44
45 #include <fcntl.h>
46 #include <errno.h>
47 #ifdef __STDC__
48 #include <stdarg.h>
49 #else
50 #include <varargs.h>
51 #endif
52 #include <stdlib.h>
53 #include <unistd.h>
54 #include <sys/types.h>
55 #include <sys/stat.h>
56 #include "extern.h"
57
58 #define BUFSIZE 4096
59
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;
65 static FILE *sync_fp;
66 #define SF "/tmp/#sailsink.%d"
67 #define LF "/tmp/#saillock.%d"
68
69 void
70 fmtship(buf, len, fmt, ship)
71 char *buf;
72 size_t len;
73 const char *fmt;
74 struct ship *ship;
75 {
76 while (*fmt) {
77 if (len-- == 0) {
78 *buf = '\0';
79 return;
80 }
81 if (*fmt == '$' && fmt[1] == '$') {
82 size_t l = snprintf(buf, len, "%s (%c%c)",
83 ship->shipname, colours(ship), sterncolour(ship));
84 buf += l;
85 len -= l - 1;
86 fmt += 2;
87 }
88 else
89 *buf++ = *fmt++;
90 }
91
92 if (len > 0)
93 *buf = '\0';
94 }
95
96
97 /*VARARGS3*/
98 void
99 #ifdef __STDC__
100 makesignal(struct ship *from, const char *fmt, struct ship *ship, ...)
101 #else
102 makesignal(va_alias)
103 va_dcl
104 #endif
105 {
106 char message[BUFSIZ];
107 char format[BUFSIZ];
108 va_list ap;
109 #ifndef __STDC__
110 struct ship *from;
111 const char *fmt;
112 struct ship *ship;
113
114 va_start(ap);
115 from = va_arg(ap, struct ship *);
116 fmt = va_arg(ap, const char *);
117 ship = va_arg(ap, struct ship *);
118 #else
119 va_start(ap, ship);
120 #endif
121 fmtship(format, sizeof(format), fmt, ship);
122 (void) vsprintf(message, format, ap);
123 va_end(ap);
124 Writestr(W_SIGNAL, from, message);
125 }
126
127 void
128 #ifdef __STDC__
129 makemsg(struct ship *from, const char *fmt, ...)
130 #else
131 makemsg(va_alias)
132 va_dcl
133 #endif
134 {
135 char message[BUFSIZ];
136 va_list ap;
137 #ifndef __STDC__
138 struct ship *from;
139 const char *fmt;
140
141 va_start(ap);
142 from = va_arg(ap, struct ship *);
143 fmt = va_arg(ap, const char *);
144 #else
145 va_start(ap, fmt);
146 #endif
147 (void) vsprintf(message, fmt, ap);
148 va_end(ap);
149 Writestr(W_SIGNAL, from, message);
150 }
151
152 int
153 sync_exists(game)
154 int game;
155 {
156 char buf[sizeof sync_file];
157 struct stat s;
158 time_t t;
159
160 (void) sprintf(buf, SF, game);
161 (void) time(&t);
162 if (stat(buf, &s) < 0)
163 return 0;
164 if (s.st_mtime < t - 60*60*2) { /* 2 hours */
165 (void) unlink(buf);
166 (void) sprintf(buf, LF, game);
167 (void) unlink(buf);
168 return 0;
169 } else
170 return 1;
171 }
172
173 int
174 sync_open()
175 {
176 if (sync_fp != NULL)
177 (void) fclose(sync_fp);
178 (void) sprintf(sync_lock, LF, game);
179 (void) sprintf(sync_file, SF, game);
180 if (access(sync_file, 0) < 0) {
181 int omask = umask(issetuid ? 077 : 011);
182 sync_fp = fopen(sync_file, "w+");
183 (void) umask(omask);
184 } else
185 sync_fp = fopen(sync_file, "r+");
186 if (sync_fp == NULL)
187 return -1;
188 sync_seek = 0;
189 return 0;
190 }
191
192 void
193 sync_close(remove)
194 char remove;
195 {
196 if (sync_fp != 0)
197 (void) fclose(sync_fp);
198 if (remove)
199 (void) unlink(sync_file);
200 }
201
202 void
203 Write(type, ship, a, b, c, d)
204 int type;
205 struct ship *ship;
206 long a, b, c, d;
207 {
208
209 (void) sprintf(sync_bp, "%d %d 0 %ld %ld %ld %ld\n",
210 type, ship->file->index, a, b, c, d);
211 while (*sync_bp++)
212 ;
213 sync_bp--;
214 if (sync_bp >= &sync_buf[sizeof sync_buf])
215 abort();
216 (void) sync_update(type, ship, NULL, a, b, c, d);
217 }
218
219 void
220 Writestr(type, ship, a)
221 int type;
222 struct ship *ship;
223 const char *a;
224 {
225
226 (void) sprintf(sync_bp, "%d %d 1 %s\n",
227 type, ship->file->index, a);
228 while (*sync_bp++)
229 ;
230 sync_bp--;
231 if (sync_bp >= &sync_buf[sizeof sync_buf])
232 abort();
233 (void) sync_update(type, ship, a, 0, 0, 0, 0);
234 }
235
236 int
237 Sync()
238 {
239 sig_t sighup, sigint;
240 int n;
241 int type, shipnum, isstr;
242 char *astr;
243 long a, b, c, d;
244 char buf[80];
245 char erred = 0;
246
247 sighup = signal(SIGHUP, SIG_IGN);
248 sigint = signal(SIGINT, SIG_IGN);
249 for (n = TIMEOUT; --n >= 0;) {
250 #ifdef LOCK_EX
251 if (flock(fileno(sync_fp), LOCK_EX|LOCK_NB) >= 0)
252 break;
253 if (errno != EWOULDBLOCK)
254 return -1;
255 #else
256 if (link(sync_file, sync_lock) >= 0)
257 break;
258 if (errno != EEXIST)
259 return -1;
260 #endif
261 sleep(1);
262 }
263 if (n <= 0)
264 return -1;
265 (void) fseek(sync_fp, sync_seek, SEEK_SET);
266 for (;;) {
267 switch (fscanf(sync_fp, "%d%d%d", &type, &shipnum, &isstr)) {
268 case 3:
269 break;
270 case EOF:
271 goto out;
272 default:
273 goto bad;
274 }
275 if (shipnum < 0 || shipnum >= cc->vessels)
276 goto bad;
277 if (isstr != 0 && isstr != 1)
278 goto bad;
279 if (isstr) {
280 char *p;
281 for (p = buf;;) {
282 switch (*p++ = getc(sync_fp)) {
283 case '\n':
284 p--;
285 case EOF:
286 break;
287 default:
288 if (p >= buf + sizeof buf)
289 p--;
290 continue;
291 }
292 break;
293 }
294 *p = 0;
295 for (p = buf; *p == ' '; p++)
296 ;
297 astr = p;
298 a = b = c = d = 0;
299 } else {
300 if (fscanf(sync_fp, "%ld%ld%ld%ld", &a, &b, &c, &d) != 4)
301 goto bad;
302 astr = NULL;
303 }
304 if (sync_update(type, SHIP(shipnum), astr, a, b, c, d) < 0)
305 goto bad;
306 }
307 bad:
308 erred++;
309 out:
310 if (!erred && sync_bp != sync_buf) {
311 (void) fseek(sync_fp, 0L, SEEK_END);
312 (void) fwrite(sync_buf, sizeof *sync_buf, sync_bp - sync_buf,
313 sync_fp);
314 (void) fflush(sync_fp);
315 sync_bp = sync_buf;
316 }
317 sync_seek = ftell(sync_fp);
318 #ifdef LOCK_EX
319 (void) flock(fileno(sync_fp), LOCK_UN);
320 #else
321 (void) unlink(sync_lock);
322 #endif
323 (void) signal(SIGHUP, sighup);
324 (void) signal(SIGINT, sigint);
325 return erred ? -1 : 0;
326 }
327
328 int
329 sync_update(type, ship, astr, a, b, c, d)
330 int type;
331 struct ship *ship;
332 const char *astr;
333 long a, b, c, d;
334 {
335 switch (type) {
336 case W_DBP: {
337 struct BP *p = &ship->file->DBP[a];
338 p->turnsent = b;
339 p->toship = SHIP(c);
340 p->mensent = d;
341 break;
342 }
343 case W_OBP: {
344 struct BP *p = &ship->file->OBP[a];
345 p->turnsent = b;
346 p->toship = SHIP(c);
347 p->mensent = d;
348 break;
349 }
350 case W_FOUL: {
351 struct snag *p = &ship->file->foul[a];
352 if (SHIP(a)->file->dir == 0)
353 break;
354 if (p->sn_count++ == 0)
355 p->sn_turn = turn;
356 ship->file->nfoul++;
357 break;
358 }
359 case W_GRAP: {
360 struct snag *p = &ship->file->grap[a];
361 if (SHIP(a)->file->dir == 0)
362 break;
363 if (p->sn_count++ == 0)
364 p->sn_turn = turn;
365 ship->file->ngrap++;
366 break;
367 }
368 case W_UNFOUL: {
369 struct snag *p = &ship->file->foul[a];
370 if (p->sn_count > 0) {
371 if (b) {
372 ship->file->nfoul -= p->sn_count;
373 p->sn_count = 0;
374 } else {
375 ship->file->nfoul--;
376 p->sn_count--;
377 }
378 }
379 break;
380 }
381 case W_UNGRAP: {
382 struct snag *p = &ship->file->grap[a];
383 if (p->sn_count > 0) {
384 if (b) {
385 ship->file->ngrap -= p->sn_count;
386 p->sn_count = 0;
387 } else {
388 ship->file->ngrap--;
389 p->sn_count--;
390 }
391 }
392 break;
393 }
394 case W_SIGNAL:
395 if (mode == MODE_PLAYER) {
396 if (nobells)
397 Signal("$$: %s", ship, astr);
398 else
399 Signal("\7$$: %s", ship, astr);
400 }
401 break;
402 case W_CREW: {
403 struct shipspecs *s = ship->specs;
404 s->crew1 = a;
405 s->crew2 = b;
406 s->crew3 = c;
407 break;
408 }
409 case W_CAPTAIN:
410 (void) strncpy(ship->file->captain, astr,
411 sizeof ship->file->captain - 1);
412 ship->file->captain[sizeof ship->file->captain - 1] = 0;
413 break;
414 case W_CAPTURED:
415 if (a < 0)
416 ship->file->captured = 0;
417 else
418 ship->file->captured = SHIP(a);
419 break;
420 case W_CLASS:
421 ship->specs->class = a;
422 break;
423 case W_DRIFT:
424 ship->file->drift = a;
425 break;
426 case W_EXPLODE:
427 if ((ship->file->explode = a) == 2)
428 ship->file->dir = 0;
429 break;
430 case W_FS:
431 ship->file->FS = a;
432 break;
433 case W_GUNL: {
434 struct shipspecs *s = ship->specs;
435 s->gunL = a;
436 s->carL = b;
437 break;
438 }
439 case W_GUNR: {
440 struct shipspecs *s = ship->specs;
441 s->gunR = a;
442 s->carR = b;
443 break;
444 }
445 case W_HULL:
446 ship->specs->hull = a;
447 break;
448 case W_MOVE:
449 (void) strncpy(ship->file->movebuf, astr,
450 sizeof ship->file->movebuf - 1);
451 ship->file->movebuf[sizeof ship->file->movebuf - 1] = 0;
452 break;
453 case W_PCREW:
454 ship->file->pcrew = a;
455 break;
456 case W_POINTS:
457 ship->file->points = a;
458 break;
459 case W_QUAL:
460 ship->specs->qual = a;
461 break;
462 case W_RIGG: {
463 struct shipspecs *s = ship->specs;
464 s->rig1 = a;
465 s->rig2 = b;
466 s->rig3 = c;
467 s->rig4 = d;
468 break;
469 }
470 case W_RIG1:
471 ship->specs->rig1 = a;
472 break;
473 case W_RIG2:
474 ship->specs->rig2 = a;
475 break;
476 case W_RIG3:
477 ship->specs->rig3 = a;
478 break;
479 case W_RIG4:
480 ship->specs->rig4 = a;
481 break;
482 case W_COL:
483 ship->file->col = a;
484 break;
485 case W_DIR:
486 ship->file->dir = a;
487 break;
488 case W_ROW:
489 ship->file->row = a;
490 break;
491 case W_SINK:
492 if ((ship->file->sink = a) == 2)
493 ship->file->dir = 0;
494 break;
495 case W_STRUCK:
496 ship->file->struck = a;
497 break;
498 case W_TA:
499 ship->specs->ta = a;
500 break;
501 case W_ALIVE:
502 alive = 1;
503 break;
504 case W_TURN:
505 turn = a;
506 break;
507 case W_WIND:
508 winddir = a;
509 windspeed = b;
510 break;
511 case W_BEGIN:
512 (void) strcpy(ship->file->captain, "begin");
513 people++;
514 break;
515 case W_END:
516 *ship->file->captain = 0;
517 ship->file->points = 0;
518 people--;
519 break;
520 case W_DDEAD:
521 hasdriver = 0;
522 break;
523 default:
524 fprintf(stderr, "sync_update: unknown type %d\r\n", type);
525 return -1;
526 }
527 return 0;
528 }