Difference between revisions of "Source:NetHack 3.4.3/src/hacklib.c"

From NetHackWiki
Jump to navigation Jump to search
(Automated source code upload)
 
m (Add headers.)
Line 1: Line 1:
 +
Below is the full text to src/hacklib.c from NetHack 3.4.3. To link to a particular line, write [[hacklib.c#line123|<nowiki>[[hacklib.c#line123]]</nowiki>]], for example.
 +
 +
== Top of file ==
  
Below is the full text to src/hacklib.c from NetHack 3.4.3. To link to a particular line, write [[hacklib.c#line123|<nowiki>[[hacklib.c#line123]]</nowiki>]], for example.
 
 
  <span id="line1">1.    /* SCCS Id: @(#)hacklib.c 3.4 2002/12/13 */</span>
 
  <span id="line1">1.    /* SCCS Id: @(#)hacklib.c 3.4 2002/12/13 */</span>
 
  <span id="line2">2.    /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */</span>
 
  <span id="line2">2.    /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */</span>
Line 56: Line 58:
 
  <span id="line51">51.  #endif</span>
 
  <span id="line51">51.  #endif</span>
 
  <span id="line52">52.  </span>
 
  <span id="line52">52.  </span>
 +
 +
== digit ==
 +
 
  <span id="line53">53.  #ifdef OVLB</span>
 
  <span id="line53">53.  #ifdef OVLB</span>
 
  <span id="line54">54.  boolean</span>
 
  <span id="line54">54.  boolean</span>
Line 64: Line 69:
 
  <span id="line59">59.  }</span>
 
  <span id="line59">59.  }</span>
 
  <span id="line60">60.  </span>
 
  <span id="line60">60.  </span>
 +
 +
== letter ==
 +
 
  <span id="line61">61.  boolean</span>
 
  <span id="line61">61.  boolean</span>
 
  <span id="line62">62.  letter(c) /* is 'c' a letter?  note: '@' classed as letter */</span>
 
  <span id="line62">62.  letter(c) /* is 'c' a letter?  note: '@' classed as letter */</span>
Line 72: Line 80:
 
  <span id="line67">67.  #endif /* OVLB */</span>
 
  <span id="line67">67.  #endif /* OVLB */</span>
 
  <span id="line68">68.  </span>
 
  <span id="line68">68.  </span>
 +
 +
== highc ==
 +
 
  <span id="line69">69.  #ifdef OVL1</span>
 
  <span id="line69">69.  #ifdef OVL1</span>
 
  <span id="line70">70.  char</span>
 
  <span id="line70">70.  char</span>
Line 80: Line 91:
 
  <span id="line75">75.  }</span>
 
  <span id="line75">75.  }</span>
 
  <span id="line76">76.  </span>
 
  <span id="line76">76.  </span>
 +
 +
== lowc ==
 +
 
  <span id="line77">77.  char</span>
 
  <span id="line77">77.  char</span>
 
  <span id="line78">78.  lowc(c) /* force 'c' into lowercase */</span>
 
  <span id="line78">78.  lowc(c) /* force 'c' into lowercase */</span>
Line 88: Line 102:
 
  <span id="line83">83.  #endif /* OVL1 */</span>
 
  <span id="line83">83.  #endif /* OVL1 */</span>
 
  <span id="line84">84.  </span>
 
  <span id="line84">84.  </span>
 +
 +
== lcase ==
 +
 
  <span id="line85">85.  #ifdef OVLB</span>
 
  <span id="line85">85.  #ifdef OVLB</span>
 
  <span id="line86">86.  char *</span>
 
  <span id="line86">86.  char *</span>
Line 100: Line 117:
 
  <span id="line95">95.  }</span>
 
  <span id="line95">95.  }</span>
 
  <span id="line96">96.  </span>
 
  <span id="line96">96.  </span>
 +
 +
== upstart ==
 +
 
  <span id="line97">97.  char *</span>
 
  <span id="line97">97.  char *</span>
 
  <span id="line98">98.  upstart(s) /* convert first character of a string to uppercase */</span>
 
  <span id="line98">98.  upstart(s) /* convert first character of a string to uppercase */</span>
Line 108: Line 128:
 
  <span id="line103">103.  }</span>
 
  <span id="line103">103.  }</span>
 
  <span id="line104">104.  </span>
 
  <span id="line104">104.  </span>
 +
 +
== mungspaces ==
 +
 
  <span id="line105">105.  /* remove excess whitespace from a string buffer (in place) */</span>
 
  <span id="line105">105.  /* remove excess whitespace from a string buffer (in place) */</span>
 
  <span id="line106">106.  char *</span>
 
  <span id="line106">106.  char *</span>
Line 128: Line 151:
 
  <span id="line123">123.  #endif /* OVLB */</span>
 
  <span id="line123">123.  #endif /* OVLB */</span>
 
  <span id="line124">124.  </span>
 
  <span id="line124">124.  </span>
 +
 +
== eos ==
 +
 
  <span id="line125">125.  #ifdef OVL0</span>
 
  <span id="line125">125.  #ifdef OVL0</span>
 
  <span id="line126">126.  char *</span>
 
  <span id="line126">126.  char *</span>
