NetBSD/games/hack/hack.unix.c

480 lines
11 KiB
C
Raw Normal View History

1997-10-19 20:56:41 +04:00
/* $NetBSD: hack.unix.c,v 1.5 1997/10/19 16:59:21 christos Exp $ */
/*
* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985.
*/
1997-10-19 20:56:41 +04:00
#include <sys/cdefs.h>
#ifndef lint
1997-10-19 20:56:41 +04:00
__RCSID("$NetBSD: hack.unix.c,v 1.5 1997/10/19 16:59:21 christos Exp $");
#endif /* not lint */
1993-03-21 12:45:37 +03:00
/* This file collects some Unix dependencies; hack.pager.c contains some more */
/*
* The time is used for:
* - seed for random()
* - year on tombstone and yymmdd in record file
* - phase of the moon (various monsters react to NEW_MOON or FULL_MOON)
* - night and midnight (the undead are dangerous at midnight)
* - determination of what files are "very old"
*/
#include <errno.h>
1997-10-19 20:56:41 +04:00
#include <sys/types.h> /* for time_t and stat */
#include <sys/stat.h>
1993-03-21 12:45:37 +03:00
#ifdef BSD
1997-10-19 20:56:41 +04:00
#include <sys/time.h>
1993-03-21 12:45:37 +03:00
#else
1997-10-19 20:56:41 +04:00
#include <time.h>
#endif /* BSD */
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <fcntl.h>
1993-03-21 12:45:37 +03:00
1997-10-19 20:56:41 +04:00
#include "hack.h" /* mainly for strchr() which depends on BSD */
#include "extern.h"
1993-03-21 12:45:37 +03:00
1997-10-19 20:56:41 +04:00
void
1993-03-21 12:45:37 +03:00
setrandom()
{
1997-10-19 20:56:41 +04:00
(void) srandom((int) time((time_t *) 0));
1993-03-21 12:45:37 +03:00
}
1997-10-19 20:56:41 +04:00
struct tm *
1993-03-21 12:45:37 +03:00
getlt()
{
1997-10-19 20:56:41 +04:00
time_t date;
1993-03-21 12:45:37 +03:00
(void) time(&date);
1997-10-19 20:56:41 +04:00
return (localtime(&date));
1993-03-21 12:45:37 +03:00
}
1997-10-19 20:56:41 +04:00
int
1993-03-21 12:45:37 +03:00
getyear()
{
1997-10-19 20:56:41 +04:00
return (1900 + getlt()->tm_year);
1993-03-21 12:45:37 +03:00
}
1997-10-19 20:56:41 +04:00
char *
1993-03-21 12:45:37 +03:00
getdate()
{
1997-10-19 20:56:41 +04:00
static char datestr[7];
struct tm *lt = getlt();
1993-03-21 12:45:37 +03:00
(void) sprintf(datestr, "%2d%2d%2d",
1997-10-19 20:56:41 +04:00
lt->tm_year, lt->tm_mon + 1, lt->tm_mday);
if (datestr[2] == ' ')
datestr[2] = '0';
if (datestr[4] == ' ')
datestr[4] = '0';
return (datestr);
1993-03-21 12:45:37 +03:00
}
1997-10-19 20:56:41 +04:00
int
phase_of_the_moon()
{ /* 0-7, with 0: new, 4: full *//* moon
* period: 29.5306 days */
/* year: 365.2422 days */
struct tm *lt = getlt();
int epact, diy, golden;
1993-03-21 12:45:37 +03:00
diy = lt->tm_yday;
golden = (lt->tm_year % 19) + 1;
epact = (11 * golden + 18) % 30;
if ((epact == 25 && golden > 11) || epact == 24)
epact++;
1997-10-19 20:56:41 +04:00
return ((((((diy + epact) * 6) + 11) % 177) / 22) & 7);
1993-03-21 12:45:37 +03:00
}
1997-10-19 20:56:41 +04:00
int
1993-03-21 12:45:37 +03:00
night()
{
1997-10-19 20:56:41 +04:00
int hour = getlt()->tm_hour;
1993-03-21 12:45:37 +03:00
1997-10-19 20:56:41 +04:00
return (hour < 6 || hour > 21);
1993-03-21 12:45:37 +03:00
}
1997-10-19 20:56:41 +04:00
int
1993-03-21 12:45:37 +03:00
midnight()
{
1997-10-19 20:56:41 +04:00
return (getlt()->tm_hour == 0);
1993-03-21 12:45:37 +03:00
}
1997-10-19 20:56:41 +04:00
struct stat buf, hbuf;
1993-03-21 12:45:37 +03:00
1997-10-19 20:56:41 +04:00
void
gethdate(name)
char *name;
{
#if 0
/* old version - for people short of space */
1993-03-21 12:45:37 +03:00
1997-10-19 20:56:41 +04:00
char *np;
1993-03-21 12:45:37 +03:00
1997-10-19 20:56:41 +04:00
if(stat(name, &hbuf))
error("Cannot get status of %s.",
(np = strrchr(name, '/')) ? np+1 : name);
#else
/* version using PATH from: seismo!gregc@ucsf-cgl.ARPA (Greg Couch) */
/*
* The problem with #include <sys/param.h> is that this include file
* does not exist on all systems, and moreover, that it sometimes includes
* <sys/types.h> again, so that the compiler sees these typedefs twice.
*/
1993-03-21 12:45:37 +03:00
#define MAXPATHLEN 1024
1997-10-19 20:56:41 +04:00
char *np, *path;
char filename[MAXPATHLEN + 1];
if (strchr(name, '/') != NULL || (path = getenv("PATH")) == NULL)
1993-03-21 12:45:37 +03:00
path = "";
for (;;) {
1997-10-19 20:56:41 +04:00
if ((np = strchr(path, ':')) == NULL)
1993-03-21 12:45:37 +03:00
np = path + strlen(path); /* point to end str */
1997-10-19 20:56:41 +04:00
if (np - path <= 1) /* %% */
1993-03-21 12:45:37 +03:00
(void) strcpy(filename, name);
else {
(void) strncpy(filename, path, np - path);
filename[np - path] = '/';
(void) strcpy(filename + (np - path) + 1, name);
}
if (stat(filename, &hbuf) == 0)
return;
if (*np == '\0')
break;
path = np + 1;
}
error("Cannot get status of %s.",
1997-10-19 20:56:41 +04:00
(np = strrchr(name, '/')) ? np + 1 : name);
#endif
1993-03-21 12:45:37 +03:00
}
1997-10-19 20:56:41 +04:00
int
uptodate(fd)
{
if (fstat(fd, &buf)) {
1993-03-21 12:45:37 +03:00
pline("Cannot get status of saved level? ");
1997-10-19 20:56:41 +04:00
return (0);
1993-03-21 12:45:37 +03:00
}
1997-10-19 20:56:41 +04:00
if (buf.st_mtime < hbuf.st_mtime) {
1993-03-21 12:45:37 +03:00
pline("Saved level is out of date. ");
1997-10-19 20:56:41 +04:00
return (0);
1993-03-21 12:45:37 +03:00
}
1997-10-19 20:56:41 +04:00
return (1);
1993-03-21 12:45:37 +03:00
}
/* see whether we should throw away this xlock file */
1997-10-19 20:56:41 +04:00
int
veryold(fd)
int fd;
{
int i;
time_t date;
1993-03-21 12:45:37 +03:00
1997-10-19 20:56:41 +04:00
if (fstat(fd, &buf))
return (0); /* cannot get status */
if (buf.st_size != sizeof(int))
return (0); /* not an xlock file */
1993-03-21 12:45:37 +03:00
(void) time(&date);
1997-10-19 20:56:41 +04:00
if (date - buf.st_mtime < 3L * 24L * 60L * 60L) { /* recent */
int lockedpid; /* should be the same size as
* hackpid */
1993-03-21 12:45:37 +03:00
1997-10-19 20:56:41 +04:00
if (read(fd, (char *) &lockedpid, sizeof(lockedpid)) !=
sizeof(lockedpid))
1993-03-21 12:45:37 +03:00
/* strange ... */
1997-10-19 20:56:41 +04:00
return (0);
/*
* From: Rick Adams <seismo!rick> This will work on
* 4.1cbsd, 4.2bsd and system 3? & 5. It will do nothing
* on V7 or 4.1bsd.
*/
if (!(kill(lockedpid, 0) == -1 && errno == ESRCH))
return (0);
1993-03-21 12:45:37 +03:00
}
(void) close(fd);
1997-10-19 20:56:41 +04:00
for (i = 1; i <= MAXLEVEL; i++) { /* try to remove all */
1993-03-21 12:45:37 +03:00
glo(i);
(void) unlink(lock);
}
glo(0);
1997-10-19 20:56:41 +04:00
if (unlink(lock))
return (0); /* cannot remove it */
return (1); /* success! */
1993-03-21 12:45:37 +03:00
}
1997-10-19 20:56:41 +04:00
void
1993-03-21 12:45:37 +03:00
getlock()
{
1997-10-19 20:56:41 +04:00
extern int hackpid, locknum;
int i = 0, fd;
1993-03-21 12:45:37 +03:00
(void) fflush(stdout);
/* we ignore QUIT and INT at this point */
if (link(HLOCK, LLOCK) == -1) {
1997-10-19 20:56:41 +04:00
int errnosv = errno;
1993-03-21 12:45:37 +03:00
perror(HLOCK);
printf("Cannot link %s to %s\n", LLOCK, HLOCK);
1997-10-19 20:56:41 +04:00
switch (errnosv) {
1993-03-21 12:45:37 +03:00
case ENOENT:
1997-10-19 20:56:41 +04:00
printf("Perhaps there is no (empty) file %s ?\n", HLOCK);
break;
1993-03-21 12:45:37 +03:00
case EACCES:
1997-10-19 20:56:41 +04:00
printf("It seems you don't have write permission here.\n");
break;
1993-03-21 12:45:37 +03:00
case EEXIST:
1997-10-19 20:56:41 +04:00
printf("(Try again or rm %s.)\n", LLOCK);
break;
1993-03-21 12:45:37 +03:00
default:
1997-10-19 20:56:41 +04:00
printf("I don't know what is wrong.");
1993-03-21 12:45:37 +03:00
}
getret();
1997-10-19 20:56:41 +04:00
error("%s", "");
/* NOTREACHED */
1993-03-21 12:45:37 +03:00
}
regularize(lock);
glo(0);
1997-10-19 20:56:41 +04:00
if (locknum > 25)
locknum = 25;
1993-03-21 12:45:37 +03:00
do {
1997-10-19 20:56:41 +04:00
if (locknum)
lock[0] = 'a' + i++;
1993-03-21 12:45:37 +03:00
1997-10-19 20:56:41 +04:00
if ((fd = open(lock, 0)) == -1) {
if (errno == ENOENT)
goto gotlock; /* no such file */
1993-03-21 12:45:37 +03:00
perror(lock);
(void) unlink(LLOCK);
error("Cannot open %s", lock);
}
1997-10-19 20:56:41 +04:00
if (veryold(fd))/* if true, this closes fd and unlinks lock */
1993-03-21 12:45:37 +03:00
goto gotlock;
(void) close(fd);
1997-10-19 20:56:41 +04:00
} while (i < locknum);
1993-03-21 12:45:37 +03:00
(void) unlink(LLOCK);
error(locknum ? "Too many hacks running now."
1997-10-19 20:56:41 +04:00
: "There is a game in progress under your name.");
1993-03-21 12:45:37 +03:00
gotlock:
fd = creat(lock, FMASK);
1997-10-19 20:56:41 +04:00
if (unlink(LLOCK) == -1)
1993-03-21 12:45:37 +03:00
error("Cannot unlink %s.", LLOCK);
1997-10-19 20:56:41 +04:00
if (fd == -1) {
1993-03-21 12:45:37 +03:00
error("cannot creat lock file.");
} else {
1997-10-19 20:56:41 +04:00
if (write(fd, (char *) &hackpid, sizeof(hackpid))
!= sizeof(hackpid)) {
1993-03-21 12:45:37 +03:00
error("cannot write lock");
}
1997-10-19 20:56:41 +04:00
if (close(fd) == -1) {
1993-03-21 12:45:37 +03:00
error("cannot close lock");
}
}
1997-10-19 20:56:41 +04:00
}
1993-03-21 12:45:37 +03:00
#ifdef MAIL
/*
* Notify user when new mail has arrived. [Idea from Merlyn Leroy, but
* I don't know the details of his implementation.]
* { Later note: he disliked my calling a general mailreader and felt that
* hack should do the paging itself. But when I get mail, I want to put it
* in some folder, reply, etc. - it would be unreasonable to put all these
* functions in hack. }
* The mail daemon '2' is at present not a real monster, but only a visual
* effect. Thus, makemon() is superfluous. This might become otherwise,
* however. The motion of '2' is less restrained than usual: diagonal moves
* from a DOOR are possible. He might also use SDOOR's. Also, '2' is visible
* in a ROOM, even when you are Blind.
* Its path should be longer when you are Telepat-hic and Blind.
*
* Interesting side effects:
* - You can get rich by sending yourself a lot of mail and selling
* it to the shopkeeper. Unfortunately mail isn't very valuable.
* - You might die in case '2' comes along at a critical moment during
* a fight and delivers a scroll the weight of which causes you to
* collapse.
*
* Possible extensions:
* - Open the file MAIL and do fstat instead of stat for efficiency.
* (But sh uses stat, so this cannot be too bad.)
* - Examine the mail and produce a scroll of mail called "From somebody".
* - Invoke MAILREADER in such a way that only this single letter is read.
*
* - Make him lose his mail when a Nymph steals the letter.
* - Do something to the text when the scroll is enchanted or cancelled.
*/
#include "def.mkroom.h"
1997-10-19 20:56:41 +04:00
static struct stat omstat, nmstat;
static char *mailbox;
static long laststattime;
1993-03-21 12:45:37 +03:00
1997-10-19 20:56:41 +04:00
void
getmailstatus()
{
if (!(mailbox = getenv("MAIL")))
1993-03-21 12:45:37 +03:00
return;
1997-10-19 20:56:41 +04:00
if (stat(mailbox, &omstat)) {
1993-03-21 12:45:37 +03:00
#ifdef PERMANENT_MAILBOX
pline("Cannot get status of MAIL=%s .", mailbox);
mailbox = 0;
#else
omstat.st_mtime = 0;
1997-10-19 20:56:41 +04:00
#endif /* PERMANENT_MAILBOX */
1993-03-21 12:45:37 +03:00
}
}
1997-10-19 20:56:41 +04:00
void
ckmailstatus()
{
if (!mailbox
1993-03-21 12:45:37 +03:00
#ifdef MAILCKFREQ
1997-10-19 20:56:41 +04:00
|| moves < laststattime + MAILCKFREQ
#endif /* MAILCKFREQ */
)
1993-03-21 12:45:37 +03:00
return;
laststattime = moves;
1997-10-19 20:56:41 +04:00
if (stat(mailbox, &nmstat)) {
1993-03-21 12:45:37 +03:00
#ifdef PERMANENT_MAILBOX
pline("Cannot get status of MAIL=%s anymore.", mailbox);
mailbox = 0;
#else
nmstat.st_mtime = 0;
1997-10-19 20:56:41 +04:00
#endif /* PERMANENT_MAILBOX */
} else if (nmstat.st_mtime > omstat.st_mtime) {
if (nmstat.st_size)
1993-03-21 12:45:37 +03:00
newmail();
1997-10-19 20:56:41 +04:00
getmailstatus();/* might be too late ... */
1993-03-21 12:45:37 +03:00
}
}
1997-10-19 20:56:41 +04:00
void
newmail()
{
1993-03-21 12:45:37 +03:00
/* produce a scroll of mail */
1997-10-19 20:56:41 +04:00
struct obj *obj;
struct monst *md;
1993-03-21 12:45:37 +03:00
obj = mksobj(SCR_MAIL);
1997-10-19 20:56:41 +04:00
if (md = makemon(&pm_mail_daemon, u.ux, u.uy)) /* always succeeds */
mdrush(md, 0);
1993-03-21 12:45:37 +03:00
pline("\"Hello, %s! I have some mail for you.\"", plname);
1997-10-19 20:56:41 +04:00
if (md) {
if (dist(md->mx, md->my) > 2)
1993-03-21 12:45:37 +03:00
pline("\"Catch!\"");
more();
/* let him disappear again */
1997-10-19 20:56:41 +04:00
mdrush(md, 1);
1993-03-21 12:45:37 +03:00
mondead(md);
}
obj = addinv(obj);
1997-10-19 20:56:41 +04:00
(void) identify(obj); /* set known and do prinv() */
1993-03-21 12:45:37 +03:00
}
/* make md run through the cave */
1997-10-19 20:56:41 +04:00
void
mdrush(md, away)
struct monst *md;
boolean away;
1993-03-21 12:45:37 +03:00
{
1997-10-19 20:56:41 +04:00
int uroom = inroom(u.ux, u.uy);
if (uroom >= 0) {
int tmp = rooms[uroom].fdoor;
int cnt = rooms[uroom].doorct;
int fx = u.ux, fy = u.uy;
while (cnt--) {
if (dist(fx, fy) < dist(doors[tmp].x, doors[tmp].y)) {
1993-03-21 12:45:37 +03:00
fx = doors[tmp].x;
fy = doors[tmp].y;
}
tmp++;
}
tmp_at(-1, md->data->mlet); /* open call */
1997-10-19 20:56:41 +04:00
if (away) { /* interchange origin and destination */
1993-03-21 12:45:37 +03:00
unpmon(md);
1997-10-19 20:56:41 +04:00
tmp = fx;
fx = md->mx;
md->mx = tmp;
tmp = fy;
fy = md->my;
md->my = tmp;
1993-03-21 12:45:37 +03:00
}
1997-10-19 20:56:41 +04:00
while (fx != md->mx || fy != md->my) {
int dx, dy, nfx = fx, nfy = fy, d1,
d2;
tmp_at(fx, fy);
d1 = DIST(fx, fy, md->mx, md->my);
for (dx = -1; dx <= 1; dx++)
for (dy = -1; dy <= 1; dy++)
if (dx || dy) {
d2 = DIST(fx + dx, fy + dy, md->mx, md->my);
if (d2 < d1) {
d1 = d2;
nfx = fx + dx;
nfy = fy + dy;
}
}
if (nfx != fx || nfy != fy) {
fx = nfx;
fy = nfy;
1993-03-21 12:45:37 +03:00
} else {
1997-10-19 20:56:41 +04:00
if (!away) {
md->mx = fx;
md->my = fy;
}
break;
}
1993-03-21 12:45:37 +03:00
}
1997-10-19 20:56:41 +04:00
tmp_at(-1, -1); /* close call */
1993-03-21 12:45:37 +03:00
}
1997-10-19 20:56:41 +04:00
if (!away)
1993-03-21 12:45:37 +03:00
pmon(md);
}
1997-10-19 20:56:41 +04:00
void
readmail()
{
#ifdef DEF_MAILREADER /* This implies that UNIX is defined */
char *mr = 0;
1993-03-21 12:45:37 +03:00
more();
1997-10-19 20:56:41 +04:00
if (!(mr = getenv("MAILREADER")))
1993-03-21 12:45:37 +03:00
mr = DEF_MAILREADER;
1997-10-19 20:56:41 +04:00
if (child(1)) {
1993-03-21 12:45:37 +03:00
execl(mr, mr, (char *) 0);
exit(1);
}
1997-10-19 20:56:41 +04:00
#else /* DEF_MAILREADER */
1993-03-21 12:45:37 +03:00
(void) page_file(mailbox, FALSE);
1997-10-19 20:56:41 +04:00
#endif /* DEF_MAILREADER */
/*
* get new stat; not entirely correct: there is a small time window
* where we do not see new mail
*/
1993-03-21 12:45:37 +03:00
getmailstatus();
}
1997-10-19 20:56:41 +04:00
#endif /* MAIL */
1993-03-21 12:45:37 +03:00
1997-10-19 20:56:41 +04:00
void
regularize(s) /* normalize file name - we don't like ..'s
* or /'s */
char *s;
1993-03-21 12:45:37 +03:00
{
1997-10-19 20:56:41 +04:00
char *lp;
1993-03-21 12:45:37 +03:00
1997-10-19 20:56:41 +04:00
while ((lp = strchr(s, '.')) || (lp = strchr(s, '/')))
1993-03-21 12:45:37 +03:00
*lp = '_';
}