Home | History | Annotate | Line # | Download | only in hack
hack.unix.c revision 1.9.16.2
      1  1.9.16.2       snj /*	$NetBSD: hack.unix.c,v 1.9.16.2 2009/06/29 23:55:23 snj 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.9.16.2       snj __RCSID("$NetBSD: hack.unix.c,v 1.9.16.2 2009/06/29 23:55:23 snj 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.1       cgd setrandom()
    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.1       cgd getlt()
    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.1       cgd getyear()
    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.1       cgd getdate()
    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.9.16.1       snj 	(void) snprintf(datestr, sizeof(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.5  christos phase_of_the_moon()
    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.1       cgd night()
    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.1       cgd midnight()
    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.5  christos gethdate(name)
    166       1.5  christos 	char           *name;
    167       1.5  christos {
    168       1.5  christos #if 0
    169       1.5  christos 	/* old version - for people short of space */
    170       1.5  christos 
    171       1.5  christos 	char *np;
    172       1.1       cgd 
    173       1.5  christos 	if(stat(name, &hbuf))
    174       1.5  christos 	    error("Cannot get status of %s.",
    175       1.5  christos 		(np = strrchr(name, '/')) ? np+1 : name);
    176       1.5  christos #else
    177       1.5  christos 	/* version using PATH from: seismo!gregc (at) ucsf-cgl.ARPA (Greg Couch) */
    178       1.1       cgd 
    179       1.1       cgd 
    180       1.5  christos 	/*
    181       1.5  christos 	 * The problem with   #include	<sys/param.h>   is that this include file
    182       1.5  christos 	 * does not exist on all systems, and moreover, that it sometimes includes
    183       1.5  christos 	 * <sys/types.h> again, so that the compiler sees these typedefs twice.
    184       1.5  christos 	 */
    185       1.1       cgd #define		MAXPATHLEN	1024
    186       1.1       cgd 
    187       1.8       jsm 	const char           *np, *path;
    188       1.5  christos 	char            filename[MAXPATHLEN + 1];
    189       1.5  christos 	if (strchr(name, '/') != NULL || (path = getenv("PATH")) == NULL)
    190       1.1       cgd 		path = "";
    191       1.1       cgd 
    192       1.1       cgd 	for (;;) {
    193       1.5  christos 		if ((np = strchr(path, ':')) == NULL)
    194       1.1       cgd 			np = path + strlen(path);	/* point to end str */
    195       1.5  christos 		if (np - path <= 1)	/* %% */
    196  1.9.16.2       snj 			(void) strlcpy(filename, name, sizeof(filename));
    197       1.1       cgd 		else {
    198  1.9.16.2       snj 			(void) snprintf(filename, sizeof(filename),
    199  1.9.16.2       snj 				"%.*s/%s",
    200  1.9.16.2       snj 				(int)(np - path), path, name);
    201       1.1       cgd 		}
    202       1.1       cgd 		if (stat(filename, &hbuf) == 0)
    203       1.1       cgd 			return;
    204       1.1       cgd 		if (*np == '\0')
    205       1.1       cgd 			break;
    206       1.1       cgd 		path = np + 1;
    207       1.1       cgd 	}
    208       1.1       cgd 	error("Cannot get status of %s.",
    209       1.5  christos 	      (np = strrchr(name, '/')) ? np + 1 : name);
    210       1.5  christos #endif
    211       1.1       cgd }
    212       1.1       cgd 
    213       1.5  christos int
    214       1.8       jsm uptodate(int fd)
    215       1.5  christos {
    216       1.5  christos 	if (fstat(fd, &buf)) {
    217       1.1       cgd 		pline("Cannot get status of saved level? ");
    218       1.5  christos 		return (0);
    219       1.1       cgd 	}
    220       1.5  christos 	if (buf.st_mtime < hbuf.st_mtime) {
    221       1.1       cgd 		pline("Saved level is out of date. ");
    222       1.5  christos 		return (0);
    223       1.1       cgd 	}
    224       1.5  christos 	return (1);
    225       1.1       cgd }
    226       1.1       cgd 
    227       1.1       cgd /* see whether we should throw away this xlock file */
    228       1.5  christos int
    229       1.5  christos veryold(fd)
    230       1.5  christos 	int fd;
    231       1.5  christos {
    232       1.5  christos 	int             i;
    233       1.5  christos 	time_t          date;
    234       1.1       cgd 
    235       1.5  christos 	if (fstat(fd, &buf))
    236       1.5  christos 		return (0);	/* cannot get status */
    237       1.5  christos 	if (buf.st_size != sizeof(int))
    238       1.5  christos 		return (0);	/* not an xlock file */
    239       1.1       cgd 	(void) time(&date);
    240       1.5  christos 	if (date - buf.st_mtime < 3L * 24L * 60L * 60L) {	/* recent */
    241       1.5  christos 		int             lockedpid;	/* should be the same size as
    242       1.5  christos 						 * hackpid */
    243       1.1       cgd 
    244       1.5  christos 		if (read(fd, (char *) &lockedpid, sizeof(lockedpid)) !=
    245       1.5  christos 		    sizeof(lockedpid))
    246       1.1       cgd 			/* strange ... */
    247       1.5  christos 			return (0);
    248       1.1       cgd 
    249       1.5  christos 		/*
    250       1.5  christos 		 * From: Rick Adams <seismo!rick> This will work on
    251       1.5  christos 		 * 4.1cbsd, 4.2bsd and system 3? & 5. It will do nothing
    252       1.5  christos 		 * on V7 or 4.1bsd.
    253       1.5  christos 		 */
    254       1.5  christos 		if (!(kill(lockedpid, 0) == -1 && errno == ESRCH))
    255       1.5  christos 			return (0);
    256       1.1       cgd 	}
    257       1.1       cgd 	(void) close(fd);
    258       1.5  christos 	for (i = 1; i <= MAXLEVEL; i++) {	/* try to remove all */
    259       1.1       cgd 		glo(i);
    260       1.1       cgd 		(void) unlink(lock);
    261       1.1       cgd 	}
    262       1.1       cgd 	glo(0);
    263       1.5  christos 	if (unlink(lock))
    264       1.5  christos 		return (0);	/* cannot remove it */
    265       1.5  christos 	return (1);		/* success! */
    266       1.1       cgd }
    267       1.1       cgd 
    268       1.5  christos void
    269       1.1       cgd getlock()
    270       1.1       cgd {
    271       1.5  christos 	int             i = 0, fd;
    272       1.1       cgd 
    273       1.1       cgd 	(void) fflush(stdout);
    274       1.1       cgd 
    275       1.1       cgd 	/* we ignore QUIT and INT at this point */
    276       1.1       cgd 	if (link(HLOCK, LLOCK) == -1) {
    277       1.5  christos 		int             errnosv = errno;
    278       1.1       cgd 
    279       1.1       cgd 		perror(HLOCK);
    280       1.1       cgd 		printf("Cannot link %s to %s\n", LLOCK, HLOCK);
    281       1.5  christos 		switch (errnosv) {
    282       1.1       cgd 		case ENOENT:
    283       1.5  christos 			printf("Perhaps there is no (empty) file %s ?\n", HLOCK);
    284       1.5  christos 			break;
    285       1.1       cgd 		case EACCES:
    286       1.5  christos 			printf("It seems you don't have write permission here.\n");
    287       1.5  christos 			break;
    288       1.1       cgd 		case EEXIST:
    289       1.5  christos 			printf("(Try again or rm %s.)\n", LLOCK);
    290       1.5  christos 			break;
    291       1.1       cgd 		default:
    292       1.5  christos 			printf("I don't know what is wrong.");
    293       1.1       cgd 		}
    294       1.1       cgd 		getret();
    295       1.5  christos 		error("%s", "");
    296       1.5  christos 		/* NOTREACHED */
    297       1.1       cgd 	}
    298       1.1       cgd 	regularize(lock);
    299       1.1       cgd 	glo(0);
    300       1.5  christos 	if (locknum > 25)
    301       1.5  christos 		locknum = 25;
    302       1.1       cgd 
    303       1.1       cgd 	do {
    304       1.5  christos 		if (locknum)
    305       1.5  christos 			lock[0] = 'a' + i++;
    306       1.1       cgd 
    307       1.8       jsm 		if ((fd = open(lock, O_RDONLY)) == -1) {
    308       1.5  christos 			if (errno == ENOENT)
    309       1.5  christos 				goto gotlock;	/* no such file */
    310       1.1       cgd 			perror(lock);
    311       1.1       cgd 			(void) unlink(LLOCK);
    312       1.1       cgd 			error("Cannot open %s", lock);
    313       1.1       cgd 		}
    314       1.5  christos 		if (veryold(fd))/* if true, this closes fd and unlinks lock */
    315       1.1       cgd 			goto gotlock;
    316       1.1       cgd 		(void) close(fd);
    317       1.5  christos 	} while (i < locknum);
    318       1.1       cgd 
    319       1.1       cgd 	(void) unlink(LLOCK);
    320       1.1       cgd 	error(locknum ? "Too many hacks running now."
    321       1.5  christos 	      : "There is a game in progress under your name.");
    322       1.1       cgd gotlock:
    323       1.1       cgd 	fd = creat(lock, FMASK);
    324       1.5  christos 	if (unlink(LLOCK) == -1)
    325       1.1       cgd 		error("Cannot unlink %s.", LLOCK);
    326       1.5  christos 	if (fd == -1) {
    327       1.1       cgd 		error("cannot creat lock file.");
    328       1.1       cgd 	} else {
    329       1.5  christos 		if (write(fd, (char *) &hackpid, sizeof(hackpid))
    330       1.5  christos 		    != sizeof(hackpid)) {
    331       1.1       cgd 			error("cannot write lock");
    332       1.1       cgd 		}
    333       1.5  christos 		if (close(fd) == -1) {
    334       1.1       cgd 			error("cannot close lock");
    335       1.1       cgd 		}
    336       1.1       cgd 	}
    337       1.5  christos }
    338       1.1       cgd 
    339       1.1       cgd #ifdef MAIL
    340       1.1       cgd 
    341       1.1       cgd /*
    342       1.1       cgd  * Notify user when new mail has arrived. [Idea from Merlyn Leroy, but
    343       1.1       cgd  * I don't know the details of his implementation.]
    344       1.1       cgd  * { Later note: he disliked my calling a general mailreader and felt that
    345       1.1       cgd  *   hack should do the paging itself. But when I get mail, I want to put it
    346       1.1       cgd  *   in some folder, reply, etc. - it would be unreasonable to put all these
    347       1.1       cgd  *   functions in hack. }
    348       1.1       cgd  * The mail daemon '2' is at present not a real monster, but only a visual
    349       1.1       cgd  * effect. Thus, makemon() is superfluous. This might become otherwise,
    350       1.1       cgd  * however. The motion of '2' is less restrained than usual: diagonal moves
    351       1.1       cgd  * from a DOOR are possible. He might also use SDOOR's. Also, '2' is visible
    352       1.1       cgd  * in a ROOM, even when you are Blind.
    353       1.1       cgd  * Its path should be longer when you are Telepat-hic and Blind.
    354       1.1       cgd  *
    355       1.1       cgd  * Interesting side effects:
    356       1.1       cgd  *	- You can get rich by sending yourself a lot of mail and selling
    357       1.1       cgd  *	  it to the shopkeeper. Unfortunately mail isn't very valuable.
    358       1.1       cgd  *	- You might die in case '2' comes along at a critical moment during
    359       1.1       cgd  *	  a fight and delivers a scroll the weight of which causes you to
    360       1.1       cgd  *	  collapse.
    361       1.1       cgd  *
    362       1.1       cgd  * Possible extensions:
    363       1.1       cgd  *	- Open the file MAIL and do fstat instead of stat for efficiency.
    364       1.1       cgd  *	  (But sh uses stat, so this cannot be too bad.)
    365       1.1       cgd  *	- Examine the mail and produce a scroll of mail called "From somebody".
    366       1.1       cgd  *	- Invoke MAILREADER in such a way that only this single letter is read.
    367       1.1       cgd  *
    368       1.1       cgd  *	- Make him lose his mail when a Nymph steals the letter.
    369       1.1       cgd  *	- Do something to the text when the scroll is enchanted or cancelled.
    370       1.1       cgd  */
    371       1.1       cgd #include	"def.mkroom.h"
    372       1.5  christos static struct stat omstat, nmstat;
    373       1.5  christos static char    *mailbox;
    374       1.5  christos static long     laststattime;
    375       1.1       cgd 
    376       1.5  christos void
    377       1.5  christos getmailstatus()
    378       1.5  christos {
    379       1.5  christos 	if (!(mailbox = getenv("MAIL")))
    380       1.1       cgd 		return;
    381       1.5  christos 	if (stat(mailbox, &omstat)) {
    382       1.1       cgd #ifdef PERMANENT_MAILBOX
    383       1.1       cgd 		pline("Cannot get status of MAIL=%s .", mailbox);
    384       1.1       cgd 		mailbox = 0;
    385       1.1       cgd #else
    386       1.1       cgd 		omstat.st_mtime = 0;
    387       1.5  christos #endif	/* PERMANENT_MAILBOX */
    388       1.1       cgd 	}
    389       1.1       cgd }
    390       1.1       cgd 
    391       1.5  christos void
    392       1.5  christos ckmailstatus()
    393       1.5  christos {
    394       1.5  christos 	if (!mailbox
    395       1.1       cgd #ifdef MAILCKFREQ
    396       1.5  christos 	    || moves < laststattime + MAILCKFREQ
    397       1.5  christos #endif	/* MAILCKFREQ */
    398       1.5  christos 		)
    399       1.1       cgd 		return;
    400       1.1       cgd 	laststattime = moves;
    401       1.5  christos 	if (stat(mailbox, &nmstat)) {
    402       1.1       cgd #ifdef PERMANENT_MAILBOX
    403       1.1       cgd 		pline("Cannot get status of MAIL=%s anymore.", mailbox);
    404       1.1       cgd 		mailbox = 0;
    405       1.1       cgd #else
    406       1.1       cgd 		nmstat.st_mtime = 0;
    407       1.5  christos #endif	/* PERMANENT_MAILBOX */
    408       1.5  christos 	} else if (nmstat.st_mtime > omstat.st_mtime) {
    409       1.5  christos 		if (nmstat.st_size)
    410       1.1       cgd 			newmail();
    411       1.5  christos 		getmailstatus();/* might be too late ... */
    412       1.1       cgd 	}
    413       1.1       cgd }
    414       1.1       cgd 
    415       1.5  christos void
    416       1.5  christos newmail()
    417       1.5  christos {
    418       1.1       cgd 	/* produce a scroll of mail */
    419       1.5  christos 	struct obj     *obj;
    420       1.5  christos 	struct monst   *md;
    421       1.1       cgd 
    422       1.1       cgd 	obj = mksobj(SCR_MAIL);
    423       1.5  christos 	if (md = makemon(&pm_mail_daemon, u.ux, u.uy))	/* always succeeds */
    424       1.5  christos 		mdrush(md, 0);
    425       1.1       cgd 
    426       1.1       cgd 	pline("\"Hello, %s! I have some mail for you.\"", plname);
    427       1.5  christos 	if (md) {
    428       1.5  christos 		if (dist(md->mx, md->my) > 2)
    429       1.1       cgd 			pline("\"Catch!\"");
    430       1.1       cgd 		more();
    431       1.1       cgd 
    432       1.1       cgd 		/* let him disappear again */
    433       1.5  christos 		mdrush(md, 1);
    434       1.1       cgd 		mondead(md);
    435       1.1       cgd 	}
    436       1.1       cgd 	obj = addinv(obj);
    437       1.5  christos 	(void) identify(obj);	/* set known and do prinv() */
    438       1.1       cgd }
    439       1.1       cgd 
    440       1.1       cgd /* make md run through the cave */
    441       1.5  christos void
    442       1.5  christos mdrush(md, away)
    443       1.5  christos 	struct monst   *md;
    444       1.5  christos 	boolean         away;
    445       1.5  christos {
    446       1.5  christos 	int             uroom = inroom(u.ux, u.uy);
    447       1.5  christos 	if (uroom >= 0) {
    448       1.5  christos 		int             tmp = rooms[uroom].fdoor;
    449       1.5  christos 		int             cnt = rooms[uroom].doorct;
    450       1.5  christos 		int             fx = u.ux, fy = u.uy;
    451       1.5  christos 		while (cnt--) {
    452       1.5  christos 			if (dist(fx, fy) < dist(doors[tmp].x, doors[tmp].y)) {
    453       1.1       cgd 				fx = doors[tmp].x;
    454       1.1       cgd 				fy = doors[tmp].y;
    455       1.1       cgd 			}
    456       1.1       cgd 			tmp++;
    457       1.1       cgd 		}
    458       1.1       cgd 		tmp_at(-1, md->data->mlet);	/* open call */
    459       1.5  christos 		if (away) {	/* interchange origin and destination */
    460       1.1       cgd 			unpmon(md);
    461       1.5  christos 			tmp = fx;
    462       1.5  christos 			fx = md->mx;
    463       1.5  christos 			md->mx = tmp;
    464       1.5  christos 			tmp = fy;
    465       1.5  christos 			fy = md->my;
    466       1.5  christos 			md->my = tmp;
    467       1.1       cgd 		}
    468       1.5  christos 		while (fx != md->mx || fy != md->my) {
    469       1.5  christos 			int             dx, dy, nfx = fx, nfy = fy, d1,
    470       1.5  christos 			                d2;
    471       1.5  christos 
    472       1.5  christos 			tmp_at(fx, fy);
    473       1.5  christos 			d1 = DIST(fx, fy, md->mx, md->my);
    474       1.5  christos 			for (dx = -1; dx <= 1; dx++)
    475       1.5  christos 				for (dy = -1; dy <= 1; dy++)
    476       1.5  christos 					if (dx || dy) {
    477       1.5  christos 						d2 = DIST(fx + dx, fy + dy, md->mx, md->my);
    478       1.5  christos 						if (d2 < d1) {
    479       1.5  christos 							d1 = d2;
    480       1.5  christos 							nfx = fx + dx;
    481       1.5  christos 							nfy = fy + dy;
    482       1.5  christos 						}
    483       1.5  christos 					}
    484       1.5  christos 			if (nfx != fx || nfy != fy) {
    485       1.5  christos 				fx = nfx;
    486       1.5  christos 				fy = nfy;
    487       1.5  christos 			} else {
    488       1.5  christos 				if (!away) {
    489       1.5  christos 					md->mx = fx;
    490       1.5  christos 					md->my = fy;
    491       1.1       cgd 				}
    492       1.5  christos 				break;
    493       1.5  christos 			}
    494       1.1       cgd 		}
    495       1.5  christos 		tmp_at(-1, -1);	/* close call */
    496       1.1       cgd 	}
    497       1.5  christos 	if (!away)
    498       1.1       cgd 		pmon(md);
    499       1.1       cgd }
    500       1.1       cgd 
    501       1.5  christos void
    502       1.5  christos readmail()
    503       1.5  christos {
    504       1.5  christos #ifdef DEF_MAILREADER		/* This implies that UNIX is defined */
    505       1.5  christos 	char           *mr = 0;
    506       1.1       cgd 	more();
    507       1.5  christos 	if (!(mr = getenv("MAILREADER")))
    508       1.1       cgd 		mr = DEF_MAILREADER;
    509       1.5  christos 	if (child(1)) {
    510       1.1       cgd 		execl(mr, mr, (char *) 0);
    511       1.1       cgd 		exit(1);
    512       1.1       cgd 	}
    513       1.5  christos #else	/* DEF_MAILREADER */
    514       1.1       cgd 	(void) page_file(mailbox, FALSE);
    515       1.5  christos #endif	/* DEF_MAILREADER */
    516       1.5  christos 	/*
    517       1.5  christos 	 * get new stat; not entirely correct: there is a small time window
    518       1.5  christos 	 * where we do not see new mail
    519       1.5  christos 	 */
    520       1.1       cgd 	getmailstatus();
    521       1.1       cgd }
    522       1.5  christos #endif	/* MAIL */
    523       1.1       cgd 
    524       1.5  christos void
    525       1.5  christos regularize(s)			/* normalize file name - we don't like ..'s
    526       1.5  christos 				 * or /'s */
    527       1.5  christos 	char           *s;
    528       1.1       cgd {
    529       1.5  christos 	char           *lp;
    530       1.1       cgd 
    531       1.5  christos 	while ((lp = strchr(s, '.')) || (lp = strchr(s, '/')))
    532       1.1       cgd 		*lp = '_';
    533       1.1       cgd }
    534