Line 137: Line 163:
 
  <span id="line132">132.  }</span>
 
  <span id="line132">132.  }</span>
 
  <span id="line133">133.  </span>
 
  <span id="line133">133.  </span>
 +
 +
== strkitten ==
 +
 
  <span id="line134">134.  /* strcat(s, {c,'\0'}); */</span>
 
  <span id="line134">134.  /* strcat(s, {c,'\0'}); */</span>
 
  <span id="line135">135.  char *</span>
 
  <span id="line135">135.  char *</span>
Line 150: Line 179:
 
  <span id="line145">145.  }</span>
 
  <span id="line145">145.  }</span>
 
  <span id="line146">146.  </span>
 
  <span id="line146">146.  </span>
 +
 +
== s_suffix ==
 +
 
  <span id="line147">147.  char *</span>
 
  <span id="line147">147.  char *</span>
 
  <span id="line148">148.  s_suffix(s) /* return a name converted to possessive */</span>
 
  <span id="line148">148.  s_suffix(s) /* return a name converted to possessive */</span>
Line 166: Line 198:
 
  <span id="line161">161.  }</span>
 
  <span id="line161">161.  }</span>
 
  <span id="line162">162.  </span>
 
  <span id="line162">162.  </span>
 +
 +
== xcrypt ==
 +
 
  <span id="line163">163.  char *</span>
 
  <span id="line163">163.  char *</span>
 
  <span id="line164">164.  xcrypt(str, buf) /* trivial text encryption routine (see makedefs) */</span>
 
  <span id="line164">164.  xcrypt(str, buf) /* trivial text encryption routine (see makedefs) */</span>
Line 185: Line 220:
 
  <span id="line180">180.  #endif /* OVL0 */</span>
 
  <span id="line180">180.  #endif /* OVL0 */</span>
 
  <span id="line181">181.  </span>
 
  <span id="line181">181.  </span>
 +
 +
== onlyspace ==
 +
 
  <span id="line182">182.  #ifdef OVL2</span>
 
  <span id="line182">182.  #ifdef OVL2</span>
 
  <span id="line183">183.  boolean</span>
 
  <span id="line183">183.  boolean</span>
Line 196: Line 234:
 
  <span id="line191">191.  #endif /* OVL2 */</span>
 
  <span id="line191">191.  #endif /* OVL2 */</span>
 
  <span id="line192">192.  </span>
 
  <span id="line192">192.  </span>
 +
 +
== tabexpand ==
 +
 
  <span id="line193">193.  #ifdef OVLB</span>
 
  <span id="line193">193.  #ifdef OVLB</span>
 
  <span id="line194">194.  char *</span>
 
  <span id="line194">194.  char *</span>
Line 219: Line 260:
 
  <span id="line214">214.  }</span>
 
  <span id="line214">214.  }</span>
 
  <span id="line215">215.  </span>
 
  <span id="line215">215.  </span>
 +
 +
== visctrl ==
 +
 
  <span id="line216">216.  char *</span>
 
  <span id="line216">216.  char *</span>
 
  <span id="line217">217.  visctrl(c) /* make a displayable string from a character */</span>
 
  <span id="line217">217.  visctrl(c) /* make a displayable string from a character */</span>
Line 242: Line 286:
 
  <span id="line237">237.  #endif /* OVLB */</span>
 
  <span id="line237">237.  #endif /* OVLB */</span>
 
  <span id="line238">238.  </span>
 
  <span id="line238">238.  </span>
 +
 +
== ordin ==
 +
 
  <span id="line239">239.  #ifdef OVL2</span>
 
  <span id="line239">239.  #ifdef OVL2</span>
 
  <span id="line240">240.  const char *</span>
 
  <span id="line240">240.  const char *</span>
Line 254: Line 301:
 
  <span id="line249">249.  #endif /* OVL2 */</span>
 
  <span id="line249">249.  #endif /* OVL2 */</span>
 
  <span id="line250">250.  </span>
 
  <span id="line250">250.  </span>
 +
 +
== sitoa ==
 +
 
  <span id="line251">251.  #ifdef OVL1</span>
 
  <span id="line251">251.  #ifdef OVL1</span>
 
  <span id="line252">252.  char *</span>
 
  <span id="line252">252.  char *</span>
Line 265: Line 315:
 
  <span id="line260">260.  }</span>
 
  <span id="line260">260.  }</span>
 
  <span id="line261">261.  </span>
 
  <span id="line261">261.  </span>
 +
 +
== sgn ==
 +
 
  <span id="line262">262.  int</span>
 
  <span id="line262">262.  int</span>
 
  <span id="line263">263.  sgn(n) /* return the sign of a number: -1, 0, or 1 */</span>
 
  <span id="line263">263.  sgn(n) /* return the sign of a number: -1, 0, or 1 */</span>
Line 273: Line 326:
 
  <span id="line268">268.  #endif /* OVL1 */</span>
 
  <span id="line268">268.  #endif /* OVL1 */</span>
 
  <span id="line269">269.  </span>
 
  <span id="line269">269.  </span>
 +
 +
== rounddiv ==
 +
 
  <span id="line270">270.  #ifdef OVLB</span>
 
  <span id="line270">270.  #ifdef OVLB</span>
 
  <span id="line271">271.  int</span>
 
  <span id="line271">271.  int</span>
