home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / HACKSRC.ZIP / UNIX.C < prev    next >
C/C++ Source or Header  |  1985-10-16  |  11KB  |  439 lines

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