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