Source:NetHack 3.6.0/src/hacklib.c

From NetHackWiki
(Redirected from Source:Hacklib.c)
Jump to: navigation, search

Below is the full text to hacklib.c from the source code of NetHack 3.6.0. To link to a particular line, write [[Source:NetHack 3.6.0/src/hacklib.c#line123]], for example.

The NetHack General Public License applies to screenshots, source code and other content from NetHack.

This content was modified from the original NetHack source code distribution (by splitting up NetHack content between wiki pages, and possibly further editing). See the page history for a list of who changed it, and on what dates.

Top of file

  1.  /* NetHack 3.6	hacklib.c	$NHDT-Date: 1446336792 2015/11/01 00:13:12 $  $NHDT-Branch: master $:$NHDT-Revision: 1.44 $ */
  2.  /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
  3.  /* Copyright (c) Robert Patrick Rankin, 1991                      */
  4.  /* NetHack may be freely redistributed.  See license for details. */
  5.  
  6.  #include "hack.h" /* for config.h+extern.h */
  7.  /*=
  8.      Assorted 'small' utility routines.  They're virtually independent of
  9.      NetHack, except that rounddiv may call panic().  setrandom calls one
  10.      of srandom(), srand48(), or srand() depending upon configuration.
  11.  
  12.        return type     routine name    argument type(s)
  13.          boolean         digit           (char)
  14.          boolean         letter          (char)
  15.          char            highc           (char)
  16.          char            lowc            (char)
  17.          char *          lcase           (char *)
  18.          char *          ucase           (char *)
  19.          char *          upstart         (char *)
  20.          char *          mungspaces      (char *)
  21.          char *          eos             (char *)
  22.          boolean         str_end_is      (const char *, const char *)
  23.          char *          strkitten       (char *,char)
  24.          void            copynchars      (char *,const char *,int)
  25.          char            chrcasecpy      (int,int)
  26.          char *          strcasecpy      (char *,const char *)
  27.          char *          s_suffix        (const char *)
  28.          char *          ing_suffix      (const char *)
  29.          char *          xcrypt          (const char *, char *)
  30.          boolean         onlyspace       (const char *)
  31.          char *          tabexpand       (char *)
  32.          char *          visctrl         (char)
  33.          char *          strsubst        (char *, const char *, const char *)
  34.          const char *    ordin           (int)
  35.          char *          sitoa           (int)
  36.          int             sgn             (int)
  37.          int             rounddiv        (long, int)
  38.          int             distmin         (int, int, int, int)
  39.          int             dist2           (int, int, int, int)
  40.          boolean         online2         (int, int)
  41.          boolean         pmatch          (const char *, const char *)
  42.          boolean         pmatchi         (const char *, const char *)
  43.          boolean         pmatchz         (const char *, const char *)
  44.          int             strncmpi        (const char *, const char *, int)
  45.          char *          strstri         (const char *, const char *)
  46.          boolean         fuzzymatch      (const char *,const char *,
  47.                                           const char *, boolean)
  48.          void            setrandom       (void)
  49.          time_t          getnow          (void)
  50.          int             getyear         (void)
  51.          char *          yymmdd          (time_t)
  52.          long            yyyymmdd        (time_t)
  53.          long            hhmmss          (time_t)
  54.          char *          yyyymmddhhmmss  (time_t)
  55.          time_t          time_from_yyyymmddhhmmss (char *)
  56.          int             phase_of_the_moon (void)
  57.          boolean         friday_13th     (void)
  58.          int             night           (void)
  59.          int             midnight        (void)
  60.  =*/
  61.  #ifdef LINT
  62.  #define Static /* pacify lint */
  63.  #else
  64.  #define Static static
  65.  #endif
  66.  
  67.  static boolean FDECL(pmatch_internal, (const char *, const char *,
  68.                                         BOOLEAN_P, const char *));
  69.  

== digit

  1.  /* is 'c' a digit? */
  2.  boolean
  3.  digit(c)
  4.  char c;
  5.  {
  6.      return (boolean) ('0' <= c && c <= '9');
  7.  }
  8.  

letter

  1.  /* is 'c' a letter?  note: '@' classed as letter */
  2.  boolean
  3.  letter(c)
  4.  char c;
  5.  {
  6.      return (boolean) ('@' <= c && c <= 'Z') || ('a' <= c && c <= 'z');
  7.  }
  8.  

highc

  1.  /* force 'c' into uppercase */
  2.  char
  3.  highc(c)
  4.  char c;
  5.  {
  6.      return (char) (('a' <= c && c <= 'z') ? (c & ~040) : c);
  7.  }
  8.  

lowc

  1.  /* force 'c' into lowercase */
  2.  char
  3.  lowc(c)
  4.  char c;
  5.  {
  6.      return (char) (('A' <= c && c <= 'Z') ? (c | 040) : c);
  7.  }
  8.  

lcase

  1.  /* convert a string into all lowercase */
  2.  char *
  3.  lcase(s)
  4.  char *s;
  5.  {
  6.      register char *p;
  7.  
  8.      for (p = s; *p; p++)
  9.          if ('A' <= *p && *p <= 'Z')
  10.              *p |= 040;
  11.      return s;
  12.  }
  13.  

ucase

  1.  /* convert a string into all uppercase */
  2.  char *
  3.  ucase(s)
  4.  char *s;
  5.  {
  6.      register char *p;
  7.  
  8.      for (p = s; *p; p++)
  9.          if ('a' <= *p && *p <= 'z')
  10.              *p &= ~040;
  11.      return s;
  12.  }
  13.  

upstart

  1.  /* convert first character of a string to uppercase */
  2.  char *
  3.  upstart(s)
  4.  char *s;
  5.  {
  6.      if (s)
  7.          *s = highc(*s);
  8.      return s;
  9.  }
  10.  

mungspaces

  1.  /* remove excess whitespace from a string buffer (in place) */
  2.  char *
  3.  mungspaces(bp)
  4.  char *bp;
  5.  {
  6.      register char c, *p, *p2;
  7.      boolean was_space = TRUE;
  8.  
  9.      for (p = p2 = bp; (c = *p) != '\0'; p++) {
  10.          if (c == '\n')
  11.              break; /* treat newline the same as end-of-string */
  12.          if (c == '\t')
  13.              c = ' ';
  14.          if (c != ' ' || !was_space)
  15.              *p2++ = c;
  16.          was_space = (c == ' ');
  17.      }
  18.      if (was_space && p2 > bp)
  19.          p2--;
  20.      *p2 = '\0';
  21.      return bp;
  22.  }
  23.  

eos

  1.  /* return the end of a string (pointing at '\0') */
  2.  char *
  3.  eos(s)
  4.  register char *s;
  5.  {
  6.      while (*s)
  7.          s++; /* s += strlen(s); */
  8.      return s;
  9.  }
  10.  

str_end_is

  1.  /* determine whether 'str' ends in 'chkstr' */
  2.  boolean
  3.  str_end_is(str, chkstr)
  4.  const char *str, *chkstr;
  5.  {
  6.      int clen = (int) strlen(chkstr);
  7.  
  8.      if ((int) strlen(str) >= clen)
  9.          return (boolean) (!strncmp(eos((char *) str) - clen, chkstr, clen));
  10.      return FALSE;
  11.  }
  12.  

strkitten

  1.  /* append a character to a string (in place): strcat(s, {c,'\0'}); */
  2.  char *
  3.  strkitten(s, c)
  4.  char *s;
  5.  char c;
  6.  {
  7.      char *p = eos(s);
  8.  
  9.      *p++ = c;
  10.      *p = '\0';
  11.      return s;
  12.  }
  13.  

copynchars

  1.  /* truncating string copy */
  2.  void
  3.  copynchars(dst, src, n)
  4.  char *dst;
  5.  const char *src;
  6.  int n;
  7.  {
  8.      /* copies at most n characters, stopping sooner if terminator reached;
  9.         treats newline as input terminator; unlike strncpy, always supplies
  10.         '\0' terminator so dst must be able to hold at least n+1 characters */
  11.      while (n > 0 && *src != '\0' && *src != '\n') {
  12.          *dst++ = *src++;
  13.          --n;
  14.      }
  15.      *dst = '\0';
  16.  }
  17.  

chrcasecpy

  1.  /* convert char nc into oc's case; mostly used by strcasecpy */
  2.  char
  3.  chrcasecpy(oc, nc)
  4.  int oc, nc;
  5.  {
  6.  #if 0 /* this will be necessary if we switch to <ctype.h> */
  7.      oc = (int) (unsigned char) oc;
  8.      nc = (int) (unsigned char) nc;
  9.  #endif
  10.      if ('a' <= oc && oc <= 'z') {
  11.          /* old char is lower case; if new char is upper case, downcase it */
  12.          if ('A' <= nc && nc <= 'Z')
  13.              nc += 'a' - 'A'; /* lowc(nc) */
  14.      } else if ('A' <= oc && oc <= 'Z') {
  15.          /* old char is upper case; if new char is lower case, upcase it */
  16.          if ('a' <= nc && nc <= 'z')
  17.              nc += 'A' - 'a'; /* highc(nc) */
  18.      }
  19.      return (char) nc;
  20.  }
  21.  

strcasecpy

  1.  /* overwrite string, preserving old chars' case;
  2.     for case-insensitive editions of makeplural() and makesingular();
  3.     src might be shorter, same length, or longer than dst */
  4.  char *
  5.  strcasecpy(dst, src)
  6.  char *dst;
  7.  const char *src;
  8.  {
  9.      char *result = dst;
  10.      int ic, oc, dst_exhausted = 0;
  11.  
  12.      /* while dst has characters, replace each one with corresponding
  13.         character from src, converting case in the process if they differ;
  14.         once dst runs out, propagate the case of its last character to any
  15.         remaining src; if dst starts empty, it must be a pointer to the
  16.         tail of some other string because we examine the char at dst[-1] */
  17.      while ((ic = (int) *src++) != '\0') {
  18.          if (!dst_exhausted && !*dst)
  19.              dst_exhausted = 1;
  20.          oc = (int) *(dst - dst_exhausted);
  21.          *dst++ = chrcasecpy(oc, ic);
  22.      }
  23.      *dst = '\0';
  24.      return result;
  25.  }
  26.  

s_suffix

  1.  /* return a name converted to possessive */
  2.  char *
  3.  s_suffix(s)
  4.  const char *s;
  5.  {
  6.      Static char buf[BUFSZ];
  7.  
  8.      Strcpy(buf, s);
  9.      if (!strcmpi(buf, "it")) /* it -> its */
  10.          Strcat(buf, "s");
  11.      else if (!strcmpi(buf, "you")) /* you -> your */
  12.          Strcat(buf, "r");
  13.      else if (*(eos(buf) - 1) == 's') /* Xs -> Xs' */
  14.          Strcat(buf, "'");
  15.      else /* X -> X's */
  16.          Strcat(buf, "'s");
  17.      return buf;
  18.  }
  19.  

ing_suffix

  1.  /* construct a gerund (a verb formed by appending "ing" to a noun) */
  2.  char *
  3.  ing_suffix(s)
  4.  const char *s;
  5.  {
  6.      const char *vowel = "aeiouy";
  7.      static char buf[BUFSZ];
  8.      char onoff[10];
  9.      char *p;
  10.  
  11.      Strcpy(buf, s);
  12.      p = eos(buf);
  13.      onoff[0] = *p = *(p + 1) = '\0';
  14.      if ((strlen(buf) > 4)
  15.          && (!strcmpi(p - 3, " on") || !strcmpi(p - 4, " off")
  16.              || !strcmpi(p - 5, " with"))) {
  17.          p = strrchr(buf, ' ');
  18.          Strcpy(onoff, p);
  19.      }
  20.      if (!index(vowel, *(p - 1)) && index(vowel, *(p - 2))
  21.          && !index(vowel, *(p - 3))) {
  22.          /* tip -> tipp + ing */
  23.          *p = *(p - 1);
  24.          *(p + 1) = '\0';
  25.      } else if (!strcmpi(p - 2, "ie")) { /* vie -> vy + ing */
  26.          *(p - 2) = 'y';
  27.          *(p - 1) = '\0';
  28.      } else if (*(p - 1) == 'e') /* grease -> greas + ing */
  29.          *(p - 1) = '\0';
  30.      Strcat(buf, "ing");
  31.      if (onoff[0])
  32.          Strcat(buf, onoff);
  33.      return buf;
  34.  }
  35.  

xcrypt

  1.  /* trivial text encryption routine (see makedefs) */
  2.  char *
  3.  xcrypt(str, buf)
  4.  const char *str;
  5.  char *buf;
  6.  {
  7.      register const char *p;
  8.      register char *q;
  9.      register int bitmask;
  10.  
  11.      for (bitmask = 1, p = str, q = buf; *p; q++) {
  12.          *q = *p++;
  13.          if (*q & (32 | 64))
  14.              *q ^= bitmask;
  15.          if ((bitmask <<= 1) >= 32)
  16.              bitmask = 1;
  17.      }
  18.      *q = '\0';
  19.      return buf;
  20.  }
  21.  

onlyspace

  1.  /* is a string entirely whitespace? */
  2.  boolean
  3.  onlyspace(s)
  4.  const char *s;
  5.  {
  6.      for (; *s; s++)
  7.          if (*s != ' ' && *s != '\t')
  8.              return FALSE;
  9.      return TRUE;
  10.  }
  11.  

tabexpand

  1.  /* expand tabs into proper number of spaces */
  2.  char *
  3.  tabexpand(sbuf)
  4.  char *sbuf;
  5.  {
  6.      char buf[BUFSZ];
  7.      register char *bp, *s = sbuf;
  8.      register int idx;
  9.  
  10.      if (!*s)
  11.          return sbuf;
  12.      /* warning: no bounds checking performed */
  13.      for (bp = buf, idx = 0; *s; s++)
  14.          if (*s == '\t') {
  15.              do
  16.                  *bp++ = ' ';
  17.              while (++idx % 8);
  18.          } else {
  19.              *bp++ = *s;
  20.              idx++;
  21.          }
  22.      *bp = 0;
  23.      return strcpy(sbuf, buf);
  24.  }
  25.  

visctrl

  1.  /* make a displayable string from a character */
  2.  char *
  3.  visctrl(c)
  4.  char c;
  5.  {
  6.      Static char ccc[3];
  7.  
  8.      c &= 0177;
  9.      ccc[2] = '\0';
  10.      if (c < 040) {
  11.          ccc[0] = '^';
  12.          ccc[1] = c | 0100; /* letter */
  13.      } else if (c == 0177) {
  14.          ccc[0] = '^';
  15.          ccc[1] = c & ~0100; /* '?' */
  16.      } else {
  17.          ccc[0] = c; /* printable character */
  18.          ccc[1] = '\0';
  19.      }
  20.      return ccc;
  21.  }
  22.  

strsubst

  1.  /* substitute a word or phrase in a string (in place) */
  2.  /* caller is responsible for ensuring that bp points to big enough buffer */
  3.  char *
  4.  strsubst(bp, orig, replacement)
  5.  char *bp;
  6.  const char *orig, *replacement;
  7.  {
  8.      char *found, buf[BUFSZ];
  9.  
  10.      if (bp) {
  11.          found = strstr(bp, orig);
  12.          if (found) {
  13.              Strcpy(buf, found + strlen(orig));
  14.              Strcpy(found, replacement);
  15.              Strcat(bp, buf);
  16.          }
  17.      }
  18.      return bp;
  19.  }
  20.  

ordin

  1.  /* return the ordinal suffix of a number */
  2.  const char *
  3.  ordin(n)
  4.  int n;               /* note: should be non-negative */
  5.  {
  6.      register int dd = n % 10;
  7.  
  8.      return (dd == 0 || dd > 3 || (n % 100) / 10 == 1) ? "th"
  9.                 : (dd == 1) ? "st" : (dd == 2) ? "nd" : "rd";
  10.  }
  11.  

sitoa

  1.  /* make a signed digit string from a number */
  2.  char *
  3.  sitoa(n)
  4.  int n;
  5.  {
  6.      Static char buf[13];
  7.  
  8.      Sprintf(buf, (n < 0) ? "%d" : "+%d", n);
  9.      return buf;
  10.  }
  11.  

sgn

  1.  /* return the sign of a number: -1, 0, or 1 */
  2.  int
  3.  sgn(n)
  4.  int n;
  5.  {
  6.      return (n < 0) ? -1 : (n != 0);
  7.  }
  8.  

rounddiv

  1.  /* calculate x/y, rounding as appropriate */
  2.  int
  3.  rounddiv(x, y)
  4.  long x;
  5.  int y;
  6.  {
  7.      int r, m;
  8.      int divsgn = 1;
  9.  
  10.      if (y == 0)
  11.          panic("division by zero in rounddiv");
  12.      else if (y < 0) {
  13.          divsgn = -divsgn;
  14.          y = -y;
  15.      }
  16.      if (x < 0) {
  17.          divsgn = -divsgn;
  18.          x = -x;
  19.      }
  20.      r = x / y;
  21.      m = x % y;
  22.      if (2 * m >= y)
  23.          r++;
  24.  
  25.      return divsgn * r;
  26.  }
  27.  

distmin

  1.  /* distance between two points, in moves */
  2.  int
  3.  distmin(x0, y0, x1, y1)
  4.  int x0, y0, x1, y1;
  5.  {
  6.      register int dx = x0 - x1, dy = y0 - y1;
  7.  
  8.      if (dx < 0)
  9.          dx = -dx;
  10.      if (dy < 0)
  11.          dy = -dy;
  12.      /*  The minimum number of moves to get from (x0,y0) to (x1,y1) is the
  13.       *  larger of the [absolute value of the] two deltas.
  14.       */
  15.      return (dx < dy) ? dy : dx;
  16.  }
  17.  

dist2

  1.  /* square of euclidean distance between pair of pts */
  2.  int
  3.  dist2(x0, y0, x1, y1)
  4.  int x0, y0, x1, y1;
  5.  {
  6.      register int dx = x0 - x1, dy = y0 - y1;
  7.  
  8.      return dx * dx + dy * dy;
  9.  }
  10.  

isqrt

  1.  /* integer square root function without using floating point */
  2.  int
  3.  isqrt(val)
  4.  int val;
  5.  {
  6.      int rt = 0;
  7.      int odd = 1;
  8.      /*
  9.       * This could be replaced by a faster algorithm, but has not been because:
  10.       * + the simple algorithm is easy to read;
  11.       * + this algorithm does not require 64-bit support;
  12.       * + in current usage, the values passed to isqrt() are not really that
  13.       *   large, so the performance difference is negligible;
  14.       * + isqrt() is used in only few places, which are not bottle-necks.
  15.       */
  16.      while (val >= odd) {
  17.          val = val - odd;
  18.          odd = odd + 2;
  19.          rt = rt + 1;
  20.      }
  21.      return rt;
  22.  }
  23.  

online2

  1.  /* are two points lined up (on a straight line)? */
  2.  boolean
  3.  online2(x0, y0, x1, y1)
  4.  int x0, y0, x1, y1;
  5.  {
  6.      int dx = x0 - x1, dy = y0 - y1;
  7.      /*  If either delta is zero then they're on an orthogonal line,
  8.       *  else if the deltas are equal (signs ignored) they're on a diagonal.
  9.       */
  10.      return (boolean) (!dy || !dx || dy == dx || dy == -dx);
  11.  }
  12.  

pmatch_internal

  1.  /* guts of pmatch(), pmatchi(), and pmatchz();
  2.     match a string against a pattern */
  3.  static boolean
  4.  pmatch_internal(patrn, strng, ci, sk)
  5.  const char *patrn, *strng;
  6.  boolean ci;     /* True => case-insensitive, False => case-sensitive */
  7.  const char *sk; /* set of characters to skip */
  8.  {
  9.      char s, p;
  10.      /*
  11.       *  Simple pattern matcher:  '*' matches 0 or more characters, '?' matches
  12.       *  any single character.  Returns TRUE if 'strng' matches 'patrn'.
  13.       */
  14.  pmatch_top:
  15.      if (!sk) {
  16.          s = *strng++;
  17.          p = *patrn++; /* get next chars and pre-advance */
  18.      } else {
  19.          /* fuzzy match variant of pmatch; particular characters are ignored */
  20.          do {
  21.              s = *strng++;
  22.          } while (index(sk, s));
  23.          do {
  24.              p = *patrn++;
  25.          } while (index(sk, p));
  26.      }
  27.      if (!p)                           /* end of pattern */
  28.          return (boolean) (s == '\0'); /* matches iff end of string too */
  29.      else if (p == '*')                /* wildcard reached */
  30.          return (boolean) ((!*patrn
  31.                             || pmatch_internal(patrn, strng - 1, ci, sk))
  32.                            ? TRUE
  33.                            : s ? pmatch_internal(patrn - 1, strng, ci, sk)
  34.                                : FALSE);
  35.      else if ((ci ? lowc(p) != lowc(s) : p != s) /* check single character */
  36.               && (p != '?' || !s))               /* & single-char wildcard */
  37.          return FALSE;                           /* doesn't match */
  38.      else                 /* return pmatch_internal(patrn, strng, ci, sk); */
  39.          goto pmatch_top; /* optimize tail recursion */
  40.  }
  41.  

pmatch

  1.  /* case-sensitive wildcard match */
  2.  boolean
  3.  pmatch(patrn, strng)
  4.  const char *patrn, *strng;
  5.  {
  6.      return pmatch_internal(patrn, strng, FALSE, (const char *) 0);
  7.  }
  8.  

pmatchi

  1.  /* case-insensitive wildcard match */
  2.  boolean
  3.  pmatchi(patrn, strng)
  4.  const char *patrn, *strng;
  5.  {
  6.      return pmatch_internal(patrn, strng, TRUE, (const char *) 0);
  7.  }
  8.  

pmatchz

  1.  /* case-insensitive wildcard fuzzymatch */
  2.  boolean
  3.  pmatchz(patrn, strng)
  4.  const char *patrn, *strng;
  5.  {
  6.      /* ignore spaces, tabs (just in case), dashes, and underscores */
  7.      static const char fuzzychars[] = " \t-_";
  8.  
  9.      return pmatch_internal(patrn, strng, TRUE, fuzzychars);
  10.  }
  11.  

strncmpi

  1.  #ifndef STRNCMPI
  2.  /* case insensitive counted string comparison */
  3.  int
  4.  strncmpi(s1, s2, n) /*{ aka strncasecmp }*/
  5.  register const char *s1, *s2;
  6.  register int n; /*(should probably be size_t, which is unsigned)*/
  7.  {
  8.      register char t1, t2;
  9.  
  10.      while (n--) {
  11.          if (!*s2)
  12.              return (*s1 != 0); /* s1 >= s2 */
  13.          else if (!*s1)
  14.              return -1; /* s1  < s2 */
  15.          t1 = lowc(*s1++);
  16.          t2 = lowc(*s2++);
  17.          if (t1 != t2)
  18.              return (t1 > t2) ? 1 : -1;
  19.      }
  20.      return 0; /* s1 == s2 */
  21.  }
  22.  #endif /* STRNCMPI */

strstri

  1.  
  2.  #ifndef STRSTRI
  3.  /* case insensitive substring search */
  4.  char *
  5.  strstri(str, sub)
  6.  const char *str;
  7.  const char *sub;
  8.  {
  9.      register const char *s1, *s2;
  10.      register int i, k;
  11.  #define TABSIZ 0x20                  /* 0x40 would be case-sensitive */
  12.      char tstr[TABSIZ], tsub[TABSIZ]; /* nibble count tables */
  13.  #if 0
  14.      assert( (TABSIZ & ~(TABSIZ-1)) == TABSIZ ); /* must be exact power of 2 */
  15.      assert( &lowc != 0 );                       /* can't be unsafe macro */
  16.  #endif
  17.  
  18.      /* special case: empty substring */
  19.      if (!*sub)
  20.          return (char *) str;
  21.  
  22.      /* do some useful work while determining relative lengths */
  23.      for (i = 0; i < TABSIZ; i++)
  24.          tstr[i] = tsub[i] = 0; /* init */
  25.      for (k = 0, s1 = str; *s1; k++)
  26.          tstr[*s1++ & (TABSIZ - 1)]++;
  27.      for (s2 = sub; *s2; --k)
  28.          tsub[*s2++ & (TABSIZ - 1)]++;
  29.  
  30.      /* evaluate the info we've collected */
  31.      if (k < 0)
  32.          return (char *) 0;       /* sub longer than str, so can't match */
  33.      for (i = 0; i < TABSIZ; i++) /* does sub have more 'x's than str? */
  34.          if (tsub[i] > tstr[i])
  35.              return (char *) 0; /* match not possible */
  36.  
  37.      /* now actually compare the substring repeatedly to parts of the string */
  38.      for (i = 0; i <= k; i++) {
  39.          s1 = &str[i];
  40.          s2 = sub;
  41.          while (lowc(*s1++) == lowc(*s2++))
  42.              if (!*s2)
  43.                  return (char *) &str[i]; /* full match */
  44.      }
  45.      return (char *) 0; /* not found */
  46.  }
  47.  #endif /* STRSTRI */

fuzzymatch

  1.  
  2.  /* compare two strings for equality, ignoring the presence of specified
  3.     characters (typically whitespace) and possibly ignoring case */
  4.  boolean
  5.  fuzzymatch(s1, s2, ignore_chars, caseblind)
  6.  const char *s1, *s2;
  7.  const char *ignore_chars;
  8.  boolean caseblind;
  9.  {
  10.      register char c1, c2;
  11.  
  12.      do {
  13.          while ((c1 = *s1++) != '\0' && index(ignore_chars, c1) != 0)
  14.              continue;
  15.          while ((c2 = *s2++) != '\0' && index(ignore_chars, c2) != 0)
  16.              continue;
  17.          if (!c1 || !c2)
  18.              break; /* stop when end of either string is reached */
  19.  
  20.          if (caseblind) {
  21.              c1 = lowc(c1);
  22.              c2 = lowc(c2);
  23.          }
  24.      } while (c1 == c2);
  25.  
  26.      /* match occurs only when the end of both strings has been reached */
  27.      return (boolean) (!c1 && !c2);
  28.  }
  29.  

setrandom

  1.  /*
  2.   * Time routines
  3.   *
  4.   * The time is used for:
  5.   *  - seed for rand()
  6.   *  - year on tombstone and yyyymmdd in record file
  7.   *  - phase of the moon (various monsters react to NEW_MOON or FULL_MOON)
  8.   *  - night and midnight (the undead are dangerous at midnight)
  9.   *  - determination of what files are "very old"
  10.   */
  11.  
  12.  /* TIME_type: type of the argument to time(); we actually use &(time_t) */
  13.  #if defined(BSD) && !defined(POSIX_TYPES)
  14.  #define TIME_type long *
  15.  #else
  16.  #define TIME_type time_t *
  17.  #endif
  18.  /* LOCALTIME_type: type of the argument to localtime() */
  19.  #if (defined(ULTRIX) && !(defined(ULTRIX_PROTO) || defined(NHSTDC))) \
  20.      || (defined(BSD) && !defined(POSIX_TYPES))
  21.  #define LOCALTIME_type long *
  22.  #else
  23.  #define LOCALTIME_type time_t *
  24.  #endif
  25.  
  26.  #if defined(AMIGA) && !defined(AZTEC_C) && !defined(__SASC_60) \
  27.      && !defined(_DCC) && !defined(__GNUC__)
  28.  extern struct tm *FDECL(localtime, (time_t *));
  29.  #endif
  30.  STATIC_DCL struct tm *NDECL(getlt);
  31.  
  32.  void
  33.  setrandom()
  34.  {
  35.      unsigned long seed = (unsigned long) getnow(); /* time((TIME_type) 0) */
  36.  
  37.  #if defined(UNIX) || defined(VMS)
  38.      {
  39.          unsigned long pid = (unsigned long) getpid();
  40.  
  41.          /* Quick dirty band-aid to prevent PRNG prediction */
  42.          if (pid) {
  43.              if (!(pid & 3L))
  44.                  pid -= 1L;
  45.              seed *= pid;
  46.          }
  47.      }
  48.  #endif
  49.  
  50.      /* the types are different enough here that sweeping the different
  51.       * routine names into one via #defines is even more confusing
  52.       */
  53.  #ifdef RANDOM /* srandom() from sys/share/random.c */
  54.      srandom((unsigned int) seed);
  55.  #else
  56.  #if defined(__APPLE__) || defined(BSD) || defined(LINUX) || defined(ULTRIX) \
  57.      || defined(CYGWIN32) /* system srandom() */
  58.  #if defined(BSD) && !defined(POSIX_TYPES) && defined(SUNOS4)
  59.      (void)
  60.  #endif
  61.          srandom((int) seed);
  62.  #else
  63.  #ifdef UNIX /* system srand48() */
  64.      srand48((long) seed);
  65.  #else       /* poor quality system routine */
  66.      srand((int) seed);
  67.  #endif
  68.  #endif
  69.  #endif
  70.  }
  71.  

getnow

  1.  time_t
  2.  getnow()
  3.  {
  4.      time_t datetime = 0;
  5.  
  6.      (void) time((TIME_type) &datetime);
  7.      return datetime;
  8.  }
  9.  

getlt

  1.  STATIC_OVL struct tm *
  2.  getlt()
  3.  {
  4.      time_t date = getnow();
  5.  
  6.      return localtime((LOCALTIME_type) &date);
  7.  }
  8.  

getyear

  1.  int
  2.  getyear()
  3.  {
  4.      return (1900 + getlt()->tm_year);
  5.  }
  6.  

yymmdd

  1.  #if 0
  2.  /* This routine is no longer used since in 20YY it yields "1YYmmdd". */
  3.  char *
  4.  yymmdd(date)
  5.  time_t date;
  6.  {
  7.      Static char datestr[10];
  8.      struct tm *lt;
  9.  
  10.      if (date == 0)
  11.          lt = getlt();
  12.      else
  13.          lt = localtime((LOCALTIME_type) &date);
  14.  
  15.      Sprintf(datestr, "%02d%02d%02d",
  16.              lt->tm_year, lt->tm_mon + 1, lt->tm_mday);
  17.      return datestr;
  18.  }
  19.  #endif

yyyymmdd

  1.  
  2.  long
  3.  yyyymmdd(date)
  4.  time_t date;
  5.  {
  6.      long datenum;
  7.      struct tm *lt;
  8.  
  9.      if (date == 0)
  10.          lt = getlt();
  11.      else
  12.          lt = localtime((LOCALTIME_type) &date);
  13.  
  14.      /* just in case somebody's localtime supplies (year % 100)
  15.         rather than the expected (year - 1900) */
  16.      if (lt->tm_year < 70)
  17.          datenum = (long) lt->tm_year + 2000L;
  18.      else
  19.          datenum = (long) lt->tm_year + 1900L;
  20.      /* yyyy --> yyyymm */
  21.      datenum = datenum * 100L + (long) (lt->tm_mon + 1);
  22.      /* yyyymm --> yyyymmdd */
  23.      datenum = datenum * 100L + (long) lt->tm_mday;
  24.      return datenum;
  25.  }
  26.  

hhmmss

  1.  long
  2.  hhmmss(date)
  3.  time_t date;
  4.  {
  5.      long timenum;
  6.      struct tm *lt;
  7.  
  8.      if (date == 0)
  9.          lt = getlt();
  10.      else
  11.          lt = localtime((LOCALTIME_type) &date);
  12.  
  13.      timenum = lt->tm_hour * 10000L + lt->tm_min * 100L + lt->tm_sec;
  14.      return timenum;
  15.  }
  16.  

yyyymmddhhmmss

  1.  char *
  2.  yyyymmddhhmmss(date)
  3.  time_t date;
  4.  {
  5.      long datenum;
  6.      static char datestr[15];
  7.      struct tm *lt;
  8.  
  9.      if (date == 0)
  10.          lt = getlt();
  11.      else
  12.  #if (defined(ULTRIX) && !(defined(ULTRIX_PROTO) || defined(NHSTDC))) \
  13.      || defined(BSD)
  14.          lt = localtime((long *) (&date));
  15.  #else
  16.          lt = localtime(&date);
  17.  #endif
  18.      /* just in case somebody's localtime supplies (year % 100)
  19.         rather than the expected (year - 1900) */
  20.      if (lt->tm_year < 70)
  21.          datenum = (long) lt->tm_year + 2000L;
  22.      else
  23.          datenum = (long) lt->tm_year + 1900L;
  24.      Sprintf(datestr, "%04ld%02d%02d%02d%02d%02d", datenum, lt->tm_mon + 1,
  25.              lt->tm_mday, lt->tm_hour, lt->tm_min, lt->tm_sec);
  26.      debugpline1("yyyymmddhhmmss() produced date string %s", datestr);
  27.      return datestr;
  28.  }
  29.  

time_from_yyyymmddhhmmss

  1.  time_t
  2.  time_from_yyyymmddhhmmss(buf)
  3.  char *buf;
  4.  {
  5.      int k;
  6.      time_t timeresult = (time_t) 0;
  7.      struct tm t, *lt;
  8.      char *g, *p, y[5], mo[3], md[3], h[3], mi[3], s[3];
  9.  
  10.      if (buf && strlen(buf) == 14) {
  11.          g = buf;
  12.          p = y; /* year */
  13.          for (k = 0; k < 4; ++k)
  14.              *p++ = *g++;
  15.          *p = '\0';
  16.          p = mo; /* month */
  17.          for (k = 0; k < 2; ++k)
  18.              *p++ = *g++;
  19.          *p = '\0';
  20.          p = md; /* day */
  21.          for (k = 0; k < 2; ++k)
  22.              *p++ = *g++;
  23.          *p = '\0';
  24.          p = h; /* hour */
  25.          for (k = 0; k < 2; ++k)
  26.              *p++ = *g++;
  27.          *p = '\0';
  28.          p = mi; /* minutes */
  29.          for (k = 0; k < 2; ++k)
  30.              *p++ = *g++;
  31.          *p = '\0';
  32.          p = s; /* seconds */
  33.          for (k = 0; k < 2; ++k)
  34.              *p++ = *g++;
  35.          *p = '\0';
  36.          lt = getlt();
  37.          if (lt) {
  38.              t = *lt;
  39.              t.tm_year = atoi(y) - 1900;
  40.              t.tm_mon = atoi(mo) - 1;
  41.              t.tm_mday = atoi(md);
  42.              t.tm_hour = atoi(h);
  43.              t.tm_min = atoi(mi);
  44.              t.tm_sec = atoi(s);
  45.              timeresult = mktime(&t);
  46.          }
  47.          if ((int) timeresult == -1)
  48.              debugpline1("time_from_yyyymmddhhmmss(%s) would have returned -1",
  49.                          buf ? buf : "");
  50.          else
  51.              return timeresult;
  52.      }
  53.      return (time_t) 0;
  54.  }
  55.  

phase_of_the_moon

  1.  /*
  2.   * moon period = 29.53058 days ~= 30, year = 365.2422 days
  3.   * days moon phase advances on first day of year compared to preceding year
  4.   *      = 365.2422 - 12*29.53058 ~= 11
  5.   * years in Metonic cycle (time until same phases fall on the same days of
  6.   *      the month) = 18.6 ~= 19
  7.   * moon phase on first day of year (epact) ~= (11*(year%19) + 29) % 30
  8.   *      (29 as initial condition)
  9.   * current phase in days = first day phase + days elapsed in year
  10.   * 6 moons ~= 177 days
  11.   * 177 ~= 8 reported phases * 22
  12.   * + 11/22 for rounding
  13.   */
  14.  int
  15.  phase_of_the_moon() /* 0-7, with 0: new, 4: full */
  16.  {
  17.      register struct tm *lt = getlt();
  18.      register int epact, diy, goldn;
  19.  
  20.      diy = lt->tm_yday;
  21.      goldn = (lt->tm_year % 19) + 1;
  22.      epact = (11 * goldn + 18) % 30;
  23.      if ((epact == 25 && goldn > 11) || epact == 24)
  24.          epact++;
  25.  
  26.      return ((((((diy + epact) * 6) + 11) % 177) / 22) & 7);
  27.  }
  28.  

friday_13th

  1.  boolean
  2.  friday_13th()
  3.  {
  4.      register struct tm *lt = getlt();
  5.  
  6.      /* tm_wday (day of week; 0==Sunday) == 5 => Friday */
  7.      return (boolean) (lt->tm_wday == 5 && lt->tm_mday == 13);
  8.  }
  9.  

night

  1.  int
  2.  night()
  3.  {
  4.      register int hour = getlt()->tm_hour;
  5.  
  6.      return (hour < 6 || hour > 21);
  7.  }
  8.  

midnight

  1.  int
  2.  midnight()
  3.  {
  4.      return (getlt()->tm_hour == 0);
  5.  }
  6.  
  7.  /*hacklib.c*/