minor bug for ~ expansion.
[unix-history] / usr / src / games / sail / sync.c
CommitLineData
15e30c53 1#ifndef lint
ce837792 2static char *sccsid = "@(#)sync.c 2.1 83/10/31";
15e30c53
EW
3#endif
4
5#include "externs.h"
6
7static char sync_buf[BUFSIZE];
8static char *sync_bufp = sync_buf;
9static char sync_lock[25];
10static char sync_file[25];
11static long sync_seek;
12static FILE *sync_fp;
13#define SF "/tmp/#sailsink.%d"
14#define LF "/tmp/#saillock.%d"
15
16/*VARARGS3*/
17makesignal(from, fmt, ship, a, b, c)
18struct ship *from;
19char *fmt;
20register struct ship *ship;
21{
22 char message[80];
23
24 if (ship == 0)
25 (void) sprintf(message, fmt, a, b, c);
26 else
27 (void) sprintf(message, fmt,
28 ship->shipname, colours(ship),
29 sterncolour(ship), a, b, c);
30 Write(W_SIGNAL, from, 1, (int)message, 0, 0, 0);
31}
32
30d1a80b
EW
33#include <sys/types.h>
34#include <sys/stat.h>
15e30c53
EW
35sync_exists(game)
36{
37 char buf[sizeof sync_file];
30d1a80b
EW
38 struct stat s;
39 time_t t;
15e30c53
EW
40
41 (void) sprintf(buf, SF, game);
30d1a80b
EW
42 time(&t);
43 if (stat(buf, &s) < 0)
44 return 0;
45 if (s.st_mtime < t - 60*60*2) { /* 2 hours */
46 unlink(buf);
47 return 0;
48 } else
49 return 1;
15e30c53
EW
50}
51
52sync_open()
53{
54 (void) sprintf(sync_lock, LF, game);
55 (void) sprintf(sync_file, SF, game);
56 if (access(sync_file, 0) < 0) {
57 int omask;
58#ifdef SETUID
59 omask = umask(077);
60#else
61 omask = umask(011);
62#endif
63 sync_fp = fopen(sync_file, "w+");
64 (void) umask(omask);
65 } else
66 sync_fp = fopen(sync_file, "r+");
67 if (sync_fp == 0)
68 return -1;
69 sync_seek == 0;
9224751f 70 return 0;
15e30c53
EW
71}
72
73sync_close(remove)
74char remove;
75{
76 (void) fclose(sync_fp);
77 if (remove)
78 (void) unlink(sync_file);
79}
80
81Write(type, ship, isstr, a, b, c, d)
82int type;
83struct ship *ship;
84char isstr;
85int a, b, c, d;
86{
87 if (isstr)
88 (void) sprintf(sync_bufp, "%d %d %d %s\n",
89 type, ship-SHIP(0), isstr, a);
90 else
91 (void) sprintf(sync_bufp, "%d %d %d %d %d %d %d\n",
92 type, ship-SHIP(0), isstr, a, b, c, d);
93 while (*sync_bufp++)
94 ;
95 sync_bufp--;
96 if (sync_bufp >= &sync_buf[sizeof sync_buf])
97 abort();
98 sync_update(type, ship, a, b, c, d);
99}
100
101Sync()
102{
103 int (*sig1)(), (*sig2)();
104 int type, shipnum, isstr, a, b, c, d;
105 char buf[60];
106 register char *p = buf;
107 int n;
108
109 sig1 = signal(SIGHUP, SIG_IGN);
110 sig2 = signal(SIGINT, SIG_IGN);
111 for (n = 0; link(sync_file, sync_lock) < 0 && n < 30; n++)
112 sleep(2);
113 (void) fseek(sync_fp, sync_seek, 0);
114 while (fscanf(sync_fp, "%d%d%d", &type, &shipnum, &isstr) != EOF) {
115 if (isstr) {
116 for (p = buf;;) {
117 switch (*p++ = getc(sync_fp)) {
118 case '\n':
119 p--;
120 case EOF:
121 break;
122 default:
123 continue;
124 }
125 break;
126 }
127 *p = 0;
128 for (p = buf; *p == ' '; p++)
129 ;
130 a = (int)p;
131 b = c = d = 0;
132 } else
133 (void) fscanf(sync_fp, "%d%d%d%d", &a, &b, &c, &d);
134 sync_update(type, SHIP(shipnum), a, b, c, d);
135 }
136 if (sync_bufp != sync_buf) {
137 (void) fseek(sync_fp, 0L, 2);
138 (void) fputs(sync_buf, sync_fp);
139 (void) fflush(sync_fp);
140 sync_bufp = sync_buf;
141 }
142 sync_seek = ftell(sync_fp);
143 (void) unlink(sync_lock);
144 (void) signal(SIGHUP, sig1);
145 (void) signal(SIGINT, sig2);
146}
147
148sync_update(type, ship, a, b, c, d)
149int type;
150register struct ship *ship;
151int a, b, c, d;
152{
153 switch (type) {
154 case W_DBP: {
155 register struct BP *p = &ship->file->DBP[a];
156 p->turnsent = b;
157 p->toship = SHIP(c);
158 p->mensent = d;
159 break;
160 }
161 case W_OBP: {
162 register struct BP *p = &ship->file->OBP[a];
163 p->turnsent = b;
164 p->toship = SHIP(c);
165 p->mensent = d;
166 break;
167 }
168 case W_FOUL: {
169 register struct snag *p = &ship->file->fouls[a];
170 p->turnfoul = b;
171 p->toship = SHIP(c);
172 break;
173 }
174 case W_GRAP: {
175 register struct snag *p = &ship->file->grapples[a];
176 p->turnfoul = b;
177 p->toship = SHIP(c);
178 break;
179 }
180 case W_SIGNAL:
0f5fabce 181 if (isplayer) {
fe0cae24 182 Signal("\7%s (%c%c): %s", ship, a);
0f5fabce 183 }
15e30c53
EW
184 break;
185 case W_CREW: {
186 register struct shipspecs *s = ship->specs;
187 s->crew1 = a;
188 s->crew2 = b;
189 s->crew3 = c;
190 break;
191 }
192 case W_CAPTAIN:
193 (void) strncpy(ship->file->captain, (char *)a,
194 sizeof ship->file->captain - 1);
195 ship->file->captain[sizeof ship->file->captain - 1] = 0;
196 break;
197 case W_CAPTURED:
66e5ed8a
EW
198 if (a < 0)
199 ship->file->captured = 0;
200 else
201 ship->file->captured = SHIP(a);
15e30c53
EW
202 break;
203 case W_CLASS:
204 ship->specs->class = a;
205 break;
206 case W_DRIFT:
207 ship->file->drift = a;
208 break;
209 case W_EXPLODE:
210 if ((ship->file->explode = a) == 2)
211 ship->file->dir = 0;
212 break;
213 case W_FS:
214 ship->file->FS = a;
215 break;
216 case W_GUNL: {
217 register struct shipspecs *s = ship->specs;
218 s->gunL = a;
219 s->carL = b;
220 break;
221 }
222 case W_GUNR: {
223 register struct shipspecs *s = ship->specs;
224 s->gunR = a;
225 s->carR = b;
226 break;
227 }
228 case W_HULL:
229 ship->specs->hull = a;
230 break;
231 case W_LAST:
232 (void) strncpy(ship->file->last, (char *)a,
233 sizeof ship->file->last - 1);
234 ship->file->last[sizeof ship->file->last - 1] = 0;
235 break;
236 case W_PCREW:
237 ship->file->pcrew = a;
238 break;
239 case W_POINTS:
240 ship->file->points = a;
241 break;
242 case W_QUAL:
243 ship->specs->qual = a;
244 break;
245 case W_RIGG: {
246 register struct shipspecs *s = ship->specs;
247 s->rig1 = a;
248 s->rig2 = b;
249 s->rig3 = c;
250 s->rig4 = d;
251 break;
252 }
253 case W_RIG1:
254 ship->specs->rig1 = a;
255 break;
256 case W_RIG2:
257 ship->specs->rig2 = a;
258 break;
259 case W_RIG3:
260 ship->specs->rig3 = a;
261 break;
262 case W_RIG4:
263 ship->specs->rig4 = a;
264 break;
265 case W_SHIPCOL:
266 ship->file->col = a;
267 break;
268 case W_SHIPDIR:
269 ship->file->dir = a;
270 break;
271 case W_SHIPROW:
272 ship->file->row = a;
273 break;
274 case W_SINK:
275 if ((ship->file->sink = a) == 2)
276 ship->file->dir = 0;
277 break;
278 case W_STRUCK:
279 ship->file->struck = a;
280 break;
281 case W_TA:
282 ship->specs->ta = a;
283 break;
284 case W_ALIVE:
285 alive = 1;
286 break;
287 case W_TURN:
288 turn = a;
289 break;
290 case W_WIND:
291 winddir = a;
292 windspeed = b;
293 break;
294 case W_BEGIN:
295 (void) strcpy(ship->file->captain, "begin");
296 people++;
297 break;
298 case W_END:
299 (void) strcpy(ship->file->captain, "");
300 people--;
301 break;
302 default:
303 fprintf(stderr, "sync_update: unknown type %d\n", type);
304 break;
305 }
306}