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