kernel - Fix excessive call stack depth on stuck interrupt
[dragonfly.git] / games / hack / hack.unix.c
blob880dae3c536d4c730113ad58f16a3879e87d0991
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 $ */
5 /* This file collects some Unix dependencies; hack.pager.c contains some more */
7 /*
8 * The time is used for:
9 * - seed for random()
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"
16 #include <errno.h>
17 #include "hack.h"
19 #include <sys/types.h> /* for time_t and stat */
20 #include <sys/stat.h>
21 #include <time.h>
23 static struct tm *getlt(void);
24 static bool veryold(int);
25 #ifdef MAIL
26 static void newmail(void);
27 static void mdrush(struct monst *, bool);
28 #endif
30 void
31 setrandom(void)
33 srandomdev();
36 static struct tm *
37 getlt(void)
39 time_t date;
41 time(&date);
42 return (localtime(&date));
45 int
46 getyear(void)
48 return (1900 + getlt()->tm_year);
51 char *
52 getdate(void)
54 static char datestr[7];
55 struct tm *lt = getlt();
57 snprintf(datestr, sizeof(datestr), "%02d%02d%02d",
58 lt->tm_year % 100, lt->tm_mon + 1, lt->tm_mday);
59 return (datestr);
62 int
63 phase_of_the_moon(void) /* 0-7, with 0: new, 4: full */
64 { /* moon period: 29.5306 days */
65 /* year: 365.2422 days */
66 struct tm *lt = getlt();
67 int epact, diy, golden;
69 diy = lt->tm_yday;
70 golden = (lt->tm_year % 19) + 1;
71 epact = (11 * golden + 18) % 30;
72 if ((epact == 25 && golden > 11) || epact == 24)
73 epact++;
75 return ((((((diy + epact) * 6) + 11) % 177) / 22) & 7);
78 bool
79 night(void)
81 int hour = getlt()->tm_hour;
83 return (hour < 6 || hour > 21);
86 bool
87 midnight(void)
89 return (getlt()->tm_hour == 0);
92 struct stat buf, hbuf;
94 void
95 gethdate(const char *name)
97 /* old version - for people short of space */
98 char *np;
100 name = "/usr/games/hide/hack";
101 if (stat(name, &hbuf))
102 error("Cannot get status of %s.",
103 (np = strrchr(name, '/')) ? np + 1 : name);
106 bool
107 uptodate(int fd)
109 if (fstat(fd, &buf)) {
110 pline("Cannot get status of saved level? ");
111 return (0);
113 if (buf.st_mtime < hbuf.st_mtime) {
114 pline("Saved level is out of date. ");
115 return (0);
117 return (1);
120 /* see whether we should throw away this xlock file */
121 static bool
122 veryold(int fd)
124 int i;
125 time_t date;
127 if (fstat(fd, &buf)) /* cannot get status */
128 return (0);
129 if (buf.st_size != sizeof(int)) /* not an xlock file */
130 return (0);
131 time(&date);
132 if (date - buf.st_mtime < 3L * 24L * 60L * 60L) { /* recent */
133 int lockedpid; /* should be the same size as hackpid */
135 if (read(fd, (char *)&lockedpid, sizeof(lockedpid)) !=
136 sizeof(lockedpid))
137 /* strange ... */
138 return (0);
140 /* From: Rick Adams <seismo!rick> */
141 /* This will work on 4.1cbsd, 4.2bsd and system 3? & 5. */
142 /* It will do nothing on V7 or 4.1bsd. */
143 if (!(kill(lockedpid, 0) == -1 && errno == ESRCH))
144 return (0);
146 close(fd);
147 for (i = 1; i <= MAXLEVEL; i++) { /* try to remove all */
148 glo(i);
149 unlink(lock);
151 glo(0);
152 if (unlink(lock)) /* cannot remove it */
153 return (0);
154 return (1); /* success! */
157 void
158 getlock(void)
160 int i = 0, fd;
162 fflush(stdout);
164 /* we ignore QUIT and INT at this point */
165 if (link(HLOCK, LLOCK) == -1) {
166 int errnosv = errno;
168 perror(HLOCK);
169 printf("Cannot link %s to %s\n", LLOCK, HLOCK);
170 switch (errnosv) {
171 case ENOENT:
172 printf("Perhaps there is no (empty) file %s ?\n", HLOCK);
173 break;
174 case EACCES:
175 printf("It seems you don't have write permission here.\n");
176 break;
177 case EEXIST:
178 printf("(Try again or rm %s.)\n", LLOCK);
179 break;
180 default:
181 printf("I don't know what is wrong.");
183 getret();
184 error("%s", "");
185 /* NOTREACHED */
188 regularize(lock);
189 glo(0);
190 if (locknum > 25)
191 locknum = 25;
193 do {
194 if (locknum)
195 lock[0] = 'a' + i++;
197 if ((fd = open(lock, O_RDONLY)) == -1) {
198 if (errno == ENOENT) /* no such file */
199 goto gotlock;
200 perror(lock);
201 unlink(LLOCK);
202 error("Cannot open %s", lock);
205 if (veryold(fd)) /* if true, this closes fd and unlinks lock */
206 goto gotlock;
207 close(fd);
208 } while (i < locknum);
210 unlink(LLOCK);
211 error(locknum ? "Too many hacks running now."
212 : "There is a game in progress under your name.");
213 gotlock:
214 fd = creat(lock, FMASK);
215 if (unlink(LLOCK) == -1)
216 error("Cannot unlink %s.", LLOCK);
217 if (fd == -1) {
218 error("cannot creat lock file.");
219 } else {
220 if (write(fd, (char *)&hackpid, sizeof(hackpid))
221 != sizeof(hackpid))
222 error("cannot write lock");
223 if (close(fd) == -1)
224 error("cannot close lock");
228 #ifdef MAIL
231 * Notify user when new mail has arrived. [Idea from Merlyn Leroy, but
232 * I don't know the details of his implementation.]
233 * { Later note: he disliked my calling a general mailreader and felt that
234 * hack should do the paging itself. But when I get mail, I want to put it
235 * in some folder, reply, etc. - it would be unreasonable to put all these
236 * functions in hack. }
237 * The mail daemon '2' is at present not a real monster, but only a visual
238 * effect. Thus, makemon() is superfluous. This might become otherwise,
239 * however. The motion of '2' is less restrained than usual: diagonal moves
240 * from a DOOR are possible. He might also use SDOOR's. Also, '2' is visible
241 * in a ROOM, even when you are Blind.
242 * Its path should be longer when you are Telepat-hic and Blind.
244 * Interesting side effects:
245 * - You can get rich by sending yourself a lot of mail and selling
246 * it to the shopkeeper. Unfortunately mail isn't very valuable.
247 * - You might die in case '2' comes along at a critical moment during
248 * a fight and delivers a scroll the weight of which causes you to
249 * collapse.
251 * Possible extensions:
252 * - Open the file MAIL and do fstat instead of stat for efficiency.
253 * (But sh uses stat, so this cannot be too bad.)
254 * - Examine the mail and produce a scroll of mail called "From somebody".
255 * - Invoke MAILREADER in such a way that only this single letter is read.
257 * - Make him lose his mail when a Nymph steals the letter.
258 * - Do something to the text when the scroll is enchanted or cancelled.
260 #include "def.mkroom.h"
261 static struct stat omstat, nmstat;
262 static char *mailbox;
263 static long laststattime;
265 void
266 getmailstatus(void)
268 if (!(mailbox = getenv("MAIL")))
269 return;
270 if (stat(mailbox, &omstat)) {
271 #ifdef PERMANENT_MAILBOX
272 pline("Cannot get status of MAIL=%s .", mailbox);
273 mailbox = NULL;
274 #else
275 omstat.st_mtime = 0;
276 #endif /* PERMANENT_MAILBOX */
280 void
281 ckmailstatus(void)
283 if (!mailbox
284 #ifdef MAILCKFREQ
285 || moves < laststattime + MAILCKFREQ
286 #endif /* MAILCKFREQ */
288 return;
289 laststattime = moves;
290 if (stat(mailbox, &nmstat)) {
291 #ifdef PERMANENT_MAILBOX
292 pline("Cannot get status of MAIL=%s anymore.", mailbox);
293 mailbox = NULL;
294 #else
295 nmstat.st_mtime = 0;
296 #endif /* PERMANENT_MAILBOX */
297 } else if (nmstat.st_mtime > omstat.st_mtime) {
298 if (nmstat.st_size)
299 newmail();
300 getmailstatus(); /* might be too late ... */
304 static void
305 newmail(void)
307 /* produce a scroll of mail */
308 struct obj *obj;
309 struct monst *md;
310 extern struct permonst pm_mail_daemon;
312 obj = mksobj(SCR_MAIL);
313 if (md = makemon(&pm_mail_daemon, u.ux, u.uy)) /* always succeeds */
314 mdrush(md, 0);
316 pline("\"Hello, %s! I have some mail for you.\"", plname);
317 if (md) {
318 if (dist(md->mx, md->my) > 2)
319 pline("\"Catch!\"");
320 more();
322 /* let him disappear again */
323 mdrush(md, 1);
324 mondead(md);
327 obj = addinv(obj);
328 identify(obj); /* set known and do prinv() */
331 /* make md run through the cave */
332 static void
333 mdrush(struct monst *md, bool away)
335 int uroom = inroom(u.ux, u.uy);
337 if (uroom >= 0) {
338 int tmp = rooms[uroom].fdoor;
339 int cnt = rooms[uroom].doorct;
340 int fx = u.ux, fy = u.uy;
341 while (cnt--) {
342 if (dist(fx, fy) < dist(doors[tmp].x, doors[tmp].y)) {
343 fx = doors[tmp].x;
344 fy = doors[tmp].y;
346 tmp++;
348 tmp_at(-1, md->data->mlet); /* open call */
349 if (away) { /* interchange origin and destination */
350 unpmon(md);
351 tmp = fx;
352 fx = md->mx;
353 md->mx = tmp;
354 tmp = fy;
355 fy = md->my;
356 md->my = tmp;
358 while (fx != md->mx || fy != md->my) {
359 int dx, dy, nfx = fx, nfy = fy, d1, d2;
361 tmp_at(fx, fy);
362 d1 = DIST(fx, fy, md->mx, md->my);
363 for (dx = -1; dx <= 1; dx++)
364 for (dy = -1; dy <= 1; dy++)
365 if (dx || dy) {
366 d2 = DIST(fx + dx, fy + dy, md->mx, md->my);
367 if (d2 < d1) {
368 d1 = d2;
369 nfx = fx + dx;
370 nfy = fy + dy;
373 if (nfx != fx || nfy != fy) {
374 fx = nfx;
375 fy = nfy;
376 } else {
377 if (!away) {
378 md->mx = fx;
379 md->my = fy;
381 break;
384 tmp_at(-1, -1); /* close call */
386 if (!away)
387 pmon(md);
390 void
391 readmail(void)
393 #ifdef DEF_MAILREADER /* This implies that UNIX is defined */
394 char *mr = NULL;
396 more();
397 if (!(mr = getenv("MAILREADER")))
398 mr = DEF_MAILREADER;
399 if (child(1)) {
400 execl(mr, mr, NULL);
401 exit(1);
403 #else /* DEF_MAILREADER */
404 page_file(mailbox, FALSE);
405 #endif /* DEF_MAILREADER */
406 /* get new stat; not entirely correct: there is a small time
407 * window where we do not see new mail */
408 getmailstatus();
410 #endif /* MAIL */
412 void
413 regularize(char *s) /* normalize file name - we don't like ..'s or /'s */
415 char *lp;
417 while ((lp = strchr(s, '.')) || (lp = strchr(s, '/')))
418 *lp = '_';