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