Line 298: Line 354:
 
  <span id="line293">293.  #endif /* OVLB */</span>
 
  <span id="line293">293.  #endif /* OVLB */</span>
 
  <span id="line294">294.  </span>
 
  <span id="line294">294.  </span>
 +
 +
== distmin ==
 +
 
  <span id="line295">295.  #ifdef OVL0</span>
 
  <span id="line295">295.  #ifdef OVL0</span>
 
  <span id="line296">296.  int</span>
 
  <span id="line296">296.  int</span>
Line 312: Line 371:
 
  <span id="line307">307.  }</span>
 
  <span id="line307">307.  }</span>
 
  <span id="line308">308.  </span>
 
  <span id="line308">308.  </span>
 +
 +
== dist2 ==
 +
 
  <span id="line309">309.  int</span>
 
  <span id="line309">309.  int</span>
 
  <span id="line310">310.  dist2(x0, y0, x1, y1) /* square of euclidean distance between pair of pts */</span>
 
  <span id="line310">310.  dist2(x0, y0, x1, y1) /* square of euclidean distance between pair of pts */</span>
Line 320: Line 382:
 
  <span id="line315">315.  }</span>
 
  <span id="line315">315.  }</span>
 
  <span id="line316">316.  </span>
 
  <span id="line316">316.  </span>
 +
 +
== online2 ==
 +
 
  <span id="line317">317.  boolean</span>
 
  <span id="line317">317.  boolean</span>
 
  <span id="line318">318.  online2(x0, y0, x1, y1) /* are two points lined up (on a straight line)? */</span>
 
  <span id="line318">318.  online2(x0, y0, x1, y1) /* are two points lined up (on a straight line)? */</span>
Line 332: Line 397:
 
  <span id="line327">327.  </span>
 
  <span id="line327">327.  </span>
 
  <span id="line328">328.  #endif /* OVL0 */</span>
 
  <span id="line328">328.  #endif /* OVL0 */</span>
 +
 +
== pmatch ==
 +
 
  <span id="line329">329.  #ifdef OVLB</span>
 
  <span id="line329">329.  #ifdef OVLB</span>
 
  <span id="line330">330.  </span>
 
  <span id="line330">330.  </span>
Line 357: Line 425:
 
  <span id="line352">352.  #endif /* OVLB */</span>
 
  <span id="line352">352.  #endif /* OVLB */</span>
 
  <span id="line353">353.  </span>
 
  <span id="line353">353.  </span>
 +
 +
== strncmpi ==
 +
 
  <span id="line354">354.  #ifdef OVL2</span>
 
  <span id="line354">354.  #ifdef OVL2</span>
 
  <span id="line355">355.  #ifndef STRNCMPI</span>
 
  <span id="line355">355.  #ifndef STRNCMPI</span>
Line 378: Line 449:
 
  <span id="line373">373.  #endif /* OVL2 */</span>
 
  <span id="line373">373.  #endif /* OVL2 */</span>
 
  <span id="line374">374.  </span>
 
  <span id="line374">374.  </span>
 +
 +
== strstri ==
 +
 
  <span id="line375">375.  #ifdef OVLB</span>
 
  <span id="line375">375.  #ifdef OVLB</span>
 
  <span id="line376">376.  #ifndef STRSTRI</span>
 
  <span id="line376">376.  #ifndef STRSTRI</span>
Line 419: Line 493:
 
  <span id="line414">414.  #endif /* STRSTRI */</span>
 
  <span id="line414">414.  #endif /* STRSTRI */</span>
 
  <span id="line415">415.  </span>
 
  <span id="line415">415.  </span>
 +
 +
== fuzzymatch ==
 +
 
  <span id="line416">416.  /* compare two strings for equality, ignoring the presence of specified</span>
 
  <span id="line416">416.  /* compare two strings for equality, ignoring the presence of specified</span>
 
  <span id="line417">417.    characters (typically whitespace) and possibly ignoring case */</span>
 
  <span id="line417">417.    characters (typically whitespace) and possibly ignoring case */</span>
Line 445: Line 522:
 
  <span id="line440">440.  </span>
 
  <span id="line440">440.  </span>
 
  <span id="line441">441.  #endif /* OVLB */</span>
 
  <span id="line441">441.  #endif /* OVLB */</span>
 +
 +
== Time routines ==
 +
 
  <span id="line442">442.  #ifdef OVL2</span>
 
  <span id="line442">442.  #ifdef OVL2</span>
 
  <span id="line443">443.  </span>
 
  <span id="line443">443.  </span>
Line 463: Line 543:
 
  <span id="line458">458.  static struct tm *NDECL(getlt);</span>
 
  <span id="line458">458.  static struct tm *NDECL(getlt);</span>
 
  <span id="line459">459.  </span>
 
  <span id="line459">459.  </span>
 +
 +
=== setrandom ===
 +
 
  <span id="line460">460.  void</span>
 
  <span id="line460">460.  void</span>
 
  <span id="line461">461.  setrandom()</span>
 
  <span id="line461">461.  setrandom()</span>
Line 491: Line 574:
 
  <span id="line486">486.  }</span>
 
  <span id="line486">486.  }</span>
 
  <span id="line487">487.  </span>
 
  <span id="line487">487.  </span>
 +
 +
