forked from sgrunt/GruntHack
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathunixmain.c
523 lines (481 loc) · 11.7 KB
/
unixmain.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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
/* SCCS Id: @(#)unixmain.c 3.4 1997/01/22 */
/* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
/* NetHack may be freely redistributed. See license for details. */
/* main.c - Unix NetHack */
#include "hack.h"
#include "dlb.h"
#include <sys/stat.h>
#include <signal.h>
#include <pwd.h>
#ifndef O_RDONLY
#include <fcntl.h>
#endif
#if !defined(_BULL_SOURCE) && !defined(__sgi) && !defined(_M_UNIX)
# if !defined(SUNOS4) && !(defined(ULTRIX) && defined(__GNUC__))
# if defined(POSIX_TYPES) || defined(SVR4) || defined(HPUX)
extern struct passwd *FDECL(getpwuid,(uid_t));
# else
extern struct passwd *FDECL(getpwuid,(int));
# endif
# endif
#endif
extern struct passwd *FDECL(getpwnam,(const char *));
#ifdef CHDIR
static void FDECL(chdirx, (const char *,BOOLEAN_P));
#endif /* CHDIR */
static boolean NDECL(whoami);
static void FDECL(process_options, (int, char **));
#ifdef _M_UNIX
extern void NDECL(check_sco_console);
extern void NDECL(init_sco_cons);
#endif
#ifdef __linux__
extern void NDECL(check_linux_console);
extern void NDECL(init_linux_cons);
#endif
static void NDECL(wd_message);
#ifdef WIZARD
static boolean wiz_error_flag = FALSE;
#endif
int
main(argc,argv)
int argc;
char *argv[];
{
register int fd;
#ifdef CHDIR
register char *dir;
#endif
boolean exact_username;
#ifdef SIMPLE_MAIL
char* e_simple = NULL;
#endif
#if defined(__APPLE__)
/* special hack to change working directory to a resource fork when
running from finder --sam */
#define MAC_PATH_VALUE ".app/Contents/MacOS/"
char mac_cwd[1024], *mac_exe = argv[0], *mac_tmp;
int arg0_len = strlen(mac_exe), mac_tmp_len, mac_lhs_len=0;
getcwd(mac_cwd, 1024);
if(mac_exe[0] == '/' && !strcmp(mac_cwd, "/")) {
if((mac_exe = strrchr(mac_exe, '/')))
mac_exe++;
else
mac_exe = argv[0];
mac_tmp_len = (strlen(mac_exe) * 2) + strlen(MAC_PATH_VALUE);
if(mac_tmp_len <= arg0_len) {
mac_tmp = malloc(mac_tmp_len + 1);
sprintf(mac_tmp, "%s%s%s", mac_exe, MAC_PATH_VALUE, mac_exe);
if(!strcmp(argv[0] + (arg0_len - mac_tmp_len), mac_tmp)) {
mac_lhs_len = (arg0_len - mac_tmp_len) + strlen(mac_exe) + 5;
if(mac_lhs_len > mac_tmp_len - 1)
mac_tmp = realloc(mac_tmp, mac_lhs_len);
strncpy(mac_tmp, argv[0], mac_lhs_len);
mac_tmp[mac_lhs_len] = '\0';
chdir(mac_tmp);
}
free(mac_tmp);
}
}
#endif
#ifdef SIMPLE_MAIL
/* figure this out early */
e_simple = nh_getenv("SIMPLEMAIL");
iflags.simplemail = (e_simple ? 1 : 0);
#endif
hname = argv[0];
hackpid = getpid();
(void) umask(0777 & ~FCMASK);
choose_windows(DEFAULT_WINDOW_SYS);
#ifdef CHDIR /* otherwise no chdir() */
/*
* See if we must change directory to the playground.
* (Perhaps hack runs suid and playground is inaccessible
* for the player.)
* The environment variable HACKDIR is overridden by a
* -d command line option (must be the first option given)
*/
dir = nh_getenv("NETHACKDIR");
if (!dir) dir = nh_getenv("HACKDIR");
#endif
if(argc > 1) {
#ifdef CHDIR
if (!strncmp(argv[1], "-d", 2) && argv[1][2] != 'e') {
/* avoid matching "-dec" for DECgraphics; since the man page
* says -d directory, hope nobody's using -desomething_else
*/
argc--;
argv++;
dir = argv[0]+2;
if(*dir == '=' || *dir == ':') dir++;
if(!*dir && argc > 1) {
argc--;
argv++;
dir = argv[0];
}
if(!*dir)
error("Flag -d must be followed by a directory name.");
}
if (argc > 1)
#endif /* CHDIR */
/*
* Now we know the directory containing 'record' and
* may do a prscore(). Exclude `-style' - it's a Qt option.
*/
if (!strncmp(argv[1], "-s", 2) && strncmp(argv[1], "-style", 6)) {
#ifdef CHDIR
chdirx(dir,0);
#endif
prscore(argc, argv);
exit(EXIT_SUCCESS);
}
}
/*
* Change directories before we initialize the window system so
* we can find the tile file.
*/
#ifdef CHDIR
chdirx(dir,1);
#endif
#ifdef _M_UNIX
check_sco_console();
#endif
#ifdef __linux__
check_linux_console();
#endif
initoptions();
init_nhwindows(&argc,argv);
exact_username = whoami();
#ifdef _M_UNIX
init_sco_cons();
#endif
#ifdef __linux__
init_linux_cons();
#endif
/*
* It seems you really want to play.
*/
u.uhp = 1; /* prevent RIP on early quits */
(void) signal(SIGHUP, (SIG_RET_TYPE) hangup);
#ifdef SIGXCPU
(void) signal(SIGXCPU, (SIG_RET_TYPE) hangup);
#endif
process_options(argc, argv); /* command line options */
#ifdef DEF_PAGER
if(!(catmore = nh_getenv("HACKPAGER")) && !(catmore = nh_getenv("PAGER")))
catmore = DEF_PAGER;
#endif
#ifdef MAIL
getmailstatus();
#endif
#ifdef WIZARD
if (wizard)
Strcpy(plname, "wizard");
else
#endif
if(!*plname || !strncmp(plname, "player", 4)
|| !strncmp(plname, "games", 4)) {
askname();
} else if (exact_username) {
/* guard against user names with hyphens in them */
int len = strlen(plname);
/* append the current role, if any, so that last dash is ours */
if (++len < sizeof plname)
(void)strncat(strcat(plname, "-"),
pl_character, sizeof plname - len - 1);
}
plnamesuffix(); /* strip suffix from name; calls askname() */
/* again if suffix was whole name */
/* accepts any suffix */
#ifdef WIZARD
if(!wizard) {
#endif
/*
* check for multiple games under the same name
* (if !locknum) or check max nr of players (otherwise)
*/
(void) signal(SIGQUIT,SIG_IGN);
(void) signal(SIGINT,SIG_IGN);
if(!locknum)
Sprintf(lock, "%d%s", (int)getuid(), plname);
getlock();
#ifdef WIZARD
} else {
Sprintf(lock, "%d%s", (int)getuid(), plname);
getlock();
}
#endif /* WIZARD */
dlb_init(); /* must be before newgame() */
/*
* Initialization of the boundaries of the mazes
* Both boundaries have to be even.
*/
x_maze_max = COLNO-1;
if (x_maze_max % 2)
x_maze_max--;
y_maze_max = ROWNO-1;
if (y_maze_max % 2)
y_maze_max--;
/*
* Initialize the vision system. This must be before mklev() on a
* new game or before a level restore on a saved game.
*/
vision_init();
display_gamewindows();
if ((fd = restore_saved_game()) >= 0) {
#ifdef WIZARD
/* Since wizard is actually flags.debug, restoring might
* overwrite it.
*/
boolean remember_wiz_mode = wizard;
#endif
const char *fq_save = fqname(SAVEF, SAVEPREFIX, 1);
(void) chmod(fq_save,0); /* disallow parallel restores */
(void) signal(SIGINT, (SIG_RET_TYPE) done1);
#ifdef NEWS
if(iflags.news) {
display_file(NEWS, FALSE);
iflags.news = FALSE; /* in case dorecover() fails */
}
#endif
pline("Restoring save file...");
mark_synch(); /* flush output */
if(!dorecover(fd))
goto not_recovered;
#ifdef WIZARD
if(!wizard && remember_wiz_mode) wizard = TRUE;
#endif
check_special_room(FALSE);
wd_message();
if (discover || wizard) {
if(yn("Do you want to keep the save file?") == 'n')
(void) delete_savefile();
else {
(void) chmod(fq_save,FCMASK); /* back to readable */
compress(fq_save);
}
}
flags.move = 0;
} else {
not_recovered:
player_selection();
newgame();
wd_message();
flags.move = 0;
set_wear();
(void) pickup(1);
}
moveloop();
exit(EXIT_SUCCESS);
/*NOTREACHED*/
return(0);
}
static void
process_options(argc, argv)
int argc;
char *argv[];
{
int i;
/*
* Process options.
*/
while(argc > 1 && argv[1][0] == '-'){
argv++;
argc--;
switch(argv[0][1]){
case 'D':
#ifdef WIZARD
wizard = TRUE;
break;
#endif
case 'X':
discover = TRUE;
break;
#ifdef NEWS
case 'n':
iflags.news = FALSE;
break;
#endif
case 'u':
if(argv[0][2])
(void) strncpy(plname, argv[0]+2, sizeof(plname)-1);
else if(argc > 1) {
argc--;
argv++;
(void) strncpy(plname, argv[0], sizeof(plname)-1);
} else
raw_print("Player name expected after -u");
break;
case 'I':
case 'i':
if (!strncmpi(argv[0]+1, "IBM", 3))
switch_graphics(IBM_GRAPHICS);
break;
/* case 'D': */
case 'd':
if (!strncmpi(argv[0]+1, "DEC", 3))
switch_graphics(DEC_GRAPHICS);
break;
case 'p': /* profession (role) */
if (argv[0][2]) {
if ((i = str2role(&argv[0][2])) >= 0)
flags.initrole = i;
} else if (argc > 1) {
argc--;
argv++;
if ((i = str2role(argv[0])) >= 0)
flags.initrole = i;
}
break;
case 'r': /* race */
if (argv[0][2]) {
if ((i = str2race(&argv[0][2])) >= 0)
flags.initrace = i;
} else if (argc > 1) {
argc--;
argv++;
if ((i = str2race(argv[0])) >= 0)
flags.initrace = i;
}
break;
case '@':
flags.randomall = 1;
break;
default:
if ((i = str2role(&argv[0][1])) >= 0) {
flags.initrole = i;
break;
}
/* else raw_printf("Unknown option: %s", *argv); */
}
}
if(argc > 1)
locknum = atoi(argv[1]);
#ifdef MAX_NR_OF_PLAYERS
if(!locknum || locknum > MAX_NR_OF_PLAYERS)
locknum = MAX_NR_OF_PLAYERS;
#endif
}
#ifdef CHDIR
static void
chdirx(dir, wr)
const char *dir;
boolean wr;
{
if (dir /* User specified directory? */
# ifdef HACKDIR
&& strcmp(dir, HACKDIR) /* and not the default? */
# endif
) {
# ifdef SECURE
(void) setgid(getgid());
(void) setuid(getuid()); /* Ron Wessels */
# endif
} else {
/* non-default data files is a sign that scores may not be
* compatible, or perhaps that a binary not fitting this
* system's layout is being used.
*/
# ifdef VAR_PLAYGROUND
int len = strlen(VAR_PLAYGROUND);
fqn_prefix[SCOREPREFIX] = (char *)alloc(len+2);
Strcpy(fqn_prefix[SCOREPREFIX], VAR_PLAYGROUND);
if (fqn_prefix[SCOREPREFIX][len-1] != '/') {
fqn_prefix[SCOREPREFIX][len] = '/';
fqn_prefix[SCOREPREFIX][len+1] = '\0';
}
# endif
}
# ifdef HACKDIR
if (dir == (const char *)0)
dir = HACKDIR;
# endif
if (dir && chdir(dir) < 0) {
perror(dir);
error("Cannot chdir to %s.", dir);
}
/* warn the player if we can't write the record file */
/* perhaps we should also test whether . is writable */
/* unfortunately the access system-call is worthless */
if (wr) {
# ifdef VAR_PLAYGROUND
fqn_prefix[LEVELPREFIX] = fqn_prefix[SCOREPREFIX];
fqn_prefix[SAVEPREFIX] = fqn_prefix[SCOREPREFIX];
fqn_prefix[BONESPREFIX] = fqn_prefix[SCOREPREFIX];
fqn_prefix[LOCKPREFIX] = fqn_prefix[SCOREPREFIX];
fqn_prefix[TROUBLEPREFIX] = fqn_prefix[SCOREPREFIX];
# endif
check_recordfile(dir);
}
}
#endif /* CHDIR */
static boolean
whoami() {
/*
* Who am i? Algorithm: 1. Use name as specified in NETHACKOPTIONS
* 2. Use $USER or $LOGNAME (if 1. fails)
* 3. Use getlogin() (if 2. fails)
* The resulting name is overridden by command line options.
* If everything fails, or if the resulting name is some generic
* account like "games", "play", "player", "hack" then eventually
* we'll ask him.
* Note that we trust the user here; it is possible to play under
* somebody else's name.
*/
register char *s;
if (*plname) return FALSE;
if(/* !*plname && */ (s = nh_getenv("USER")))
(void) strncpy(plname, s, sizeof(plname)-1);
if(!*plname && (s = nh_getenv("LOGNAME")))
(void) strncpy(plname, s, sizeof(plname)-1);
if(!*plname && (s = getlogin()))
(void) strncpy(plname, s, sizeof(plname)-1);
return TRUE;
}
#ifdef PORT_HELP
void
port_help()
{
/*
* Display unix-specific help. Just show contents of the helpfile
* named by PORT_HELP.
*/
display_file(PORT_HELP, TRUE);
}
#endif
static void
wd_message()
{
#ifdef WIZARD
if (wiz_error_flag) {
pline("Only user \"%s\" may access debug (wizard) mode.",
# ifndef KR1ED
WIZARD);
# else
WIZARD_NAME);
# endif
pline("Entering discovery mode instead.");
} else
#endif
if (discover)
You("are in non-scoring discovery mode.");
}
/*
* Add a slash to any name not ending in /. There must
* be room for the /
*/
void
append_slash(name)
char *name;
{
char *ptr;
if (!*name)
return;
ptr = name + (strlen(name) - 1);
if (*ptr != '/') {
*++ptr = '/';
*++ptr = '\0';
}
return;
}
/*unixmain.c*/