]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - hack/hack.unix.c
1 /* $NetBSD: hack.unix.c,v 1.5 1997/10/19 16:59:21 christos Exp $ */
4 * Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985.
9 __RCSID("$NetBSD: hack.unix.c,v 1.5 1997/10/19 16:59:21 christos Exp $");
12 /* This file collects some Unix dependencies; hack.pager.c contains some more */
15 * The time is used for:
17 * - year on tombstone and yymmdd in record file
18 * - phase of the moon (various monsters react to NEW_MOON or FULL_MOON)
19 * - night and midnight (the undead are dangerous at midnight)
20 * - determination of what files are "very old"
24 #include <sys/types.h> /* for time_t and stat */
36 #include "hack.h" /* mainly for strchr() which depends on BSD */
43 (void) srandom((int) time((time_t *) 0));
52 return (localtime(&date
));
58 return (1900 + getlt()->tm_year
);
64 static char datestr
[7];
65 struct tm
*lt
= getlt();
67 (void) sprintf(datestr
, "%2d%2d%2d",
68 lt
->tm_year
, lt
->tm_mon
+ 1, lt
->tm_mday
);
69 if (datestr
[2] == ' ')
71 if (datestr
[4] == ' ')
78 { /* 0-7, with 0: new, 4: full *//* moon
79 * period: 29.5306 days */
80 /* year: 365.2422 days */
81 struct tm
*lt
= getlt();
82 int epact
, diy
, golden
;
85 golden
= (lt
->tm_year
% 19) + 1;
86 epact
= (11 * golden
+ 18) % 30;
87 if ((epact
== 25 && golden
> 11) || epact
== 24)
90 return ((((((diy
+ epact
) * 6) + 11) % 177) / 22) & 7);
96 int hour
= getlt()->tm_hour
;
98 return (hour
< 6 || hour
> 21);
104 return (getlt()->tm_hour
== 0);
107 struct stat buf
, hbuf
;
114 /* old version - for people short of space */
118 if(stat(name
, &hbuf
))
119 error("Cannot get status of %s.",
120 (np
= strrchr(name
, '/')) ? np
+1 : name
);
122 /* version using PATH from: seismo!gregc@ucsf-cgl.ARPA (Greg Couch) */
126 * The problem with #include <sys/param.h> is that this include file
127 * does not exist on all systems, and moreover, that it sometimes includes
128 * <sys/types.h> again, so that the compiler sees these typedefs twice.
130 #define MAXPATHLEN 1024
133 char filename
[MAXPATHLEN
+ 1];
134 if (strchr(name
, '/') != NULL
|| (path
= getenv("PATH")) == NULL
)
138 if ((np
= strchr(path
, ':')) == NULL
)
139 np
= path
+ strlen(path
); /* point to end str */
140 if (np
- path
<= 1) /* %% */
141 (void) strcpy(filename
, name
);
143 (void) strncpy(filename
, path
, np
- path
);
144 filename
[np
- path
] = '/';
145 (void) strcpy(filename
+ (np
- path
) + 1, name
);
147 if (stat(filename
, &hbuf
) == 0)
153 error("Cannot get status of %s.",
154 (np
= strrchr(name
, '/')) ? np
+ 1 : name
);
161 if (fstat(fd
, &buf
)) {
162 pline("Cannot get status of saved level? ");
165 if (buf
.st_mtime
< hbuf
.st_mtime
) {
166 pline("Saved level is out of date. ");
172 /* see whether we should throw away this xlock file */
181 return (0); /* cannot get status */
182 if (buf
.st_size
!= sizeof(int))
183 return (0); /* not an xlock file */
185 if (date
- buf
.st_mtime
< 3L * 24L * 60L * 60L) { /* recent */
186 int lockedpid
; /* should be the same size as
189 if (read(fd
, (char *) &lockedpid
, sizeof(lockedpid
)) !=
195 * From: Rick Adams <seismo!rick> This will work on
196 * 4.1cbsd, 4.2bsd and system 3? & 5. It will do nothing
199 if (!(kill(lockedpid
, 0) == -1 && errno
== ESRCH
))
203 for (i
= 1; i
<= MAXLEVEL
; i
++) { /* try to remove all */
209 return (0); /* cannot remove it */
210 return (1); /* success! */
216 extern int hackpid
, locknum
;
219 (void) fflush(stdout
);
221 /* we ignore QUIT and INT at this point */
222 if (link(HLOCK
, LLOCK
) == -1) {
226 printf("Cannot link %s to %s\n", LLOCK
, HLOCK
);
229 printf("Perhaps there is no (empty) file %s ?\n", HLOCK
);
232 printf("It seems you don't have write permission here.\n");
235 printf("(Try again or rm %s.)\n", LLOCK
);
238 printf("I don't know what is wrong.");
253 if ((fd
= open(lock
, 0)) == -1) {
255 goto gotlock
; /* no such file */
257 (void) unlink(LLOCK
);
258 error("Cannot open %s", lock
);
260 if (veryold(fd
))/* if true, this closes fd and unlinks lock */
263 } while (i
< locknum
);
265 (void) unlink(LLOCK
);
266 error(locknum
? "Too many hacks running now."
267 : "There is a game in progress under your name.");
269 fd
= creat(lock
, FMASK
);
270 if (unlink(LLOCK
) == -1)
271 error("Cannot unlink %s.", LLOCK
);
273 error("cannot creat lock file.");
275 if (write(fd
, (char *) &hackpid
, sizeof(hackpid
))
276 != sizeof(hackpid
)) {
277 error("cannot write lock");
279 if (close(fd
) == -1) {
280 error("cannot close lock");
288 * Notify user when new mail has arrived. [Idea from Merlyn Leroy, but
289 * I don't know the details of his implementation.]
290 * { Later note: he disliked my calling a general mailreader and felt that
291 * hack should do the paging itself. But when I get mail, I want to put it
292 * in some folder, reply, etc. - it would be unreasonable to put all these
293 * functions in hack. }
294 * The mail daemon '2' is at present not a real monster, but only a visual
295 * effect. Thus, makemon() is superfluous. This might become otherwise,
296 * however. The motion of '2' is less restrained than usual: diagonal moves
297 * from a DOOR are possible. He might also use SDOOR's. Also, '2' is visible
298 * in a ROOM, even when you are Blind.
299 * Its path should be longer when you are Telepat-hic and Blind.
301 * Interesting side effects:
302 * - You can get rich by sending yourself a lot of mail and selling
303 * it to the shopkeeper. Unfortunately mail isn't very valuable.
304 * - You might die in case '2' comes along at a critical moment during
305 * a fight and delivers a scroll the weight of which causes you to
308 * Possible extensions:
309 * - Open the file MAIL and do fstat instead of stat for efficiency.
310 * (But sh uses stat, so this cannot be too bad.)
311 * - Examine the mail and produce a scroll of mail called "From somebody".
312 * - Invoke MAILREADER in such a way that only this single letter is read.
314 * - Make him lose his mail when a Nymph steals the letter.
315 * - Do something to the text when the scroll is enchanted or cancelled.
317 #include "def.mkroom.h"
318 static struct stat omstat
, nmstat
;
319 static char *mailbox
;
320 static long laststattime
;
325 if (!(mailbox
= getenv("MAIL")))
327 if (stat(mailbox
, &omstat
)) {
328 #ifdef PERMANENT_MAILBOX
329 pline("Cannot get status of MAIL=%s .", mailbox
);
333 #endif /* PERMANENT_MAILBOX */
342 || moves
< laststattime
+ MAILCKFREQ
343 #endif /* MAILCKFREQ */
346 laststattime
= moves
;
347 if (stat(mailbox
, &nmstat
)) {
348 #ifdef PERMANENT_MAILBOX
349 pline("Cannot get status of MAIL=%s anymore.", mailbox
);
353 #endif /* PERMANENT_MAILBOX */
354 } else if (nmstat
.st_mtime
> omstat
.st_mtime
) {
357 getmailstatus();/* might be too late ... */
364 /* produce a scroll of mail */
368 obj
= mksobj(SCR_MAIL
);
369 if (md
= makemon(&pm_mail_daemon
, u
.ux
, u
.uy
)) /* always succeeds */
372 pline("\"Hello, %s! I have some mail for you.\"", plname
);
374 if (dist(md
->mx
, md
->my
) > 2)
378 /* let him disappear again */
383 (void) identify(obj
); /* set known and do prinv() */
386 /* make md run through the cave */
392 int uroom
= inroom(u
.ux
, u
.uy
);
394 int tmp
= rooms
[uroom
].fdoor
;
395 int cnt
= rooms
[uroom
].doorct
;
396 int fx
= u
.ux
, fy
= u
.uy
;
398 if (dist(fx
, fy
) < dist(doors
[tmp
].x
, doors
[tmp
].y
)) {
404 tmp_at(-1, md
->data
->mlet
); /* open call */
405 if (away
) { /* interchange origin and destination */
414 while (fx
!= md
->mx
|| fy
!= md
->my
) {
415 int dx
, dy
, nfx
= fx
, nfy
= fy
, d1
,
419 d1
= DIST(fx
, fy
, md
->mx
, md
->my
);
420 for (dx
= -1; dx
<= 1; dx
++)
421 for (dy
= -1; dy
<= 1; dy
++)
423 d2
= DIST(fx
+ dx
, fy
+ dy
, md
->mx
, md
->my
);
430 if (nfx
!= fx
|| nfy
!= fy
) {
441 tmp_at(-1, -1); /* close call */
450 #ifdef DEF_MAILREADER /* This implies that UNIX is defined */
453 if (!(mr
= getenv("MAILREADER")))
456 execl(mr
, mr
, (char *) 0);
459 #else /* DEF_MAILREADER */
460 (void) page_file(mailbox
, FALSE
);
461 #endif /* DEF_MAILREADER */
463 * get new stat; not entirely correct: there is a small time window
464 * where we do not see new mail
471 regularize(s
) /* normalize file name - we don't like ..'s
477 while ((lp
= strchr(s
, '.')) || (lp
= strchr(s
, '/')))