1 /* $OpenBSD: hack.unix.c,v 1.20 2016/09/11 14:21:17 tb 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 <sys/stat.h> 76 77 #include <err.h> 78 #include <errno.h> 79 #include <limits.h> 80 #include <signal.h> 81 #include <stdio.h> 82 #include <stdlib.h> 83 #include <time.h> 84 #include <unistd.h> 85 86 #include "hack.h" 87 88 89 static struct tm *getlt(void); 90 static int veryold(int); 91 #ifdef MAIL 92 static void newmail(void); 93 static void mdrush(struct monst *, boolean); 94 #endif 95 96 static struct tm * 97 getlt(void) 98 { 99 time_t date; 100 struct tm *localtime(); 101 102 (void) time(&date); 103 return(localtime(&date)); 104 } 105 106 int 107 getyear(void) 108 { 109 return(1900 + getlt()->tm_year); 110 } 111 112 char * 113 getdate(void) 114 { 115 static char datestr[7]; 116 struct tm *lt = getlt(); 117 118 (void) snprintf(datestr, sizeof(datestr), "%02d%02d%02d", 119 lt->tm_year % 100, lt->tm_mon + 1, lt->tm_mday); 120 return(datestr); 121 } 122 123 /* 124 * 0-7, with 0: new, 4: full 125 * moon period: 29.5306 days 126 * year: 365.2422 days 127 */ 128 int 129 phase_of_the_moon(void) 130 { 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(void) 145 { 146 int hour = getlt()->tm_hour; 147 148 return(hour < 6 || hour > 21); 149 } 150 151 int 152 midnight(void) 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[PATH_MAX+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(void) 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 } 268 269 regularize(lock); 270 glo(0); 271 if(locknum > 25) locknum = 25; 272 273 do { 274 if(locknum) lock[0] = 'a' + i++; 275 276 if((fd = open(lock, O_RDONLY)) == -1) { 277 if(errno == ENOENT) goto gotlock; /* no such file */ 278 perror(lock); 279 (void) unlink(LLOCK); 280 error("Cannot open %s", lock); 281 } 282 283 if(veryold(fd)) /* if true, this closes fd and unlinks lock */ 284 goto gotlock; 285 (void) close(fd); 286 } while(i < locknum); 287 288 (void) unlink(LLOCK); 289 error(locknum ? "Too many hacks running now." 290 : "There is a game in progress under your name."); 291 gotlock: 292 fd = open(lock, O_CREAT | O_TRUNC | O_WRONLY, FMASK); 293 if(unlink(LLOCK) == -1) 294 error("Cannot unlink %s.", LLOCK); 295 if(fd == -1) { 296 error("cannot creat lock file."); 297 } else { 298 if(write(fd, (char *) &hackpid, sizeof(hackpid)) 299 != sizeof(hackpid)){ 300 error("cannot write lock"); 301 } 302 if(close(fd) == -1) { 303 error("cannot close lock"); 304 } 305 } 306 } 307 308 #ifdef MAIL 309 310 /* 311 * Notify user when new mail has arrived. [Idea from Merlyn Leroy, but 312 * I don't know the details of his implementation.] 313 * { Later note: he disliked my calling a general mailreader and felt that 314 * hack should do the paging itself. But when I get mail, I want to put it 315 * in some folder, reply, etc. - it would be unreasonable to put all these 316 * functions in hack. } 317 * The mail daemon '2' is at present not a real monster, but only a visual 318 * effect. Thus, makemon() is superfluous. This might become otherwise, 319 * however. The motion of '2' is less restrained than usual: diagonal moves 320 * from a DOOR are possible. He might also use SDOOR's. Also, '2' is visible 321 * in a ROOM, even when you are Blind. 322 * Its path should be longer when you are Telepat-hic and Blind. 323 * 324 * Interesting side effects: 325 * - You can get rich by sending yourself a lot of mail and selling 326 * it to the shopkeeper. Unfortunately mail isn't very valuable. 327 * - You might die in case '2' comes along at a critical moment during 328 * a fight and delivers a scroll the weight of which causes you to 329 * collapse. 330 * 331 * Possible extensions: 332 * - Open the file MAIL and do fstat instead of stat for efficiency. 333 * (But sh uses stat, so this cannot be too bad.) 334 * - Examine the mail and produce a scroll of mail called "From somebody". 335 * - Invoke MAILREADER in such a way that only this single letter is read. 336 * 337 * - Make him lose his mail when a Nymph steals the letter. 338 * - Do something to the text when the scroll is enchanted or cancelled. 339 */ 340 static struct stat omstat,nmstat; 341 static char *mailbox; 342 static long laststattime; 343 344 void 345 getmailstatus(void) 346 { 347 if(!(mailbox = getenv("MAIL"))) 348 return; 349 if(stat(mailbox, &omstat)){ 350 #ifdef PERMANENT_MAILBOX 351 pline("Cannot get status of MAIL=%s .", mailbox); 352 mailbox = 0; 353 #else 354 omstat.st_mtime = 0; 355 #endif /* PERMANENT_MAILBOX */ 356 } 357 } 358 359 void 360 ckmailstatus(void) 361 { 362 if(!mailbox 363 #ifdef MAILCKFREQ 364 || moves < laststattime + MAILCKFREQ 365 #endif /* MAILCKFREQ */ 366 ) 367 return; 368 laststattime = moves; 369 if(stat(mailbox, &nmstat)){ 370 #ifdef PERMANENT_MAILBOX 371 pline("Cannot get status of MAIL=%s anymore.", mailbox); 372 mailbox = 0; 373 #else 374 nmstat.st_mtime = 0; 375 #endif /* PERMANENT_MAILBOX */ 376 } else if(nmstat.st_mtime > omstat.st_mtime) { 377 if(nmstat.st_size) 378 newmail(); 379 getmailstatus(); /* might be too late ... */ 380 } 381 } 382 383 static void 384 newmail(void) 385 { 386 /* produce a scroll of mail */ 387 struct obj *obj; 388 struct monst *md; 389 extern char plname[]; 390 extern struct obj *mksobj(); 391 extern struct monst *makemon(); 392 extern struct permonst pm_mail_daemon; 393 394 obj = mksobj(SCR_MAIL); 395 if(md = makemon(&pm_mail_daemon, u.ux, u.uy)) /* always succeeds */ 396 mdrush(md,0); 397 398 pline("\"Hello, %s! I have some mail for you.\"", plname); 399 if(md) { 400 if(dist(md->mx,md->my) > 2) 401 pline("\"Catch!\""); 402 more(); 403 404 /* let him disappear again */ 405 mdrush(md,1); 406 mondead(md); 407 } 408 409 obj = addinv(obj); 410 (void) identify(obj); /* set known and do prinv() */ 411 } 412 413 /* make md run through the cave */ 414 static void 415 mdrush(struct monst *md, boolean away) 416 { 417 int uroom = inroom(u.ux, u.uy); 418 if(uroom >= 0) { 419 int tmp = rooms[uroom].fdoor; 420 int cnt = rooms[uroom].doorct; 421 int fx = u.ux, fy = u.uy; 422 while(cnt--) { 423 if(dist(fx,fy) < dist(doors[tmp].x, doors[tmp].y)){ 424 fx = doors[tmp].x; 425 fy = doors[tmp].y; 426 } 427 tmp++; 428 } 429 tmp_at(-1, md->data->mlet); /* open call */ 430 if(away) { /* interchange origin and destination */ 431 unpmon(md); 432 tmp = fx; fx = md->mx; md->mx = tmp; 433 tmp = fy; fy = md->my; md->my = tmp; 434 } 435 while(fx != md->mx || fy != md->my) { 436 int dx,dy,nfx = fx,nfy = fy,d1,d2; 437 438 tmp_at(fx,fy); 439 d1 = DIST(fx,fy,md->mx,md->my); 440 for(dx = -1; dx <= 1; dx++) for(dy = -1; dy <= 1; dy++) 441 if(dx || dy) { 442 d2 = DIST(fx+dx,fy+dy,md->mx,md->my); 443 if(d2 < d1) { 444 d1 = d2; 445 nfx = fx+dx; 446 nfy = fy+dy; 447 } 448 } 449 if(nfx != fx || nfy != fy) { 450 fx = nfx; 451 fy = nfy; 452 } else { 453 if(!away) { 454 md->mx = fx; 455 md->my = fy; 456 } 457 break; 458 } 459 } 460 tmp_at(-1,-1); /* close call */ 461 } 462 if(!away) 463 pmon(md); 464 } 465 466 void 467 readmail(void) 468 { 469 #ifdef DEF_MAILREADER /* This implies that UNIX is defined */ 470 char *mr = 0; 471 more(); 472 if(!(mr = getenv("MAILREADER"))) 473 mr = DEF_MAILREADER; 474 if(child(1)){ 475 execl(mr, mr, (char *)NULL); 476 exit(1); 477 } 478 #else /* DEF_MAILREADER */ 479 (void) page_file(mailbox, FALSE); 480 #endif /* DEF_MAILREADER */ 481 /* get new stat; not entirely correct: there is a small time 482 window where we do not see new mail */ 483 getmailstatus(); 484 } 485 #endif /* MAIL */ 486 487 /* normalize file name - we don't like ..'s or /'s */ 488 void 489 regularize(char *s) 490 { 491 char *lp; 492 493 while((lp = strchr(s, '.')) || (lp = strchr(s, '/'))) 494 *lp = '_'; 495 } 496