hack.main.c revision 1.13 1 1.13 dholland /* $NetBSD: hack.main.c,v 1.13 2009/06/29 23:05:33 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.13 dholland __RCSID("$NetBSD: hack.main.c,v 1.13 2009/06/29 23:05:33 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.13 dholland (void) strlcpy(genocided, sfoo,
304 1.13 dholland sizeof(genocided));
305 1.1 cgd (void) strcpy(fut_geno, genocided);
306 1.1 cgd }
307 1.1 cgd }
308 1.1 cgd #endif
309 1.1 cgd setftty();
310 1.12 dholland (void) snprintf(SAVEF, sizeof(SAVEF), "save/%d%s", getuid(), plname);
311 1.4 christos regularize(SAVEF + 5); /* avoid . or / in name */
312 1.6 jsm if ((fd = open(SAVEF, O_RDONLY)) >= 0 &&
313 1.4 christos (uptodate(fd) || unlink(SAVEF) == 666)) {
314 1.4 christos (void) signal(SIGINT, done1);
315 1.1 cgd pline("Restoring old save file...");
316 1.1 cgd (void) fflush(stdout);
317 1.4 christos if (!dorecover(fd))
318 1.1 cgd goto not_recovered;
319 1.1 cgd pline("Hello %s, welcome to %s!", plname, gamename);
320 1.1 cgd flags.move = 0;
321 1.1 cgd } else {
322 1.1 cgd not_recovered:
323 1.1 cgd fobj = fcobj = invent = 0;
324 1.1 cgd fmon = fallen_down = 0;
325 1.1 cgd ftrap = 0;
326 1.1 cgd fgold = 0;
327 1.1 cgd flags.ident = 1;
328 1.1 cgd init_objects();
329 1.1 cgd u_init();
330 1.1 cgd
331 1.4 christos (void) signal(SIGINT, done1);
332 1.1 cgd mklev();
333 1.1 cgd u.ux = xupstair;
334 1.1 cgd u.uy = yupstair;
335 1.1 cgd (void) inshop();
336 1.1 cgd setsee();
337 1.1 cgd flags.botlx = 1;
338 1.1 cgd makedog();
339 1.4 christos {
340 1.4 christos struct monst *mtmp;
341 1.4 christos if ((mtmp = m_at(u.ux, u.uy)) != NULL)
342 1.4 christos mnexto(mtmp); /* riv05!a3 */
343 1.1 cgd }
344 1.1 cgd seemons();
345 1.1 cgd #ifdef NEWS
346 1.4 christos if (flags.nonews || !readnews())
347 1.1 cgd /* after reading news we did docrt() already */
348 1.1 cgd #endif
349 1.1 cgd docrt();
350 1.1 cgd
351 1.1 cgd /* give welcome message before pickup messages */
352 1.1 cgd pline("Hello %s, welcome to %s!", plname, gamename);
353 1.1 cgd
354 1.1 cgd pickup(1);
355 1.4 christos read_engr_at(u.ux, u.uy);
356 1.1 cgd flags.move = 1;
357 1.1 cgd }
358 1.1 cgd
359 1.1 cgd flags.moonphase = phase_of_the_moon();
360 1.4 christos if (flags.moonphase == FULL_MOON) {
361 1.1 cgd pline("You are lucky! Full moon tonight.");
362 1.1 cgd u.uluck++;
363 1.4 christos } else if (flags.moonphase == NEW_MOON) {
364 1.1 cgd pline("Be careful! New moon tonight.");
365 1.1 cgd }
366 1.1 cgd initrack();
367 1.1 cgd
368 1.4 christos for (;;) {
369 1.4 christos if (flags.move) { /* actual time passed */
370 1.1 cgd
371 1.1 cgd settrack();
372 1.1 cgd
373 1.4 christos if (moves % 2 == 0 ||
374 1.4 christos (!(Fast & ~INTRINSIC) && (!Fast || rn2(3)))) {
375 1.1 cgd movemon();
376 1.4 christos if (!rn2(70))
377 1.4 christos (void) makemon((struct permonst *) 0, 0, 0);
378 1.1 cgd }
379 1.4 christos if (Glib)
380 1.4 christos glibr();
381 1.1 cgd timeout();
382 1.1 cgd ++moves;
383 1.4 christos if (flags.time)
384 1.4 christos flags.botl = 1;
385 1.4 christos if (u.uhp < 1) {
386 1.1 cgd pline("You die...");
387 1.1 cgd done("died");
388 1.1 cgd }
389 1.4 christos if (u.uhp * 10 < u.uhpmax && moves - wailmsg > 50) {
390 1.4 christos wailmsg = moves;
391 1.4 christos if (u.uhp == 1)
392 1.4 christos pline("You hear the wailing of the Banshee...");
393 1.4 christos else
394 1.4 christos pline("You hear the howling of the CwnAnnwn...");
395 1.1 cgd }
396 1.4 christos if (u.uhp < u.uhpmax) {
397 1.4 christos if (u.ulevel > 9) {
398 1.4 christos if (Regeneration || !(moves % 3)) {
399 1.4 christos flags.botl = 1;
400 1.4 christos u.uhp += rnd((int) u.ulevel - 9);
401 1.4 christos if (u.uhp > u.uhpmax)
402 1.4 christos u.uhp = u.uhpmax;
403 1.1 cgd }
404 1.4 christos } else if (Regeneration ||
405 1.4 christos (!(moves % (22 - u.ulevel * 2)))) {
406 1.1 cgd flags.botl = 1;
407 1.1 cgd u.uhp++;
408 1.1 cgd }
409 1.1 cgd }
410 1.4 christos if (Teleportation && !rn2(85))
411 1.4 christos tele();
412 1.4 christos if (Searching && multi >= 0)
413 1.4 christos (void) dosearch();
414 1.1 cgd gethungry();
415 1.1 cgd invault();
416 1.1 cgd amulet();
417 1.1 cgd }
418 1.4 christos if (multi < 0) {
419 1.4 christos if (!++multi) {
420 1.1 cgd pline(nomovemsg ? nomovemsg :
421 1.4 christos "You can move again.");
422 1.1 cgd nomovemsg = 0;
423 1.4 christos if (afternmv)
424 1.4 christos (*afternmv) ();
425 1.1 cgd afternmv = 0;
426 1.1 cgd }
427 1.1 cgd }
428 1.1 cgd find_ac();
429 1.1 cgd #ifndef QUEST
430 1.4 christos if (!flags.mv || Blind)
431 1.1 cgd #endif
432 1.1 cgd {
433 1.1 cgd seeobjs();
434 1.1 cgd seemons();
435 1.1 cgd nscr();
436 1.1 cgd }
437 1.4 christos if (flags.botl || flags.botlx)
438 1.4 christos bot();
439 1.1 cgd
440 1.1 cgd flags.move = 1;
441 1.1 cgd
442 1.4 christos if (multi >= 0 && occupation) {
443 1.4 christos if (monster_nearby())
444 1.1 cgd stop_occupation();
445 1.4 christos else if ((*occupation) () == 0)
446 1.1 cgd occupation = 0;
447 1.1 cgd continue;
448 1.1 cgd }
449 1.4 christos if (multi > 0) {
450 1.1 cgd #ifdef QUEST
451 1.4 christos if (flags.run >= 4)
452 1.4 christos finddir();
453 1.1 cgd #endif
454 1.1 cgd lookaround();
455 1.4 christos if (!multi) { /* lookaround may clear multi */
456 1.1 cgd flags.move = 0;
457 1.1 cgd continue;
458 1.1 cgd }
459 1.4 christos if (flags.mv) {
460 1.4 christos if (multi < COLNO && !--multi)
461 1.1 cgd flags.mv = flags.run = 0;
462 1.1 cgd domove();
463 1.1 cgd } else {
464 1.1 cgd --multi;
465 1.1 cgd rhack(save_cm);
466 1.1 cgd }
467 1.4 christos } else if (multi == 0) {
468 1.1 cgd #ifdef MAIL
469 1.1 cgd ckmailstatus();
470 1.1 cgd #endif
471 1.1 cgd rhack((char *) 0);
472 1.1 cgd }
473 1.4 christos if (multi && multi % 7 == 0)
474 1.1 cgd (void) fflush(stdout);
475 1.1 cgd }
476 1.1 cgd }
477 1.1 cgd
478 1.4 christos void
479 1.11 dholland glo(int foo)
480 1.1 cgd {
481 1.1 cgd /* construct the string xlock.n */
482 1.13 dholland size_t pos;
483 1.1 cgd
484 1.13 dholland pos = 0;
485 1.13 dholland while (lock[pos] && lock[pos] != '.')
486 1.13 dholland pos++;
487 1.13 dholland (void) snprintf(lock + pos, sizeof(lock) - pos, ".%d", foo);
488 1.1 cgd }
489 1.1 cgd
490 1.1 cgd /*
491 1.1 cgd * plname is filled either by an option (-u Player or -uPlayer) or
492 1.1 cgd * explicitly (-w implies wizard) or by askname.
493 1.1 cgd * It may still contain a suffix denoting pl_character.
494 1.1 cgd */
495 1.4 christos void
496 1.11 dholland askname(void)
497 1.4 christos {
498 1.4 christos int c, ct;
499 1.1 cgd printf("\nWho are you? ");
500 1.1 cgd (void) fflush(stdout);
501 1.1 cgd ct = 0;
502 1.4 christos while ((c = getchar()) != '\n') {
503 1.4 christos if (c == EOF)
504 1.4 christos error("End of input\n");
505 1.1 cgd /* some people get confused when their erase char is not ^H */
506 1.4 christos if (c == '\010') {
507 1.4 christos if (ct)
508 1.4 christos ct--;
509 1.1 cgd continue;
510 1.1 cgd }
511 1.4 christos if (c != '-')
512 1.4 christos if (c < 'A' || (c > 'Z' && c < 'a') || c > 'z')
513 1.4 christos c = '_';
514 1.10 dholland if (ct < (int)sizeof(plname) - 1)
515 1.4 christos plname[ct++] = c;
516 1.1 cgd }
517 1.1 cgd plname[ct] = 0;
518 1.4 christos if (ct == 0)
519 1.4 christos askname();
520 1.1 cgd }
521 1.1 cgd
522 1.4 christos /* VARARGS1 */
523 1.4 christos void
524 1.4 christos impossible(const char *s, ...)
525 1.1 cgd {
526 1.4 christos va_list ap;
527 1.4 christos
528 1.4 christos va_start(ap, s);
529 1.4 christos vpline(s, ap);
530 1.4 christos va_end(ap);
531 1.1 cgd pline("Program in disorder - perhaps you'd better Quit.");
532 1.1 cgd }
533 1.1 cgd
534 1.1 cgd #ifdef CHDIR
535 1.1 cgd static void
536 1.11 dholland chdirx(const char *dir, boolean wr)
537 1.1 cgd {
538 1.1 cgd
539 1.1 cgd #ifdef SECURE
540 1.4 christos if (dir /* User specified directory? */
541 1.1 cgd #ifdef HACKDIR
542 1.4 christos && strcmp(dir, HACKDIR) /* and not the default? */
543 1.1 cgd #endif
544 1.1 cgd ) {
545 1.4 christos (void) setuid(getuid()); /* Ron Wessels */
546 1.1 cgd (void) setgid(getgid());
547 1.1 cgd }
548 1.1 cgd #endif
549 1.1 cgd
550 1.1 cgd #ifdef HACKDIR
551 1.4 christos if (dir == NULL)
552 1.1 cgd dir = HACKDIR;
553 1.1 cgd #endif
554 1.1 cgd
555 1.4 christos if (dir && chdir(dir) < 0) {
556 1.1 cgd perror(dir);
557 1.1 cgd error("Cannot chdir to %s.", dir);
558 1.1 cgd }
559 1.1 cgd /* warn the player if he cannot write the record file */
560 1.1 cgd /* perhaps we should also test whether . is writable */
561 1.1 cgd /* unfortunately the access systemcall is worthless */
562 1.4 christos if (wr) {
563 1.4 christos int fd;
564 1.1 cgd
565 1.4 christos if (dir == NULL)
566 1.4 christos dir = ".";
567 1.6 jsm if ((fd = open(RECORD, O_RDWR)) < 0) {
568 1.4 christos printf("Warning: cannot write %s/%s", dir, RECORD);
569 1.4 christos getret();
570 1.4 christos } else
571 1.4 christos (void) close(fd);
572 1.1 cgd }
573 1.1 cgd }
574 1.1 cgd #endif
575 1.1 cgd
576 1.4 christos void
577 1.11 dholland stop_occupation(void)
578 1.1 cgd {
579 1.4 christos if (occupation) {
580 1.1 cgd pline("You stop %s.", occtxt);
581 1.1 cgd occupation = 0;
582 1.1 cgd }
583 1.1 cgd }
584