=== getlt ===
 +
 
  <span id="line488">488.  static struct tm *</span>
 
  <span id="line488">488.  static struct tm *</span>
 
  <span id="line489">489.  getlt()</span>
 
  <span id="line489">489.  getlt()</span>
Line 508: Line 594:
 
  <span id="line503">503.  }</span>
 
  <span id="line503">503.  }</span>
 
  <span id="line504">504.  </span>
 
  <span id="line504">504.  </span>
 +
 +
=== getyear ===
 +
 
  <span id="line505">505.  int</span>
 
  <span id="line505">505.  int</span>
 
  <span id="line506">506.  getyear()</span>
 
  <span id="line506">506.  getyear()</span>
Line 514: Line 603:
 
  <span id="line509">509.  }</span>
 
  <span id="line509">509.  }</span>
 
  <span id="line510">510.  </span>
 
  <span id="line510">510.  </span>
 +
 +
=== yymmdd ===
 +
 
  <span id="line511">511.  #if 0</span>
 
  <span id="line511">511.  #if 0</span>
 
  <span id="line512">512.  /* This routine is no longer used since in 2000 it will yield "100mmdd". */</span>
 
  <span id="line512">512.  /* This routine is no longer used since in 2000 it will yield "100mmdd". */</span>
Line 538: Line 630:
 
  <span id="line533">533.  #endif</span>
 
  <span id="line533">533.  #endif</span>
 
  <span id="line534">534.  </span>
 
  <span id="line534">534.  </span>
 +
 +
=== yyyymmdd ===
 +
 
  <span id="line535">535.  long</span>
 
  <span id="line535">535.  long</span>
 
  <span id="line536">536.  yyyymmdd(date)</span>
 
  <span id="line536">536.  yyyymmdd(date)</span>
Line 567: Line 662:
 
  <span id="line562">562.  }</span>
 
  <span id="line562">562.  }</span>
 
  <span id="line563">563.  </span>
 
  <span id="line563">563.  </span>
 +
 +
=== phase_of_the_moon ===
 +
 
  <span id="line564">564.  /*</span>
 
  <span id="line564">564.  /*</span>
 
  <span id="line565">565.  * moon period = 29.53058 days ~= 30, year = 365.2422 days</span>
 
  <span id="line565">565.  * moon period = 29.53058 days ~= 30, year = 365.2422 days</span>
Line 595: Line 693:
 
  <span id="line590">590.  }</span>
 
  <span id="line590">590.  }</span>
 
  <span id="line591">591.  </span>
 
  <span id="line591">591.  </span>
 +
 +
=== friday_13th ===
 +
 
  <span id="line592">592.  boolean</span>
 
  <span id="line592">592.  boolean</span>
 
  <span id="line593">593.  friday_13th()</span>
 
  <span id="line593">593.  friday_13th()</span>
Line 603: Line 704:
 
  <span id="line598">598.  }</span>
 
  <span id="line598">598.  }</span>
 
  <span id="line599">599.  </span>
 
  <span id="line599">599.  </span>
 +
 +
=== night ===
 +
 
  <span id="line600">600.  int</span>
 
  <span id="line600">600.  int</span>
 
  <span id="line601">601.  night()</span>
 
  <span id="line601">601.  night()</span>
Line 611: Line 715:
 
  <span id="line606">606.  }</span>
 
  <span id="line606">606.  }</span>
 
  <span id="line607">607.  </span>
 
  <span id="line607">607.  </span>
 +
 +
=== midnight ===
 +
 
  <span id="line608">608.  int</span>
 
  <span id="line608">608.  int</span>
 
  <span id="line609">609.  midnight()</span>
 
  <span id="line609">609.  midnight()</span>

Revision as of 22:49, 23 August 2006

Below is the full text to src/hacklib.c from NetHack 3.4.3. To link to a particular line, write [[hacklib.c#line123]], for example.

Top of file

1.    /*	SCCS Id: @(#)hacklib.c	3.4	2002/12/13	*/
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. */

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.

5.    
6.    /* We could include only config.h, except for the overlay definitions... */
7.    #include "hack.h"
8.    /*=
9.        Assorted 'small' utility routines.	They're virtually independent of
10.   NetHack, except that rounddiv may call panic().
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 *		upstart		(char *)
19.   	char *		mungspaces	(char *)
20.   	char *		eos		(char *)
21.   	char *		strkitten	(char *,char)
22.   	char *		s_suffix	(const char *)
23.   	char *		xcrypt		(const char *, char *)
24.   	boolean		onlyspace	(const char *)
25.   	char *		tabexpand	(char *)
26.   	char *		visctrl		(char)
27.   	const char *	ordin		(int)
28.   	char *		sitoa		(int)
29.   	int		sgn		(int)
30.   	int		rounddiv	(long, int)
31.   	int		distmin		(int, int, int, int)
32.   	int		dist2		(int, int, int, int)
33.   	boolean		online2		(int, int)
34.   	boolean		pmatch		(const char *, const char *)
35.   	int		strncmpi	(const char *, const char *, int)
36.   	char *		strstri		(const char *, const char *)
37.   	boolean		fuzzymatch	(const char *,const char *,const char *,boolean)
38.   	void		setrandom	(void)
39.   	int		getyear		(void)
40.   	char *		yymmdd		(time_t)
41.   	long		yyyymmdd	(time_t)
42.   	int		phase_of_the_moon	(void)
43.   	boolean		friday_13th	(void)
44.   	int		night		(void)
45.   	int		midnight	(void)
46.   =*/
47.   #ifdef LINT
48.   # define Static		/* pacify lint */
49.   #else
50.   # define Static static
51.   #endif
52.   

