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