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