digit

53.   #ifdef OVLB
54.   boolean
55.   digit(c)		/* is 'c' a digit? */
56.       char c;
57.   {
58.       return((boolean)('0' <= c && c <= '9'));
59.   }
60.   

letter

61.   boolean
62.   letter(c)		/* is 'c' a letter?  note: '@' classed as letter */
63.       char c;
64.   {
65.       return((boolean)(('@' <= c && c <= 'Z') || ('a' <= c && c <= 'z')));
66.   }
67.   #endif /* OVLB */
68.   

highc

69.   #ifdef OVL1
70.   char
71.   highc(c)			/* force 'c' into uppercase */
72.       char c;
73.   {
74.       return((char)(('a' <= c && c <= 'z') ? (c & ~040) : c));
75.   }
76.   

lowc

77.   char
78.   lowc(c)			/* force 'c' into lowercase */
79.       char c;
80.   {
81.       return((char)(('A' <= c && c <= 'Z') ? (c | 040) : c));
82.   }
83.   #endif /* OVL1 */
84.   

lcase

85.   #ifdef OVLB
86.   char *
87.   lcase(s)		/* convert a string into all lowercase */
88.       char *s;
89.   {
90.       register char *p;
91.   
92.       for (p = s; *p; p++)
93.   	if ('A' <= *p && *p <= 'Z') *p |= 040;
94.       return s;
95.   }
96.   

upstart

97.   char *
98.   upstart(s)		/* convert first character of a string to uppercase */
99.       char *s;
100.  {
101.      if (s) *s = highc(*s);
102.      return s;
103.  }
104.  

mungspaces

105.  /* remove excess whitespace from a string buffer (in place) */
106.  char *
107.  mungspaces(bp)
108.  char *bp;
109.  {
110.      register char c, *p, *p2;
111.      boolean was_space = TRUE;
112.  
113.      for (p = p2 = bp; (c = *p) != '\0'; p++) {
114.  	if (c == '\t') c = ' ';
115.  	if (c != ' ' || !was_space) *p2++ = c;
116.  	was_space = (c == ' ');
117.      }
118.      if (was_space && p2 > bp) p2--;
119.      *p2 = '\0';
120.      return bp;
121.  }
122.  
123.  #endif /* OVLB */
124.  

eos

125.  #ifdef OVL0
126.  char *
127.  eos(s)			/* return the end of a string (pointing at '\0') */
128.      register char *s;
129.  {
130.      while (*s) s++;	/* s += strlen(s); */
131.      return s;
132.  }
133.  

strkitten

134.  /* strcat(s, {c,'\0'}); */
135.  char *
136.  strkitten(s, c)		/* append a character to a string (in place) */
137.      char *s;
138.      char c;
139.  {
140.      char *p = eos(s);
141.  
142.      *p++ = c;
143.      *p = '\0';
144.      return s;
145.  }
146.  

s_suffix

147.  char *
148.  s_suffix(s)		/* return a name converted to possessive */
149.      const char *s;
150.  {
151.      Static char buf[BUFSZ];
152.  
153.      Strcpy(buf, s);
154.      if(!strcmpi(buf, "it"))
155.  	Strcat(buf, "s");
156.      else if(*(eos(buf)-1) == 's')
157.  	Strcat(buf, "'");
158.      else
159.  	Strcat(buf, "'s");
160.      return buf;
161.  }
162.  

xcrypt

163.  char *
164.  xcrypt(str, buf)	/* trivial text encryption routine (see makedefs) */
165.  const char *str;
166.  char *buf;
167.  {
168.      register const char *p;
169.      register char *q;
170.      register int bitmask;
171.  
172.      for (bitmask = 1, p = str, q = buf; *p; q++) {
173.  	*q = *p++;
174.  	if (*q & (32|64)) *q ^= bitmask;
175.  	if ((bitmask <<= 1) >= 32) bitmask = 1;
176.      }
177.      *q = '\0';
178.      return buf;
179.  }
180.  #endif /* OVL0 */
181.  

onlyspace

182.  #ifdef OVL2
183.  boolean
184.  onlyspace(s)		/* is a string entirely whitespace? */
185.      const char *s;
186.  {
187.      for (; *s; s++)
188.  	if (*s != ' ' && *s != '\t') return FALSE;
189.      return TRUE;
190.  }
191.  #endif /* OVL2 */
192.  

tabexpand

193.  #ifdef OVLB
194.  char *
195.  tabexpand(sbuf)		/* expand tabs into proper number of spaces */
196.      char *sbuf;
197.  {
198.      char buf[BUFSZ];
199.      register char *bp, *s = sbuf;
200.      register int idx;
201.  
202.      if (!*s) return sbuf;
203.  
204.      /* warning: no bounds checking performed */
205.      for (bp = buf, idx = 0; *s; s++)
206.  	if (*s == '\t') {
207.  	    do *bp++ = ' '; while (++idx % 8);
208.  	} else {
209.  	    *bp++ = *s;
210.  	    idx++;
211.  	}
212.      *bp = 0;
213.      return strcpy(sbuf, buf);
214.  }
215.  

