hack.main.c revision 1.9 1 1.9 jsm /* $NetBSD: hack.main.c,v 1.9 2004/01/27 20:30:29 jsm 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.9 jsm __RCSID("$NetBSD: hack.main.c,v 1.9 2004/01/27 20:30:29 jsm 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.4 christos main(argc, argv)
100 1.4 christos int argc;
101 1.4 christos char *argv[];
102 1.1 cgd {
103 1.4 christos int fd;
104 1.1 cgd #ifdef CHDIR
105 1.4 christos char *dir;
106 1.1 cgd #endif
107 1.1 cgd
108 1.6 jsm /* Check for dirty tricks with closed fds 0, 1, 2 */
109 1.6 jsm fd = open("/dev/null", O_RDONLY);
110 1.6 jsm if (fd < 3)
111 1.6 jsm exit(1);
112 1.6 jsm close(fd);
113 1.6 jsm
114 1.1 cgd hname = argv[0];
115 1.1 cgd hackpid = getpid();
116 1.1 cgd
117 1.1 cgd #ifdef CHDIR /* otherwise no chdir() */
118 1.1 cgd /*
119 1.1 cgd * See if we must change directory to the playground.
120 1.1 cgd * (Perhaps hack runs suid and playground is inaccessible
121 1.1 cgd * for the player.)
122 1.1 cgd * The environment variable HACKDIR is overridden by a
123 1.1 cgd * -d command line option (must be the first option given)
124 1.1 cgd */
125 1.1 cgd
126 1.1 cgd dir = getenv("HACKDIR");
127 1.4 christos if (argc > 1 && !strncmp(argv[1], "-d", 2)) {
128 1.1 cgd argc--;
129 1.1 cgd argv++;
130 1.4 christos dir = argv[0] + 2;
131 1.4 christos if (*dir == '=' || *dir == ':')
132 1.4 christos dir++;
133 1.4 christos if (!*dir && argc > 1) {
134 1.1 cgd argc--;
135 1.1 cgd argv++;
136 1.1 cgd dir = argv[0];
137 1.1 cgd }
138 1.4 christos if (!*dir)
139 1.4 christos error("Flag -d must be followed by a directory name.");
140 1.1 cgd }
141 1.1 cgd #endif
142 1.1 cgd
143 1.1 cgd /*
144 1.1 cgd * Who am i? Algorithm: 1. Use name as specified in HACKOPTIONS
145 1.1 cgd * 2. Use $USER or $LOGNAME (if 1. fails)
146 1.1 cgd * 3. Use getlogin() (if 2. fails)
147 1.1 cgd * The resulting name is overridden by command line options.
148 1.1 cgd * If everything fails, or if the resulting name is some generic
149 1.1 cgd * account like "games", "play", "player", "hack" then eventually
150 1.1 cgd * we'll ask him.
151 1.1 cgd * Note that we trust him here; it is possible to play under
152 1.1 cgd * somebody else's name.
153 1.1 cgd */
154 1.4 christos {
155 1.4 christos char *s;
156 1.1 cgd
157 1.4 christos initoptions();
158 1.4 christos if (!*plname && (s = getenv("USER")))
159 1.4 christos (void) strncpy(plname, s, sizeof(plname) - 1);
160 1.4 christos if (!*plname && (s = getenv("LOGNAME")))
161 1.4 christos (void) strncpy(plname, s, sizeof(plname) - 1);
162 1.4 christos if (!*plname && (s = getlogin()))
163 1.4 christos (void) strncpy(plname, s, sizeof(plname) - 1);
164 1.1 cgd }
165 1.1 cgd
166 1.1 cgd /*
167 1.1 cgd * Now we know the directory containing 'record' and
168 1.1 cgd * may do a prscore().
169 1.1 cgd */
170 1.4 christos if (argc > 1 && !strncmp(argv[1], "-s", 2)) {
171 1.1 cgd #ifdef CHDIR
172 1.4 christos chdirx(dir, 0);
173 1.1 cgd #endif
174 1.1 cgd prscore(argc, argv);
175 1.1 cgd exit(0);
176 1.1 cgd }
177 1.1 cgd /*
178 1.1 cgd * It seems he really wants to play.
179 1.1 cgd * Remember tty modes, to be restored on exit.
180 1.1 cgd */
181 1.1 cgd gettty();
182 1.4 christos setbuf(stdout, obuf);
183 1.1 cgd setrandom();
184 1.1 cgd startup();
185 1.1 cgd cls();
186 1.4 christos u.uhp = 1; /* prevent RIP on early quits */
187 1.4 christos u.ux = FAR; /* prevent nscr() */
188 1.1 cgd (void) signal(SIGHUP, hangup);
189 1.1 cgd
190 1.1 cgd /*
191 1.1 cgd * Find the creation date of this game,
192 1.1 cgd * so as to avoid restoring outdated savefiles.
193 1.1 cgd */
194 1.1 cgd gethdate(hname);
195 1.1 cgd
196 1.1 cgd /*
197 1.1 cgd * We cannot do chdir earlier, otherwise gethdate will fail.
198 1.1 cgd */
199 1.1 cgd #ifdef CHDIR
200 1.4 christos chdirx(dir, 1);
201 1.1 cgd #endif
202 1.1 cgd
203 1.1 cgd /*
204 1.1 cgd * Process options.
205 1.1 cgd */
206 1.4 christos while (argc > 1 && argv[1][0] == '-') {
207 1.1 cgd argv++;
208 1.1 cgd argc--;
209 1.4 christos switch (argv[0][1]) {
210 1.1 cgd #ifdef WIZARD
211 1.1 cgd case 'D':
212 1.4 christos /* if(!strcmp(getlogin(), WIZARD)) */
213 1.4 christos wizard = TRUE;
214 1.4 christos /*
215 1.4 christos * else printf("Sorry.\n");
216 1.4 christos */
217 1.1 cgd break;
218 1.1 cgd #endif
219 1.1 cgd #ifdef NEWS
220 1.1 cgd case 'n':
221 1.1 cgd flags.nonews = TRUE;
222 1.1 cgd break;
223 1.1 cgd #endif
224 1.1 cgd case 'u':
225 1.4 christos if (argv[0][2])
226 1.4 christos (void) strncpy(plname, argv[0] + 2, sizeof(plname) - 1);
227 1.4 christos else if (argc > 1) {
228 1.4 christos argc--;
229 1.4 christos argv++;
230 1.4 christos (void) strncpy(plname, argv[0], sizeof(plname) - 1);
231 1.1 cgd } else
232 1.1 cgd printf("Player name expected after -u\n");
233 1.1 cgd break;
234 1.1 cgd default:
235 1.1 cgd /* allow -T for Tourist, etc. */
236 1.4 christos (void) strncpy(pl_character, argv[0] + 1,
237 1.4 christos sizeof(pl_character) - 1);
238 1.1 cgd
239 1.1 cgd /* printf("Unknown option: %s\n", *argv); */
240 1.1 cgd }
241 1.1 cgd }
242 1.1 cgd
243 1.4 christos if (argc > 1)
244 1.1 cgd locknum = atoi(argv[1]);
245 1.1 cgd #ifdef MAX_NR_OF_PLAYERS
246 1.4 christos if (!locknum || locknum > MAX_NR_OF_PLAYERS)
247 1.1 cgd locknum = MAX_NR_OF_PLAYERS;
248 1.1 cgd #endif
249 1.1 cgd #ifdef DEF_PAGER
250 1.5 kleink if (((catmore = getenv("HACKPAGER")) == NULL &&
251 1.5 kleink (catmore = getenv("PAGER")) == NULL) ||
252 1.5 kleink catmore[0] == '\0')
253 1.1 cgd catmore = DEF_PAGER;
254 1.1 cgd #endif
255 1.1 cgd #ifdef MAIL
256 1.1 cgd getmailstatus();
257 1.1 cgd #endif
258 1.1 cgd #ifdef WIZARD
259 1.4 christos if (wizard)
260 1.4 christos (void) strcpy(plname, "wizard");
261 1.4 christos else
262 1.1 cgd #endif
263 1.4 christos if (!*plname || !strncmp(plname, "player", 4)
264 1.1 cgd || !strncmp(plname, "games", 4))
265 1.1 cgd askname();
266 1.1 cgd plnamesuffix(); /* strip suffix from name; calls askname() */
267 1.4 christos /* again if suffix was whole name */
268 1.4 christos /* accepts any suffix */
269 1.1 cgd #ifdef WIZARD
270 1.4 christos if (!wizard) {
271 1.1 cgd #endif
272 1.1 cgd /*
273 1.1 cgd * check for multiple games under the same name
274 1.1 cgd * (if !locknum) or check max nr of players (otherwise)
275 1.1 cgd */
276 1.4 christos (void) signal(SIGQUIT, SIG_IGN);
277 1.4 christos (void) signal(SIGINT, SIG_IGN);
278 1.4 christos if (!locknum)
279 1.4 christos (void) strcpy(lock, plname);
280 1.1 cgd getlock(); /* sets lock if locknum != 0 */
281 1.1 cgd #ifdef WIZARD
282 1.1 cgd } else {
283 1.4 christos char *sfoo;
284 1.4 christos (void) strcpy(lock, plname);
285 1.4 christos if ((sfoo = getenv("MAGIC")) != NULL)
286 1.4 christos while (*sfoo) {
287 1.4 christos switch (*sfoo++) {
288 1.4 christos case 'n':
289 1.4 christos (void) srandom(*sfoo++);
290 1.1 cgd break;
291 1.1 cgd }
292 1.1 cgd }
293 1.4 christos if ((sfoo = getenv("GENOCIDED")) != NULL) {
294 1.4 christos if (*sfoo == '!') {
295 1.6 jsm const struct permonst *pm = mons;
296 1.4 christos char *gp = genocided;
297 1.1 cgd
298 1.4 christos while (pm < mons + CMNUM + 2) {
299 1.4 christos if (!strchr(sfoo, pm->mlet))
300 1.1 cgd *gp++ = pm->mlet;
301 1.1 cgd pm++;
302 1.1 cgd }
303 1.1 cgd *gp = 0;
304 1.1 cgd } else
305 1.1 cgd (void) strcpy(genocided, sfoo);
306 1.1 cgd (void) strcpy(fut_geno, genocided);
307 1.1 cgd }
308 1.1 cgd }
309 1.1 cgd #endif
310 1.1 cgd setftty();
311 1.1 cgd (void) sprintf(SAVEF, "save/%d%s", getuid(), plname);
312 1.4 christos regularize(SAVEF + 5); /* avoid . or / in name */
313 1.6 jsm if ((fd = open(SAVEF, O_RDONLY)) >= 0 &&
314 1.4 christos (uptodate(fd) || unlink(SAVEF) == 666)) {
315 1.4 christos (void) signal(SIGINT, done1);
316 1.1 cgd pline("Restoring old save file...");
317 1.1 cgd (void) fflush(stdout);
318 1.4 christos if (!dorecover(fd))
319 1.1 cgd goto not_recovered;
320 1.1 cgd pline("Hello %s, welcome to %s!", plname, gamename);
321 1.1 cgd flags.move = 0;
322 1.1 cgd } else {
323 1.1 cgd not_recovered:
324 1.1 cgd fobj = fcobj = invent = 0;
325 1.1 cgd fmon = fallen_down = 0;
326 1.1 cgd ftrap = 0;
327 1.1 cgd fgold = 0;
328 1.1 cgd flags.ident = 1;
329 1.1 cgd init_objects();
330 1.1 cgd u_init();
331 1.1 cgd
332 1.4 christos (void) signal(SIGINT, done1);
333 1.1 cgd mklev();
334 1.1 cgd u.ux = xupstair;
335 1.1 cgd u.uy = yupstair;
336 1.1 cgd (void) inshop();
337 1.1 cgd setsee();
338 1.1 cgd flags.botlx = 1;
339 1.1 cgd makedog();
340 1.4 christos {
341 1.4 christos struct monst *mtmp;
342 1.4 christos if ((mtmp = m_at(u.ux, u.uy)) != NULL)
343 1.4 christos mnexto(mtmp); /* riv05!a3 */
344 1.1 cgd }
345 1.1 cgd seemons();
346 1.1 cgd #ifdef NEWS
347 1.4 christos if (flags.nonews || !readnews())
348 1.1 cgd /* after reading news we did docrt() already */
349 1.1 cgd #endif
350 1.1 cgd docrt();
351 1.1 cgd
352 1.1 cgd /* give welcome message before pickup messages */
353 1.1 cgd pline("Hello %s, welcome to %s!", plname, gamename);
354 1.1 cgd
355 1.1 cgd pickup(1);
356 1.4 christos read_engr_at(u.ux, u.uy);
357 1.1 cgd flags.move = 1;
358 1.1 cgd }
359 1.1 cgd
360 1.1 cgd flags.moonphase = phase_of_the_moon();
361 1.4 christos if (flags.moonphase == FULL_MOON) {
362 1.1 cgd pline("You are lucky! Full moon tonight.");
363 1.1 cgd u.uluck++;
364 1.4 christos } else if (flags.moonphase == NEW_MOON) {
365 1.1 cgd pline("Be careful! New moon tonight.");
366 1.1 cgd }
367 1.1 cgd initrack();
368 1.1 cgd
369 1.4 christos for (;;) {
370 1.4 christos if (flags.move) { /* actual time passed */
371 1.1 cgd
372 1.1 cgd settrack();
373 1.1 cgd
374 1.4 christos if (moves % 2 == 0 ||
375 1.4 christos (!(Fast & ~INTRINSIC) && (!Fast || rn2(3)))) {
376 1.1 cgd movemon();
377 1.4 christos if (!rn2(70))
378 1.4 christos (void) makemon((struct permonst *) 0, 0, 0);
379 1.1 cgd }
380 1.4 christos if (Glib)
381 1.4 christos glibr();
382 1.1 cgd timeout();
383 1.1 cgd ++moves;
384 1.4 christos if (flags.time)
385 1.4 christos flags.botl = 1;
386 1.4 christos if (u.uhp < 1) {
387 1.1 cgd pline("You die...");
388 1.1 cgd done("died");
389 1.1 cgd }
390 1.4 christos if (u.uhp * 10 < u.uhpmax && moves - wailmsg > 50) {
391 1.4 christos wailmsg = moves;
392 1.4 christos if (u.uhp == 1)
393 1.4 christos pline("You hear the wailing of the Banshee...");
394 1.4 christos else
395 1.4 christos pline("You hear the howling of the CwnAnnwn...");
396 1.1 cgd }
397 1.4 christos if (u.uhp < u.uhpmax) {
398 1.4 christos if (u.ulevel > 9) {
399 1.4 christos if (Regeneration || !(moves % 3)) {
400 1.4 christos flags.botl = 1;
401 1.4 christos u.uhp += rnd((int) u.ulevel - 9);
402 1.4 christos if (u.uhp > u.uhpmax)
403 1.4 christos u.uhp = u.uhpmax;
404 1.1 cgd }
405 1.4 christos } else if (Regeneration ||
406 1.4 christos (!(moves % (22 - u.ulevel * 2)))) {
407 1.1 cgd flags.botl = 1;
408 1.1 cgd u.uhp++;
409 1.1 cgd }
410 1.1 cgd }
411 1.4 christos if (Teleportation && !rn2(85))
412 1.4 christos tele();
413 1.4 christos if (Searching && multi >= 0)
414 1.4 christos (void) dosearch();
415 1.1 cgd gethungry();
416 1.1 cgd invault();
417 1.1 cgd amulet();
418 1.1 cgd }
419 1.4 christos if (multi < 0) {
420 1.4 christos if (!++multi) {
421 1.1 cgd pline(nomovemsg ? nomovemsg :
422 1.4 christos "You can move again.");
423 1.1 cgd nomovemsg = 0;
424 1.4 christos if (afternmv)
425 1.4 christos (*afternmv) ();
426 1.1 cgd afternmv = 0;
427 1.1 cgd }
428 1.1 cgd }
429 1.1 cgd find_ac();
430 1.1 cgd #ifndef QUEST
431 1.4 christos if (!flags.mv || Blind)
432 1.1 cgd #endif
433 1.1 cgd {
434 1.1 cgd seeobjs();
435 1.1 cgd seemons();
436 1.1 cgd nscr();
437 1.1 cgd }
438 1.4 christos if (flags.botl || flags.botlx)
439 1.4 christos bot();
440 1.1 cgd
441 1.1 cgd flags.move = 1;
442 1.1 cgd
443 1.4 christos if (multi >= 0 && occupation) {
444 1.4 christos if (monster_nearby())
445 1.1 cgd stop_occupation();
446 1.4 christos else if ((*occupation) () == 0)
447 1.1 cgd occupation = 0;
448 1.1 cgd continue;
449 1.1 cgd }
450 1.4 christos if (multi > 0) {
451 1.1 cgd #ifdef QUEST
452 1.4 christos if (flags.run >= 4)
453 1.4 christos finddir();
454 1.1 cgd #endif
455 1.1 cgd lookaround();
456 1.4 christos if (!multi) { /* lookaround may clear multi */
457 1.1 cgd flags.move = 0;
458 1.1 cgd continue;
459 1.1 cgd }
460 1.4 christos if (flags.mv) {
461 1.4 christos if (multi < COLNO && !--multi)
462 1.1 cgd flags.mv = flags.run = 0;
463 1.1 cgd domove();
464 1.1 cgd } else {
465 1.1 cgd --multi;
466 1.1 cgd rhack(save_cm);
467 1.1 cgd }
468 1.4 christos } else if (multi == 0) {
469 1.1 cgd #ifdef MAIL
470 1.1 cgd ckmailstatus();
471 1.1 cgd #endif
472 1.1 cgd rhack((char *) 0);
473 1.1 cgd }
474 1.4 christos if (multi && multi % 7 == 0)
475 1.1 cgd (void) fflush(stdout);
476 1.1 cgd }
477 1.1 cgd }
478 1.1 cgd
479 1.4 christos void
480 1.1 cgd glo(foo)
481 1.4 christos int foo;
482 1.1 cgd {
483 1.1 cgd /* construct the string xlock.n */
484 1.4 christos char *tf;
485 1.1 cgd
486 1.1 cgd tf = lock;
487 1.4 christos while (*tf && *tf != '.')
488 1.4 christos tf++;
489 1.1 cgd (void) sprintf(tf, ".%d", foo);
490 1.1 cgd }
491 1.1 cgd
492 1.1 cgd /*
493 1.1 cgd * plname is filled either by an option (-u Player or -uPlayer) or
494 1.1 cgd * explicitly (-w implies wizard) or by askname.
495 1.1 cgd * It may still contain a suffix denoting pl_character.
496 1.1 cgd */
497 1.4 christos void
498 1.4 christos askname()
499 1.4 christos {
500 1.4 christos int c, ct;
501 1.1 cgd printf("\nWho are you? ");
502 1.1 cgd (void) fflush(stdout);
503 1.1 cgd ct = 0;
504 1.4 christos while ((c = getchar()) != '\n') {
505 1.4 christos if (c == EOF)
506 1.4 christos error("End of input\n");
507 1.1 cgd /* some people get confused when their erase char is not ^H */
508 1.4 christos if (c == '\010') {
509 1.4 christos if (ct)
510 1.4 christos ct--;
511 1.1 cgd continue;
512 1.1 cgd }
513 1.4 christos if (c != '-')
514 1.4 christos if (c < 'A' || (c > 'Z' && c < 'a') || c > 'z')
515 1.4 christos c = '_';
516 1.4 christos if (ct < sizeof(plname) - 1)
517 1.4 christos plname[ct++] = c;
518 1.1 cgd }
519 1.1 cgd plname[ct] = 0;
520 1.4 christos if (ct == 0)
521 1.4 christos askname();
522 1.1 cgd }
523 1.1 cgd
524 1.4 christos /* VARARGS1 */
525 1.4 christos void
526 1.4 christos impossible(const char *s, ...)
527 1.1 cgd {
528 1.4 christos va_list ap;
529 1.4 christos
530 1.4 christos va_start(ap, s);
531 1.4 christos vpline(s, ap);
532 1.4 christos va_end(ap);
533 1.1 cgd pline("Program in disorder - perhaps you'd better Quit.");
534 1.1 cgd }
535 1.1 cgd
536 1.1 cgd #ifdef CHDIR
537 1.1 cgd static void
538 1.1 cgd chdirx(dir, wr)
539 1.6 jsm const char *dir;
540 1.4 christos boolean wr;
541 1.1 cgd {
542 1.1 cgd
543 1.1 cgd #ifdef SECURE
544 1.4 christos if (dir /* User specified directory? */
545 1.1 cgd #ifdef HACKDIR
546 1.4 christos && strcmp(dir, HACKDIR) /* and not the default? */
547 1.1 cgd #endif
548 1.1 cgd ) {
549 1.4 christos (void) setuid(getuid()); /* Ron Wessels */
550 1.1 cgd (void) setgid(getgid());
551 1.1 cgd }
552 1.1 cgd #endif
553 1.1 cgd
554 1.1 cgd #ifdef HACKDIR
555 1.4 christos if (dir == NULL)
556 1.1 cgd dir = HACKDIR;
557 1.1 cgd #endif
558 1.1 cgd
559 1.4 christos if (dir && chdir(dir) < 0) {
560 1.1 cgd perror(dir);
561 1.1 cgd error("Cannot chdir to %s.", dir);
562 1.1 cgd }
563 1.1 cgd /* warn the player if he cannot write the record file */
564 1.1 cgd /* perhaps we should also test whether . is writable */
565 1.1 cgd /* unfortunately the access systemcall is worthless */
566 1.4 christos if (wr) {
567 1.4 christos int fd;
568 1.1 cgd
569 1.4 christos if (dir == NULL)
570 1.4 christos dir = ".";
571 1.6 jsm if ((fd = open(RECORD, O_RDWR)) < 0) {
572 1.4 christos printf("Warning: cannot write %s/%s", dir, RECORD);
573 1.4 christos getret();
574 1.4 christos } else
575 1.4 christos (void) close(fd);
576 1.1 cgd }
577 1.1 cgd }
578 1.1 cgd #endif
579 1.1 cgd
580 1.4 christos void
581 1.1 cgd stop_occupation()
582 1.1 cgd {
583 1.4 christos if (occupation) {
584 1.1 cgd pline("You stop %s.", occtxt);
585 1.1 cgd occupation = 0;
586 1.1 cgd }
587 1.1 cgd }
588