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