visctrl

216.  char *
217.  visctrl(c)		/* make a displayable string from a character */
218.      char c;
219.  {
220.      Static char ccc[3];
221.  
222.      c &= 0177;
223.  
224.      ccc[2] = '\0';
225.      if (c < 040) {
226.  	ccc[0] = '^';
227.  	ccc[1] = c | 0100;	/* letter */
228.      } else if (c == 0177) {
229.  	ccc[0] = '^';
230.  	ccc[1] = c & ~0100;	/* '?' */
231.      } else {
232.  	ccc[0] = c;		/* printable character */
233.  	ccc[1] = '\0';
234.      }
235.      return ccc;
236.  }
237.  #endif /* OVLB */
238.  

ordin

239.  #ifdef OVL2
240.  const char *
241.  ordin(n)		/* return the ordinal suffix of a number */
242.      int n;			/* note: should be non-negative */
243.  {
244.      register int dd = n % 10;
245.  
246.      return (dd == 0 || dd > 3 || (n % 100) / 10 == 1) ? "th" :
247.  	    (dd == 1) ? "st" : (dd == 2) ? "nd" : "rd";
248.  }
249.  #endif /* OVL2 */
250.  

sitoa

251.  #ifdef OVL1
252.  char *
253.  sitoa(n)		/* make a signed digit string from a number */
254.      int n;
255.  {
256.      Static char buf[13];
257.  
258.      Sprintf(buf, (n < 0) ? "%d" : "+%d", n);
259.      return buf;
260.  }
261.  

sgn

262.  int
263.  sgn(n)			/* return the sign of a number: -1, 0, or 1 */
264.      int n;
265.  {
266.      return (n < 0) ? -1 : (n != 0);
267.  }
268.  #endif /* OVL1 */
269.  

rounddiv

270.  #ifdef OVLB
271.  int
272.  rounddiv(x, y)		/* calculate x/y, rounding as appropriate */
273.      long x;
274.      int  y;
275.  {
276.      int r, m;
277.      int divsgn = 1;
278.  
279.      if (y == 0)
280.  	panic("division by zero in rounddiv");
281.      else if (y < 0) {
282.  	divsgn = -divsgn;  y = -y;
283.      }
284.      if (x < 0) {
285.  	divsgn = -divsgn;  x = -x;
286.      }
287.      r = x / y;
288.      m = x % y;
289.      if (2*m >= y) r++;
290.  
291.      return divsgn * r;
292.  }
293.  #endif /* OVLB */
294.  

distmin

295.  #ifdef OVL0
296.  int
297.  distmin(x0, y0, x1, y1) /* distance between two points, in moves */
298.      int x0, y0, x1, y1;
299.  {
300.      register int dx = x0 - x1, dy = y0 - y1;
301.      if (dx < 0) dx = -dx;
302.      if (dy < 0) dy = -dy;
303.    /*  The minimum number of moves to get from (x0,y0) to (x1,y1) is the
304.     :  larger of the [absolute value of the] two deltas.
305.     */
306.      return (dx < dy) ? dy : dx;
307.  }
308.  

dist2

309.  int
310.  dist2(x0, y0, x1, y1)	/* square of euclidean distance between pair of pts */
311.      int x0, y0, x1, y1;
312.  {
313.      register int dx = x0 - x1, dy = y0 - y1;
314.      return dx * dx + dy * dy;
315.  }
316.  

online2

317.  boolean
318.  online2(x0, y0, x1, y1) /* are two points lined up (on a straight line)? */
319.      int x0, y0, x1, y1;
320.  {
321.      int dx = x0 - x1, dy = y0 - y1;
322.      /*  If either delta is zero then they're on an orthogonal line,
323.       *  else if the deltas are equal (signs ignored) they're on a diagonal.
324.       */
325.      return((boolean)(!dy || !dx || (dy == dx) || (dy + dx == 0)));	/* (dy == -dx) */
326.  }
327.  
328.  #endif /* OVL0 */

pmatch

329.  #ifdef OVLB
330.  
331.  boolean
332.  pmatch(patrn, strng)	/* match a string against a pattern */
333.      const char *patrn, *strng;
334.  {
335.      char s, p;
336.    /*
337.     :  Simple pattern matcher:  '*' matches 0 or more characters, '?' matches
338.     :  any single character.  Returns TRUE if 'strng' matches 'patrn'.
339.     */
340.  pmatch_top:
341.      s = *strng++;  p = *patrn++;	/* get next chars and pre-advance */
342.      if (!p)			/* end of pattern */
343.  	return((boolean)(s == '\0'));		/* matches iff end of string too */
344.      else if (p == '*')		/* wildcard reached */
345.  	return((boolean)((!*patrn || pmatch(patrn, strng-1)) ? TRUE :
346.  		s ? pmatch(patrn-1, strng) : FALSE));
347.      else if (p != s && (p != '?' || !s))  /* check single character */
348.  	return FALSE;		/* doesn't match */
349.      else				/* return pmatch(patrn, strng); */
350.  	goto pmatch_top;	/* optimize tail recursion */
351.  }
352.  #endif /* OVLB */
353.  

