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