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