static char *sccsid
= "@(#)sync.c 2.6 84/04/28";
static char sync_buf
[BUFSIZE
];
static char *sync_bp
= sync_buf
;
static char sync_lock
[25];
static char sync_file
[25];
#define SF "/tmp/#sailsink.%d"
#define LF "/tmp/#saillock.%d"
makesignal(from
, fmt
, ship
, a
, b
, c
)
register struct ship
*ship
;
(void) sprintf(message
, fmt
, a
, b
, c
);
(void) sprintf(message
, fmt
,
ship
->shipname
, colours(ship
),
sterncolour(ship
), a
, b
, c
);
Write(W_SIGNAL
, from
, 1, (int)message
, 0, 0, 0);
char buf
[sizeof sync_file
];
(void) sprintf(buf
, SF
, game
);
if (s
.st_mtime
< t
- 60*60*2) { /* 2 hours */
(void) sprintf(buf
, LF
, game
);
(void) sprintf(sync_lock
, LF
, game
);
(void) sprintf(sync_file
, SF
, game
);
if (access(sync_file
, 0) < 0) {
sync_fp
= fopen(sync_file
, "w+");
sync_fp
= fopen(sync_file
, "r+");
(void) unlink(sync_file
);
Write(type
, ship
, isstr
, a
, b
, c
, d
)
(void) sprintf(sync_bp
, "%d %d %d %s\n",
type
, ship
->file
->index
, isstr
, a
);
(void) sprintf(sync_bp
, "%d %d %d %d %d %d %d\n",
type
, ship
->file
->index
, isstr
, a
, b
, c
, d
);
if (sync_bp
>= &sync_buf
[sizeof sync_buf
])
(void) sync_update(type
, ship
, a
, b
, c
, d
);
int (*sig1
)(), (*sig2
)();
int type
, shipnum
, isstr
, a
, b
, c
, d
;
sig1
= signal(SIGHUP
, SIG_IGN
);
sig2
= signal(SIGINT
, SIG_IGN
);
for (n
= TIMEOUT
; --n
>= 0;) {
if (flock(fileno(sync_fp
), LOCK_EX
|LOCK_NB
) >= 0)
if (errno
!= EWOULDBLOCK
)
if (link(sync_file
, sync_lock
) >= 0)
(void) fseek(sync_fp
, sync_seek
, 0);
switch (fscanf(sync_fp
, "%d%d%d", &type
, &shipnum
, &isstr
)) {
if (shipnum
< 0 || shipnum
>= cc
->vessels
)
if (isstr
!= 0 && isstr
!= 1)
switch (*p
++ = getc(sync_fp
)) {
if (p
>= buf
+ sizeof buf
)
for (p
= buf
; *p
== ' '; p
++)
if (fscanf(sync_fp
, "%d%d%d%d", &a
, &b
, &c
, &d
) != 4)
if (sync_update(type
, SHIP(shipnum
), a
, b
, c
, d
) < 0)
if (!erred
&& sync_bp
!= sync_buf
) {
(void) fseek(sync_fp
, 0L, 2);
(void) fputs(sync_buf
, sync_fp
);
sync_seek
= ftell(sync_fp
);
(void) flock(fileno(sync_fp
), LOCK_UN
);
(void) unlink(sync_lock
);
(void) signal(SIGHUP
, sig1
);
(void) signal(SIGINT
, sig2
);
sync_update(type
, ship
, a
, b
, c
, d
)
register struct ship
*ship
;
register struct BP
*p
= &ship
->file
->DBP
[a
];
register struct BP
*p
= &ship
->file
->OBP
[a
];
register struct snag
*p
= &ship
->file
->foul
[a
];
if (SHIP(a
)->file
->dir
== 0)
register struct snag
*p
= &ship
->file
->grap
[a
];
if (SHIP(a
)->file
->dir
== 0)
register struct snag
*p
= &ship
->file
->foul
[a
];
ship
->file
->nfoul
-= p
->sn_count
;
register struct snag
*p
= &ship
->file
->grap
[a
];
ship
->file
->ngrap
-= p
->sn_count
;
Signal("\7%s (%c%c): %s", ship
, a
);
register struct shipspecs
*s
= ship
->specs
;
(void) strncpy(ship
->file
->captain
, (char *)a
,
sizeof ship
->file
->captain
- 1);
ship
->file
->captain
[sizeof ship
->file
->captain
- 1] = 0;
ship
->file
->captured
= 0;
ship
->file
->captured
= SHIP(a
);
if ((ship
->file
->explode
= a
) == 2)
register struct shipspecs
*s
= ship
->specs
;
register struct shipspecs
*s
= ship
->specs
;
(void) strncpy(ship
->file
->movebuf
, (char *)a
,
sizeof ship
->file
->movebuf
- 1);
ship
->file
->movebuf
[sizeof ship
->file
->movebuf
- 1] = 0;
register struct shipspecs
*s
= ship
->specs
;
if ((ship
->file
->sink
= a
) == 2)
(void) strcpy(ship
->file
->captain
, "begin");
*ship
->file
->captain
= 0;
fprintf(stderr
, "sync_update: unknown type %d\r\n", type
);