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