1 /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
2 /* hack.unix.c - version 1.0.3 */
5 /* This file collects some Unix dependencies; hack.pager.c contains some more */
8 * The time is used for:
10 * - year on tombstone and yymmdd in record file
11 * - phase of the moon (various monsters react to NEW_MOON or FULL_MOON)
12 * - night and midnight (the undead are dangerous at midnight)
13 * - determination of what files are "very old"
19 #include "hack.h" /* mainly for index() which depends on BSD */
21 #include <sys/types.h> /* for time_t and stat */
34 struct tm *localtime();
37 return(localtime(&date));
42 return(1900 + getlt()->tm_year);
48 static char datestr[7];
49 struct tm *lt = getlt();
51 (void) snprintf(datestr, sizeof(datestr), "%02d%02d%02d",
52 lt->tm_year % 100, lt->tm_mon + 1, lt->tm_mday);
56 phase_of_the_moon() /* 0-7, with 0: new, 4: full */
57 { /* moon period: 29.5306 days */
58 /* year: 365.2422 days */
59 struct tm *lt = getlt();
60 int epact, diy, golden;
63 golden = (lt->tm_year % 19) + 1;
64 epact = (11 * golden + 18) % 30;
65 if ((epact == 25 && golden > 11) || epact == 24)
68 return( (((((diy + epact) * 6) + 11) % 177) / 22) & 7 );
73 int hour = getlt()->tm_hour;
75 return(hour < 6 || hour > 21);
80 return(getlt()->tm_hour == 0);
83 struct stat buf, hbuf;
85 gethdate(name) char *name; {
86 /* old version - for people short of space */
89 name = "/usr/games/hide/hack";
91 error("Cannot get status of %s.",
92 (np = rindex(name, '/')) ? np+1 : name);
97 pline("Cannot get status of saved level? ");
100 if(buf.st_mtime < hbuf.st_mtime) {
101 pline("Saved level is out of date. ");
107 /* see whether we should throw away this xlock file */
112 if(fstat(fd, &buf)) return(0); /* cannot get status */
113 if(buf.st_size != sizeof(int)) return(0); /* not an xlock file */
115 if(date - buf.st_mtime < 3L*24L*60L*60L) { /* recent */
117 int lockedpid; /* should be the same size as hackpid */
119 if(read(fd, (char *)&lockedpid, sizeof(lockedpid)) !=
124 /* From: Rick Adams <seismo!rick> */
125 /* This will work on 4.1cbsd, 4.2bsd and system 3? & 5. */
126 /* It will do nothing on V7 or 4.1bsd. */
127 if(!(kill(lockedpid, 0) == -1 && errno == ESRCH))
131 for(i = 1; i <= MAXLEVEL; i++) { /* try to remove all */
136 if(unlink(lock)) return(0); /* cannot remove it */
137 return(1); /* success! */
142 extern int errno, hackpid, locknum;
145 (void) fflush(stdout);
147 /* we ignore QUIT and INT at this point */
148 if (link(HLOCK, LLOCK) == -1) {
152 printf("Cannot link %s to %s\n", LLOCK, HLOCK);
155 printf("Perhaps there is no (empty) file %s ?\n", HLOCK);
158 printf("It seems you don't have write permission here.\n");
161 printf("(Try again or rm %s.)\n", LLOCK);
164 printf("I don't know what is wrong.");
173 if(locknum > 25) locknum = 25;
176 if(locknum) lock[0] = 'a' + i++;
178 if((fd = open(lock, 0)) == -1) {
179 if(errno == ENOENT) goto gotlock; /* no such file */
181 (void) unlink(LLOCK);
182 error("Cannot open %s", lock);
185 if(veryold(fd)) /* if true, this closes fd and unlinks lock */
188 } while(i < locknum);
190 (void) unlink(LLOCK);
191 error(locknum ? "Too many hacks running now."
192 : "There is a game in progress under your name.");
194 fd = creat(lock, FMASK);
195 if(unlink(LLOCK) == -1)
196 error("Cannot unlink %s.", LLOCK);
198 error("cannot creat lock file.");
200 if(write(fd, (char *) &hackpid, sizeof(hackpid))
202 error("cannot write lock");
204 if(close(fd) == -1) {
205 error("cannot close lock");
213 * Notify user when new mail has arrived. [Idea from Merlyn Leroy, but
214 * I don't know the details of his implementation.]
215 * { Later note: he disliked my calling a general mailreader and felt that
216 * hack should do the paging itself. But when I get mail, I want to put it
217 * in some folder, reply, etc. - it would be unreasonable to put all these
218 * functions in hack. }
219 * The mail daemon '2' is at present not a real monster, but only a visual
220 * effect. Thus, makemon() is superfluous. This might become otherwise,
221 * however. The motion of '2' is less restrained than usual: diagonal moves
222 * from a DOOR are possible. He might also use SDOOR's. Also, '2' is visible
223 * in a ROOM, even when you are Blind.
224 * Its path should be longer when you are Telepat-hic and Blind.
226 * Interesting side effects:
227 * - You can get rich by sending yourself a lot of mail and selling
228 * it to the shopkeeper. Unfortunately mail isn't very valuable.
229 * - You might die in case '2' comes along at a critical moment during
230 * a fight and delivers a scroll the weight of which causes you to
233 * Possible extensions:
234 * - Open the file MAIL and do fstat instead of stat for efficiency.
235 * (But sh uses stat, so this cannot be too bad.)
236 * - Examine the mail and produce a scroll of mail called "From somebody".
237 * - Invoke MAILREADER in such a way that only this single letter is read.
239 * - Make him lose his mail when a Nymph steals the letter.
240 * - Do something to the text when the scroll is enchanted or cancelled.
242 #include "def.mkroom.h"
243 static struct stat omstat,nmstat;
244 static char *mailbox;
245 static long laststattime;
248 if(!(mailbox = getenv("MAIL")))
250 if(stat(mailbox, &omstat)){
251 #ifdef PERMANENT_MAILBOX
252 pline("Cannot get status of MAIL=%s .", mailbox);
256 #endif PERMANENT_MAILBOX
263 || moves < laststattime + MAILCKFREQ
267 laststattime = moves;
268 if(stat(mailbox, &nmstat)){
269 #ifdef PERMANENT_MAILBOX
270 pline("Cannot get status of MAIL=%s anymore.", mailbox);
274 #endif PERMANENT_MAILBOX
275 } else if(nmstat.st_mtime > omstat.st_mtime) {
278 getmailstatus(); /* might be too late ... */
283 /* produce a scroll of mail */
286 extern char plname[];
287 extern struct obj *mksobj(), *addinv();
288 extern struct monst *makemon();
289 extern struct permonst pm_mail_daemon;
291 obj = mksobj(SCR_MAIL);
292 if(md = makemon(&pm_mail_daemon, u.ux, u.uy)) /* always succeeds */
295 pline("\"Hello, %s! I have some mail for you.\"", plname);
297 if(dist(md->mx,md->my) > 2)
301 /* let him disappear again */
307 (void) identify(obj); /* set known and do prinv() */
310 /* make md run through the cave */
315 int uroom = inroom(u.ux, u.uy);
317 int tmp = rooms[uroom].fdoor;
318 int cnt = rooms[uroom].doorct;
319 int fx = u.ux, fy = u.uy;
321 if(dist(fx,fy) < dist(doors[tmp].x, doors[tmp].y)){
327 tmp_at(-1, md->data->mlet); /* open call */
328 if(away) { /* interchange origin and destination */
330 tmp = fx; fx = md->mx; md->mx = tmp;
331 tmp = fy; fy = md->my; md->my = tmp;
333 while(fx != md->mx || fy != md->my) {
334 int dx,dy,nfx = fx,nfy = fy,d1,d2;
337 d1 = DIST(fx,fy,md->mx,md->my);
338 for(dx = -1; dx <= 1; dx++) for(dy = -1; dy <= 1; dy++)
340 d2 = DIST(fx+dx,fy+dy,md->mx,md->my);
347 if(nfx != fx || nfy != fy) {
358 tmp_at(-1,-1); /* close call */
365 #ifdef DEF_MAILREADER /* This implies that UNIX is defined */
368 if(!(mr = getenv("MAILREADER")))
371 execl(mr, mr, (char *) 0);
375 (void) page_file(mailbox, FALSE);
376 #endif DEF_MAILREADER
377 /* get new stat; not entirely correct: there is a small time
378 window where we do not see new mail */
383 regularize(s) /* normalize file name - we don't like ..'s or /'s */
388 while((lp = index(s, '.')) || (lp = index(s, '/')))