1 /* $NetBSD: hack.unix.c,v 1.18 2019/02/03 10:48:46 mrg 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 /* This file collects some Unix dependencies; hack.pager.c contains some more */ 65 66 /* 67 * The time is used for: 68 * - seed for random() 69 * - year on tombstone and yymmdd in record file 70 * - phase of the moon (various monsters react to NEW_MOON or FULL_MOON) 71 * - night and midnight (the undead are dangerous at midnight) 72 * - determination of what files are "very old" 73 */ 74 75 #include <errno.h> 76 #include <sys/types.h> /* for time_t and stat */ 77 #include <sys/stat.h> 78 #ifdef BSD 79 #include <sys/time.h> 80 #else 81 #include <time.h> 82 #endif /* BSD */ 83 #include <stdlib.h> 84 #include <unistd.h> 85 #include <signal.h> 86 #include <fcntl.h> 87 88 #include "hack.h" /* mainly for strchr() which depends on BSD */ 89 #include "extern.h" 90 91 extern int locknum; 92 93 static struct tm *getlt(void); 94 static int veryold(int); 95 96 97 void 98 setrandom(void) 99 { 100 (void) srandom((int) time((time_t *) 0)); 101 } 102 103 static struct tm * 104 getlt(void) 105 { 106 time_t date; 107 108 (void) time(&date); 109 return (localtime(&date)); 110 } 111 112 int 113 getyear(void) 114 { 115 return (1900 + getlt()->tm_year); 116 } 117 118 char * 119 getdatestr(void) 120 { 121 static char datestr[32]; 122 struct tm *lt = getlt(); 123 124 (void) snprintf(datestr, sizeof(datestr), "%02d%02d%02d", 125 lt->tm_year % 100, lt->tm_mon + 1, lt->tm_mday); 126 return (datestr); 127 } 128 129 int 130 phase_of_the_moon(void) 131 { /* 0-7, with 0: new, 4: full *//* moon 132 * period: 29.5306 days */ 133 /* year: 365.2422 days */ 134 struct tm *lt = getlt(); 135 int epact, diy, golden; 136 137 diy = lt->tm_yday; 138 golden = (lt->tm_year % 19) + 1; 139 epact = (11 * golden + 18) % 30; 140 if ((epact == 25 && golden > 11) || epact == 24) 141 epact++; 142 143 return ((((((diy + epact) * 6) + 11) % 177) / 22) & 7); 144 } 145 146 int 147 night(void) 148 { 149 int hour = getlt()->tm_hour; 150 151 return (hour < 6 || hour > 21); 152 } 153 154 int 155 midnight(void) 156 { 157 return (getlt()->tm_hour == 0); 158 } 159 160 static struct stat buf, hbuf; 161 162 void 163 gethdate(char *name) 164 { 165 #if 0 166 /* old version - for people short of space */ 167 168 char *np; 169 170 if(stat(name, &hbuf)) 171 error("Cannot get status of %s.", 172 (np = strrchr(name, '/')) ? np+1 : name); 173 #else 174 /* version using PATH from: seismo!gregc@ucsf-cgl.ARPA (Greg Couch) */ 175 176 177 /* 178 * The problem with #include <sys/param.h> is that this include file 179 * does not exist on all systems, and moreover, that it sometimes includes 180 * <sys/types.h> again, so that the compiler sees these typedefs twice. 181 */ 182 #define MAXPATHLEN 1024 183 184 const char *np, *path; 185 char filename[MAXPATHLEN + 1]; 186 if (strchr(name, '/') != NULL || (path = getenv("PATH")) == NULL) 187 path = ""; 188 189 for (;;) { 190 if ((np = strchr(path, ':')) == NULL) 191 np = path + strlen(path); /* point to end str */ 192 if (np - path <= 1) /* %% */ 193 (void) strlcpy(filename, name, sizeof(filename)); 194 else { 195 (void) snprintf(filename, sizeof(filename), 196 "%.*s/%s", 197 (int)(np - path), path, name); 198 } 199 if (stat(filename, &hbuf) == 0) 200 return; 201 if (*np == '\0') 202 break; 203 path = np + 1; 204 } 205 error("Cannot get status of %s.", 206 (np = strrchr(name, '/')) ? np + 1 : name); 207 #endif 208 } 209 210 int 211 uptodate(int fd) 212 { 213 if (fstat(fd, &buf)) { 214 pline("Cannot get status of saved level? "); 215 return (0); 216 } 217 if (buf.st_mtime < hbuf.st_mtime) { 218 pline("Saved level is out of date. "); 219 return (0); 220 } 221 return (1); 222 } 223 224 /* see whether we should throw away this xlock file */ 225 static int 226 veryold(int fd) 227 { 228 int i; 229 time_t date; 230 231 if (fstat(fd, &buf)) 232 return (0); /* cannot get status */ 233 if (buf.st_size != sizeof(int)) 234 return (0); /* not an xlock file */ 235 (void) time(&date); 236 if (date - buf.st_mtime < 3L * 24L * 60L * 60L) { /* recent */ 237 int lockedpid; /* should be the same size as 238 * hackpid */ 239 240 if (read(fd, &lockedpid, sizeof(lockedpid)) != 241 sizeof(lockedpid)) 242 /* strange ... */ 243 return (0); 244 245 /* 246 * From: Rick Adams <seismo!rick> This will work on 247 * 4.1cbsd, 4.2bsd and system 3? & 5. It will do nothing 248 * on V7 or 4.1bsd. 249 */ 250 if (!(kill(lockedpid, 0) == -1 && errno == ESRCH)) 251 return (0); 252 } 253 (void) close(fd); 254 for (i = 1; i <= MAXLEVEL; i++) { /* try to remove all */ 255 glo(i); 256 (void) unlink(lock); 257 } 258 glo(0); 259 if (unlink(lock)) 260 return (0); /* cannot remove it */ 261 return (1); /* success! */ 262 } 263 264 void 265 getlock(void) 266 { 267 int i = 0, fd; 268 269 (void) fflush(stdout); 270 271 /* we ignore QUIT and INT at this point */ 272 if (link(HLOCK, LLOCK) == -1) { 273 int errnosv = errno; 274 275 perror(HLOCK); 276 printf("Cannot link %s to %s\n", LLOCK, HLOCK); 277 switch (errnosv) { 278 case ENOENT: 279 printf("Perhaps there is no (empty) file %s ?\n", HLOCK); 280 break; 281 case EACCES: 282 printf("It seems you don't have write permission here.\n"); 283 break; 284 case EEXIST: 285 printf("(Try again or rm %s.)\n", LLOCK); 286 break; 287 default: 288 printf("I don't know what is wrong."); 289 } 290 getret(); 291 error("%s", ""); 292 /* NOTREACHED */ 293 } 294 regularize(lock); 295 glo(0); 296 if (locknum > 25) 297 locknum = 25; 298 299 do { 300 if (locknum) 301 lock[0] = 'a' + i++; 302 303 if ((fd = open(lock, O_RDONLY)) == -1) { 304 if (errno == ENOENT) 305 goto gotlock; /* no such file */ 306 perror(lock); 307 (void) unlink(LLOCK); 308 error("Cannot open %s", lock); 309 } 310 if (veryold(fd))/* if true, this closes fd and unlinks lock */ 311 goto gotlock; 312 (void) close(fd); 313 } while (i < locknum); 314 315 (void) unlink(LLOCK); 316 error(locknum ? "Too many hacks running now." 317 : "There is a game in progress under your name."); 318 gotlock: 319 fd = creat(lock, FMASK); 320 if (unlink(LLOCK) == -1) 321 error("Cannot unlink %s.", LLOCK); 322 if (fd == -1) { 323 error("cannot creat lock file."); 324 } else { 325 if (write(fd, &hackpid, sizeof(hackpid)) 326 != sizeof(hackpid)) { 327 error("cannot write lock"); 328 } 329 if (close(fd) == -1) { 330 error("cannot close lock"); 331 } 332 } 333 } 334 335 #ifdef MAIL 336 337 /* 338 * Notify user when new mail has arrived. [Idea from Merlyn Leroy, but 339 * I don't know the details of his implementation.] 340 * { Later note: he disliked my calling a general mailreader and felt that 341 * hack should do the paging itself. But when I get mail, I want to put it 342 * in some folder, reply, etc. - it would be unreasonable to put all these 343 * functions in hack. } 344 * The mail daemon '2' is at present not a real monster, but only a visual 345 * effect. Thus, makemon() is superfluous. This might become otherwise, 346 * however. The motion of '2' is less restrained than usual: diagonal moves 347 * from a DOOR are possible. He might also use SDOOR's. Also, '2' is visible 348 * in a ROOM, even when you are Blind. 349 * Its path should be longer when you are Telepat-hic and Blind. 350 * 351 * Interesting side effects: 352 * - You can get rich by sending yourself a lot of mail and selling 353 * it to the shopkeeper. Unfortunately mail isn't very valuable. 354 * - You might die in case '2' comes along at a critical moment during 355 * a fight and delivers a scroll the weight of which causes you to 356 * collapse. 357 * 358 * Possible extensions: 359 * - Open the file MAIL and do fstat instead of stat for efficiency. 360 * (But sh uses stat, so this cannot be too bad.) 361 * - Examine the mail and produce a scroll of mail called "From somebody". 362 * - Invoke MAILREADER in such a way that only this single letter is read. 363 * 364 * - Make him lose his mail when a Nymph steals the letter. 365 * - Do something to the text when the scroll is enchanted or cancelled. 366 */ 367 #include "def.mkroom.h" 368 static struct stat omstat, nmstat; 369 static char *mailbox; 370 static long laststattime; 371 372 void 373 getmailstatus(void) 374 { 375 if (!(mailbox = getenv("MAIL"))) 376 return; 377 if (stat(mailbox, &omstat)) { 378 #ifdef PERMANENT_MAILBOX 379 pline("Cannot get status of MAIL=%s .", mailbox); 380 mailbox = 0; 381 #else 382 omstat.st_mtime = 0; 383 #endif /* PERMANENT_MAILBOX */ 384 } 385 } 386 387 void 388 ckmailstatus(void) 389 { 390 if (!mailbox 391 #ifdef MAILCKFREQ 392 || moves < laststattime + MAILCKFREQ 393 #endif /* MAILCKFREQ */ 394 ) 395 return; 396 laststattime = moves; 397 if (stat(mailbox, &nmstat)) { 398 #ifdef PERMANENT_MAILBOX 399 pline("Cannot get status of MAIL=%s anymore.", mailbox); 400 mailbox = 0; 401 #else 402 nmstat.st_mtime = 0; 403 #endif /* PERMANENT_MAILBOX */ 404 } else if (nmstat.st_mtime > omstat.st_mtime) { 405 if (nmstat.st_size) 406 newmail(); 407 getmailstatus();/* might be too late ... */ 408 } 409 } 410 411 void 412 newmail(void) 413 { 414 /* produce a scroll of mail */ 415 struct obj *obj; 416 struct monst *md; 417 418 obj = mksobj(SCR_MAIL); 419 if (md = makemon(&pm_mail_daemon, u.ux, u.uy)) /* always succeeds */ 420 mdrush(md, 0); 421 422 pline("\"Hello, %s! I have some mail for you.\"", plname); 423 if (md) { 424 if (dist(md->mx, md->my) > 2) 425 pline("\"Catch!\""); 426 more(); 427 428 /* let him disappear again */ 429 mdrush(md, 1); 430 mondead(md); 431 } 432 obj = addinv(obj); 433 (void) identify(obj); /* set known and do prinv() */ 434 } 435 436 /* make md run through the cave */ 437 void 438 mdrush(struct monst *md, boolean away) 439 { 440 int uroom = inroom(u.ux, u.uy); 441 if (uroom >= 0) { 442 int tmp = rooms[uroom].fdoor; 443 int cnt = rooms[uroom].doorct; 444 int fx = u.ux, fy = u.uy; 445 while (cnt--) { 446 if (dist(fx, fy) < dist(doors[tmp].x, doors[tmp].y)) { 447 fx = doors[tmp].x; 448 fy = doors[tmp].y; 449 } 450 tmp++; 451 } 452 tmp_at(-1, md->data->mlet); /* open call */ 453 if (away) { /* interchange origin and destination */ 454 unpmon(md); 455 tmp = fx; 456 fx = md->mx; 457 md->mx = tmp; 458 tmp = fy; 459 fy = md->my; 460 md->my = tmp; 461 } 462 while (fx != md->mx || fy != md->my) { 463 int dx, dy, nfx = fx, nfy = fy, d1, 464 d2; 465 466 tmp_at(fx, fy); 467 d1 = DIST(fx, fy, md->mx, md->my); 468 for (dx = -1; dx <= 1; dx++) 469 for (dy = -1; dy <= 1; dy++) 470 if (dx || dy) { 471 d2 = DIST(fx + dx, fy + dy, md->mx, md->my); 472 if (d2 < d1) { 473 d1 = d2; 474 nfx = fx + dx; 475 nfy = fy + dy; 476 } 477 } 478 if (nfx != fx || nfy != fy) { 479 fx = nfx; 480 fy = nfy; 481 } else { 482 if (!away) { 483 md->mx = fx; 484 md->my = fy; 485 } 486 break; 487 } 488 } 489 tmp_at(-1, -1); /* close call */ 490 } 491 if (!away) 492 pmon(md); 493 } 494 495 void 496 readmail(void) 497 { 498 #ifdef DEF_MAILREADER /* This implies that UNIX is defined */ 499 char *mr = 0; 500 more(); 501 if (!(mr = getenv("MAILREADER"))) 502 mr = DEF_MAILREADER; 503 if (child(1)) { 504 execl(mr, mr, (char *)NULL); 505 exit(1); 506 } 507 #else /* DEF_MAILREADER */ 508 (void) page_file(mailbox, FALSE); 509 #endif /* DEF_MAILREADER */ 510 /* 511 * get new stat; not entirely correct: there is a small time window 512 * where we do not see new mail 513 */ 514 getmailstatus(); 515 } 516 #endif /* MAIL */ 517 518 /* 519 * normalize file name - we don't like ..'s or /'s 520 */ 521 void 522 regularize(char *s) 523 { 524 char *lp; 525 526 while ((lp = strchr(s, '.')) || (lp = strchr(s, '/'))) 527 *lp = '_'; 528 } 529