-
Notifications
You must be signed in to change notification settings - Fork 1
/
mach_dep.c
470 lines (393 loc) · 8.18 KB
/
mach_dep.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
/*
* Various installation dependent routines
*
* @(#)mach_dep.c 4.37 (Berkeley) 05/23/83
*
* Rogue: Exploring the Dungeons of Doom
* Copyright (C) 1980-1983, 1985, 1999 Michael Toy, Ken Arnold and Glenn Wichman
* All rights reserved.
*
* See the file LICENSE.TXT for full copyright and licensing information.
*/
/*
* The various tuneable defines are:
*
* SCOREFILE Where/if the score file should live.
* ALLSCORES Score file is top ten scores, not top ten
* players. This is only useful when only a few
* people will be playing; otherwise the score file
* gets hogged by just a few people.
* NUMSCORES Number of scores in the score file (default 10).
* NUMNAME String version of NUMSCORES (first character
* should be capitalized) (default "Ten").
* MAXLOAD What (if any) the maximum load average should be
* when people are playing. Since it is divided
* by 10, to specify a load limit of 4.0, MAXLOAD
* should be "40". If defined, then
* LOADAV Should it use it's own routine to get
* the load average?
* NAMELIST If so, where does the system namelist
* hide?
* MAXUSERS What (if any) the maximum user count should be
* when people are playing. If defined, then
* UCOUNT Should it use it's own routine to count
* users?
* UTMP If so, where does the user list hide?
* CHECKTIME How often/if it should check during the game
* for high load average.
*/
#include <signal.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <limits.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <time.h>
#include <curses.h>
#include "extern.h"
#define NOOP(x) (x += 0)
# ifndef NUMSCORES
# define NUMSCORES 10
# define NUMNAME "Ten"
# endif
unsigned int numscores = NUMSCORES;
char *Numname = NUMNAME;
# ifdef ALLSCORES
bool allscore = TRUE;
# else /* ALLSCORES */
bool allscore = FALSE;
# endif /* ALLSCORES */
#ifdef CHECKTIME
static int num_checks; /* times we've gone over in checkout() */
#endif /* CHECKTIME */
/*
* init_check:
* Check out too see if it is proper to play the game now
*/
void
init_check()
{
#if defined(MAXLOAD) || defined(MAXUSERS)
if (too_much()) {
printf("Sorry, %s, but the system is too loaded now.\n", whoami);
printf("Try again later. Meanwhile, why not enjoy a%s %s?\n",
vowelstr(fruit), fruit);
if (author())
printf("However, since you're a good guy, it's up to you\n");
else
exit(1);
}
#endif
}
/*
* open_score:
* Open up the score file for future use
*/
void
open_score()
{
#ifdef SCOREFILE
char *scorefile = SCOREFILE;
/*
* We drop setgid privileges after opening the score file, so subsequent
* open()'s will fail. Just reuse the earlier filehandle.
*/
if (scoreboard != NULL) {
rewind(scoreboard);
return;
}
scoreboard = fopen(scorefile, "r+");
if ((scoreboard == NULL) && (errno == ENOENT)) {
scoreboard = fopen(scorefile, "w+");
md_chmod(scorefile,0664);
}
if (scoreboard == NULL) {
fprintf(stderr, "Could not open %s for writing: %s\n", scorefile, strerror(errno));
fflush(stderr);
}
#else
scoreboard = NULL;
#endif
}
/*
* setup:
* Get starting setup for all games
*/
void
setup()
{
#ifdef CHECKTIME
int checkout();
#endif
#ifdef DUMP
md_onsignal_autosave();
#else
md_onsignal_default();
#endif
#ifdef CHECKTIME
md_start_checkout_timer(CHECKTIME*60);
num_checks = 0;
#endif
raw(); /* Raw mode */
noecho(); /* Echo off */
keypad(stdscr,1);
getltchars(); /* get the local tty chars */
}
/*
* getltchars:
* Get the local tty chars for later use
*/
void
getltchars()
{
got_ltc = TRUE;
orig_dsusp = md_dsuspchar();
md_setdsuspchar( md_suspchar() );
}
/*
* resetltchars:
* Reset the local tty chars to original values.
*/
void
resetltchars(void)
{
if (got_ltc) {
md_setdsuspchar(orig_dsusp);
}
}
/*
* playltchars:
* Set local tty chars to the values we use when playing.
*/
void
playltchars(void)
{
if (got_ltc) {
md_setdsuspchar( md_suspchar() );
}
}
/*
* start_score:
* Start the scoring sequence
*/
void
start_score()
{
#ifdef CHECKTIME
md_stop_checkout_timer();
#endif
}
/*
* is_symlink:
* See if the file has a symbolic link
*/
bool
is_symlink(char *sp)
{
#ifdef S_IFLNK
struct stat sbuf2;
if (lstat(sp, &sbuf2) < 0)
return FALSE;
else
return ((sbuf2.st_mode & S_IFMT) != S_IFREG);
#else
NOOP(sp);
return FALSE;
#endif
}
#if defined(MAXLOAD) || defined(MAXUSERS)
/*
* too_much:
* See if the system is being used too much for this game
*/
bool
too_much()
{
#ifdef MAXLOAD
double avec[3];
#else
int cnt;
#endif
#ifdef MAXLOAD
md_loadav(avec);
if (avec[1] > (MAXLOAD / 10.0))
return TRUE;
#endif
#ifdef MAXUSERS
if (ucount() > MAXUSERS)
return TRUE;
#endif
return FALSE;
}
/*
* author:
* See if a user is an author of the program
*/
bool
author()
{
#ifdef MASTER
if (wizard)
return TRUE;
#endif
switch (md_getuid()) {
case -1:
return TRUE;
default:
return FALSE;
}
}
#endif
#ifdef CHECKTIME
/*
* checkout:
* Check each CHECKTIME seconds to see if the load is too high
*/
checkout(int sig)
{
static char *msgs[] = {
"The load is too high to be playing. Please leave in %0.1f minutes",
"Please save your game. You have %0.1f minutes",
"Last warning. You have %0.1f minutes to leave",
};
int checktime;
if (too_much()) {
if (author()) {
num_checks = 1;
chmsg("The load is rather high, O exaulted one");
}
else if (num_checks++ == 3)
fatal("Sorry. You took too long. You are dead\n");
checktime = (CHECKTIME * 60) / num_checks;
chmsg(msgs[num_checks - 1], ((double) checktime / 60.0));
}
else {
if (num_checks) {
num_checks = 0;
chmsg("The load has dropped back down. You have a reprieve");
}
checktime = (CHECKTIME * 60);
}
md_start_checkout_timer(checktime);
}
/*
* chmsg:
* checkout()'s version of msg. If we are in the middle of a
* shell, do a printf instead of a msg to a the refresh.
*/
/* VARARGS1 */
chmsg(char *fmt, int arg)
{
if (!in_shell)
msg(fmt, arg);
else {
printf(fmt, arg);
putchar('\n');
fflush(stdout);
}
}
#endif
#ifdef UCOUNT
/*
* ucount:
* count number of users on the system
*/
#include <utmp.h>
struct utmp buf;
int
ucount()
{
struct utmp *up;
FILE *utmp;
int count;
if ((utmp = fopen(UTMP, "r")) == NULL)
return 0;
up = &buf;
count = 0;
while (fread(up, 1, sizeof (*up), utmp) > 0)
if (buf.ut_name[0] != '\0')
count++;
fclose(utmp);
return count;
}
#endif
/*
* lock_sc:
* lock the score file. If it takes too long, ask the user if
* they care to wait. Return TRUE if the lock is successful.
*/
static FILE *lfd = NULL;
bool
lock_sc()
{
#if defined(SCOREFILE) && defined(LOCKFILE)
int cnt;
static struct stat sbuf;
char *lockfile = LOCKFILE;
over:
if ((lfd=fopen(lockfile, "w+")) != NULL)
return TRUE;
for (cnt = 0; cnt < 5; cnt++) {
md_sleep(1);
if ((lfd=fopen(lockfile, "w+")) != NULL)
return TRUE;
}
if (stat(lockfile, &sbuf) < 0) {
lfd=fopen(lockfile, "w+");
return TRUE;
}
if (time(NULL) - sbuf.st_mtime > 10) {
if (md_unlink(lockfile) < 0)
return FALSE;
goto over;
}
else {
printf("The score file is very busy. Do you want to wait longer\n");
printf("for it to become free so your score can get posted?\n");
printf("If so, type \"y\"\n");
(void) fgets(prbuf, MAXSTR, stdin);
if (prbuf[0] == 'y')
for (;;) {
if ((lfd=fopen(lockfile, "w+")) != 0)
return TRUE;
if (stat(lockfile, &sbuf) < 0) {
lfd=fopen(lockfile, "w+");
return TRUE;
}
if (time(NULL) - sbuf.st_mtime > 10) {
if (md_unlink(lockfile) < 0)
return FALSE;
}
md_sleep(1);
}
else
return FALSE;
}
#else
return TRUE;
#endif
}
/*
* unlock_sc:
* Unlock the score file
*/
void
unlock_sc()
{
#if defined(SCOREFILE) && defined(LOCKFILE)
if (lfd != NULL)
fclose(lfd);
lfd = NULL;
md_unlink(LOCKFILE);
#endif
}
/*
* flush_type:
* Flush typeahead for traps, etc.
*/
void
flush_type()
{
flushinp();
}