hack.main.c revision 1.11 1 1.11 dholland /* $NetBSD: hack.main.c,v 1.11 2009/06/07 18:30:39 dholland Exp $ */
2 1.4 christos
3 1.2 mycroft /*
4 1.8 jsm * Copyright (c) 1985, Stichting Centrum voor Wiskunde en Informatica,
5 1.8 jsm * Amsterdam
6 1.8 jsm * All rights reserved.
7 1.8 jsm *
8 1.8 jsm * Redistribution and use in source and binary forms, with or without
9 1.8 jsm * modification, are permitted provided that the following conditions are
10 1.8 jsm * met:
11 1.8 jsm *
12 1.8 jsm * - Redistributions of source code must retain the above copyright notice,
13 1.8 jsm * this list of conditions and the following disclaimer.
14 1.8 jsm *
15 1.8 jsm * - Redistributions in binary form must reproduce the above copyright
16 1.8 jsm * notice, this list of conditions and the following disclaimer in the
17 1.8 jsm * documentation and/or other materials provided with the distribution.
18 1.8 jsm *
19 1.8 jsm * - Neither the name of the Stichting Centrum voor Wiskunde en
20 1.8 jsm * Informatica, nor the names of its contributors may be used to endorse or
21 1.8 jsm * promote products derived from this software without specific prior
22 1.8 jsm * written permission.
23 1.8 jsm *
24 1.8 jsm * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
25 1.8 jsm * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
26 1.8 jsm * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
27 1.8 jsm * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
28 1.8 jsm * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29 1.8 jsm * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
30 1.8 jsm * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
31 1.8 jsm * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
32 1.8 jsm * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
33 1.8 jsm * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
34 1.8 jsm * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 1.8 jsm */
36 1.8 jsm
37 1.8 jsm /*
38 1.8 jsm * Copyright (c) 1982 Jay Fenlason <hack (at) gnu.org>
39 1.8 jsm * All rights reserved.
40 1.8 jsm *
41 1.8 jsm * Redistribution and use in source and binary forms, with or without
42 1.8 jsm * modification, are permitted provided that the following conditions
43 1.8 jsm * are met:
44 1.8 jsm * 1. Redistributions of source code must retain the above copyright
45 1.8 jsm * notice, this list of conditions and the following disclaimer.
46 1.8 jsm * 2. Redistributions in binary form must reproduce the above copyright
47 1.8 jsm * notice, this list of conditions and the following disclaimer in the
48 1.8 jsm * documentation and/or other materials provided with the distribution.
49 1.8 jsm * 3. The name of the author may not be used to endorse or promote products
50 1.8 jsm * derived from this software without specific prior written permission.
51 1.8 jsm *
52 1.8 jsm * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
53 1.8 jsm * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
54 1.8 jsm * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
55 1.8 jsm * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
56 1.8 jsm * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
57 1.8 jsm * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
58 1.8 jsm * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
59 1.8 jsm * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
60 1.8 jsm * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
61 1.8 jsm * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
62 1.2 mycroft */
63 1.2 mycroft
64 1.4 christos #include <sys/cdefs.h>
65 1.2 mycroft #ifndef lint
66 1.11 dholland __RCSID("$NetBSD: hack.main.c,v 1.11 2009/06/07 18:30:39 dholland Exp $");
67 1.4 christos #endif /* not lint */
68 1.1 cgd
69 1.1 cgd #include <signal.h>
70 1.4 christos #include <stdlib.h>
71 1.4 christos #include <unistd.h>
72 1.4 christos #include <fcntl.h>
73 1.1 cgd #include "hack.h"
74 1.4 christos #include "extern.h"
75 1.1 cgd
76 1.1 cgd #ifdef QUEST
77 1.1 cgd #define gamename "quest"
78 1.1 cgd #else
79 1.1 cgd #define gamename "hack"
80 1.1 cgd #endif
81 1.1 cgd
82 1.9 jsm int (*afternmv)(void);
83 1.9 jsm int (*occupation)(void);
84 1.6 jsm const char *occtxt; /* defined when occupation != NULL */
85 1.1 cgd
86 1.4 christos int hackpid; /* current pid */
87 1.4 christos int locknum; /* max num of players */
88 1.1 cgd #ifdef DEF_PAGER
89 1.6 jsm const char *catmore; /* default pager */
90 1.1 cgd #endif
91 1.4 christos char SAVEF[PL_NSIZ + 11] = "save/"; /* save/99999player */
92 1.4 christos char *hname; /* name of the game (argv[0] of call) */
93 1.4 christos char obuf[BUFSIZ]; /* BUFSIZ is defined in stdio.h */
94 1.4 christos
95 1.9 jsm int main(int, char *[]);
96 1.9 jsm static void chdirx(const char *, boolean);
97 1.4 christos
98 1.4 christos int
99 1.11 dholland main(int argc, char *argv[])
100 1.1 cgd {
101 1.4 christos int fd;
102 1.1 cgd #ifdef CHDIR
103 1.4 christos char *dir;
104 1.1 cgd #endif
105 1.1 cgd
106 1.6 jsm /* Check for dirty tricks with closed fds 0, 1, 2 */
107 1.6 jsm fd = open("/dev/null", O_RDONLY);
108 1.6 jsm if (fd < 3)
109 1.6 jsm exit(1);
110 1.6 jsm close(fd);
111 1.6 jsm
112 1.1 cgd hname = argv[0];
113 1.1 cgd hackpid = getpid();
114 1.1 cgd
115 1.1 cgd #ifdef CHDIR /* otherwise no chdir() */
116 1.1 cgd /*
117 1.1 cgd * See if we must change directory to the playground.
118 1.1 cgd * (Perhaps hack runs suid and playground is inaccessible
119 1.1 cgd * for the player.)
120 1.1 cgd * The environment variable HACKDIR is overridden by a
121 1.1 cgd * -d command line option (must be the first option given)
122 1.1 cgd */
123 1.1 cgd
124 1.1 cgd dir = getenv("HACKDIR");
125 1.4 christos if (argc > 1 && !strncmp(argv[1], "-d", 2)) {
126 1.1 cgd argc--;
127 1.1 cgd argv++;
128 1.4 christos dir = argv[0] + 2;
129 1.4 christos if (*dir == '=' || *dir == ':')
130 1.4 christos dir++;
131 1.4 christos if (!*dir && argc > 1) {
132 1.1 cgd argc--;
133 1.1 cgd argv++;
134 1.1 cgd dir = argv[0];
135 1.1 cgd }
136 1.4 christos if (!*dir)
137 1.4 christos error("Flag -d must be followed by a directory name.");
138 1.1 cgd }
139 1.1 cgd #endif
140 1.1 cgd
141 1.1 cgd /*
142 1.1 cgd * Who am i? Algorithm: 1. Use name as specified in HACKOPTIONS
143 1.1 cgd * 2. Use $USER or $LOGNAME (if 1. fails)
144 1.1 cgd * 3. Use getlogin() (if 2. fails)
145 1.1 cgd * The resulting name is overridden by command line options.
146 1.1 cgd * If everything fails, or if the resulting name is some generic
147 1.1 cgd * account like "games", "play", "player", "hack" then eventually
148 1.1 cgd * we'll ask him.
149 1.1 cgd * Note that we trust him here; it is possible to play under
150 1.1 cgd * somebody else's name.
151 1.1 cgd */
152 1.4 christos {
153 1.4 christos char *s;
154 1.1 cgd
155 1.4 christos initoptions();
156 1.4 christos if (!*plname && (s = getenv("USER")))
157 1.4 christos (void) strncpy(plname, s, sizeof(plname) - 1);
158 1.4 christos if (!*plname && (s = getenv("LOGNAME")))
159 1.4 christos (void) strncpy(plname, s, sizeof(plname) - 1);
160 1.4 christos if (!*plname && (s = getlogin()))
161 1.4 christos (void) strncpy(plname, s, sizeof(plname) - 1);
162 1.1 cgd }
163 1.1 cgd
164 1.1 cgd /*
165 1.1 cgd * Now we know the directory containing 'record' and
166 1.1 cgd * may do a prscore().
167 1.1 cgd */
168 1.4 christos if (argc > 1 && !strncmp(argv[1], "-s", 2)) {
169 1.1 cgd #ifdef CHDIR
170 1.4 christos chdirx(dir, 0);
171 1.1 cgd #endif
172 1.1 cgd prscore(argc, argv);
173 1.1 cgd exit(0);
174 1.1 cgd }
175 1.1 cgd /*
176 1.1 cgd * It seems he really wants to play.
177 1.1 cgd * Remember tty modes, to be restored on exit.
178 1.1 cgd */
179 1.1 cgd gettty();
180 1.4 christos setbuf(stdout, obuf);
181 1.1 cgd setrandom();
182 1.1 cgd startup();
183 1.1 cgd cls();
184 1.4 christos u.uhp = 1; /* prevent RIP on early quits */
185 1.4 christos u.ux = FAR; /* prevent nscr() */
186 1.1 cgd (void) signal(SIGHUP, hangup);
187 1.1 cgd
188 1.1 cgd /*
189 1.1 cgd * Find the creation date of this game,
190 1.1 cgd * so as to avoid restoring outdated savefiles.
191 1.1 cgd */
192 1.1 cgd gethdate(hname);
193 1.1 cgd
194 1.1 cgd /*
195 1.1 cgd * We cannot do chdir earlier, otherwise gethdate will fail.
196 1.1 cgd */
197 1.1 cgd #ifdef CHDIR
198 1.4 christos chdirx(dir, 1);
199 1.1 cgd #endif
200 1.1 cgd
201 1.1 cgd /*
202 1.1 cgd * Process options.
203 1.1 cgd */
204 1.4 christos while (argc > 1 && argv[1][0] == '-') {
205 1.1 cgd argv++;
206 1.1 cgd argc--;
207 1.4 christos switch (argv[0][1]) {
208 1.1 cgd #ifdef WIZARD
209 1.1 cgd case 'D':
210 1.4 christos /* if(!strcmp(getlogin(), WIZARD)) */
211 1.4 christos wizard = TRUE;
212 1.4 christos /*
213 1.4 christos * else printf("Sorry.\n");
214 1.4 christos */
215 1.1 cgd break;
216 1.1 cgd #endif
217 1.1 cgd #ifdef NEWS
218 1.1 cgd case 'n':
219 1.1 cgd flags.nonews = TRUE;
220 1.1 cgd break;
221 1.1 cgd #endif
222 1.1 cgd case 'u':
223 1.4 christos if (argv[0][2])
224 1.4 christos (void) strncpy(plname, argv[0] + 2, sizeof(plname) - 1);
225 1.4 christos else if (argc > 1) {
226 1.4 christos argc--;
227 1.4 christos argv++;
228 1.4 christos (void) strncpy(plname, argv[0], sizeof(plname) - 1);
229 1.1 cgd } else
230 1.1 cgd printf("Player name expected after -u\n");
231 1.1 cgd break;
232 1.1 cgd default:
233 1.1 cgd /* allow -T for Tourist, etc. */
234 1.4 christos (void) strncpy(pl_character, argv[0] + 1,
235 1.4 christos sizeof(pl_character) - 1);
236 1.1 cgd
237 1.1 cgd /* printf("Unknown option: %s\n", *argv); */
238 1.1 cgd }
239 1.1 cgd }
240 1.1 cgd
241 1.4 christos if (argc > 1)
242 1.1 cgd locknum = atoi(argv[1]);
243 1.1 cgd #ifdef MAX_NR_OF_PLAYERS
244 1.4 christos if (!locknum || locknum > MAX_NR_OF_PLAYERS)
245 1.1 cgd locknum = MAX_NR_OF_PLAYERS;
246 1.1 cgd #endif
247 1.1 cgd #ifdef DEF_PAGER
248 1.5 kleink if (((catmore = getenv("HACKPAGER")) == NULL &&
249 1.5 kleink (catmore = getenv("PAGER")) == NULL) ||
250 1.5 kleink catmore[0] == '\0')
251 1.1 cgd catmore = DEF_PAGER;
252 1.1 cgd #endif
253 1.1 cgd #ifdef MAIL
254 1.1 cgd getmailstatus();
255 1.1 cgd #endif
256 1.1 cgd #ifdef WIZARD
257 1.4 christos if (wizard)
258 1.4 christos (void) strcpy(plname, "wizard");
259 1.4 christos else
260 1.1 cgd #endif
261 1.4 christos if (!*plname || !strncmp(plname, "player", 4)
262 1.1 cgd || !strncmp(plname, "games", 4))
263 1.1 cgd askname();
264 1.1 cgd plnamesuffix(); /* strip suffix from name; calls askname() */
265 1.4 christos /* again if suffix was whole name */
266 1.4 christos /* accepts any suffix */
267 1.1 cgd #ifdef WIZARD
268 1.4 christos if (!wizard) {
269 1.1 cgd #endif
270 1.1 cgd /*
271 1.1 cgd * check for multiple games under the same name
272 1.1 cgd * (if !locknum) or check max nr of players (otherwise)
273 1.1 cgd */
274 1.4 christos (void) signal(SIGQUIT, SIG_IGN);
275 1.4 christos (void) signal(SIGINT, SIG_IGN);
276 1.4 christos if (!locknum)
277 1.4 christos (void) strcpy(lock, plname);
278 1.1 cgd getlock(); /* sets lock if locknum != 0 */
279 1.1 cgd #ifdef WIZARD
280 1.1 cgd } else {
281 1.4 christos char *sfoo;
282 1.4 christos (void) strcpy(lock, plname);
283 1.4 christos if ((sfoo = getenv("MAGIC")) != NULL)
284 1.4 christos while (*sfoo) {
285 1.4 christos switch (*sfoo++) {
286 1.4 christos case 'n':
287 1.4 christos (void) srandom(*sfoo++);
288 1.1 cgd break;
289 1.1 cgd }
290 1.1 cgd }
291 1.4 christos if ((sfoo = getenv("GENOCIDED")) != NULL) {
292 1.4 christos if (*sfoo == '!') {
293 1.6 jsm const struct permonst *pm = mons;
294 1.4 christos char *gp = genocided;
295 1.1 cgd
296 1.4 christos while (pm < mons + CMNUM + 2) {
297 1.4 christos if (!strchr(sfoo, pm->mlet))
298 1.1 cgd *gp++ = pm->mlet;
299 1.1 cgd pm++;
300 1.1 cgd }
301 1.1 cgd *gp = 0;
302 1.1 cgd } else
303 1.1 cgd (void) strcpy(genocided, sfoo);
304 1.1 cgd (void) strcpy(fut_geno, genocided);
305 1.1 cgd }
306 1.1 cgd }
307 1.1 cgd #endif
308 1.1 cgd setftty();
309 1.1 cgd (void) sprintf(SAVEF, "save/%d%s", getuid(), plname);
310 1.4 christos regularize(SAVEF + 5); /* avoid . or / in name */
311 1.6 jsm if ((fd = open(SAVEF, O_RDONLY)) >= 0 &&
312 1.4 christos (uptodate(fd) || unlink(SAVEF) == 666)) {
313 1.4 christos (void) signal(SIGINT, done1);
314 1.1 cgd pline("Restoring old save file...");
315 1.1 cgd (void) fflush(stdout);
316 1.4 christos if (!dorecover(fd))
317 1.1 cgd goto not_recovered;
318 1.1 cgd pline("Hello %s, welcome to %s!", plname, gamename);
319 1.1 cgd flags.move = 0;
320 1.1 cgd } else {
321 1.1 cgd not_recovered:
322 1.1 cgd fobj = fcobj = invent = 0;
323 1.1 cgd fmon = fallen_down = 0;
324 1.1 cgd ftrap = 0;
325 1.1 cgd fgold = 0;
326 1.1 cgd flags.ident = 1;
327 1.1 cgd init_objects();
328 1.1 cgd u_init();
329 1.1 cgd
330 1.4 christos (void) signal(SIGINT, done1);
331 1.1 cgd mklev();
332 1.1 cgd u.ux = xupstair;
333 1.1 cgd u.uy = yupstair;
334 1.1 cgd (void) inshop();
335 1.1 cgd setsee();
336 1.1 cgd flags.botlx = 1;
337 1.1 cgd makedog();
338 1.4 christos {
339 1.4 christos struct monst *mtmp;
340 1.4 christos if ((mtmp = m_at(u.ux, u.uy)) != NULL)
341 1.4 christos mnexto(mtmp); /* riv05!a3 */
342 1.1 cgd }
343 1.1 cgd seemons();
344 1.1 cgd #ifdef NEWS
345 1.4 christos if (flags.nonews || !readnews())
346 1.1 cgd /* after reading news we did docrt() already */
347 1.1 cgd #endif
348 1.1 cgd docrt();
349 1.1 cgd
350 1.1 cgd /* give welcome message before pickup messages */
351 1.1 cgd pline("Hello %s, welcome to %s!", plname, gamename);
352 1.1 cgd
353 1.1 cgd pickup(1);
354 1.4 christos read_engr_at(u.ux, u.uy);
355 1.1 cgd flags.move = 1;
356 1.1 cgd }
357 1.1 cgd
358 1.1 cgd flags.moonphase = phase_of_the_moon();
359 1.4 christos if (flags.moonphase == FULL_MOON) {
360 1.1 cgd pline("You are lucky! Full moon tonight.");
361 1.1 cgd u.uluck++;
362 1.4 christos } else if (flags.moonphase == NEW_MOON) {
363 1.1 cgd pline("Be careful! New moon tonight.");
364 1.1 cgd }
365 1.1 cgd initrack();
366 1.1 cgd
367 1.4 christos for (;;) {
368 1.4 christos if (flags.move) { /* actual time passed */
369 1.1 cgd
370 1.1 cgd settrack();
371 1.1 cgd
372 1.4 christos if (moves % 2 == 0 ||
373 1.4 christos (!(Fast & ~INTRINSIC) && (!Fast || rn2(3)))) {
374 1.1 cgd movemon();
375 1.4 christos if (!rn2(70))
376 1.4 christos (void) makemon((struct permonst *) 0, 0, 0);
377 1.1 cgd }
378 1.4 christos if (Glib)
379 1.4 christos glibr();
380 1.1 cgd timeout();
381 1.1 cgd ++moves;
382 1.4 christos if (flags.time)
383 1.4 christos flags.botl = 1;
384 1.4 christos if (u.uhp < 1) {
385 1.1 cgd pline("You die...");
386 1.1 cgd done("died");
387 1.1 cgd }
388 1.4 christos if (u.uhp * 10 < u.uhpmax && moves - wailmsg > 50) {
389 1.4 christos wailmsg = moves;
390 1.4 christos if (u.uhp == 1)
391 1.4 christos pline("You hear the wailing of the Banshee...");
392 1.4 christos else
393 1.4 christos pline("You hear the howling of the CwnAnnwn...");
394 1.1 cgd }
395 1.4 christos if (u.uhp < u.uhpmax) {
396 1.4 christos if (u.ulevel > 9) {
397 1.4 christos if (Regeneration || !(moves % 3)) {
398 1.4 christos flags.botl = 1;
399 1.4 christos u.uhp += rnd((int) u.ulevel - 9);
400 1.4 christos if (u.uhp > u.uhpmax)
401 1.4 christos u.uhp = u.uhpmax;
402 1.1 cgd }
403 1.4 christos } else if (Regeneration ||
404 1.4 christos (!(moves % (22 - u.ulevel * 2)))) {
405 1.1 cgd flags.botl = 1;
406 1.1 cgd u.uhp++;
407 1.1 cgd }
408 1.1 cgd }
409 1.4 christos if (Teleportation && !rn2(85))
410 1.4 christos tele();
411 1.4 christos if (Searching && multi >= 0)
412 1.4 christos (void) dosearch();
413 1.1 cgd gethungry();
414 1.1 cgd invault();
415 1.1 cgd amulet();
416 1.1 cgd }
417 1.4 christos if (multi < 0) {
418 1.4 christos if (!++multi) {
419 1.1 cgd pline(nomovemsg ? nomovemsg :
420 1.4 christos "You can move again.");
421 1.1 cgd nomovemsg = 0;
422 1.4 christos if (afternmv)
423 1.4 christos (*afternmv) ();
424 1.1 cgd afternmv = 0;
425 1.1 cgd }
426 1.1 cgd }
427 1.1 cgd find_ac();
428 1.1 cgd #ifndef QUEST
429 1.4 christos if (!flags.mv || Blind)
430 1.1 cgd #endif
431 1.1 cgd {
432 1.1 cgd seeobjs();
433 1.1 cgd seemons();
434 1.1 cgd nscr();
435 1.1 cgd }
436 1.4 christos if (flags.botl || flags.botlx)
437 1.4 christos bot();
438 1.1 cgd
439 1.1 cgd flags.move = 1;
440 1.1 cgd
441 1.4 christos if (multi >= 0 && occupation) {
442 1.4 christos if (monster_nearby())
443 1.1 cgd stop_occupation();
444 1.4 christos else if ((*occupation) () == 0)
445 1.1 cgd occupation = 0;
446 1.1 cgd continue;
447 1.1 cgd }
448 1.4 christos if (multi > 0) {
449 1.1 cgd #ifdef QUEST
450 1.4 christos if (flags.run >= 4)
451 1.4 christos finddir();
452 1.1 cgd #endif
453 1.1 cgd lookaround();
454 1.4 christos if (!multi) { /* lookaround may clear multi */
455 1.1 cgd flags.move = 0;
456 1.1 cgd continue;
457 1.1 cgd }
458 1.4 christos if (flags.mv) {
459 1.4 christos if (multi < COLNO && !--multi)
460 1.1 cgd flags.mv = flags.run = 0;
461 1.1 cgd domove();
462 1.1 cgd } else {
463 1.1 cgd --multi;
464 1.1 cgd rhack(save_cm);
465 1.1 cgd }
466 1.4 christos } else if (multi == 0) {
467 1.1 cgd #ifdef MAIL
468 1.1 cgd ckmailstatus();
469 1.1 cgd #endif
470 1.1 cgd rhack((char *) 0);
471 1.1 cgd }
472 1.4 christos if (multi && multi % 7 == 0)
473 1.1 cgd (void) fflush(stdout);
474 1.1 cgd }
475 1.1 cgd }
476 1.1 cgd
477 1.4 christos void
478 1.11 dholland glo(int foo)
479 1.1 cgd {
480 1.1 cgd /* construct the string xlock.n */
481 1.4 christos char *tf;
482 1.1 cgd
483 1.1 cgd tf = lock;
484 1.4 christos while (*tf && *tf != '.')
485 1.4 christos tf++;
486 1.1 cgd (void) sprintf(tf, ".%d", foo);
487 1.1 cgd }
488 1.1 cgd
489 1.1 cgd /*
490 1.1 cgd * plname is filled either by an option (-u Player or -uPlayer) or
491 1.1 cgd * explicitly (-w implies wizard) or by askname.
492 1.1 cgd * It may still contain a suffix denoting pl_character.
493 1.1 cgd */
494 1.4 christos void
495 1.11 dholland askname(void)
496 1.4 christos {
497 1.4 christos int c, ct;
498 1.1 cgd printf("\nWho are you? ");
499 1.1 cgd (void) fflush(stdout);
500 1.1 cgd ct = 0;
501 1.4 christos while ((c = getchar()) != '\n') {
502 1.4 christos if (c == EOF)
503 1.4 christos error("End of input\n");
504 1.1 cgd /* some people get confused when their erase char is not ^H */
505 1.4 christos if (c == '\010') {
506 1.4 christos if (ct)
507 1.4 christos ct--;
508 1.1 cgd continue;
509 1.1 cgd }
510 1.4 christos if (c != '-')
511 1.4 christos if (c < 'A' || (c > 'Z' && c < 'a') || c > 'z')
512 1.4 christos c = '_';
513 1.10 dholland if (ct < (int)sizeof(plname) - 1)
514 1.4 christos plname[ct++] = c;
515 1.1 cgd }
516 1.1 cgd plname[ct] = 0;
517 1.4 christos if (ct == 0)
518 1.4 christos askname();
519 1.1 cgd }
520 1.1 cgd
521 1.4 christos /* VARARGS1 */
522 1.4 christos void
523 1.4 christos impossible(const char *s, ...)
524 1.1 cgd {
525 1.4 christos va_list ap;
526 1.4 christos
527 1.4 christos va_start(ap, s);
528 1.4 christos vpline(s, ap);
529 1.4 christos va_end(ap);
530 1.1 cgd pline("Program in disorder - perhaps you'd better Quit.");
531 1.1 cgd }
532 1.1 cgd
533 1.1 cgd #ifdef CHDIR
534 1.1 cgd static void
535 1.11 dholland chdirx(const char *dir, boolean wr)
536 1.1 cgd {
537 1.1 cgd
538 1.1 cgd #ifdef SECURE
539 1.4 christos if (dir /* User specified directory? */
540 1.1 cgd #ifdef HACKDIR
541 1.4 christos && strcmp(dir, HACKDIR) /* and not the default? */
542 1.1 cgd #endif
543 1.1 cgd ) {
544 1.4 christos (void) setuid(getuid()); /* Ron Wessels */
545 1.1 cgd (void) setgid(getgid());
546 1.1 cgd }
547 1.1 cgd #endif
548 1.1 cgd
549 1.1 cgd #ifdef HACKDIR
550 1.4 christos if (dir == NULL)
551 1.1 cgd dir = HACKDIR;
552 1.1 cgd #endif
553 1.1 cgd
554 1.4 christos if (dir && chdir(dir) < 0) {
555 1.1 cgd perror(dir);
556 1.1 cgd error("Cannot chdir to %s.", dir);
557 1.1 cgd }
558 1.1 cgd /* warn the player if he cannot write the record file */
559 1.1 cgd /* perhaps we should also test whether . is writable */
560 1.1 cgd /* unfortunately the access systemcall is worthless */
561 1.4 christos if (wr) {
562 1.4 christos int fd;
563 1.1 cgd
564 1.4 christos if (dir == NULL)
565 1.4 christos dir = ".";
566 1.6 jsm if ((fd = open(RECORD, O_RDWR)) < 0) {
567 1.4 christos printf("Warning: cannot write %s/%s", dir, RECORD);
568 1.4 christos getret();
569 1.4 christos } else
570 1.4 christos (void) close(fd);
571 1.1 cgd }
572 1.1 cgd }
573 1.1 cgd #endif
574 1.1 cgd
575 1.4 christos void
576 1.11 dholland stop_occupation(void)
577 1.1 cgd {
578 1.4 christos if (occupation) {
579 1.1 cgd pline("You stop %s.", occtxt);
580 1.1 cgd occupation = 0;
581 1.1 cgd }
582 1.1 cgd }
583