hack(6): fix very old mis-merge by restoring code from OpenBSD
[games.git] / games / hack / hack.unix.c
CommitLineData
984263bc
MD
1/* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
2/* hack.unix.c - version 1.0.3 */
3/* $FreeBSD: src/games/hack/hack.unix.c,v 1.8 1999/11/16 02:57:13 billf Exp $ */
c7106d58 4/* $DragonFly: src/games/hack/hack.unix.c,v 1.7 2006/08/21 19:45:32 pavalos Exp $ */
984263bc
MD
5
6/* This file collects some Unix dependencies; hack.pager.c contains some more */
7
8/*
9 * The time is used for:
10 * - seed for random()
11 * - year on tombstone and yymmdd in record file
12 * - phase of the moon (various monsters react to NEW_MOON or FULL_MOON)
13 * - night and midnight (the undead are dangerous at midnight)
14 * - determination of what files are "very old"
15 */
16
984263bc 17#include <errno.h>
984263bc
MD
18#include "hack.h" /* mainly for index() which depends on BSD */
19
20#include <sys/types.h> /* for time_t and stat */
21#include <sys/stat.h>
c91833b6 22#include <sys/param.h>
984263bc 23#include <time.h>
c91833b6 24#include <err.h>
984263bc 25
c7106d58
PA
26static struct tm *getlt(void);
27static bool veryold(int);
28#ifdef MAIL
29static void newmail(void);
30static void mdrush(struct monst *, bool);
31#endif
32
33void
34setrandom(void)
984263bc 35{
c7106d58 36 srandomdev();
984263bc
MD
37}
38
c7106d58
PA
39static struct tm *
40getlt(void)
984263bc
MD
41{
42 time_t date;
984263bc 43
c7106d58 44 time(&date);
984263bc
MD
45 return(localtime(&date));
46}
47
c7106d58
PA
48int
49getyear(void)
984263bc
MD
50{
51 return(1900 + getlt()->tm_year);
52}
53
54char *
c7106d58 55getdate(void)
984263bc
MD
56{
57 static char datestr[7];
58 struct tm *lt = getlt();
59
c7106d58 60 snprintf(datestr, sizeof(datestr), "%02d%02d%02d",
984263bc
MD
61 lt->tm_year % 100, lt->tm_mon + 1, lt->tm_mday);
62 return(datestr);
63}
64
c7106d58
PA
65int
66phase_of_the_moon(void) /* 0-7, with 0: new, 4: full */
984263bc
MD
67{ /* moon period: 29.5306 days */
68 /* year: 365.2422 days */
69 struct tm *lt = getlt();
70 int epact, diy, golden;
71
72 diy = lt->tm_yday;
73 golden = (lt->tm_year % 19) + 1;
74 epact = (11 * golden + 18) % 30;
75 if ((epact == 25 && golden > 11) || epact == 24)
76 epact++;
77
78 return( (((((diy + epact) * 6) + 11) % 177) / 22) & 7 );
79}
80
c7106d58
PA
81bool
82night(void)
984263bc
MD
83{
84 int hour = getlt()->tm_hour;
85
86 return(hour < 6 || hour > 21);
87}
88
c7106d58
PA
89bool
90midnight(void)
984263bc
MD
91{
92 return(getlt()->tm_hour == 0);
93}
94
95struct stat buf, hbuf;
96
fd2d026f
YT
97void
98gethdate(const char *name)
99{
c91833b6
US
100 char *p, *np, *path;
101 char filename[MAXPATHLEN+1];
102
103 if (strchr(name, '/') != NULL || (p = getenv("PATH")) == NULL)
104 p = "";
105 np = path = strdup(p); /* Make a copy for strsep. */
106 if (path == NULL)
107 err(1, NULL);
108
109 for (;;) {
110 if ((p = strsep(&np, ":")) == NULL)
111 break;
112 if (*p == '\0') /* :: */
113 (void) strlcpy(filename, name, sizeof(filename));
114 else
115 (void) snprintf(filename, sizeof(filename),
116 "%s/%s", p, name);
117
118 if (stat(filename, &hbuf) == 0) {
119 free(path);
120 return;
121 }
122 }
123 error("Cannot get status of %s.",
124 (p = strrchr(name, '/')) ? p+1 : name);
125 free(path);
984263bc
MD
126}
127
c7106d58
PA
128bool
129uptodate(int fd)
130{
984263bc
MD
131 if(fstat(fd, &buf)) {
132 pline("Cannot get status of saved level? ");
133 return(0);
134 }
135 if(buf.st_mtime < hbuf.st_mtime) {
136 pline("Saved level is out of date. ");
137 return(0);
138 }
139 return(1);
140}
141
142/* see whether we should throw away this xlock file */
c7106d58
PA
143static bool
144veryold(int fd)
145{
984263bc
MD
146 int i;
147 time_t date;
148
149 if(fstat(fd, &buf)) return(0); /* cannot get status */
150 if(buf.st_size != sizeof(int)) return(0); /* not an xlock file */
c7106d58 151 time(&date);
984263bc 152 if(date - buf.st_mtime < 3L*24L*60L*60L) { /* recent */
984263bc
MD
153 int lockedpid; /* should be the same size as hackpid */
154
155 if(read(fd, (char *)&lockedpid, sizeof(lockedpid)) !=
156 sizeof(lockedpid))
157 /* strange ... */
158 return(0);
159
160 /* From: Rick Adams <seismo!rick> */
161 /* This will work on 4.1cbsd, 4.2bsd and system 3? & 5. */
162 /* It will do nothing on V7 or 4.1bsd. */
163 if(!(kill(lockedpid, 0) == -1 && errno == ESRCH))
164 return(0);
165 }
c7106d58 166 close(fd);
984263bc
MD
167 for(i = 1; i <= MAXLEVEL; i++) { /* try to remove all */
168 glo(i);
c7106d58 169 unlink(lock);
984263bc
MD
170 }
171 glo(0);
172 if(unlink(lock)) return(0); /* cannot remove it */
173 return(1); /* success! */
174}
175
c7106d58
PA
176void
177getlock(void)
984263bc 178{
984263bc
MD
179 int i = 0, fd;
180
c7106d58 181 fflush(stdout);
984263bc
MD
182
183 /* we ignore QUIT and INT at this point */
184 if (link(HLOCK, LLOCK) == -1) {
185 int errnosv = errno;
186
187 perror(HLOCK);
188 printf("Cannot link %s to %s\n", LLOCK, HLOCK);
189 switch(errnosv) {
190 case ENOENT:
191 printf("Perhaps there is no (empty) file %s ?\n", HLOCK);
192 break;
193 case EACCES:
194 printf("It seems you don't have write permission here.\n");
195 break;
196 case EEXIST:
197 printf("(Try again or rm %s.)\n", LLOCK);
198 break;
199 default:
200 printf("I don't know what is wrong.");
201 }
202 getret();
203 error("");
204 /*NOTREACHED*/
205 }
206
207 regularize(lock);
208 glo(0);
209 if(locknum > 25) locknum = 25;
210
211 do {
212 if(locknum) lock[0] = 'a' + i++;
213
214 if((fd = open(lock, 0)) == -1) {
215 if(errno == ENOENT) goto gotlock; /* no such file */
216 perror(lock);
c7106d58 217 unlink(LLOCK);
984263bc
MD
218 error("Cannot open %s", lock);
219 }
220
221 if(veryold(fd)) /* if true, this closes fd and unlinks lock */
222 goto gotlock;
c7106d58 223 close(fd);
984263bc
MD
224 } while(i < locknum);
225
c7106d58 226 unlink(LLOCK);
984263bc
MD
227 error(locknum ? "Too many hacks running now."
228 : "There is a game in progress under your name.");
229gotlock:
230 fd = creat(lock, FMASK);
231 if(unlink(LLOCK) == -1)
232 error("Cannot unlink %s.", LLOCK);
233 if(fd == -1) {
234 error("cannot creat lock file.");
235 } else {
236 if(write(fd, (char *) &hackpid, sizeof(hackpid))
237 != sizeof(hackpid)){
238 error("cannot write lock");
239 }
240 if(close(fd) == -1) {
241 error("cannot close lock");
242 }
243 }
244}
245
246#ifdef MAIL
247
248/*
249 * Notify user when new mail has arrived. [Idea from Merlyn Leroy, but
250 * I don't know the details of his implementation.]
251 * { Later note: he disliked my calling a general mailreader and felt that
252 * hack should do the paging itself. But when I get mail, I want to put it
253 * in some folder, reply, etc. - it would be unreasonable to put all these
254 * functions in hack. }
255 * The mail daemon '2' is at present not a real monster, but only a visual
256 * effect. Thus, makemon() is superfluous. This might become otherwise,
257 * however. The motion of '2' is less restrained than usual: diagonal moves
258 * from a DOOR are possible. He might also use SDOOR's. Also, '2' is visible
259 * in a ROOM, even when you are Blind.
260 * Its path should be longer when you are Telepat-hic and Blind.
261 *
262 * Interesting side effects:
263 * - You can get rich by sending yourself a lot of mail and selling
264 * it to the shopkeeper. Unfortunately mail isn't very valuable.
265 * - You might die in case '2' comes along at a critical moment during
266 * a fight and delivers a scroll the weight of which causes you to
267 * collapse.
268 *
269 * Possible extensions:
270 * - Open the file MAIL and do fstat instead of stat for efficiency.
271 * (But sh uses stat, so this cannot be too bad.)
272 * - Examine the mail and produce a scroll of mail called "From somebody".
273 * - Invoke MAILREADER in such a way that only this single letter is read.
274 *
275 * - Make him lose his mail when a Nymph steals the letter.
276 * - Do something to the text when the scroll is enchanted or cancelled.
277 */
278#include "def.mkroom.h"
279static struct stat omstat,nmstat;
280static char *mailbox;
281static long laststattime;
282
c7106d58
PA
283void
284getmailstatus(void)
285{
984263bc
MD
286 if(!(mailbox = getenv("MAIL")))
287 return;
288 if(stat(mailbox, &omstat)){
289#ifdef PERMANENT_MAILBOX
290 pline("Cannot get status of MAIL=%s .", mailbox);
291 mailbox = 0;
292#else
293 omstat.st_mtime = 0;
304d60d9 294#endif /* PERMANENT_MAILBOX */
984263bc
MD
295 }
296}
297
c7106d58
PA
298void
299ckmailstatus(void)
300{
984263bc
MD
301 if(!mailbox
302#ifdef MAILCKFREQ
303 || moves < laststattime + MAILCKFREQ
304d60d9 304#endif /* MAILCKFREQ */
984263bc
MD
305 )
306 return;
307 laststattime = moves;
308 if(stat(mailbox, &nmstat)){
309#ifdef PERMANENT_MAILBOX
310 pline("Cannot get status of MAIL=%s anymore.", mailbox);
311 mailbox = 0;
312#else
313 nmstat.st_mtime = 0;
304d60d9 314#endif /* PERMANENT_MAILBOX */
984263bc
MD
315 } else if(nmstat.st_mtime > omstat.st_mtime) {
316 if(nmstat.st_size)
317 newmail();
318 getmailstatus(); /* might be too late ... */
319 }
320}
321
c7106d58
PA
322static void
323newmail(void)
324{
984263bc
MD
325 /* produce a scroll of mail */
326 struct obj *obj;
327 struct monst *md;
984263bc
MD
328 extern struct permonst pm_mail_daemon;
329
330 obj = mksobj(SCR_MAIL);
331 if(md = makemon(&pm_mail_daemon, u.ux, u.uy)) /* always succeeds */
332 mdrush(md,0);
333
334 pline("\"Hello, %s! I have some mail for you.\"", plname);
335 if(md) {
336 if(dist(md->mx,md->my) > 2)
337 pline("\"Catch!\"");
338 more();
339
340 /* let him disappear again */
341 mdrush(md,1);
342 mondead(md);
343 }
344
345 obj = addinv(obj);
c7106d58 346 identify(obj); /* set known and do prinv() */
984263bc
MD
347}
348
349/* make md run through the cave */
c7106d58
PA
350static void
351mdrush(struct monst *md, bool away)
984263bc
MD
352{
353 int uroom = inroom(u.ux, u.uy);
354 if(uroom >= 0) {
355 int tmp = rooms[uroom].fdoor;
356 int cnt = rooms[uroom].doorct;
357 int fx = u.ux, fy = u.uy;
358 while(cnt--) {
359 if(dist(fx,fy) < dist(doors[tmp].x, doors[tmp].y)){
360 fx = doors[tmp].x;
361 fy = doors[tmp].y;
362 }
363 tmp++;
364 }
365 tmp_at(-1, md->data->mlet); /* open call */
366 if(away) { /* interchange origin and destination */
367 unpmon(md);
368 tmp = fx; fx = md->mx; md->mx = tmp;
369 tmp = fy; fy = md->my; md->my = tmp;
370 }
371 while(fx != md->mx || fy != md->my) {
372 int dx,dy,nfx = fx,nfy = fy,d1,d2;
373
374 tmp_at(fx,fy);
375 d1 = DIST(fx,fy,md->mx,md->my);
376 for(dx = -1; dx <= 1; dx++) for(dy = -1; dy <= 1; dy++)
377 if(dx || dy) {
378 d2 = DIST(fx+dx,fy+dy,md->mx,md->my);
379 if(d2 < d1) {
380 d1 = d2;
381 nfx = fx+dx;
382 nfy = fy+dy;
383 }
384 }
385 if(nfx != fx || nfy != fy) {
386 fx = nfx;
387 fy = nfy;
388 } else {
389 if(!away) {
390 md->mx = fx;
391 md->my = fy;
392 }
393 break;
394 }
395 }
396 tmp_at(-1,-1); /* close call */
397 }
398 if(!away)
399 pmon(md);
400}
401
c7106d58
PA
402void
403readmail(void)
404{
984263bc
MD
405#ifdef DEF_MAILREADER /* This implies that UNIX is defined */
406 char *mr = 0;
407 more();
408 if(!(mr = getenv("MAILREADER")))
409 mr = DEF_MAILREADER;
410 if(child(1)){
902ec341 411 execl(mr, mr, NULL);
984263bc
MD
412 exit(1);
413 }
69e9165e 414#else /* DEF_MAILREADER */
c7106d58 415 page_file(mailbox, FALSE);
304d60d9 416#endif /* DEF_MAILREADER */
984263bc
MD
417 /* get new stat; not entirely correct: there is a small time
418 window where we do not see new mail */
419 getmailstatus();
420}
304d60d9 421#endif /* MAIL */
984263bc 422
c7106d58
PA
423void
424regularize(char *s) /* normalize file name - we don't like ..'s or /'s */
984263bc
MD
425{
426 char *lp;
427
428 while((lp = index(s, '.')) || (lp = index(s, '/')))
429 *lp = '_';
430}