Home | History | Annotate | Line # | Download | only in hack
hack.unix.c revision 1.11
      1  1.11  dholland /*	$NetBSD: hack.unix.c,v 1.11 2009/06/07 18:30:39 dholland Exp $	*/
      2   1.5  christos 
      3   1.2   mycroft /*
      4   1.9       jsm  * Copyright (c) 1985, Stichting Centrum voor Wiskunde en Informatica,
      5   1.9       jsm  * Amsterdam
      6   1.9       jsm  * All rights reserved.
      7   1.9       jsm  *
      8   1.9       jsm  * Redistribution and use in source and binary forms, with or without
      9   1.9       jsm  * modification, are permitted provided that the following conditions are
     10   1.9       jsm  * met:
     11   1.9       jsm  *
     12   1.9       jsm  * - Redistributions of source code must retain the above copyright notice,
     13   1.9       jsm  * this list of conditions and the following disclaimer.
     14   1.9       jsm  *
     15   1.9       jsm  * - Redistributions in binary form must reproduce the above copyright
     16   1.9       jsm  * notice, this list of conditions and the following disclaimer in the
     17   1.9       jsm  * documentation and/or other materials provided with the distribution.
     18   1.9       jsm  *
     19   1.9       jsm  * - Neither the name of the Stichting Centrum voor Wiskunde en
     20   1.9       jsm  * Informatica, nor the names of its contributors may be used to endorse or
     21   1.9       jsm  * promote products derived from this software without specific prior
     22   1.9       jsm  * written permission.
     23   1.9       jsm  *
     24   1.9       jsm  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
     25   1.9       jsm  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     26   1.9       jsm  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
     27   1.9       jsm  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
     28   1.9       jsm  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     29   1.9       jsm  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     30   1.9       jsm  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     31   1.9       jsm  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
     32   1.9       jsm  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
     33   1.9       jsm  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     34   1.9       jsm  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     35   1.9       jsm  */
     36   1.9       jsm 
     37   1.9       jsm /*
     38   1.9       jsm  * Copyright (c) 1982 Jay Fenlason <hack (at) gnu.org>
     39   1.9       jsm  * All rights reserved.
     40   1.9       jsm  *
     41   1.9       jsm  * Redistribution and use in source and binary forms, with or without
     42   1.9       jsm  * modification, are permitted provided that the following conditions
     43   1.9       jsm  * are met:
     44   1.9       jsm  * 1. Redistributions of source code must retain the above copyright
     45   1.9       jsm  *    notice, this list of conditions and the following disclaimer.
     46   1.9       jsm  * 2. Redistributions in binary form must reproduce the above copyright
     47   1.9       jsm  *    notice, this list of conditions and the following disclaimer in the
     48   1.9       jsm  *    documentation and/or other materials provided with the distribution.
     49   1.9       jsm  * 3. The name of the author may not be used to endorse or promote products
     50   1.9       jsm  *    derived from this software without specific prior written permission.
     51   1.9       jsm  *
     52   1.9       jsm  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
     53   1.9       jsm  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
     54   1.9       jsm  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
     55   1.9       jsm  * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     56   1.9       jsm  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     57   1.9       jsm  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
     58   1.9       jsm  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     59   1.9       jsm  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     60   1.9       jsm  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
     61   1.9       jsm  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     62   1.2   mycroft  */
     63   1.2   mycroft 
     64   1.5  christos #include <sys/cdefs.h>
     65   1.2   mycroft #ifndef lint
     66  1.11  dholland __RCSID("$NetBSD: hack.unix.c,v 1.11 2009/06/07 18:30:39 dholland Exp $");
     67   1.5  christos #endif				/* not lint */
     68   1.1       cgd 
     69   1.1       cgd /* This file collects some Unix dependencies; hack.pager.c contains some more */
     70   1.1       cgd 
     71   1.1       cgd /*
     72   1.1       cgd  * The time is used for:
     73   1.1       cgd  *	- seed for random()
     74   1.1       cgd  *	- year on tombstone and yymmdd in record file
     75   1.1       cgd  *	- phase of the moon (various monsters react to NEW_MOON or FULL_MOON)
     76   1.1       cgd  *	- night and midnight (the undead are dangerous at midnight)
     77   1.1       cgd  *	- determination of what files are "very old"
     78   1.1       cgd  */
     79   1.1       cgd 
     80   1.1       cgd #include <errno.h>
     81   1.5  christos #include <sys/types.h>	/* for time_t and stat */
     82   1.5  christos #include <sys/stat.h>
     83   1.1       cgd #ifdef BSD
     84   1.5  christos #include <sys/time.h>
     85   1.1       cgd #else
     86   1.5  christos #include <time.h>
     87   1.5  christos #endif	/* BSD */
     88   1.5  christos #include <stdlib.h>
     89   1.5  christos #include <unistd.h>
     90   1.5  christos #include <signal.h>
     91   1.5  christos #include <fcntl.h>
     92   1.1       cgd 
     93   1.5  christos #include "hack.h"		/* mainly for strchr() which depends on BSD */
     94   1.5  christos #include "extern.h"
     95   1.1       cgd 
     96   1.7  christos extern int locknum;
     97   1.7  christos 
     98   1.5  christos 
     99   1.5  christos void
    100  1.11  dholland setrandom(void)
    101   1.1       cgd {
    102   1.5  christos 	(void) srandom((int) time((time_t *) 0));
    103   1.1       cgd }
    104   1.1       cgd 
    105   1.5  christos struct tm      *
    106  1.11  dholland getlt(void)
    107   1.1       cgd {
    108   1.5  christos 	time_t          date;
    109   1.1       cgd 
    110   1.1       cgd 	(void) time(&date);
    111   1.5  christos 	return (localtime(&date));
    112   1.1       cgd }
    113   1.1       cgd 
    114   1.5  christos int
    115  1.11  dholland getyear(void)
    116   1.1       cgd {
    117   1.5  christos 	return (1900 + getlt()->tm_year);
    118   1.1       cgd }
    119   1.1       cgd 
    120   1.5  christos char           *
    121  1.11  dholland getdatestr(void)
    122   1.1       cgd {
    123   1.5  christos 	static char     datestr[7];
    124   1.5  christos 	struct tm      *lt = getlt();
    125   1.1       cgd 
    126   1.6  kristerw 	(void) sprintf(datestr, "%02d%02d%02d",
    127   1.6  kristerw 		       lt->tm_year % 100, lt->tm_mon + 1, lt->tm_mday);
    128   1.5  christos 	return (datestr);
    129   1.1       cgd }
    130   1.1       cgd 
    131   1.5  christos int
    132  1.11  dholland phase_of_the_moon(void)
    133   1.5  christos {				/* 0-7, with 0: new, 4: full *//* moon
    134   1.5  christos 				 * period: 29.5306 days */
    135   1.5  christos 	/* year: 365.2422 days */
    136   1.5  christos 	struct tm      *lt = getlt();
    137   1.5  christos 	int             epact, diy, golden;
    138   1.1       cgd 
    139   1.1       cgd 	diy = lt->tm_yday;
    140   1.1       cgd 	golden = (lt->tm_year % 19) + 1;
    141   1.1       cgd 	epact = (11 * golden + 18) % 30;
    142   1.1       cgd 	if ((epact == 25 && golden > 11) || epact == 24)
    143   1.1       cgd 		epact++;
    144   1.1       cgd 
    145   1.5  christos 	return ((((((diy + epact) * 6) + 11) % 177) / 22) & 7);
    146   1.1       cgd }
    147   1.1       cgd 
    148   1.5  christos int
    149  1.11  dholland night(void)
    150   1.1       cgd {
    151   1.5  christos 	int             hour = getlt()->tm_hour;
    152   1.1       cgd 
    153   1.5  christos 	return (hour < 6 || hour > 21);
    154   1.1       cgd }
    155   1.1       cgd 
    156   1.5  christos int
    157  1.11  dholland midnight(void)
    158   1.1       cgd {
    159   1.5  christos 	return (getlt()->tm_hour == 0);
    160   1.1       cgd }
    161   1.1       cgd 
    162   1.5  christos struct stat     buf, hbuf;
    163   1.5  christos 
    164   1.5  christos void
    165  1.11  dholland gethdate(char *name)
    166   1.5  christos {
    167   1.5  christos #if 0
    168   1.5  christos 	/* old version - for people short of space */
    169   1.5  christos 
    170   1.5  christos 	char *np;
    171   1.1       cgd 
    172   1.5  christos 	if(stat(name, &hbuf))
    173   1.5  christos 	    error("Cannot get status of %s.",
    174   1.5  christos 		(np = strrchr(name, '/')) ? np+1 : name);
    175   1.5  christos #else
    176   1.5  christos 	/* version using PATH from: seismo!gregc (at) ucsf-cgl.ARPA (Greg Couch) */
    177   1.1       cgd 
    178   1.1       cgd 
    179   1.5  christos 	/*
    180   1.5  christos 	 * The problem with   #include	<sys/param.h>   is that this include file
    181   1.5  christos 	 * does not exist on all systems, and moreover, that it sometimes includes
    182   1.5  christos 	 * <sys/types.h> again, so that the compiler sees these typedefs twice.
    183   1.5  christos 	 */
    184   1.1       cgd #define		MAXPATHLEN	1024
    185   1.1       cgd 
    186   1.8       jsm 	const char           *np, *path;
    187   1.5  christos 	char            filename[MAXPATHLEN + 1];
    188   1.5  christos 	if (strchr(name, '/') != NULL || (path = getenv("PATH")) == NULL)
    189   1.1       cgd 		path = "";
    190   1.1       cgd 
    191   1.1       cgd 	for (;;) {
    192   1.5  christos 		if ((np = strchr(path, ':')) == NULL)
    193   1.1       cgd 			np = path + strlen(path);	/* point to end str */
    194   1.5  christos 		if (np - path <= 1)	/* %% */
    195   1.1       cgd 			(void) strcpy(filename, name);
    196   1.1       cgd 		else {
    197   1.1       cgd 			(void) strncpy(filename, path, np - path);
    198   1.1       cgd 			filename[np - path] = '/';
    199   1.1       cgd 			(void) strcpy(filename + (np - path) + 1, name);
    200   1.1       cgd 		}
    201   1.1       cgd 		if (stat(filename, &hbuf) == 0)
    202   1.1       cgd 			return;
    203   1.1       cgd 		if (*np == '\0')
    204   1.1       cgd 			break;
    205   1.1       cgd 		path = np + 1;
    206   1.1       cgd 	}
    207   1.1       cgd 	error("Cannot get status of %s.",
    208   1.5  christos 	      (np = strrchr(name, '/')) ? np + 1 : name);
    209   1.5  christos #endif
    210   1.1       cgd }
    211   1.1       cgd 
    212   1.5  christos int
    213   1.8       jsm uptodate(int fd)
    214   1.5  christos {
    215   1.5  christos 	if (fstat(fd, &buf)) {
    216   1.1       cgd 		pline("Cannot get status of saved level? ");
    217   1.5  christos 		return (0);
    218   1.1       cgd 	}
    219   1.5  christos 	if (buf.st_mtime < hbuf.st_mtime) {
    220   1.1       cgd 		pline("Saved level is out of date. ");
    221   1.5  christos 		return (0);
    222   1.1       cgd 	}
    223   1.5  christos 	return (1);
    224   1.1       cgd }
    225   1.1       cgd 
    226   1.1       cgd /* see whether we should throw away this xlock file */
    227   1.5  christos int
    228  1.11  dholland veryold(int fd)
    229   1.5  christos {
    230   1.5  christos 	int             i;
    231   1.5  christos 	time_t          date;
    232   1.1       cgd 
    233   1.5  christos 	if (fstat(fd, &buf))
    234   1.5  christos 		return (0);	/* cannot get status */
    235   1.5  christos 	if (buf.st_size != sizeof(int))
    236   1.5  christos 		return (0);	/* not an xlock file */
    237   1.1       cgd 	(void) time(&date);
    238   1.5  christos 	if (date - buf.st_mtime < 3L * 24L * 60L * 60L) {	/* recent */
    239   1.5  christos 		int             lockedpid;	/* should be the same size as
    240   1.5  christos 						 * hackpid */
    241   1.1       cgd 
    242   1.5  christos 		if (read(fd, (char *) &lockedpid, sizeof(lockedpid)) !=
    243   1.5  christos 		    sizeof(lockedpid))
    244   1.1       cgd 			/* strange ... */
    245   1.5  christos 			return (0);
    246   1.1       cgd 
    247   1.5  christos 		/*
    248   1.5  christos 		 * From: Rick Adams <seismo!rick> This will work on
    249   1.5  christos 		 * 4.1cbsd, 4.2bsd and system 3? & 5. It will do nothing
    250   1.5  christos 		 * on V7 or 4.1bsd.
    251   1.5  christos 		 */
    252   1.5  christos 		if (!(kill(lockedpid, 0) == -1 && errno == ESRCH))
    253   1.5  christos 			return (0);
    254   1.1       cgd 	}
    255   1.1       cgd 	(void) close(fd);
    256   1.5  christos 	for (i = 1; i <= MAXLEVEL; i++) {	/* try to remove all */
    257   1.1       cgd 		glo(i);
    258   1.1       cgd 		(void) unlink(lock);
    259   1.1       cgd 	}
    260   1.1       cgd 	glo(0);
    261   1.5  christos 	if (unlink(lock))
    262   1.5  christos 		return (0);	/* cannot remove it */
    263   1.5  christos 	return (1);		/* success! */
    264   1.1       cgd }
    265   1.1       cgd 
    266   1.5  christos void
    267  1.11  dholland getlock(void)
    268   1.1       cgd {
    269   1.5  christos 	int             i = 0, fd;
    270   1.1       cgd 
    271   1.1       cgd 	(void) fflush(stdout);
    272   1.1       cgd 
    273   1.1       cgd 	/* we ignore QUIT and INT at this point */
    274   1.1       cgd 	if (link(HLOCK, LLOCK) == -1) {
    275   1.5  christos 		int             errnosv = errno;
    276   1.1       cgd 
    277   1.1       cgd 		perror(HLOCK);
    278   1.1       cgd 		printf("Cannot link %s to %s\n", LLOCK, HLOCK);
    279   1.5  christos 		switch (errnosv) {
    280   1.1       cgd 		case ENOENT:
    281   1.5  christos 			printf("Perhaps there is no (empty) file %s ?\n", HLOCK);
    282   1.5  christos 			break;
    283   1.1       cgd 		case EACCES:
    284   1.5  christos 			printf("It seems you don't have write permission here.\n");
    285   1.5  christos 			break;
    286   1.1       cgd 		case EEXIST:
    287   1.5  christos 			printf("(Try again or rm %s.)\n", LLOCK);
    288   1.5  christos 			break;
    289   1.1       cgd 		default:
    290   1.5  christos 			printf("I don't know what is wrong.");
    291   1.1       cgd 		}
    292   1.1       cgd 		getret();
    293   1.5  christos 		error("%s", "");
    294   1.5  christos 		/* NOTREACHED */
    295   1.1       cgd 	}
    296   1.1       cgd 	regularize(lock);
    297   1.1       cgd 	glo(0);
    298   1.5  christos 	if (locknum > 25)
    299   1.5  christos 		locknum = 25;
    300   1.1       cgd 
    301   1.1       cgd 	do {
    302   1.5  christos 		if (locknum)
    303   1.5  christos 			lock[0] = 'a' + i++;
    304   1.1       cgd 
    305   1.8       jsm 		if ((fd = open(lock, O_RDONLY)) == -1) {
    306   1.5  christos 			if (errno == ENOENT)
    307   1.5  christos 				goto gotlock;	/* no such file */
    308   1.1       cgd 			perror(lock);
    309   1.1       cgd 			(void) unlink(LLOCK);
    310   1.1       cgd 			error("Cannot open %s", lock);
    311   1.1       cgd 		}
    312   1.5  christos 		if (veryold(fd))/* if true, this closes fd and unlinks lock */
    313   1.1       cgd 			goto gotlock;
    314   1.1       cgd 		(void) close(fd);
    315   1.5  christos 	} while (i < locknum);
    316   1.1       cgd 
    317   1.1       cgd 	(void) unlink(LLOCK);
    318   1.1       cgd 	error(locknum ? "Too many hacks running now."
    319   1.5  christos 	      : "There is a game in progress under your name.");
    320   1.1       cgd gotlock:
    321   1.1       cgd 	fd = creat(lock, FMASK);
    322   1.5  christos 	if (unlink(LLOCK) == -1)
    323   1.1       cgd 		error("Cannot unlink %s.", LLOCK);
    324   1.5  christos 	if (fd == -1) {
    325   1.1       cgd 		error("cannot creat lock file.");
    326   1.1       cgd 	} else {
    327   1.5  christos 		if (write(fd, (char *) &hackpid, sizeof(hackpid))
    328   1.5  christos 		    != sizeof(hackpid)) {
    329   1.1       cgd 			error("cannot write lock");
    330   1.1       cgd 		}
    331   1.5  christos 		if (close(fd) == -1) {
    332   1.1       cgd 			error("cannot close lock");
    333   1.1       cgd 		}
    334   1.1       cgd 	}
    335   1.5  christos }
    336   1.1       cgd 
    337   1.1       cgd #ifdef MAIL
    338   1.1       cgd 
    339   1.1       cgd /*
    340   1.1       cgd  * Notify user when new mail has arrived. [Idea from Merlyn Leroy, but
    341   1.1       cgd  * I don't know the details of his implementation.]
    342   1.1       cgd  * { Later note: he disliked my calling a general mailreader and felt that
    343   1.1       cgd  *   hack should do the paging itself. But when I get mail, I want to put it
    344   1.1       cgd  *   in some folder, reply, etc. - it would be unreasonable to put all these
    345   1.1       cgd  *   functions in hack. }
    346   1.1       cgd  * The mail daemon '2' is at present not a real monster, but only a visual
    347   1.1       cgd  * effect. Thus, makemon() is superfluous. This might become otherwise,
    348   1.1       cgd  * however. The motion of '2' is less restrained than usual: diagonal moves
    349   1.1       cgd  * from a DOOR are possible. He might also use SDOOR's. Also, '2' is visible
    350   1.1       cgd  * in a ROOM, even when you are Blind.
    351   1.1       cgd  * Its path should be longer when you are Telepat-hic and Blind.
    352   1.1       cgd  *
    353   1.1       cgd  * Interesting side effects:
    354   1.1       cgd  *	- You can get rich by sending yourself a lot of mail and selling
    355   1.1       cgd  *	  it to the shopkeeper. Unfortunately mail isn't very valuable.
    356   1.1       cgd  *	- You might die in case '2' comes along at a critical moment during
    357   1.1       cgd  *	  a fight and delivers a scroll the weight of which causes you to
    358   1.1       cgd  *	  collapse.
    359   1.1       cgd  *
    360   1.1       cgd  * Possible extensions:
    361   1.1       cgd  *	- Open the file MAIL and do fstat instead of stat for efficiency.
    362   1.1       cgd  *	  (But sh uses stat, so this cannot be too bad.)
    363   1.1       cgd  *	- Examine the mail and produce a scroll of mail called "From somebody".
    364   1.1       cgd  *	- Invoke MAILREADER in such a way that only this single letter is read.
    365   1.1       cgd  *
    366   1.1       cgd  *	- Make him lose his mail when a Nymph steals the letter.
    367   1.1       cgd  *	- Do something to the text when the scroll is enchanted or cancelled.
    368   1.1       cgd  */
    369   1.1       cgd #include	"def.mkroom.h"
    370   1.5  christos static struct stat omstat, nmstat;
    371   1.5  christos static char    *mailbox;
    372   1.5  christos static long     laststattime;
    373   1.1       cgd 
    374   1.5  christos void
    375  1.11  dholland getmailstatus(void)
    376   1.5  christos {
    377   1.5  christos 	if (!(mailbox = getenv("MAIL")))
    378   1.1       cgd 		return;
    379   1.5  christos 	if (stat(mailbox, &omstat)) {
    380   1.1       cgd #ifdef PERMANENT_MAILBOX
    381   1.1       cgd 		pline("Cannot get status of MAIL=%s .", mailbox);
    382   1.1       cgd 		mailbox = 0;
    383   1.1       cgd #else
    384   1.1       cgd 		omstat.st_mtime = 0;
    385   1.5  christos #endif	/* PERMANENT_MAILBOX */
    386   1.1       cgd 	}
    387   1.1       cgd }
    388   1.1       cgd 
    389   1.5  christos void
    390  1.11  dholland ckmailstatus(void)
    391   1.5  christos {
    392   1.5  christos 	if (!mailbox
    393   1.1       cgd #ifdef MAILCKFREQ
    394   1.5  christos 	    || moves < laststattime + MAILCKFREQ
    395   1.5  christos #endif	/* MAILCKFREQ */
    396   1.5  christos 		)
    397   1.1       cgd 		return;
    398   1.1       cgd 	laststattime = moves;
    399   1.5  christos 	if (stat(mailbox, &nmstat)) {
    400   1.1       cgd #ifdef PERMANENT_MAILBOX
    401   1.1       cgd 		pline("Cannot get status of MAIL=%s anymore.", mailbox);
    402   1.1       cgd 		mailbox = 0;
    403   1.1       cgd #else
    404   1.1       cgd 		nmstat.st_mtime = 0;
    405   1.5  christos #endif	/* PERMANENT_MAILBOX */
    406   1.5  christos 	} else if (nmstat.st_mtime > omstat.st_mtime) {
    407   1.5  christos 		if (nmstat.st_size)
    408   1.1       cgd 			newmail();
    409   1.5  christos 		getmailstatus();/* might be too late ... */
    410   1.1       cgd 	}
    411   1.1       cgd }
    412   1.1       cgd 
    413   1.5  christos void
    414  1.11  dholland newmail(void)
    415   1.5  christos {
    416   1.1       cgd 	/* produce a scroll of mail */
    417   1.5  christos 	struct obj     *obj;
    418   1.5  christos 	struct monst   *md;
    419   1.1       cgd 
    420   1.1       cgd 	obj = mksobj(SCR_MAIL);
    421   1.5  christos 	if (md = makemon(&pm_mail_daemon, u.ux, u.uy))	/* always succeeds */
    422   1.5  christos 		mdrush(md, 0);
    423   1.1       cgd 
    424   1.1       cgd 	pline("\"Hello, %s! I have some mail for you.\"", plname);
    425   1.5  christos 	if (md) {
    426   1.5  christos 		if (dist(md->mx, md->my) > 2)
    427   1.1       cgd 			pline("\"Catch!\"");
    428   1.1       cgd 		more();
    429   1.1       cgd 
    430   1.1       cgd 		/* let him disappear again */
    431   1.5  christos 		mdrush(md, 1);
    432   1.1       cgd 		mondead(md);
    433   1.1       cgd 	}
    434   1.1       cgd 	obj = addinv(obj);
    435   1.5  christos 	(void) identify(obj);	/* set known and do prinv() */
    436   1.1       cgd }
    437   1.1       cgd 
    438   1.1       cgd /* make md run through the cave */
    439   1.5  christos void
    440  1.11  dholland mdrush(struct monst *md, boolean away)
    441   1.5  christos {
    442   1.5  christos 	int             uroom = inroom(u.ux, u.uy);
    443   1.5  christos 	if (uroom >= 0) {
    444   1.5  christos 		int             tmp = rooms[uroom].fdoor;
    445   1.5  christos 		int             cnt = rooms[uroom].doorct;
    446   1.5  christos 		int             fx = u.ux, fy = u.uy;
    447   1.5  christos 		while (cnt--) {
    448   1.5  christos 			if (dist(fx, fy) < dist(doors[tmp].x, doors[tmp].y)) {
    449   1.1       cgd 				fx = doors[tmp].x;
    450   1.1       cgd 				fy = doors[tmp].y;
    451   1.1       cgd 			}
    452   1.1       cgd 			tmp++;
    453   1.1       cgd 		}
    454   1.1       cgd 		tmp_at(-1, md->data->mlet);	/* open call */
    455   1.5  christos 		if (away) {	/* interchange origin and destination */
    456   1.1       cgd 			unpmon(md);
    457   1.5  christos 			tmp = fx;
    458   1.5  christos 			fx = md->mx;
    459   1.5  christos 			md->mx = tmp;
    460   1.5  christos 			tmp = fy;
    461   1.5  christos 			fy = md->my;
    462   1.5  christos 			md->my = tmp;
    463   1.1       cgd 		}
    464   1.5  christos 		while (fx != md->mx || fy != md->my) {
    465   1.5  christos 			int             dx, dy, nfx = fx, nfy = fy, d1,
    466   1.5  christos 			                d2;
    467   1.5  christos 
    468   1.5  christos 			tmp_at(fx, fy);
    469   1.5  christos 			d1 = DIST(fx, fy, md->mx, md->my);
    470   1.5  christos 			for (dx = -1; dx <= 1; dx++)
    471   1.5  christos 				for (dy = -1; dy <= 1; dy++)
    472   1.5  christos 					if (dx || dy) {
    473   1.5  christos 						d2 = DIST(fx + dx, fy + dy, md->mx, md->my);
    474   1.5  christos 						if (d2 < d1) {
    475   1.5  christos 							d1 = d2;
    476   1.5  christos 							nfx = fx + dx;
    477   1.5  christos 							nfy = fy + dy;
    478   1.5  christos 						}
    479   1.5  christos 					}
    480   1.5  christos 			if (nfx != fx || nfy != fy) {
    481   1.5  christos 				fx = nfx;
    482   1.5  christos 				fy = nfy;
    483   1.5  christos 			} else {
    484   1.5  christos 				if (!away) {
    485   1.5  christos 					md->mx = fx;
    486   1.5  christos 					md->my = fy;
    487   1.1       cgd 				}
    488   1.5  christos 				break;
    489   1.5  christos 			}
    490   1.1       cgd 		}
    491   1.5  christos 		tmp_at(-1, -1);	/* close call */
    492   1.1       cgd 	}
    493   1.5  christos 	if (!away)
    494   1.1       cgd 		pmon(md);
    495   1.1       cgd }
    496   1.1       cgd 
    497   1.5  christos void
    498  1.11  dholland readmail(void)
    499   1.5  christos {
    500   1.5  christos #ifdef DEF_MAILREADER		/* This implies that UNIX is defined */
    501   1.5  christos 	char           *mr = 0;
    502   1.1       cgd 	more();
    503   1.5  christos 	if (!(mr = getenv("MAILREADER")))
    504   1.1       cgd 		mr = DEF_MAILREADER;
    505   1.5  christos 	if (child(1)) {
    506   1.1       cgd 		execl(mr, mr, (char *) 0);
    507   1.1       cgd 		exit(1);
    508   1.1       cgd 	}
    509   1.5  christos #else	/* DEF_MAILREADER */
    510   1.1       cgd 	(void) page_file(mailbox, FALSE);
    511   1.5  christos #endif	/* DEF_MAILREADER */
    512   1.5  christos 	/*
    513   1.5  christos 	 * get new stat; not entirely correct: there is a small time window
    514   1.5  christos 	 * where we do not see new mail
    515   1.5  christos 	 */
    516   1.1       cgd 	getmailstatus();
    517   1.1       cgd }
    518   1.5  christos #endif	/* MAIL */
    519   1.1       cgd 
    520  1.11  dholland /*
    521  1.11  dholland  * normalize file name - we don't like ..'s or /'s
    522  1.11  dholland  */
    523   1.5  christos void
    524  1.11  dholland regularize(char *s)
    525   1.1       cgd {
    526   1.5  christos 	char           *lp;
    527   1.1       cgd 
    528   1.5  christos 	while ((lp = strchr(s, '.')) || (lp = strchr(s, '/')))
    529   1.1       cgd 		*lp = '_';
    530   1.1       cgd }
    531