]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - hack/hack.unix.c
2 * Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985.
6 static char rcsid
[] = "$NetBSD: hack.unix.c,v 1.4 1996/02/06 22:47:25 jtc Exp $";
9 /* This file collects some Unix dependencies; hack.pager.c contains some more */
12 * The time is used for:
14 * - year on tombstone and yymmdd in record file
15 * - phase of the moon (various monsters react to NEW_MOON or FULL_MOON)
16 * - night and midnight (the undead are dangerous at midnight)
17 * - determination of what files are "very old"
22 #include "hack.h" /* mainly for index() which depends on BSD */
24 #include <sys/types.h> /* for time_t and stat */
32 extern char *getenv();
37 (void) srandom((int) time ((time_t *) 0));
44 struct tm
*localtime();
47 return(localtime(&date
));
52 return(1900 + getlt()->tm_year
);
58 static char datestr
[7];
59 register struct tm
*lt
= getlt();
61 (void) sprintf(datestr
, "%2d%2d%2d",
62 lt
->tm_year
, lt
->tm_mon
+ 1, lt
->tm_mday
);
63 if(datestr
[2] == ' ') datestr
[2] = '0';
64 if(datestr
[4] == ' ') datestr
[4] = '0';
68 phase_of_the_moon() /* 0-7, with 0: new, 4: full */
69 { /* moon period: 29.5306 days */
70 /* year: 365.2422 days */
71 register struct tm
*lt
= getlt();
72 register int epact
, diy
, golden
;
75 golden
= (lt
->tm_year
% 19) + 1;
76 epact
= (11 * golden
+ 18) % 30;
77 if ((epact
== 25 && golden
> 11) || epact
== 24)
80 return( (((((diy
+ epact
) * 6) + 11) % 177) / 22) & 7 );
85 register int hour
= getlt()->tm_hour
;
87 return(hour
< 6 || hour
> 21);
92 return(getlt()->tm_hour
== 0);
95 struct stat buf
, hbuf
;
97 gethdate(name
) char *name
; {
98 /* old version - for people short of space */
100 /* register char *np;
101 /* if(stat(name, &hbuf))
102 /* error("Cannot get status of %s.",
103 /* (np = rindex(name, '/')) ? np+1 : name);
105 /* version using PATH from: seismo!gregc@ucsf-cgl.ARPA (Greg Couch) */
109 * The problem with #include <sys/param.h> is that this include file
110 * does not exist on all systems, and moreover, that it sometimes includes
111 * <sys/types.h> again, so that the compiler sees these typedefs twice.
113 #define MAXPATHLEN 1024
115 register char *np
, *path
;
116 char filename
[MAXPATHLEN
+1];
117 if (index(name
, '/') != NULL
|| (path
= getenv("PATH")) == NULL
)
121 if ((np
= index(path
, ':')) == NULL
)
122 np
= path
+ strlen(path
); /* point to end str */
123 if (np
- path
<= 1) /* %% */
124 (void) strcpy(filename
, name
);
126 (void) strncpy(filename
, path
, np
- path
);
127 filename
[np
- path
] = '/';
128 (void) strcpy(filename
+ (np
- path
) + 1, name
);
130 if (stat(filename
, &hbuf
) == 0)
136 error("Cannot get status of %s.",
137 (np
= rindex(name
, '/')) ? np
+1 : name
);
141 if(fstat(fd
, &buf
)) {
142 pline("Cannot get status of saved level? ");
145 if(buf
.st_mtime
< hbuf
.st_mtime
) {
146 pline("Saved level is out of date. ");
152 /* see whether we should throw away this xlock file */
157 if(fstat(fd
, &buf
)) return(0); /* cannot get status */
158 if(buf
.st_size
!= sizeof(int)) return(0); /* not an xlock file */
160 if(date
- buf
.st_mtime
< 3L*24L*60L*60L) { /* recent */
161 int lockedpid
; /* should be the same size as hackpid */
163 if(read(fd
, (char *)&lockedpid
, sizeof(lockedpid
)) !=
168 /* From: Rick Adams <seismo!rick>
169 /* This will work on 4.1cbsd, 4.2bsd and system 3? & 5.
170 /* It will do nothing on V7 or 4.1bsd. */
171 if(!(kill(lockedpid
, 0) == -1 && errno
== ESRCH
))
175 for(i
= 1; i
<= MAXLEVEL
; i
++) { /* try to remove all */
180 if(unlink(lock
)) return(0); /* cannot remove it */
181 return(1); /* success! */
186 extern int hackpid
, locknum
;
187 register int i
= 0, fd
;
189 (void) fflush(stdout
);
191 /* we ignore QUIT and INT at this point */
192 if (link(HLOCK
, LLOCK
) == -1) {
193 register int errnosv
= errno
;
196 printf("Cannot link %s to %s\n", LLOCK
, HLOCK
);
199 printf("Perhaps there is no (empty) file %s ?\n", HLOCK
);
202 printf("It seems you don't have write permission here.\n");
205 printf("(Try again or rm %s.)\n", LLOCK
);
208 printf("I don't know what is wrong.");
217 if(locknum
> 25) locknum
= 25;
220 if(locknum
) lock
[0] = 'a' + i
++;
222 if((fd
= open(lock
, 0)) == -1) {
223 if(errno
== ENOENT
) goto gotlock
; /* no such file */
225 (void) unlink(LLOCK
);
226 error("Cannot open %s", lock
);
229 if(veryold(fd
)) /* if true, this closes fd and unlinks lock */
232 } while(i
< locknum
);
234 (void) unlink(LLOCK
);
235 error(locknum
? "Too many hacks running now."
236 : "There is a game in progress under your name.");
238 fd
= creat(lock
, FMASK
);
239 if(unlink(LLOCK
) == -1)
240 error("Cannot unlink %s.", LLOCK
);
242 error("cannot creat lock file.");
244 if(write(fd
, (char *) &hackpid
, sizeof(hackpid
))
246 error("cannot write lock");
248 if(close(fd
) == -1) {
249 error("cannot close lock");
257 * Notify user when new mail has arrived. [Idea from Merlyn Leroy, but
258 * I don't know the details of his implementation.]
259 * { Later note: he disliked my calling a general mailreader and felt that
260 * hack should do the paging itself. But when I get mail, I want to put it
261 * in some folder, reply, etc. - it would be unreasonable to put all these
262 * functions in hack. }
263 * The mail daemon '2' is at present not a real monster, but only a visual
264 * effect. Thus, makemon() is superfluous. This might become otherwise,
265 * however. The motion of '2' is less restrained than usual: diagonal moves
266 * from a DOOR are possible. He might also use SDOOR's. Also, '2' is visible
267 * in a ROOM, even when you are Blind.
268 * Its path should be longer when you are Telepat-hic and Blind.
270 * Interesting side effects:
271 * - You can get rich by sending yourself a lot of mail and selling
272 * it to the shopkeeper. Unfortunately mail isn't very valuable.
273 * - You might die in case '2' comes along at a critical moment during
274 * a fight and delivers a scroll the weight of which causes you to
277 * Possible extensions:
278 * - Open the file MAIL and do fstat instead of stat for efficiency.
279 * (But sh uses stat, so this cannot be too bad.)
280 * - Examine the mail and produce a scroll of mail called "From somebody".
281 * - Invoke MAILREADER in such a way that only this single letter is read.
283 * - Make him lose his mail when a Nymph steals the letter.
284 * - Do something to the text when the scroll is enchanted or cancelled.
286 #include "def.mkroom.h"
287 static struct stat omstat
,nmstat
;
288 static char *mailbox
;
289 static long laststattime
;
292 if(!(mailbox
= getenv("MAIL")))
294 if(stat(mailbox
, &omstat
)){
295 #ifdef PERMANENT_MAILBOX
296 pline("Cannot get status of MAIL=%s .", mailbox
);
300 #endif PERMANENT_MAILBOX
307 || moves
< laststattime
+ MAILCKFREQ
311 laststattime
= moves
;
312 if(stat(mailbox
, &nmstat
)){
313 #ifdef PERMANENT_MAILBOX
314 pline("Cannot get status of MAIL=%s anymore.", mailbox
);
318 #endif PERMANENT_MAILBOX
319 } else if(nmstat
.st_mtime
> omstat
.st_mtime
) {
322 getmailstatus(); /* might be too late ... */
327 /* produce a scroll of mail */
328 register struct obj
*obj
;
329 register struct monst
*md
;
330 extern char plname
[];
331 extern struct obj
*mksobj(), *addinv();
332 extern struct monst
*makemon();
333 extern struct permonst pm_mail_daemon
;
335 obj
= mksobj(SCR_MAIL
);
336 if(md
= makemon(&pm_mail_daemon
, u
.ux
, u
.uy
)) /* always succeeds */
339 pline("\"Hello, %s! I have some mail for you.\"", plname
);
341 if(dist(md
->mx
,md
->my
) > 2)
345 /* let him disappear again */
351 (void) identify(obj
); /* set known and do prinv() */
354 /* make md run through the cave */
356 register struct monst
*md
;
359 register int uroom
= inroom(u
.ux
, u
.uy
);
361 register int tmp
= rooms
[uroom
].fdoor
;
362 register int cnt
= rooms
[uroom
].doorct
;
363 register int fx
= u
.ux
, fy
= u
.uy
;
365 if(dist(fx
,fy
) < dist(doors
[tmp
].x
, doors
[tmp
].y
)){
371 tmp_at(-1, md
->data
->mlet
); /* open call */
372 if(away
) { /* interchange origin and destination */
374 tmp
= fx
; fx
= md
->mx
; md
->mx
= tmp
;
375 tmp
= fy
; fy
= md
->my
; md
->my
= tmp
;
377 while(fx
!= md
->mx
|| fy
!= md
->my
) {
378 register int dx
,dy
,nfx
= fx
,nfy
= fy
,d1
,d2
;
381 d1
= DIST(fx
,fy
,md
->mx
,md
->my
);
382 for(dx
= -1; dx
<= 1; dx
++) for(dy
= -1; dy
<= 1; dy
++)
384 d2
= DIST(fx
+dx
,fy
+dy
,md
->mx
,md
->my
);
391 if(nfx
!= fx
|| nfy
!= fy
) {
402 tmp_at(-1,-1); /* close call */
409 #ifdef DEF_MAILREADER /* This implies that UNIX is defined */
410 register char *mr
= 0;
412 if(!(mr
= getenv("MAILREADER")))
415 execl(mr
, mr
, (char *) 0);
419 (void) page_file(mailbox
, FALSE
);
420 #endif DEF_MAILREADER
421 /* get new stat; not entirely correct: there is a small time
422 window where we do not see new mail */
427 regularize(s
) /* normalize file name - we don't like ..'s or /'s */
432 while((lp
= index(s
, '.')) || (lp
= index(s
, '/')))