/* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
/* hack.unix.c - version 1.0.3 */
/* This file collects some Unix dependencies; hack.pager.c contains some more */
* - 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 "hack.h" /* mainly for index() which depends on BSD */
#include <sys/types.h> /* for time_t and stat */
(void) srandom((int) time ((time_t *) 0));
return(localtime(&date
));
return(1900 + getlt()->tm_year
);
register struct tm
*lt
= getlt();
(void) sprintf(datestr
, "%2d%2d%2d",
lt
->tm_year
, lt
->tm_mon
+ 1, lt
->tm_mday
);
if(datestr
[2] == ' ') datestr
[2] = '0';
if(datestr
[4] == ' ') datestr
[4] = '0';
phase_of_the_moon() /* 0-7, with 0: new, 4: full */
{ /* moon period: 29.5306 days */
/* year: 365.2422 days */
register struct tm
*lt
= getlt();
register int epact
, diy
, golden
;
golden
= (lt
->tm_year
% 19) + 1;
epact
= (11 * golden
+ 18) % 30;
if ((epact
== 25 && golden
> 11) || epact
== 24)
return( (((((diy
+ epact
) * 6) + 11) % 177) / 22) & 7 );
register int hour
= getlt()->tm_hour
;
return(hour
< 6 || hour
> 21);
return(getlt()->tm_hour
== 0);
gethdate(name
) char *name
; {
/* old version - for people short of space */
/* error("Cannot get status of %s.",
/* (np = rindex(name, '/')) ? np+1 : name);
/* 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.
register char *np
, *path
;
char filename
[MAXPATHLEN
+1];
if (index(name
, '/') != NULL
|| (path
= getenv("PATH")) == NULL
)
if ((np
= index(path
, ':')) == NULL
)
np
= path
+ strlen(path
); /* point to end str */
if (np
- path
<= 1) /* %% */
(void) strcpy(filename
, name
);
(void) strncpy(filename
, path
, np
- path
);
filename
[np
- path
] = '/';
(void) strcpy(filename
+ (np
- path
) + 1, name
);
if (stat(filename
, &hbuf
) == 0)
error("Cannot get status of %s.",
(np
= rindex(name
, '/')) ? np
+1 : name
);
pline("Cannot get status of saved level? ");
if(buf
.st_mtime
< hbuf
.st_mtime
) {
pline("Saved level is out of date. ");
/* see whether we should throw away this xlock file */
if(fstat(fd
, &buf
)) return(0); /* cannot get status */
if(buf
.st_size
!= sizeof(int)) return(0); /* not an xlock file */
if(date
- buf
.st_mtime
< 3L*24L*60L*60L) { /* recent */
int lockedpid
; /* should be the same size as hackpid */
if(read(fd
, (char *)&lockedpid
, sizeof(lockedpid
)) !=
/* 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
))
for(i
= 1; i
<= MAXLEVEL
; i
++) { /* try to remove all */
if(unlink(lock
)) return(0); /* cannot remove it */
return(1); /* success! */
extern int errno
, hackpid
, locknum
;
/* we ignore QUIT and INT at this point */
if (link(HLOCK
, LLOCK
) == -1) {
register int errnosv
= errno
;
printf("Cannot link %s to %s\n", LLOCK
, HLOCK
);
printf("Perhaps there is no (empty) file %s ?\n", HLOCK
);
printf("It seems you don't have write permission here.\n");
printf("(Try again or rm %s.)\n", LLOCK
);
printf("I don't know what is wrong.");
if(locknum
> 25) locknum
= 25;
if(locknum
) lock
[0] = 'a' + i
++;
if((fd
= open(lock
, 0)) == -1) {
if(errno
== ENOENT
) goto gotlock
; /* no such file */
error("Cannot open %s", lock
);
if(veryold(fd
)) /* if true, this closes fd and unlinks lock */
error(locknum
? "Too many hacks running now."
: "There is a game in progress under your name.");
error("Cannot unlink %s.", LLOCK
);
error("cannot creat lock file.");
if(write(fd
, (char *) &hackpid
, sizeof(hackpid
))
error("cannot write lock");
error("cannot close lock");
* 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
* 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
* - 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.
static struct stat omstat
,nmstat
;
static long laststattime
;
if(!(mailbox
= getenv("MAIL")))
if(stat(mailbox
, &omstat
)){
pline("Cannot get status of MAIL=%s .", mailbox
);
|| moves
< laststattime
+ MAILCKFREQ
if(stat(mailbox
, &nmstat
)){
pline("Cannot get status of MAIL=%s anymore.", mailbox
);
} else if(nmstat
.st_mtime
> omstat
.st_mtime
) {
getmailstatus(); /* might be too late ... */
/* produce a scroll of mail */
register struct obj
*obj
;
register struct monst
*md
;
extern struct obj
*mksobj(), *addinv();
extern struct monst
*makemon();
extern struct permonst pm_mail_daemon
;
if(md
= makemon(&pm_mail_daemon
, u
.ux
, u
.uy
)) /* always succeeds */
pline("\"Hello, %s! I have some mail for you.\"", plname
);
if(dist(md
->mx
,md
->my
) > 2)
/* let him disappear again */
(void) identify(obj
); /* set known and do prinv() */
/* make md run through the cave */
register struct monst
*md
;
register int uroom
= inroom(u
.ux
, u
.uy
);
register int tmp
= rooms
[uroom
].fdoor
;
register int cnt
= rooms
[uroom
].doorct
;
register int fx
= u
.ux
, fy
= u
.uy
;
if(dist(fx
,fy
) < dist(doors
[tmp
].x
, doors
[tmp
].y
)){
tmp_at(-1, md
->data
->mlet
); /* open call */
if(away
) { /* interchange origin and destination */
tmp
= fx
; fx
= md
->mx
; md
->mx
= tmp
;
tmp
= fy
; fy
= md
->my
; md
->my
= tmp
;
while(fx
!= md
->mx
|| fy
!= md
->my
) {
register int dx
,dy
,nfx
= fx
,nfy
= fy
,d1
,d2
;
d1
= DIST(fx
,fy
,md
->mx
,md
->my
);
for(dx
= -1; dx
<= 1; dx
++) for(dy
= -1; dy
<= 1; dy
++)
d2
= DIST(fx
+dx
,fy
+dy
,md
->mx
,md
->my
);
if(nfx
!= fx
|| nfy
!= fy
) {
tmp_at(-1,-1); /* close call */
#ifdef DEF_MAILREADER /* This implies that UNIX is defined */
if(!(mr
= getenv("MAILREADER")))
execl(mr
, mr
, (char *) 0);
(void) page_file(mailbox
, FALSE
);
/* get new stat; not entirely correct: there is a small time
window where we do not see new mail */
regularize(s
) /* normalize file name - we don't like ..'s or /'s */
while((lp
= index(s
, '.')) || (lp
= index(s
, '/')))