strncmpi

354.  #ifdef OVL2
355.  #ifndef STRNCMPI
356.  int
357.  strncmpi(s1, s2, n)	/* case insensitive counted string comparison */
358.      register const char *s1, *s2;
359.      register int n; /*(should probably be size_t, which is usually unsigned)*/
360.  {					/*{ aka strncasecmp }*/
361.      register char t1, t2;
362.  
363.      while (n--) {
364.  	if (!*s2) return (*s1 != 0);	/* s1 >= s2 */
365.  	else if (!*s1) return -1;	/* s1  < s2 */
366.  	t1 = lowc(*s1++);
367.  	t2 = lowc(*s2++);
368.  	if (t1 != t2) return (t1 > t2) ? 1 : -1;
369.      }
370.      return 0;				/* s1 == s2 */
371.  }
372.  #endif	/* STRNCMPI */
373.  #endif /* OVL2 */
374.  

strstri

375.  #ifdef OVLB
376.  #ifndef STRSTRI
377.  
378.  char *
379.  strstri(str, sub)	/* case insensitive substring search */
380.      const char *str;
381.      const char *sub;
382.  {
383.      register const char *s1, *s2;
384.      register int i, k;
385.  # define TABSIZ 0x20	/* 0x40 would be case-sensitive */
386.      char tstr[TABSIZ], tsub[TABSIZ];	/* nibble count tables */
387.  # if 0
388.      assert( (TABSIZ & ~(TABSIZ-1)) == TABSIZ ); /* must be exact power of 2 */
389.      assert( &lowc != 0 );			/* can't be unsafe macro */
390.  # endif
391.  
392.      /* special case: empty substring */
393.      if (!*sub)	return (char *) str;
394.  
395.      /* do some useful work while determining relative lengths */
396.      for (i = 0; i < TABSIZ; i++)  tstr[i] = tsub[i] = 0;	/* init */
397.      for (k = 0, s1 = str; *s1; k++)  tstr[*s1++ & (TABSIZ-1)]++;
398.      for (	s2 = sub; *s2; --k)  tsub[*s2++ & (TABSIZ-1)]++;
399.  
400.      /* evaluate the info we've collected */
401.      if (k < 0)	return (char *) 0;  /* sub longer than str, so can't match */
402.      for (i = 0; i < TABSIZ; i++)	/* does sub have more 'x's than str? */
403.  	if (tsub[i] > tstr[i])	return (char *) 0;  /* match not possible */
404.  
405.      /* now actually compare the substring repeatedly to parts of the string */
406.      for (i = 0; i <= k; i++) {
407.  	s1 = &str[i];
408.  	s2 = sub;
409.  	while (lowc(*s1++) == lowc(*s2++))
410.  	    if (!*s2)  return (char *) &str[i];		/* full match */
411.      }
412.      return (char *) 0;	/* not found */
413.  }
414.  #endif	/* STRSTRI */
415.  

fuzzymatch

416.  /* compare two strings for equality, ignoring the presence of specified
417.     characters (typically whitespace) and possibly ignoring case */
418.  boolean
419.  fuzzymatch(s1, s2, ignore_chars, caseblind)
420.      const char *s1, *s2;
421.      const char *ignore_chars;
422.      boolean caseblind;
423.  {
424.      register char c1, c2;
425.  
426.      do {
427.  	while ((c1 = *s1++) != '\0' && index(ignore_chars, c1) != 0) continue;
428.  	while ((c2 = *s2++) != '\0' && index(ignore_chars, c2) != 0) continue;
429.  	if (!c1 || !c2) break;	/* stop when end of either string is reached */
430.  
431.  	if (caseblind) {
432.  	    c1 = lowc(c1);
433.  	    c2 = lowc(c2);
434.  	}
435.      } while (c1 == c2);
436.  
437.      /* match occurs only when the end of both strings has been reached */
438.      return (boolean)(!c1 && !c2);
439.  }
440.  
441.  #endif /* OVLB */

Time routines

442.  #ifdef OVL2
443.  
444.  /*
445.   * Time routines
446.   *
447.   * The time is used for:
448.   *	- seed for rand()
449.   *	- year on tombstone and yyyymmdd in record file
450.   *	- phase of the moon (various monsters react to NEW_MOON or FULL_MOON)
451.   *	- night and midnight (the undead are dangerous at midnight)
452.   *	- determination of what files are "very old"
453.   */
454.  
455.  #if defined(AMIGA) && !defined(AZTEC_C) && !defined(__SASC_60) && !defined(_DCC) && !defined(__GNUC__)
456.  extern struct tm *FDECL(localtime,(time_t *));
457.  #endif
458.  static struct tm *NDECL(getlt);
459.  

setrandom

