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