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