460.  void
461.  setrandom()
462.  {
463.  	/* the types are different enough here that sweeping the different
464.  	 * routine names into one via #defines is even more confusing
465.  	 */
466.  #ifdef RANDOM	/* srandom() from sys/share/random.c */
467.  	srandom((unsigned int) time((time_t *)0));
468.  #else
469.  # if defined(__APPLE__) || defined(BSD) || defined(LINUX) || defined(ULTRIX) || defined(CYGWIN32) /* system srandom() */
470.  #  if defined(BSD) && !defined(POSIX_TYPES)
471.  #   if defined(SUNOS4)
472.  	(void)
473.  #   endif
474.  		srandom((int) time((long *)0));
475.  #  else
476.  		srandom((int) time((time_t *)0));
477.  #  endif
478.  # else
479.  #  ifdef UNIX	/* system srand48() */
480.  	srand48((long) time((time_t *)0));
481.  #  else		/* poor quality system routine */
482.  	srand((int) time((time_t *)0));
483.  #  endif
484.  # endif
485.  #endif
486.  }
487.  

getlt

488.  static struct tm *
489.  getlt()
490.  {
491.  	time_t date;
492.  
493.  #if defined(BSD) && !defined(POSIX_TYPES)
494.  	(void) time((long *)(&date));
495.  #else
496.  	(void) time(&date);
497.  #endif
498.  #if (defined(ULTRIX) && !(defined(ULTRIX_PROTO) || defined(NHSTDC))) || (defined(BSD) && !defined(POSIX_TYPES))
499.  	return(localtime((long *)(&date)));
500.  #else
501.  	return(localtime(&date));
502.  #endif
503.  }
504.  

getyear

505.  int
506.  getyear()
507.  {
508.  	return(1900 + getlt()->tm_year);
509.  }
510.  

yymmdd

511.  #if 0
512.  /* This routine is no longer used since in 2000 it will yield "100mmdd". */
513.  char *
514.  yymmdd(date)
515.  time_t date;
516.  {
517.  	Static char datestr[10];
518.  	struct tm *lt;
519.  
520.  	if (date == 0)
521.  		lt = getlt();
522.  	else
523.  #if (defined(ULTRIX) && !(defined(ULTRIX_PROTO) || defined(NHSTDC))) || defined(BSD)
524.  		lt = localtime((long *)(&date));
525.  #else
526.  		lt = localtime(&date);
527.  #endif
528.  
529.  	Sprintf(datestr, "%02d%02d%02d",
530.  		lt->tm_year, lt->tm_mon + 1, lt->tm_mday);
531.  	return(datestr);
532.  }
533.  #endif
534.  

yyyymmdd

535.  long
536.  yyyymmdd(date)
537.  time_t date;
538.  {
539.  	long datenum;
540.  	struct tm *lt;
541.  
542.  	if (date == 0)
543.  		lt = getlt();
544.  	else
545.  #if (defined(ULTRIX) && !(defined(ULTRIX_PROTO) || defined(NHSTDC))) || (defined(BSD) && !defined(POSIX_TYPES))
546.  		lt = localtime((long *)(&date));
547.  #else
548.  		lt = localtime(&date);
549.  #endif
550.  
551.  	/* just in case somebody's localtime supplies (year % 100)
552.  	   rather than the expected (year - 1900) */
553.  	if (lt->tm_year < 70)
554.  	    datenum = (long)lt->tm_year + 2000L;
555.  	else
556.  	    datenum = (long)lt->tm_year + 1900L;
557.  	/* yyyy --> yyyymm */
558.  	datenum = datenum * 100L + (long)(lt->tm_mon + 1);
559.  	/* yyyymm --> yyyymmdd */
560.  	datenum = datenum * 100L + (long)lt->tm_mday;
561.  	return datenum;
562.  }
563.  

phase_of_the_moon

564.  /*
565.   * moon period = 29.53058 days ~= 30, year = 365.2422 days
566.   * days moon phase advances on first day of year compared to preceding year
567.   *	= 365.2422 - 12*29.53058 ~= 11
568.   * years in Metonic cycle (time until same phases fall on the same days of
569.   *	the month) = 18.6 ~= 19
570.   * moon phase on first day of year (epact) ~= (11*(year%19) + 29) % 30
571.   *	(29 as initial condition)
572.   * current phase in days = first day phase + days elapsed in year
573.   * 6 moons ~= 177 days
574.   * 177 ~= 8 reported phases * 22
575.   * + 11/22 for rounding
576.   */
577.  int
578.  phase_of_the_moon()		/* 0-7, with 0: new, 4: full */
579.  {
580.  	register struct tm *lt = getlt();
581.  	register int epact, diy, goldn;
582.  
583.  	diy = lt->tm_yday;
584.  	goldn = (lt->tm_year % 19) + 1;
585.  	epact = (11 * goldn + 18) % 30;
586.  	if ((epact == 25 && goldn > 11) || epact == 24)
587.  		epact++;
588.  
589.  	return( (((((diy + epact) * 6) + 11) % 177) / 22) & 7 );
590.  }
591.  

friday_13th

592.  boolean
593.  friday_13th()
594.  {
595.  	register struct tm *lt = getlt();
596.  
597.  	return((boolean)(lt->tm_wday == 5 /* friday */ && lt->tm_mday == 13));
598.  }
599.  

night

600.  int
601.  night()
602.  {
603.  	register int hour = getlt()->tm_hour;
604.  
605.  	return(hour < 6 || hour > 21);
606.  }
607.  

midnight

608.  int
609.  midnight()
610.  {
611.  	return(getlt()->tm_hour == 0);
612.  }
613.  #endif /* OVL2 */
614.  
615.  /*hacklib.c*/