home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1994 March / Source_Code_CD-ROM_Walnut_Creek_March_1994.iso / compsrcs / games / vmsnet / ldb / part13 < prev    next >
Encoding:
Internet Message Format  |  1993-04-07  |  47.9 KB

  1. Path: uunet!zaphod.mps.ohio-state.edu!usc!news.service.uci.edu!unogate!mvb.saic.com!dayton.saic.com!dayvd.dayton.saic.com!ake
  2. From: ake@dayvd.dayton.saic.com (Earle Ake)
  3. Newsgroups: vmsnet.sources.games
  4. Subject: ldb - Long Distance Backgammon [13/16]
  5. Date: 8 Apr 93 11:04:09 EST
  6. Organization: Science Applications Intl Corp - Dayton, OH
  7. Lines: 1291
  8. Message-ID: <1993Apr8.110409.1@dayvd.dayton.saic.com>
  9. NNTP-Posting-Host: dayvd.dayton.saic.com
  10. Xref: uunet vmsnet.sources.games:674
  11.  
  12. -+-+-+-+-+-+-+-+ START OF PART 13 -+-+-+-+-+-+-+-+
  13. X`20*`20Note`20that`20the`20file`20argument`20can`20be`20actually`20be`20a`20pa
  14. Vttern,`20causing
  15. X`20*`20all`20matching`20files`20to`20be`20scanned.`20`20On`20UNIX`20systems,
  16. V`20patterns`20are
  17. X`20*`20interpreted`20in`20the`20same`20manner`20as`20the`20shell.`20`20On`20VM
  18. VS,`20they
  19. X`20*`20are`20interpreted`20in`20the`20same`20manner`20as`20DCL.
  20. X`20*----------------------------------------------------------------------
  21. X`20*/
  22. X
  23. Xreadmail(file)
  24. Xchar`20*file;
  25. X`7B
  26. Xstruct`20flist`20*fl,`20*t;
  27. X
  28. Xfl`20=`20filelist(file);`09`09/*`20generate`20a`20list`20of`20all`20matching
  29. V`20files`20*/
  30. Xwhile`20(fl`20!=`20NULL)`20`7B`09`09/*`20for`20each`20matching`20file`20found
  31. V`20*/
  32. X`09readfile(fl->name);`09/*`20scan`20it`20*/
  33. X`09free(fl->name);`09`09/*`20free`20the`20string`20*/
  34. X`09t`20=`20fl;`09`09`09/*`20keep`20a`20pointer`20to`20the`20struct`20*/
  35. X`09fl`20=`20fl->next;`09`09/*`20advance`20fl`20pointer`20*/
  36. X`09free(t);`09`09/*`20free`20the`20previous`20struct`20*/
  37. X`09`7D
  38. X`7D
  39. X
  40. X
  41. X/*----------------------------------------------------------------------
  42. X`20*`09readfile`20--`20scan`20a`20file`20looking`20for`20incoming`20messages
  43. X`20*
  44. X`20*`20This`20function`20is`20called`20by`20readmail`20to`20scan`20a`20file
  45. V`20that`20matches
  46. X`20*`20the`20pattern.
  47. X`20*----------------------------------------------------------------------
  48. X`20*/
  49. X
  50. Xreadfile(name)
  51. Xchar`20*name;
  52. X`7B
  53. XFILE`20*fp;
  54. Xint`20d,`20c1,`20c2;
  55. Xint`20flags`20=`200,`20match`20=`200;
  56. Xstruct`20people`20*p;
  57. Xstruct`20game`20*g;
  58. X
  59. Xif`20(`20(fp`20=`20fopen(name,"r"))`20==`20NULL)
  60. X`09return;
  61. Xif`20(rc.debug`20`26`20DB_READFILE)
  62. X`09message("DB-readfile:`20scanning`20%s`5Cn",name);
  63. Xwhile`20(getpkt(fp)`20>`200)`20`7B`09/*`20as`20long`20as`20we`20found`20a`20va
  64. Vlid`20packet`20*/
  65. X`09if`20(P.gameptr`20==`20NULL)`20`7B
  66. X`09`09if`20(rc.debug`20`26`20DB_READFILE)
  67. X`09`09`09message("DB-readfile:`20found`20packet`20for`20<unknown>`5Cn");
  68. X`09`09if`20(P.opcode`20==`20START)`20`7B
  69. X`09`09`09if`20(`20((p`20=`20findppl(P.addr,P_ADDR))`20!=`20NULL)`20`26`26
  70. X`09`09`09`20`20`20`20`20(p->fence`20>=`20P.timestamp)`20)
  71. X`09`09`09`09continue;`09/*`20ignore`20old`20start`20packets`20*/
  72. X`09`09`09P.gameptr`20=`20addgame();`20/*`20init`20later`20in`20start()`20*/
  73. X`09`09`09P.gameptr->ppl`20=`20p;
  74. X`09`09`09P.gameptr->gameid`20=`20P.gameid;
  75. X`09`09`09P.gameptr->state`20=`20ST_OPSTART;
  76. X`09`09`09`7D
  77. X`09`09else`20if`20(P.opcode`20==`20RSTART)`20`7B`09/*`20remote`20start`20packe
  78. Vt`20*/
  79. X`09`09`09if`20(rc.debug`20`26`20DB_RSTART)
  80. X`09`09`09`09message("DB-readfile:`20remstart`20to`20%s`5Cn",P.addr);
  81. X`09`09`09if`20(`20(p`20=`20findppl(P.addr,P_ADDR))`20!=`20NULL)
  82. X`09`09`09`09P.addr`20=`20p->addr;`09/*`20use`20real`20addr`20*/
  83. X`09`09`09for`20(g`20=`20ghead;`20g`20!=`20NULL;`20g`20=`20g->next)
  84. X`09`09`09`09if`20(`20(P.timestamp`20==`20g->starttime)`20`26`26
  85. X`09`09`09`09`20`20`20`20`20(strcmp(P.addr,g->opaddr)`20==`200)`20)
  86. X`09`09`09`09`09break;`09/*`20already`20seen`20this`20packet`20*/
  87. X`09`09`09if`20(g`20!=`20NULL)
  88. X`09`09`09`09continue;
  89. X`09`09`09if`20(`20(p`20!=`20NULL)`20`26`26`20(p->fence`20>=`20P.timestamp)`20)
  90. V
  91. X`09`09`09`09continue;`09/*`20this`20game`20already`20played`20*/
  92. X`09`09`09if`20(rc.debug`20`26`20DB_RSTART)
  93. X`09`09`09`09message("DB-readfile:`20address`20is`20%s`5Cn",P.addr);
  94. X`09`09`09if`20(P.dir`20==`20NULL)`09/*`20if`20no`20direction`20was`20given`20*
  95. V/
  96. X`09`09`09`09d`20=`20cr_mydir;`09/*`20use`20my`20default`20*/
  97. X`09`09`09else`09`09`09/*`20dir`20was`20given,`20grab`20it`20*/
  98. X`09`09`09`09d`20=`20(*P.dir`20==`20'u')`20?`201`20:`20-1;
  99. X`09`09`09if`20(P.colors`20==`20NULL)`20`7B`09/*`20if`20no`20colors`20were`20gi
  100. Vven`20*/
  101. X`09`09`09`09c1`20=`20cr_mycolor;`09/*`20use`20my`20defaults`20*/
  102. X`09`09`09`09c2`20=`20cr_opcolor;
  103. X`09`09`09`09`7D
  104. X`09`09`09else`20`7B`09`09`09`09/*`20colors`20were`20given`20*/
  105. X`09`09`09`09c1`20=`20*P.colors;`09`09/*`20use`20them`20*/
  106. X`09`09`09`09c2`20=`20P.colors`5B1`5D;
  107. X`09`09`09`09`7D
  108. X`09`09`09if`20(P.jacoby`20!=`20NULL)
  109. X`09`09`09`09flags`20`7C=`20F_JACOBY;
  110. X`09`09`09if`20(P.crawford`20!=`20NULL)
  111. X`09`09`09`09flags`20`7C=`20F_CRAWFORD;
  112. X`09`09`09if`20(P.european`20!=`20NULL)
  113. X`09`09`09`09flags`20`7C=`20F_EUROPE;
  114. X`09`09`09if`20(P.perm`20!=`20NULL)
  115. X`09`09`09`09flags`20`7C=`20F_PERM;
  116. X`09`09`09if`20(P.match`20!=`20NULL)
  117. X`09`09`09`09match`20=`20atoi(P.match);
  118. X`09`09`09notify`20=`20P.notify;`09/*`20store`20notify`20address`20*/
  119. X`09`09`09startgame(P.addr,d,c1,c2,flags,match,P.timestamp);
  120. X`09`09`09continue;`09/*`20game`20started,`20discard`20this`20packet`20*/
  121. X`09`09`09`7D
  122. X`09`09else`20`7B
  123. X`09`09`09message("ERROR:`20no`20such`20gameid:`20%s`20(ignored)`5Cn",
  124. X`09`09`09`09P.gameid);
  125. X`09`09`09continue;
  126. X`09`09`09`7D
  127. X`09`09`7D
  128. X`09if`20(rc.debug`20`26`20DB_READFILE)
  129. X`09`09message("DB-readfile:`20found`20packet`20for`20%s`5Cn",
  130. X`09`09`09P.gameptr->gameid);
  131. X`09if`20(P.gameptr->state`20>=`20OPSTATES)`20`7B`09/*`20hey,`20it's`20still
  132. V`20my`20turn`20*/
  133. X`09`09message("ERROR:`20move`20out`20of`20turn:`20%s`20(ignored)`5Cn",P.gameid
  134. V);
  135. X`09`09continue;
  136. X`09`09`7D
  137. X`09if`20(P.name`20!=`20NULL)`20`7B`09`09/*`20snarf`20opponent's`20name`20*/
  138. X`09`09P.gameptr->opname`20=`20P.name;
  139. X`09`09if`20(P.gameptr->ppl`20!=`20NULL)
  140. X`09`09`09P.gameptr->ppl->name`20=`20save(P.name);
  141. X`09`09`7D
  142. X`09if`20(P.notify`20!=`20NULL)
  143. X`09`09P.gameptr->notify`20=`20P.notify;
  144. X`09(*func`5BP.gameptr->state`5D`5BP.opcode`5D)(P.gameptr);`09/*`20call`20handl
  145. Ver`20*/
  146. X`09`7D
  147. Xfclose(fp);
  148. Xif`20(`20((*rc.delmail`20==`20'y')`20`7C`7C`20(*rc.delmail`20==`20'Y'))
  149. X#ifndef`20VMS
  150. X`09`09`26`26`20(*name`20!=`20'/')`09/*`20absolute`20paths`20not`20deleted`20*/
  151. V
  152. X#endif
  153. X`09)`20`7B
  154. X`09if`20(rc.debug`20`26`20DB_READFILE)
  155. X`09`09message("DB-readfile:`20deleting`20mail`20file`20%s`5Cn",name);
  156. X`09unlink(name);
  157. X`09`7D
  158. X`7D
  159. X
  160. X
  161. X/*---------------------------------------------------------------------------
  162. X`20*`09getpkt`20--`20read`20one`20packet`20from`20a`20file
  163. X`20*
  164. X`20*`20This`20function`20reads`20the`20next`20packet`20from`20the`20specified
  165. V`20file.
  166. X`20*`20Getpkt()`20is`20passed`20a`20open`20file`20pointer`20to`20the`20file
  167. V`20it`20is`20to`20scan.
  168. X`20*`20Lines`20are`20read`20and`20discarded`20until`20a`20line`20is`20found
  169. V`20that`20contains`20only:
  170. X`20*`09`09<<<===LDB===>>>
  171. X`20*`20Subsequent`20lines`20should`20contain`20name/value`20pairs`20as`20speci
  172. Vfied
  173. X`20*`20in`20nv_packet.`20`20The`20packet`20ends`20with`20end`20of`20file`20or
  174. V`20a`20line`20beginning
  175. X`20*`20with`20"end=".`20`20Getpkt`20reads`20from`20the`20input`20file`20until
  176. V`20one
  177. X`20*`20packet`20has`20been`20found`20and`20processed,`20then`20returns.`20`20S
  178. Vubsequent`20calls
  179. X`20*`20to`20getpkt`20with`20the`20same`20file`20pointer`20will`20process`20add
  180. Vitional`20packets.
  181. X`20*`20Getpkt`20returns`201`20if`20a`20valid`20packet`20was`20read,`200`20if
  182. V`20EOF`20was`20encountered.
  183. X`20*`20Getpkt`20ignores`20incoming`20packets`20with`20the`20incorrect`20sequen
  184. Vce`20number.
  185. X`20*
  186. X`20*`20As`20a`20compatibility`20hook`20with`20old`20versions,`20getpkt`20check
  187. Vs`20that`20the
  188. X`20*`20version`20field`20on`20the`20incoming`20packet`20is`20high`20enough`20t
  189. Vo`20support
  190. X`20*`20the`20following`20features`20if`20they`20are`20enabled`20in`20the`20gam
  191. Ve:
  192. X`20*`09feature`09`09need`20at`20least`20version
  193. X`20*`09-------------------------------------
  194. X`20*`09match`20play`09`091.1
  195. X`20*`09jacoby`20rule`09`091.1
  196. X`20*`09crawford`20rule`09`091.1
  197. X`20*`09european`20rule`09`091.1
  198. X`20*`09permanent`20games`09`091.1
  199. X`20*`20If`20the`20incoming`20packet`20indicates`20a`20feature`20is`20not`20sup
  200. Vported`20by`20the
  201. X`20*`20remote`20ldb,`20it`20is`20disabled`20and`20the`20game`20continues`20as
  202. V`20if`20it`20had
  203. X`20*`20never`20been`20set.`20`20The`20Crawford`20rule`20contained`20a`20bug
  204. V`20in`20pre-1.3`20games,
  205. X`20*`20so`201.3`20will`20print`20a`20warning`20if`20an`20older`20version`20tri
  206. Ves`20to`20start
  207. X`20*`20a`20game`20with`20the`20crawford`20rule`20enabled.
  208. X`20*
  209. X`20*`20Getpkt`20handles`20RESEND`20packets`20itself,`20performing`20a`20resend
  210. V`20and
  211. X`20*`20discarding`20the`20packet.
  212. X`20*--------------------------------------------------------------------------
  213. V-
  214. X`20*/
  215. X
  216. Xgetpkt(fp)
  217. XFILE`20*fp;
  218. X`7B
  219. Xstatic`20char`20buf`5B128`5D;
  220. Xint`20i;
  221. X
  222. Xwhile`20(fgets(buf,sizeof(buf),fp)`20!=`20NULL)`20`7B
  223. X`09if`20(strcmp(buf,"<<<===LDB===>>>`5Cn"))/*`20skip`20all`20other`20lines`20*
  224. V/
  225. X`09`09continue;
  226. X`09P.gameid`20=`20NULL;`09/*`20init`20P`20structure`20*/
  227. X`09P.version`20=`20100;`09/*`20default`20to`20oldest`20version`20*/
  228. X`09P.timestamp`20=`200L;
  229. X`09P.opcode`20=`20-1;
  230. X`09P.name`20=`20NULL;
  231. X`09P.addr`20=`20NULL;
  232. X`09P.comment`20=`20NULL;
  233. X`09P.comment2`20=`20NULL;
  234. X`09P.seq`20=`20-1;
  235. X`09P.autodbl`20=`20NULL;
  236. X`09P.jacoby`20=`20NULL;`09/*`20jacoby`20is`20off`20by`20default`20*/
  237. X`09P.crawford`20=`20NULL;`09/*`20so`20is`20crawford`20*/
  238. X`09P.perm`20=`20NULL;`09`09/*`20so`20is`20permanent`20option`20*/
  239. X`09P.european`20=`20NULL;`09/*`20so`20is`20european`20rule`20*/
  240. X`09P.match`20=`20NULL;`09`09/*`20so`20is`20match`20play`20*/
  241. X`09clearmvs(P.mvs);
  242. X`09P.gameptr`20=`20NULL;
  243. X`09P.notify`20=`20NULL;
  244. X`09nvscan(fp,nv_packet,`26P);`09/*`20scan`20the`20packet`20into`20P`20*/
  245. X`09if`20(P.gameid`20==`20NULL)`20`7B`09`09/*`20didn't`20get`20a`20gameid`20*/
  246. X`09`09message("ERROR:`20missing`20gameid`20in`20packet`20--`20ignored`5Cn");
  247. X`09`09continue;
  248. X`09`09`7D
  249. X`09if`20(P.version`20>`20100)`20`7B`09`09/*`20versions`20after`201.0`20rot13
  250. V`20comments`20*/
  251. X`09`09if`20(P.comment`20!=`20NULL)
  252. X`09`09`09rotate(P.comment);
  253. X`09`09if`20(P.comment2`20!=`20NULL)
  254. X`09`09`09rotate(P.comment2);
  255. X`09`09`7D
  256. X`09if`20(`20(P.gameptr`20=`20findgame(P.gameid))`20==`20NULL)`20`7B/*`20doesn'
  257. Vt`20exist`20*/
  258. X`09`09if`20(`20(P.opcode`20!=`20START)`20`26`26`20(P.opcode`20!=`20RSTART)`20)
  259. V
  260. X`09`09`09continue;`09/*`20ignore`20pkts`20for`20dead`20games`20*/
  261. X`09`09i`20=`201;`09`09`09/*`20initial`20seq`20==`201`20*/
  262. X`09`09`7D
  263. X`09else`20`7B
  264. X`09`09if`20(P.opcode`20==`20RESEND)`20`7B`09/*`20resend`20request`20*/
  265. X`09`09`09if`20((P.seq`20+`201)`20!=`20P.gameptr->seq)
  266. X`09`09`09`09continue;`09/*`20old`20resend`20request,`20ignore`20*/
  267. X`09`09`09if`20(P.timestamp`20<`20P.gameptr->lastacc)
  268. X`09`09`09`09continue;`09/*`20old`20resend`20request,`20ignore`20*/
  269. X`09`09`09message(
  270. X`09`09`09`09"Resend`20requested`20for`20game`20%s`20--`20sending...`5Cn",
  271. X`09`09`09`09P.gameid);
  272. X`09`09`09resendpkt(P.gameptr);`09/*`20resend`20*/
  273. X`09`09`09P.gameptr->lastacc`20=`20time(`20(long`20*)`200);`09/*set`20lastacc*/
  274. V
  275. X`09`09`09if`20(P.timestamp`20>`20P.gameptr->lastacc)
  276. X`09`09`09`09P.gameptr->lastacc`20=`20P.timestamp;
  277. X`09`09`09continue;`09`09/*`20and`20ignore`20packet`20*/
  278. X`09`09`09`7D
  279. X`09`09i`20=`20P.gameptr->seq+1;`09/*`20get`20current`20seq`20*/
  280. X`09`09`7D
  281. X`09if`20(P.seq`20!=`20i)`20`7B`09`09/*`20sequence`20number`20is`20wrong`20*/
  282. X`09`09if`20(P.seq`20>`20i)`09`09/*`20rec'd`20seq`20#`20is`20too`20big`20*/
  283. X`09`09`09message(`09`09/*`20shouldn't`20happen`20*/
  284. X`09`09`09"WARNING:`20game`20%s,`20seq`20no.`20is`20%d,`20s/b`20%d`20--`20ignor
  285. Ved.`5Cn"
  286. X`09`09`09,P.gameid,P.seq,i);
  287. X`09`09continue;`09`09/*`20ignore`20pkts`20with`20bad`20sequence`20#s`20*/
  288. X`09`09`7D
  289. X`09if`20(`20(P.opcode`20<`200)`20`7C`7C`20(P.opcode`20>=`20NOP)`20)`20`7B`09/*
  290. V`20bad`20opcode`20*/
  291. X`09`09message("ERROR:`20bad`20opcode`20for`20game`20%s:`20%d`20--`20ignored.
  292. V`5Cn",
  293. X`09`09`09P.gameid,P.opcode);
  294. X`09`09continue;
  295. X`09`09`7D
  296. X`09if`20(P.gameptr`20!=`20NULL)`20`7B
  297. X`09`09P.gameptr->seq`20+=`202;`09/*`20bump`20sequence`20number`20*/
  298. X`09`09P.gameptr->lastacc`20=`20time(`20(long`20*)`200);`09/*set`20lastacc*/
  299. X`09`09if`20(P.timestamp`20>`20P.gameptr->lastacc)
  300. X`09`09`09P.gameptr->lastacc`20=`20P.timestamp;
  301. X`09`09if`20(P.gameptr->opcmt`20!=`20NULL)
  302. X`09`09`09free(P.gameptr->opcmt);`09/*`20discard`20old`20comment`20*/
  303. X`09`09P.gameptr->opcmt`20=`20P.comment;`09/*`20copy`20new`20comment`20*/
  304. X`09`09if`20(P.gameptr->opcmt2`20!=`20NULL)
  305. X`09`09`09free(P.gameptr->opcmt2);/*`20discard`20old`20comment`20*/
  306. X`09`09P.gameptr->opcmt2`20=`20P.comment2;`09/*`20copy`20new`20comment`20*/
  307. X`09`09P.gameptr->opver`20=`20P.version;`09/*in`20case`20he`20changed`20version
  308. Vs*/
  309. X`09`09`7D
  310. X`09if`20(P.gameptr`20==`20NULL)`09`09/*`20everything`20after`20here`20needs
  311. V`20ptr`20*/
  312. X`09`09return(1);`09`09/*`20to`20game`20structure`20*/
  313. X
  314. X`09`09`09/*`20any`201.1`20features`20used`20with`201.0?`20*/
  315. X`09if`20(`20(P.version`20<`20110)`20`26`26`20(`20(P.gameptr->mtotal`20>`200)
  316. V`20`7C`7C
  317. X`09`20`20`20`20`20(P.gameptr->flags`20`26`20(F_JACOBY`7CF_CRAWFORD`7CF_PERM
  318. V`7CF_EUROPE)))`20)`20`7B
  319. X`09`09message("Warning:`20in`20game`20%s:`5Cn",P.gameid);
  320. X`09`09message(
  321. X"The`20following`20features`20are`20not`20supported`20by`20your`20opponent's
  322. V`20version`20of`20ldb:`5Cn"
  323. X`09`09);
  324. X`09`09if`20(P.gameptr->flags`20`26`20F_JACOBY)
  325. X`09`09`09message("`5CtJacoby`20rule.`5Cn");
  326. X`09`09if`20(P.gameptr->flags`20`26`20F_CRAWFORD)
  327. X`09`09`09message("`5CtCrawford`20rule.`5Cn");
  328. X`09`09if`20(P.gameptr->flags`20`26`20F_EUROPE)
  329. X`09`09`09message("`5CtEuropean`20rule.`5Cn");
  330. X`09`09if`20(P.gameptr->flags`20`26`20F_PERM)
  331. X`09`09`09message("`5CtPermanent`20games.`5Cn");
  332. X`09`09if`20(P.gameptr->mtotal`20>`200)
  333. X`09`09`09message("`5CtMatch`20play.`5Cn");
  334. X`09`09P.gameptr->flags`20`26=
  335. X`09`09`09`7E(F_CRAWFORD`7CF_JACOBY`7CF_PERM`7CF_EUROPE);
  336. X`09`09P.gameptr->mtotal`20=`200;
  337. X`09`09message(
  338. X"This`20game`20will`20continue`20as`20if`20those`20features`20had`20not`20been
  339. V`20used.`5Cn");
  340. X`09`09`7D
  341. X`09if`20(`20(P.version`20<`20130)`20`26`26`20(P.gameptr->flags`20`26`20F_CRAWF
  342. VORD)`20)
  343. X`09`09message(
  344. X"Warning:`20opponent`20using`20pre-1.3`20ldb`20--`20using`20Crawford`20rule
  345. V`20not`20recommended!`5Cn"
  346. X`09`09);
  347. X`09return(1);`09`09`09/*`20return`20success`20*/
  348. X`09`7D
  349. Xreturn(0);`09`09/*`20return`20this`20to`20mean`20end`20of`20file`20*/
  350. X`7D
  351. $ call unpack READMAIL.C;1 2020022216 ""
  352. $!
  353. $ create 'f'
  354. X/*`09rename.c`09`094/10/92
  355. X`20*
  356. X`20*`20Copyright`201991`20`20Perry`20R.`20Ross
  357. X`20*
  358. X`20*`20Permission`20to`20use,`20copy,`20modify,`20and`20distribute`20this`20so
  359. Vftware`20and`20its
  360. X`20*`20documentation`20without`20fee`20is`20hereby`20granted,`20subject`20to
  361. V`20the`20restrictions
  362. X`20*`20detailed`20in`20the`20README`20file,`20which`20is`20included`20here`20b
  363. Vy`20reference.
  364. X`20*`20Any`20other`20use`20requires`20written`20permission`20from`20the`20auth
  365. Vor.`20`20This`20software
  366. X`20*`20is`20distributed`20"as`20is"`20without`20any`20warranty,`20including
  367. V`20any`20implied
  368. X`20*`20warranties`20of`20merchantability`20or`20fitness`20for`20a`20particular
  369. V`20purpose.
  370. X`20*`20The`20author`20shall`20not`20be`20liable`20for`20any`20damages`20result
  371. Ving`20from`20the
  372. X`20*`20use`20of`20this`20software.`20`20By`20using`20this`20software,`20the
  373. V`20user`20agrees
  374. X`20*`20to`20these`20terms.
  375. X`20*/
  376. X
  377. X#include`20"ldb.h"
  378. X
  379. X
  380. X/*----------------------------------------------------------------------
  381. X`20*`09rename`20--`20rename`20a`20file
  382. X`20*
  383. X`20*`20This`20function`20may`20be`20used`20to`20rename`20a`20file`20if`20the
  384. V`20local`20C`20library
  385. X`20*`20does`20not`20have`20such`20a`20function.`20`20It`20uses`20the`20link
  386. V`20and`20unlink`20calls,
  387. X`20*`20which`20should`20be`20on`20all`20systems,`20but`20is`20not`20atomic`20l
  388. Vike`20real`20rename's.
  389. X`20*`20This`20rename`20is`20not`20meant`20for`20general`20use,`20as`20it`20doe
  390. Vs`20no`20error`20checking.
  391. X`20*`20Since`20it`20is`20only`20used`20to`20save`20the`20old`20copy`20of`20a
  392. V`20file`20as`20a`20backup,
  393. X`20*`20it`20is`20better`20to`20fail`20silently`20than`20complain`20about`20som
  394. Vething`20the`20user
  395. X`20*`20can't`20do`20anything`20about`20anyway.`20`20The`20old`20file`20is`20al
  396. Vways`20unlinked,
  397. X`20*`20even`20if`20the`20link`20to`20the`20new`20name`20succeeded,`20since`20t
  398. Vhe`20next`20thing
  399. X`20*`20we`20are`20going`20to`20do`20is`20write`20over`20the`20old`20file`20any
  400. Vway.
  401. X`20*----------------------------------------------------------------------
  402. X`20*/
  403. X
  404. Xrename(old,new)
  405. Xchar`20*old;
  406. Xchar`20*new;
  407. X`7B
  408. X
  409. Xunlink(new);`09`09/*`20get`20rid`20of`20existing`20file`20named`20NEW`20*/
  410. Xlink(old,new);`09`09/*`20make`20link`20from`20OLD`20to`20NEW`20*/
  411. Xunlink(old);`09`09/*`20get`20rid`20of`20link`20to`20OLD`20*/
  412. X`7D
  413. $ call unpack RENAME.C;1 1866027360 ""
  414. $!
  415. $ create 'f'
  416. X/*`09r_xrand.c
  417. X`20*/
  418. X
  419. X#include`20"ldb.h"
  420. X#include`20<math.h>
  421. X
  422. Xlong`20rnd_ri();
  423. X
  424. X/*======================================================================
  425. X`20*`20This`20file`20contains`20the`20random`20number`20generator.`20`20It`20i
  426. Vs`20accessed`20by
  427. X`20*`20calling`20RandomInit`20with`20a`2032-bit`20seed,`20then`20calling`20Rol
  428. Vldie()`20to
  429. X`20*`20generate`20integers`20in`20the`20range`20`5B1-6`5D.`20`20This`20particu
  430. Vlar`20implementation
  431. X`20*`20was`20taken`20from`20xrand`20in`20volume`202`20of`20comp.sources.misc.
  432. V`20`20It`20was
  433. X`20*`20written`20by`20Andreas`20Nowatzyk,`20then`20of`20Carnegie-Mellon`20Univ
  434. Versity,`20and
  435. X`20*`20is`20reproduced`20here`20essentially`20unchanged`20(although`20I`20omit
  436. Vted
  437. X`20*`20all`20routines`20but`20the`20ones`20needed`20for`20integers`20in`20`5B1
  438. V-6`5D).
  439. X`20*`20It`20is`20used`20by`20permission`20of`20the`20author,`20whose`20origina
  440. Vl`20copyright
  441. X`20*`20and`20permission-to-use`20appears`20below.
  442. X`20*======================================================================
  443. X`20*/
  444. X
  445. X
  446. X
  447. X/*----------------------------------------------------------------------
  448. X`20*`09Rolldie`20--`20return`20a`20random`20number`20between`201`20and`206
  449. X`20*----------------------------------------------------------------------
  450. X`20*/
  451. X
  452. XRolldie()
  453. X`7B
  454. X
  455. Xreturn(`20(`20(int)`20rnd_ri(6L)`20)`20+`201);
  456. X`7D
  457. X
  458. X
  459. X
  460. X/*----------------------------------------------------------------------
  461. X`20*`09RandomInit`20--`20initialize`20the`20random`20number`20generator
  462. X`20*
  463. X`20*`20This`20function`20should`20be`20called`20once,`20before`20Rolldie`20is
  464. V`20called.
  465. X`20*`20The`20seed`20may`20be`20any`20more`20or`20less`20random`20number;`20the
  466. V`20time`20seems
  467. X`20*`20to`20be`20conventional.
  468. X`20*----------------------------------------------------------------------
  469. X`20*/
  470. X
  471. XRandomInit(seed)
  472. Xlong`20seed;
  473. X`7B
  474. X
  475. Xrnd_init(seed);
  476. X`7D
  477. X
  478. X
  479. X
  480. X/*----------------------------------------------------------------------
  481. X`20*From:`09EMF780::WINS%"Andreas.Nowatzyk@eng.sun.com"`2020-NOV-1991`2017:50:
  482. V38.73
  483. X`20*To:`09ROSS
  484. X`20*CC:`09
  485. X`20*Subj:`09Re:`20`20xrand
  486. X`20*
  487. X`20*Return-Path:`20<Andreas.Nowatzyk@eng.sun.com>
  488. X`20*Received:`20from`20everest.den.mmc.com`20by`20emf780.den.mmc.com`20with
  489. V`20SMTP`20;`20
  490. X`20*`20`20`20`20`20`20`20`20`20`20Wed,`2020`20Nov`2091`2016:50:29`20MDT
  491. X`20*Received:`20from`20Sun.COM`20by`20everest.den.mmc.com`20(4.1/1.34.a)
  492. X`20*`09id`20AA27426;`20Wed,`2020`20Nov`2091`2017:52:10`20MST
  493. X`20*Received:`20from`20Eng.Sun.COM`20(zigzag-bb.Corp.Sun.COM)`20by`20Sun.COM
  494. V`20(4.1/SMI-4.1)
  495. X`20*`09id`20AA07190;`20Wed,`2020`20Nov`2091`2016:50:53`20PST
  496. X`20*Received:`20from`20bovic.Eng.Sun.COM`20by`20Eng.Sun.COM`20(4.1/SMI-4.1)
  497. X`20*`09id`20AA21474;`20Wed,`2020`20Nov`2091`2016:49:44`20PST
  498. X`20*Received:`20by`20bovic.Eng.Sun.COM`20(4.1/SMI-4.1)
  499. X`20*`09id`20AA02462;`20Wed,`2020`20Nov`2091`2016:49:16`20PST
  500. X`20*Date:`20Wed,`2020`20Nov`2091`2016:49:16`20PST
  501. X`20*From:`20Andreas.Nowatzyk@eng.sun.com`20(Andreas`20G.`20Nowatzyk)
  502. X`20*Message-Id:`20<9111210049.AA02462@bovic.Eng.Sun.COM>
  503. X`20*To:`20ross@emf780.den.mmc.com
  504. X`20*Subject:`20Re:`20`20xrand
  505. X`20*
  506. X`20*I`20have`20no`20objection`20to`20distributing`20xrand,`20provided`20that
  507. V`20this`20is`20not`20done
  508. X`20*for`20profit`20and`20that`20the`20source`20is`20credited.
  509. X`20*
  510. X`20*`20`20--`20`20Andreas
  511. X`20*
  512. X`20*PS:`20There`20was`20a`20bug`20is`20some`20version`20of`20xrand.c:`20The
  513. V`20constant`20for`20the`20most
  514. X`20*`20`20`20`20positive`20integer`20(0x7fffffff)`20had`20an`20'f'`20missing
  515. V`20in`20two`20places.`20It`20should
  516. X`20*`20`20`20`20have`20used`20a`20#define`20MAX_INT`20with`20apropriate`20valu
  517. Ve`20instead`20of`20hardwiring
  518. X`20*`20`20`20`20an`20hex-constant.
  519. X`20*----------------------------------------------------------------------
  520. X`20*/
  521. X
  522. X#define`20MAX_INT`200x7fffffff
  523. X
  524. X/*`20Random`20number`20generators:
  525. X`20*
  526. X`20*`20`20rnd_init`20(unsigned`20seed)`20
  527. X`20*`09`09`09:`20initializes`20the`20generator
  528. X`20*
  529. X`20*`20`20rnd_i`20()`09`09:`20returns`20positive`20integers`20`5B0,0x7fffffff
  530. V`5D
  531. X`20*`20`20rnd_ri`20(long`20n)`09:`20returns`20positive`20integers`20`5B0,n-1
  532. V`5D
  533. X`20*
  534. X`20*`20`20Algorithm`20M`20as`20describes`20in`20Knuth's`20"Art`20of`20Computer
  535. V`20Programming",`20Vol`202.`201969
  536. X`20*`20`20is`20used`20with`20a`20linear`20congruential`20generator`20(to`20get
  537. V`20a`20good`20uniform
  538. X`20*`20`20distribution)`20that`20is`20permuted`20with`20a`20Fibonacci`20additi
  539. Vve`20congruential
  540. X`20*`20`20generator`20to`20get`20good`20independence.
  541. X`20*
  542. X`20*`20`20Bit,`20byte,`20and`20word`20distributions`20were`20extensively`20tes
  543. Vted`20and`20pass
  544. X`20*`20`20Chi-squared`20test`20near`20perfect`20scores`20(>7E8`20numbers`20tes
  545. Vted,`20Uniformity
  546. X`20*`20`20assumption`20holds`20with`20probability`20>`200.999)
  547. X`20*
  548. X`20*`20`20Run-up`20tests`20for`20on`207E8`20numbers`20confirm`20independence
  549. V`20with
  550. X`20*`20`20probability`20>`200.97.
  551. X`20*
  552. X`20*`20`20Plotting`20random`20points`20in`202d`20reveals`20no`20apparent`20str
  553. Vucture.
  554. X`20*
  555. X`20*`20`20Autocorrelation`20on`20sequences`20of`205E5`20numbers`20(A(i)`20=
  556. V`20SUM`20X(n)*X(n-i),`20i=1..512)
  557. X`20*`20`20results`20in`20no`20obvious`20structure`20(A(i)`20`7E`20const).
  558. X`20*
  559. X`20*`20`20On`20a`20SUN`203/60,`20rnd_u()`20takes`20about`2019.4`20usec`20per
  560. V`20call,`20which`20is`20about`2044%
  561. X`20*`20`20slower`20than`20Berkeley's`20random()`20(13.5`20usec/call).
  562. X`20*
  563. X`20*`20`20Except`20for`20speed`20and`20memory`20requirements,`20this`20generat
  564. Vor`20outperforms
  565. X`20*`20`20random()`20for`20all`20tests.`20(random()`20scored`20rather`20low
  566. V`20on`20uniformity`20tests,
  567. X`20*`20`20while`20independence`20test`20differences`20were`20less`20dramatic).
  568. V
  569. X`20*
  570. X`20*`20`20Thanks`20to`20M.Mauldin,`20H.Walker,`20J.Saxe`20and`20M.Molloy`20for
  571. V`20inspiration`20`26`20help.
  572. X`20*
  573. X`20*`20`20(c)`20Copyright`201988`20by`20A.`20Nowatzyk
  574. X`20*
  575. X`20*/
  576. X
  577. X/*`20LC-parameter`20selection`20follows`20recommendations`20in`20
  578. X`20*`20"Handbook`20of`20Mathematical`20Functions"`20by`20Abramowitz`20`26`20St
  579. Vegun`2010th,`20edi.
  580. X`20*/
  581. X#define`20LC_A`2066049L`09`09`20`20`20`20/*`20=`20251`5E2,`20`7E=`20sqrt(2`5E3
  582. V2)`09`09`09*/
  583. X#define`20LC_C`20390786457L`09/*`20result`20of`20a`20long`20trial`20`26`20erro
  584. Vr`20series`20`20`20`20*/
  585. X
  586. X/*
  587. X#define`20Xrnd(x)`20(long)((long)x`20*`20LC_A`20+`20LC_C)`20*/`20`20`20/*`20th
  588. Ve`20LC`20polynomial`09`09`09*/
  589. X`09`09`09
  590. Xstatic`20unsigned`20long`20Fib`5B55`5D;`09`20`20`20`20/*`20will`20use`20X(n)
  591. V`20=`20X(n-55)`20-`20X(n-24)`09*/
  592. Xstatic`20int`20Fib_ind;`09`09`20`20`20`20/*`20current`20index`20in`20circular
  593. V`20buffer`09`09*/
  594. Xstatic`20unsigned`20long`20Xrnd_var;`09`20`20`20`20/*`20LCA`20-`20recurrence
  595. V`20variable`09`09*/
  596. Xstatic`20unsigned`20long`20auxtab`5B256`5D;`20`20`20/*`20temporal`20permutatio
  597. Vn`20table`09`09*/
  598. Xstatic`20unsigned`20long`20prmtab`5B64`5D`20=`20`7B`20/*`20spatial`20permutati
  599. Von`20table`09`09*/
  600. X`20`20`20`200xffffffff,`200x00000000,`20`200x00000000,`20`200x00000000,`20`20/
  601. V*`203210`20*/
  602. X`20`20`20`200x0000ffff,`200x00ff0000,`20`200x00000000,`20`200xff000000,`20`20/
  603. V*`202310`20*/
  604. X`20`20`20`200xff0000ff,`200x0000ff00,`20`200x00000000,`20`200x00ff0000,`20`20/
  605. V*`203120`20*/
  606. X`20`20`20`200x00ff00ff,`200x00000000,`20`200xff00ff00,`20`200x00000000,`20`20/
  607. V*`201230`20*/
  608. X
  609. X`20`20`20`200xffff0000,`200x000000ff,`20`200x00000000,`20`200x0000ff00,`20`20/
  610. V*`203201`20*/
  611. X`20`20`20`200x00000000,`200x00ff00ff,`20`200x00000000,`20`200xff00ff00,`20`20/
  612. V*`202301`20*/
  613. X`20`20`20`200xff000000,`200x00000000,`20`200x000000ff,`20`200x00ffff00,`20`20/
  614. V*`203102`20*/
  615. X`20`20`20`200x00000000,`200x00000000,`20`200x00000000,`20`200xffffffff,`20`20/
  616. V*`202103`20*/
  617. X
  618. X`20`20`20`200xff00ff00,`200x00000000,`20`200x00ff00ff,`20`200x00000000,`20`20/
  619. V*`203012`20*/
  620. X`20`20`20`200x0000ff00,`200x00000000,`20`200x00ff0000,`20`200xff0000ff,`20`20/
  621. V*`202013`20*/
  622. X`20`20`20`200x00000000,`200x00000000,`20`200xffffffff,`20`200x00000000,`20`20/
  623. V*`201032`20*/
  624. X`20`20`20`200x00000000,`200x0000ff00,`20`200xffff0000,`20`200x000000ff,`20`20/
  625. V*`201023`20*/
  626. X
  627. X`20`20`20`200x00000000,`200xffffffff,`20`200x00000000,`20`200x00000000,`20`20/
  628. V*`200321`20*/
  629. X`20`20`20`200x00ffff00,`200xff000000,`20`200x00000000,`20`200x000000ff,`20`20/
  630. V*`200213`20*/
  631. X`20`20`20`200x00000000,`200xff000000,`20`200x0000ffff,`20`200x00ff0000,`20`20/
  632. V*`200132`20*/
  633. X`20`20`20`200x00000000,`200xff00ff00,`20`200x00000000,`20`200x00ff00ff`20`20
  634. V`20/*`200123`20*/
  635. X`7D;
  636. X
  637. Xunion`20hack`20`7B`09`09`09`20`20`20`20/*`20used`20to`20access`20doubles`20as
  638. V`20unsigneds`09*/
  639. X`20`20`20`20double`20d;
  640. X`20`20`20`20unsigned`20long`20u`5B2`5D;
  641. X`7D;
  642. X
  643. Xstatic`20union`20hack`20man;`09`09`20`20`20`20/*`20mantissa`20bit`20vector`09
  644. V`09`09*/
  645. X
  646. Xlong`20Xrnd(x)`20
  647. Xlong`20x;
  648. X`7B
  649. X`09long`20tmp;
  650. X
  651. X`09tmp`20=`20x*`20LC_A;
  652. X`09tmp`20+=`20LC_C;
  653. X`09return`20tmp;
  654. X`7D
  655. X
  656. Xrnd_init`20(seed)`09`09`09`20`20`20`20/*`20modified:`20seed`200-31`20use`20pre
  657. Vcomputed`20stuff`20*/
  658. X`20`20`20`20unsigned`20seed;
  659. X`7B
  660. X`20`20`20`20register`20unsigned`20long`20u;
  661. X`20`20`20`20register`20int`20i;
  662. X`20`20`20`20double`20x,`20y;
  663. X`20`20`20`20union`20hack`20t;
  664. X
  665. X`20`20`20`20static`20unsigned`20seed_tab`5B32`5D`20=`20`7B
  666. X`09`090xbdcc47e5,`200x54aea45d,`200xec0df859,`200xda84637b,
  667. X`09`090xc8c6cb4f,`200x35574b01,`200x28260b7d,`200x0d07fdbf,
  668. X`09`090x9faaeeb0,`200x613dd169,`200x5ce2d818,`200x85b9e706,
  669. X`09`090xab2469db,`200xda02b0dc,`200x45c60d6e,`200xffe49d10,
  670. X`09`090x7224fea3,`200xf9684fc9,`200xfc7ee074,`200x326ce92a,
  671. X`09`090x366d13b5,`200x17aaa731,`200xeb83a675,`200x7781cb32,
  672. X`09`090x4ec7c92d,`200x7f187521,`200x2cf346b4,`200xad13310f,
  673. X`09`090xb89cff2b,`200x12164de1,`200xa865168d,`200x32b56cdf`20`20`7D;
  674. X
  675. X`20`20`20`20if`20(seed`20<`2032)
  676. X`09u`20=`20seed_tab`5Bseed`5D;
  677. X`20`20`20`20else
  678. X`09u`20=`20seed`20`5E`20seed_tab`5Bseed`20`26`2031`5D;
  679. X
  680. X`20`20`20`20for`20(i`20=`2055;`20i--;)`09`09`20`20`20`20/*`20set`20up`20Fibona
  681. Vcci`20additive`20congruential`09*/
  682. X`09Fib`5Bi`5D`20=`20u`20=`20Xrnd(u);
  683. X
  684. X`20`20`20`20for`20(i`20=`20256;`20i--;)
  685. X`09auxtab`5Bi`5D`20=`20u`20=`20Xrnd(u);
  686. X
  687. X`20`20`20`20Fib_ind`20=`20u`20%`2055;`09`09`20`20`20`20/*`20select`20a`20start
  688. Ving`20point`09`09`09*/
  689. X
  690. X`20`20`20`20Xrnd_var`20=`20u;
  691. X
  692. X`09/*`09The`20following`20tests`20to`20see`20if`20a`20double`20fits`20into
  693. X`09`092`20unsigned`20longs.`20`20If`20not,`20rnd_01d`20will`20not`20work
  694. X`09`09on`20this`20machine.`09`09`09`09`09*/
  695. X
  696. X`20`20`20`20if`20(sizeof(x)`20!=`202`20*`20sizeof(unsigned`20long))
  697. X`09fatal("ERROR:`20xrand`20roller`20does`20not`20work`20on`20this`20machine!")
  698. V;
  699. X
  700. X`20`20`20`20x`20=`201.0;
  701. X`20`20`20`20y`20=`200.5;
  702. X`20`20`20`20do`20`7B`09`09`09`20`20`20`20/*`20find`20largest`20fp-number`20<
  703. V`202.0`09*/
  704. X`09t.d`20=`20x;
  705. X`09x`20+=`20y;
  706. X`09y`20*=`200.5;
  707. X`20`20`20`20`7D`20while`20(x`20!=`20t.d`20`26`26`20x`20<`202.0);
  708. X
  709. X`20`20`20`20man.d`20=`201.0;
  710. X`20`20`20`20man.u`5B0`5D`20`5E=`20t.u`5B0`5D;
  711. X`20`20`20`20man.u`5B1`5D`20`5E=`20t.u`5B1`5D;`09`09`20`20`20`20/*`20man`20is
  712. V`20now`201`20for`20each`20mantissa`20bit`09*/
  713. X`7D
  714. X
  715. Xlong`20rnd_i`20()
  716. X/*
  717. X`20*`20returns`20a`20positive,`20uniformly`20distributed`20random`20number`20i
  718. Vn`20`5B0,0x7fffffff`5D
  719. X`20*/
  720. X`7B`20
  721. X`20`20`20`20register`20unsigned`20long`20i,`20j,`20*t`20=`20Fib;
  722. X
  723. X`20`20`20`20i`20=`20Fib_ind;
  724. X`20`20`20`20j`20=`20t`5Bi`5D;`09`09`09`09`20`20`20`20/*`20=`20X(n-55)`20*/
  725. X`20`20`20`20j`20-=`20(i`20>=`2024)`20?`20t`5Bi`20-`2024`5D`20:`20t`5Bi`20+`202
  726. V1`5D;`20/*`20=`20X(n-24)`20*/
  727. X`20`20`20`20t`5Bi`5D`20=`20j;
  728. X`20`20`20`20if`20(++i`20>=`2055)`20i`20=`200;
  729. X`20`20`20`20Fib_ind`20=`20i;
  730. X
  731. X`20`20`20`20t`20=`20`26auxtab`5B(j`20>>`2024)`20`26`200xff`5D;
  732. X`20`20`20`20i`20=`20*t;
  733. X`20`20`20`20Xrnd_var`20=`20*t`20=`20Xrnd(Xrnd_var);
  734. X`20`20`20`20t`20=`20`26prmtab`5Bj`20`26`200x3c`5D;
  735. X
  736. X`20`20`20`20j`20=`20`20*t++`20`26`20i;
  737. X`20`20`20`20j`20`7C=`20*t++`20`26`20((i`20<<`2024)`20`7C`20((i`20>>`20`208)
  738. V`20`26`200x00ffffff));
  739. X`20`20`20`20j`20`7C=`20*t++`20`26`20((i`20<<`2016)`20`7C`20((i`20>>`2016)`20
  740. V`26`200x0000ffff));
  741. X`20`20`20`20j`20`7C=`20*t`20`20`20`26`20((i`20<<`20`208)`20`7C`20((i`20>>`2024
  742. V)`20`26`200x000000ff));
  743. X`20`20`20`20
  744. X`20`20`20`20return`20j`20`26`20MAX_INT;
  745. X`7D
  746. X
  747. X
  748. Xlong`20rnd_ri`20(rng)
  749. X`20`20`20`20long`20rng;
  750. X/*
  751. X`20*`20randint:`20Return`20a`20random`20integer`20in`20a`20given`20Range`20
  752. V`5B0..rng-1`5D
  753. X`20*`20`20`20`20`20`20`20`20`20`20Note:`20`200`20<`20rng
  754. X`20*/
  755. X`7B
  756. X`20`20`20`20register`20unsigned`20long`20`20r,`20a;
  757. X
  758. X`20`20`20`20do`20`7B
  759. X`09r`20=`20rnd_i();
  760. X`09a`20=`20(r`20/`20rng)`20+`201;
  761. X`09a`20*=`20rng;
  762. X`20`20`20`20`7D`20while`20(a`20>=`20MAX_INT);
  763. X`20`20`20`20
  764. X`20`20`20`20a--;
  765. X`20`20`20`20return`20a`20-`20r;
  766. X`7D
  767. $ call unpack R_XRAND.C;1 172441753 ""
  768. $!
  769. $ create 'f'
  770. X/*`09save.c`09`098/4/91
  771. X`20*
  772. X`20*`20Copyright`201991`20`20Perry`20R.`20Ross
  773. X`20*
  774. X`20*`20Permission`20to`20use,`20copy,`20modify,`20and`20distribute`20this`20so
  775. Vftware`20and`20its
  776. X`20*`20documentation`20without`20fee`20is`20hereby`20granted,`20subject`20to
  777. V`20the`20restrictions
  778. X`20*`20detailed`20in`20the`20README`20file,`20which`20is`20included`20here`20b
  779. Vy`20reference.
  780. X`20*`20Any`20other`20use`20requires`20written`20permission`20from`20the`20auth
  781. Vor.`20`20This`20software
  782. X`20*`20is`20distributed`20"as`20is"`20without`20any`20warranty,`20including
  783. V`20any`20implied
  784. X`20*`20warranties`20of`20merchantability`20or`20fitness`20for`20a`20particular
  785. V`20purpose.
  786. X`20*`20The`20author`20shall`20not`20be`20liable`20for`20any`20damages`20result
  787. Ving`20from`20the
  788. X`20*`20use`20of`20this`20software.`20`20By`20using`20this`20software,`20the
  789. V`20user`20agrees
  790. X`20*`20to`20these`20terms.
  791. X`20*/
  792. X
  793. X#include`20"ldb.h"
  794. X
  795. X/*===========================================================================
  796. X`20*`20This`20file`20contains`20miscellaneous`20functions`20that`20save`20and
  797. V`20load`20things.
  798. X`20*==========================================================================
  799. V=
  800. X`20*/
  801. X
  802. X/*---------------------------------------------------------------------------
  803. X`20*`09save`20--`20make`20a`20copy`20of`20a`20string
  804. X`20*
  805. X`20*`20This`20function`20makes`20a`20copy`20of`20a`20string`20in`20malloc`20me
  806. Vmory,`20and`20returns
  807. X`20*`20a`20pointer`20to`20the`20copy.
  808. X`20*--------------------------------------------------------------------------
  809. V-
  810. X`20*/
  811. X
  812. Xchar`20*save(s)
  813. Xchar`20*s;
  814. X`7B
  815. Xchar`20*n;
  816. X
  817. Xif`20(s`20==`20NULL)`20`7B
  818. X`09return(NULL);
  819. X`09`7D
  820. Xelse`20`7B
  821. X`09if`20(`20(n`20=`20calloc(strlen(s)+1,1))`20==`20NULL)
  822. X`09`09fatal("Out`20of`20memory!");
  823. X`09strcpy(n,s);
  824. X`09return(n);
  825. X`09`7D
  826. X`7D
  827. X
  828. X
  829. X/*---------------------------------------------------------------------------
  830. X`20*`09readldbrc`20--`20read`20in`20the`20.ldbrc`20file
  831. X`20*
  832. X`20*`20This`20function`20reads`20the`20.ldbrc`20file,`20which`20contains`20the
  833. V`20setup`20info
  834. X`20*`20for`20this`20user.`20`20If`20the`20HOME`20environment`20variable`20is
  835. V`20set,`20we`20chdir`20to`20it.
  836. X`20*`20If`20the`20LDBRC`20environment`20variable`20is`20set,`20it`20is`20used
  837. V`20as`20the`20file`20to
  838. X`20*`20read`20instead`20of`20.ldbrc.
  839. X`20*--------------------------------------------------------------------------
  840. V-
  841. X`20*/
  842. X
  843. Xreadldbrc()
  844. X`7B
  845. XFILE`20*fp;
  846. Xchar`20*s,`20*n,`20*getenv();
  847. Xchar`20buf`5B80`5D;
  848. X
  849. Xif`20(`20(s`20=`20getenv("HOME"))`20!=`20NULL)`09/*`20if`20we`20have`20a`20hom
  850. Ve`20*/
  851. X`09chdir(s);`09`09`09/*`20go`20there`20*/
  852. Xif`20(`20(s`20=`20getenv("LDBRC"))`20==`20NULL)`09/*`20LDBRC`20not`20set`20*/
  853. X#ifdef`20VMS
  854. X`09s`20=`20"ldb.rc";`09`09`09/*`20use`20default`20file`20name`20*/
  855. X#else
  856. X`09s`20=`20".ldbrc";`09`09`09/*`20use`20default`20file`20name`20*/
  857. X#endif
  858. Xif`20(`20(fp`20=`20fopen(s,"r"))`20==`20NULL)`20`7B
  859. X`09printf("'%s'`20does`20not`20exist.`20`20Do`20you`20want`20to`20create`20it?
  860. V",s);
  861. X`09if`20(`20(fgets(buf,sizeof(buf),stdin)`20==`20NULL)
  862. X`09`20`20`20`20`20`7C`7C`20(`20(*buf`20!=`20'y')`20`26`26`20(*buf`20!=`20'Y')
  863. V`20)`20)
  864. X`09`09fatal("ldb`20aborted.");
  865. X`09if`20(`20(fp`20=`20fopen(s,"w"))`20==`20NULL)`20`7B
  866. X`09`09printf("Sorry,`20could`20not`20create`20%s.`5Cn",s);
  867. X`09`09ldbexit(STAT_ABORT);
  868. X`09`09`7D
  869. X`09printf("Please`20enter`20your`20personal`20name:`20");
  870. X`09if`20(fgets(buf,sizeof(buf),stdin)`20==`20NULL)`20`7B
  871. X`09`09fclose(fp);
  872. X`09`09unlink(s);
  873. X`09`09printf("ldb`20aborted.`5Cn");
  874. X`09`09ldbexit(STAT_ABORT);
  875. X`09`09`7D
  876. X`09buf`5Bstrlen(buf)-1`5D`20=`20'`5C0';`09/*`20clobber`20the`20newline`20char
  877. V`20*/
  878. X`09fprintf(fp,"myname=%s`5Cn",buf);
  879. X`09printf("Please`20enter`20your`20e-mail`20address:`20");
  880. X`09if`20(fgets(buf,sizeof(buf),stdin)`20==`20NULL)`20`7B
  881. X`09`09fclose(fp);
  882. X`09`09unlink(s);
  883. X`09`09printf("ldb`20aborted.`5Cn");
  884. X`09`09ldbexit(STAT_ABORT);
  885. X`09`09`7D
  886. X`09buf`5Bstrlen(buf)-1`5D`20=`20'`5C0';`09/*`20clobber`20the`20newline`20char
  887. V`20*/
  888. X`09fprintf(fp,"myaddr=%s`5Cn",buf);
  889. X`09fprintf(fp,"mailfile=ldb.rcv`5Cn");
  890. X#ifdef`20VMS
  891. X`09fprintf(fp,"sendcmd=mail/subject=`5C"$s`5C"`20$f`20IN%%`5C"`5C"`5C"$a`5C"
  892. V`5C"`5C"`5Cn");
  893. X`09fprintf(fp,"gamefile=ldb.data`5Cn");
  894. X`09fprintf(fp,"backupfile=ldb.olddata`5Cn");
  895. X`09fprintf(fp,"peoplefile=ldb.people`5Cn");
  896. X`09fprintf(fp,"lockfile=ldb.lock`5Cn");
  897. X`09fprintf(fp,"supercmd=`5Cn");
  898. X#else
  899. X`09fprintf(fp,"sendcmd=mail`20-s`20'$s'`20$a`20<`20$f`5Cn");
  900. X`09fprintf(fp,"gamefile=.ldbdata`5Cn");
  901. X`09fprintf(fp,"backupfile=.oldldbdata`5Cn");
  902. X`09fprintf(fp,"peoplefile=.ldbpeople`5Cn");
  903. X`09fprintf(fp,"lockfile=.ldb_lock`5Cn");
  904. X`09fprintf(fp,"supercmd=sh`5Cn");
  905. X#endif
  906. X`09fprintf(fp,"delmail=no`5Cn");
  907. X`09fprintf(fp,"tempfile=ldb.tmp`5Cn");
  908. X`09fprintf(fp,"colors=rw`5Cn");
  909. X`09fprintf(fp,"direction=up`5Cn");
  910. X`09fprintf(fp,"initialboard=current`5Cn");
  911. X`09fprintf(fp,"autoroll=yes`5Cn");
  912. X`09fprintf(fp,"automove=no`5Cn");
  913. X`09fprintf(fp,"autodouble=0`5Cn");
  914. X`09fprintf(fp,"superkey=0`5Cn");
  915. X`09fprintf(fp,"checkpoint=yes`5Cn");
  916. X`09fprintf(fp,"timeout=7`5Cn");
  917. X`09fprintf(fp,"keepold=7`5Cn");
  918. X`09fprintf(fp,"debug=0`5Cn");
  919. X`09fclose(fp);
  920. X`09printf("`5CnYour`20%s`20file`20was`20created.`20`20You`20may`20want`20to
  921. V`20read`20the`5Cn",s);
  922. X`09printf("manual`20for`20additional`20options`20available`20in`20this`20file.
  923. V`5Cn`5Cn");
  924. X#ifdef`20VMS
  925. X`09printf("VMS`20users`20should`20edit`20the`20sendcmd`20command`20in`20%s`20t
  926. Vo`20use`5Cn",s);
  927. X`09printf("the`20appropriate`20mailer.`20`20The`20IN%%`20mailer`20is`20assumed
  928. V.`5Cn`5Cn");
  929. X#endif
  930. X`09if`20(`20(fp`20=`20fopen(s,"r"))`20==`20NULL)`20`7B
  931. X`09`09printf("I`20can't`20re-open`20your`20%s`20file!`5Cn",s);
  932. X`09`09ldbexit(STAT_ABORT);
  933. X`09`09`7D
  934. X`09`7D
  935. Xrc.myname`20=`20NULL;`09`09/*`20these`20fields`20are`20required`20*/
  936. Xrc.myaddr`20=`20NULL;
  937. X#ifdef`20VMS
  938. Xrc.gfile`20=`20"ldb.data";`09/*`20default`20game`20storage`20file`20*/
  939. Xrc.pfile`20=`20"ldb.people";`09/*`20default`20opponent`20file`20*/
  940. Xrc.gbackup`20=`20"ldb.olddata";`09/*`20game`20backup`20file`20*/
  941. Xrc.sendcmd`20=`20"mail/subject=`5C"$s`5C"`20$f`20IN%%`5C"`5C"`5C"$a`5C"`5C"
  942. V`5C"";`20/*`20dflt`20mail`20cmd`20*/
  943. Xrc.lockfile`20=`20"ldb.lock";`09/*`20default`20lock`20file`20*/
  944. Xrc.supercmd`20=`20"";`09`09/*`20command`20to`20run`20when`20we`20have`20compan
  945. Vy`20*/
  946. X#else
  947. Xrc.gfile`20=`20".ldbdata";`09/*`20default`20game`20storage`20file`20*/
  948. Xrc.pfile`20=`20".ldbpeople";`09/*`20default`20opponent`20file`20*/
  949. Xrc.gbackup`20=`20".oldldbdata";`09/*`20game`20backup`20file`20*/
  950. Xrc.sendcmd`20=`20"mail`20-s`20'$s'`20$a`20<`20$f";`09/*`20default`20mail`20com
  951. Vmand`20*/
  952. Xrc.lockfile`20=`20".ldb_lock";`09/*`20default`20lock`20file`20*/
  953. Xrc.supercmd`20=`20"sh";`09`09/*`20command`20to`20run`20when`20we`20have`20comp
  954. Vany`20*/
  955. X#endif
  956. Xrc.mfile`20=`20"ldb.rcv";`09/*`20default`20file`20for`20received`20mail`20*/
  957. Xrc.delmail`20=`20"no";`09`09`09/*`20don't`20delete`20mail`20by`20default`20*/
  958. Xrc.tempfile`20=`20"ldb.tmp";`09`09/*`20default`20temp`20file`20*/
  959. Xrc.defclrs`20=`20"rw";`09`09`09/*`20"default"`20default`20colors`20*/
  960. Xrc.defdir`20=`20"u";`09`09`09/*`20and`20direction`20*/
  961. Xrc.initboard`20=`20"c";`09`09`09/*`20show`20current`20board`20by`20default`20*
  962. V/
  963. Xrc.autoroll`20=`20"y";`09`09`09/*`20enable`20autoroll`20by`20default`20*/
  964. Xrc.automove`20=`20"n";`09`09/*`20disabled`20by`20default`20(it's`20really`20an
  965. Vnoying`20*/
  966. Xrc.autodouble`20=`200;`09`09/*`20disable`20autodouble`20by`20default`20*/
  967. Xrc.superkey`20=`200;`09`09/*`20key`20to`20activate`20supercmd`20(dflt=disabled
  968. V)`20*/
  969. Xrc.chkpt`20=`20"y";`09`09`09/*`20checkpoint`20is`20enabled`20by`20default`20*/
  970. V
  971. Xrc.acctime`20=`207;`09`09`09/*`20access`20timeout`20in`207`20days`20by`20defau
  972. Vlt`20*/
  973. Xrc.keepold`20=`207;`09`09`09/*`20keep`20dead`20games`207`20days`20by`20default
  974. V`20*/
  975. Xrc.debug`20=`200;`09`09`09/*`20default`20to`20no`20debug`20*/
  976. Xif`20(`20(n`20=`20nvscan(fp,nv_rcfile,`26rc))`20!=`20NULL)`20`7B
  977. X`09fprintf(stderr,"Invalid`20line`20in`20.ldbrc:`20%s`5Cn",n);
  978. X`09ldbexit(STAT_ABORT);
  979. X`09`7D
  980. Xfclose(fp);
  981. Xif`20(rc.myname`20==`20NULL)`20`7B
  982. X`09fprintf(stderr,"ERROR:`20missing`20'myname'`20line`20in`20%s`5Cn",s);
  983. X`09ldbexit(STAT_ABORT);
  984. X`09`7D
  985. Xif`20(rc.myaddr`20==`20NULL)`20`7B
  986. X`09fprintf(stderr,"ERROR:`20missing`20'myaddr'`20line`20in`20%s`5Cn",s);
  987. X`09ldbexit(STAT_ABORT);
  988. X`09`7D
  989. Xif`20(`20(strlen(rc.defclrs)`20!=`202)`20`7C`7C`20(!`20isalpha(rc.defclrs`5B0
  990. V`5D))`20`7C`7C
  991. X`20`20`20`20`20(!`20isalpha(rc.defclrs`5B1`5D))`20`7C`7C`20(rc.defclrs`5B0`5D
  992. V`20==`20rc.defclrs`5B1`5D)`20)`20`7B
  993. X`09fprintf(stderr,"ERROR:`20invalid`20color`20string`20in`20%s:`20%s",s,rc.def
  994. Vclrs);
  995. X`09ldbexit(STAT_ABORT);
  996. X`09`7D
  997. Xif`20(strchr("ud",*rc.defdir)`20==`20NULL)`20`7B
  998. X`09fprintf(stderr,"ERROR:`20direction`20must`20be`20'up'`20or`20'down'`20in
  999. V`20%s`5Cn",s);
  1000. X`09ldbexit(STAT_ABORT);
  1001. X`09`7D
  1002. Xif`20(strchr("bac",*rc.initboard)`20==`20NULL)`20`7B
  1003. X`09fprintf(stderr,
  1004. X`09`20"ERROR:`20initialboard`20must`20be`20'before',`20'after',`20or`20'curren
  1005. Vt'`20in`20%s`5Cn"
  1006. X`09`20,s);
  1007. X`09ldbexit(STAT_ABORT);
  1008. X`09`7D
  1009. Xif`20(strchr("yn",*rc.autoroll)`20==`20NULL)`20`7B
  1010. X`09fprintf(stderr,"ERROR:`20autoroll`20must`20be`20'yes'`20or`20'no'`20in`20%s
  1011. V`5Cn",s);
  1012. X`09ldbexit(STAT_ABORT);
  1013. X`09`7D
  1014. Xif`20(strchr("yn",*rc.automove)`20==`20NULL)`20`7B
  1015. X`09fprintf(stderr,"ERROR:`20automove`20must`20be`20'yes'`20or`20'no'`20in`20%s
  1016. V`5Cn",s);
  1017. X`09ldbexit(STAT_ABORT);
  1018. X`09`7D
  1019. Xif`20(strchr("yn",*rc.chkpt)`20==`20NULL)`20`7B
  1020. X`09fprintf(stderr,"ERROR:`20checkpoint`20must`20be`20'yes'`20or`20'no'`20in
  1021. V`20%s`5Cn",s);
  1022. X`09ldbexit(STAT_ABORT);
  1023. X`09`7D
  1024. X`7D
  1025. X
  1026. X
  1027. X/*---------------------------------------------------------------------------
  1028. X`20*`09readgames`20--`20read`20in`20games`20in`20progress
  1029. X`20*
  1030. X`20*`20This`20function`20reads`20the`20games`20file`20specified`20in`20.ldbrc
  1031. V`20and`20loads
  1032. X`20*`20the`20games`20into`20the`20games`20list`20(ghead/gtail).
  1033. X`20*--------------------------------------------------------------------------
  1034. V-
  1035. X`20*/
  1036. X
  1037. Xreadgames()
  1038. X`7B
  1039. XFILE`20*fp;
  1040. Xchar`20c,`20*s;
  1041. Xstruct`20game`20*g;
  1042. Xlong`20old;
  1043. X
  1044. Xreadpeople();
  1045. Xif`20(`20(fp`20=`20fopen(rc.gfile,"r"))`20==`20NULL)
  1046. X`09return;`09`09`09`09/*`20no`20games`20*/
  1047. Xold`20=`20time(`20(long`20*)`200);
  1048. Xif`20(rc.keepold`20>`200)`09`09/*`20if`20keeping`20old`20games`20is`20enabled
  1049. V`20*/
  1050. X`09old`20-=`20(rc.keepold`20*`2086400);`09/*`20how`20old`20is`20too`20old?`20*
  1051. V/
  1052. Xwhile`20(`20(c`20=`20getc(fp))`20!=`20EOF)`20`7B
  1053. X`09ungetc(c,fp);`09/*`20put`20char`20back`20*/
  1054. X`09g`20=`20addgame();`09`09/*`20insert`20a`20new`20game`20*/
  1055. X`09g->opaddr`20=`20NULL;
  1056. X`09g->opname`20=`20NULL;
  1057. X`09g->myaddr`20=`20NULL;
  1058. X`09g->mycmt`20=`20NULL;
  1059. X`09g->mycmt2`20=`20NULL;
  1060. X`09g->opcmt`20=`20NULL;
  1061. X`09g->opcmt2`20=`20NULL;
  1062. X`09g->dispmsg`20=`20NULL;
  1063. X`09g->hiused`20=`200;
  1064. X`09g->maxused`20=`200;
  1065. X`09g->opver`20=`20100;`09`09/*`20default`20to`20version`201.0`20*/
  1066. X`09g->starttime`20=`200L;
  1067. X`09clearmvs(g->opmvs);
  1068. X`09clearmvs(g->mvs);
  1069. X`09if`20(`20(s`20=`20nvscan(fp,nv_gfile,g))`20!=`20NULL)`20`7B`09/*`20read`20g
  1070. Vame`20*/
  1071. X`09`09FeFinishSession();`09/*`20close`20down`20front-end`20*/
  1072. X`09`09TFinishSession();`09/*`20close`20down`20transport`20*/
  1073. X`09`09fprintf(stderr,"ERROR:`20invalid`20line`20in`20%s:`20%s`5Cn",`20rc.gfile
  1074. V,`20s);
  1075. X`09`09ldbexit(STAT_ABORT);
  1076. X`09`09`7D
  1077. X`09if`20(g->gameid`20==`20NULL)`20`7B`09`09/*`20empty`20game`20(prob.`20EOF)
  1078. V`20*/
  1079. X`09`09deletegame(g);
  1080. X`09`09continue;
  1081. X`09`09`7D
  1082. X`09if`20(g->myaddr`20==`20NULL)`09`09/*`20no`20myaddr`20line`20*/
  1083. X`09`09g->myaddr`20=`20save(rc.myaddr);`09/*`20dflt`20to`20ldbrc`20setting`20*/
  1084. V
  1085. X`09if`20(`20(g->ppl`20=`20findppl(g->opaddr,P_ADDR))`20==`20NULL)
  1086. X`09`09newppl(g);
  1087. X`09if`20(
  1088. X`09`20`20`20(g->state`20==`20ST_GAMEOVER)`09`26`26
  1089. X`09`20`20`20(g->flags`20`26`20F_DISPLAYED)`09`26`26
  1090. X`09`20`20`20(g->lastacc`20<`20old)`20)
  1091. X`09`09g->flags`20`7C=`20F_DELETE;`09/*`20game`20is`20old,`20mark`20it`20for
  1092. V`20deletion`20*/
  1093. X`09if`20(rc.debug`20`26`20DB_RWGAMES)
  1094. X`09`09message("DB-readgames:`20read`20game`20%s`5Cn",g->gameid);
  1095. X`09`7D
  1096. Xfclose(fp);
  1097. X`7D
  1098. X
  1099. X
  1100. X/*----------------------------------------------------------------------
  1101. X`20*`09writegames`20--`20save`20the`20game`20list`20to`20a`20file
  1102. X`20*
  1103. X`20*`20This`20function`20writes`20each`20game`20in`20the`20game`20list`20to
  1104. V`20the`20specified
  1105. X`20*`20file.`20`20Games`20with`20F_DELETE`20set`20in`20their`20flags`20are`20s
  1106. Vkipped,`20and
  1107. X`20*`20are`20thus`20effectively`20deleted.`20`20If`20the`20bkup`20arg`20is`20n
  1108. Vot`20NULL`20and
  1109. X`20*`20is`20not`20an`20empty`20string,`20the`20old`20file`20is`20renamed`20to
  1110. V`20the`20string
  1111. X`20*`20specified`20in`20bkup`20before`20the`20new`20file`20is`20created.
  1112. X`20*----------------------------------------------------------------------
  1113. X`20*/
  1114. X
  1115. Xwritegames(file,bkup,pfile)
  1116. Xchar`20*file,`20*bkup,`20*pfile;
  1117. X`7B
  1118. XFILE`20*fp;
  1119. Xstruct`20game`20*g;
  1120. X
  1121. Xwritepeople(pfile);`09`09`09/*`20save`20the`20people`20file`20*/
  1122. Xif`20(`20(bkup`20!=`20NULL)`20`26`26`20(*bkup`20!=`20'`5C0')`20)`20`7B
  1123. X`09unlink(bkup);`09`09/*`20prevent`20multiple`20versions`20on`20VMS`20*/
  1124. X`09rename(file,bkup);`09/*`20save`20old`20game`20file`20*/
  1125. X`09`7D
  1126. Xelse
  1127. X`09unlink(file);`09`09/*`20prevent`20multiple`20versions`20on`20VMS`20*/
  1128. Xif`20(`20(fp`20=`20fopen(file,"w"))`20==`20NULL)`20`7B
  1129. X`09message("ERROR:`20can't`20save`20games`20in`20%s!`5Cn",file);
  1130. X`09return;`09`09`09/*`20should`20do`20something`20to`20save`20games...`20*/
  1131. X`09`7D
  1132. Xfor`20(g`20=`20ghead;`20g`20!=`20NULL;`20g`20=`20g->next)`20`7B
  1133. X`09if`20(g->flags`20`26`20F_DELETE)
  1134. X`09`09continue;`09`09`09/*`20game`20is`20over,`20delete`20it`20*/
  1135. X`09if`20(g->state`20==`20ST_GAMEOVER)
  1136. X`09`09continue;`09`09`09/*`20skip`20complete`20games`204`20now`20*/
  1137. X`09nvwrite(fp,nv_gfile,g);`09/*`20write`20the`20struct`20*/
  1138. X`09if`20(rc.debug`20`26`20DB_RWGAMES)
  1139. X`09`09message("DB-writegames:`20write`20game`20%s`5Cn",g->gameid);
  1140. X`09`7D
  1141. Xfor`20(g`20=`20ghead;`20g`20!=`20NULL;`20g`20=`20g->next)`20`7B
  1142. X`09if`20(g->flags`20`26`20F_DELETE)
  1143. X`09`09continue;`09`09/*`20game`20is`20over,`20delete`20it`20*/
  1144. X`09if`20(g->state`20!=`20ST_GAMEOVER)
  1145. X`09`09continue;`09`09/*`20put`20complete`20games`20@`20end`20*/
  1146. X`09nvwrite(fp,nv_gfile,g);`09/*`20write`20the`20struct`20*/
  1147. X`09if`20(rc.debug`20`26`20DB_RWGAMES)
  1148. X`09`09message("DB-writegames:`20write`20game`20%s`5Cn",g->gameid);
  1149. X`09`7D
  1150. Xfclose(fp);
  1151. X`7D
  1152. X
  1153. X
  1154. X/*---------------------------------------------------------------------------
  1155. X`20*`09readpeople`20--`20read`20in`20the`20people`20file
  1156. X`20*
  1157. X`20*`20This`20function`20reads`20the`20"people"`20file,`20which`20is`20the`20l
  1158. Vist`20of`20all`20of`20the
  1159. X`20*`20opponents`20we`20have`20ever`20played.`20`20The`20people`20file`20serve
  1160. Vs`20three`20purposes:
  1161. X`20*`091.`20It`20keeps`20a`20record`20of`20games/gammons/backgammons/matches
  1162. V`20won/lost
  1163. X`20*`09`20`20`20to`20each`20person.
  1164. X`20*`092.`20It`20stores`20the`20mail`20address`20so`20you`20don't`20have`20to
  1165. V`20remember,
  1166. X`20*`09`20`20`20and`20allows`20you`20to`20assign`20an`20alias`20to`20each`20pe
  1167. Vrson.`20`20This
  1168. X`20*`09`20`20`20alias`20can`20be`20used`20in`20the`20-start`20argument`20inste
  1169. Vad`20of`20the
  1170. X`20*`09`20`20`20mail`20address.
  1171. X`20*`093.`20It`20stores`20the`20"fence"`20time,`20which`20is`20the`20time`20th
  1172. Vat`20the
  1173. X`20*`09`20`20`20(newest`20game`20played`20with`20that`20person`20that`20has
  1174. V`20finished)
  1175. X`20*`09`20`20`20was`20started.`20`20(Read`20it`20again,`20it`20makes`20sense).
  1176. V
  1177. X`20*`09`20`20`20This`20information`20allows`20us`20to`20discard`20START`20pack
  1178. Vets`20that
  1179. X`20*`09`20`20`20have`20remained`20in`20our`20mail`20(that`20we`20should`20have
  1180. V`20deleted,
  1181. X`20*`09`20`20`20but`20didn't)`20by`20discarding`20START`20packets`20that`20are
  1182. V`20not`20newer
  1183. X`20*`09`20`20`20than`20the`20fence`20time.
  1184. X`20*--------------------------------------------------------------------------
  1185. V-
  1186. X`20*/
  1187. X
  1188. Xreadpeople()
  1189. X`7B
  1190. XFILE`20*fp;
  1191. Xchar`20c,`20*s;
  1192. Xstruct`20people`20*p;
  1193. X
  1194. Xif`20(`20(fp`20=`20fopen(rc.pfile,"r"))`20==`20NULL)
  1195. X`09return;`09`09`09`09/*`20no`20people`20file`20*/
  1196. Xwhile`20(`20(c`20=`20getc(fp))`20!=`20EOF)`20`7B
  1197. X`09ungetc(c,fp);`09/*`20put`20char`20back`20*/
  1198. X`09p`20=`20addppl();`09`09/*`20insert`20a`20new`20record`20*/
  1199. X`09p->name`20=`20NULL;`09`09
  1200. X`09p->addr`20=`20NULL;
  1201. X`09p->alias`20=`20NULL;
  1202. X`09p->myaddr`20=`20NULL;
  1203. X`09p->equiv`20=`20NULL;
  1204. X`09p->fence`20=`200L;
  1205. X`09if`20(`20(s`20=`20nvscan(fp,nv_pfile,p))`20!=`20NULL)`20`7B`09/*`20read`20r
  1206. Vecord`20*/
  1207. X`09`09FeFinishSession();`09/*`20close`20down`20front-end`20*/
  1208. X`09`09TFinishSession();`09/*`20close`20down`20transport`20*/
  1209. X`09`09fprintf(stderr,"ERROR:`20invalid`20line`20in`20%s:`20%s`5Cn",`20rc.pfile
  1210. V,`20s);
  1211. X`09`09ldbexit(STAT_ABORT);
  1212. X`09`09`7D
  1213. X`09if`20(p->equiv`20!=`20NULL)`09`09/*`20an`20equiv`20record`20*/
  1214. X`09`09continue;`09`09/*`20no`20other`20fields`20*/
  1215. X`09if`20(p->myaddr`20==`20NULL)`09`09`09/*`20if`20no`20myaddr`20line`20*/
  1216. X`09`09p->myaddr`20=`20save(rc.myaddr);`09/*`20dflt`20to`20ldbrc`20setting`20*/
  1217. V
  1218. X`09if`20(rc.debug`20`26`20DB_RWGAMES)
  1219. X`09`09message("DB-readpeople:`20read`20'%s'`5Cn",p->name);
  1220. X`09`7D
  1221. Xfclose(fp);
  1222. X`7D
  1223. X
  1224. X
  1225. X/*----------------------------------------------------------------------
  1226. X`20*`09writepeople`20--`20save`20the`20people`20list`20to`20a`20file
  1227. X`20*
  1228. X`20*`20This`20function`20writes`20the`20people`20list`20to`20rc.pfile.
  1229. X`20*----------------------------------------------------------------------
  1230. X`20*/
  1231. X
  1232. Xwritepeople(file)
  1233. Xchar`20*file;
  1234. X`7B
  1235. XFILE`20*fp;
  1236. Xstruct`20people`20*p;
  1237. X
  1238. Xunlink(file);`09`09/*`20prevent`20multiple`20versions`20on`20VMS`20*/
  1239. Xif`20(`20(fp`20=`20fopen(file,"w"))`20==`20NULL)`20`7B
  1240. X`09message("ERROR:`20can't`20save`20people`20in`20%s!`5Cn",file);
  1241. X`09return;`09`09`09/*`20should`20do`20something`20to`20save`20people`20list
  1242. V`20*/
  1243. X`09`7D
  1244. Xfor`20(p`20=`20phead;`20p`20!=`20NULL;`20p`20=`20p->next)`20`7B
  1245. X`09if`20(p->equiv`20==`20NULL)`20`7B
  1246. X`09`09nvwrite(fp,nv_pfile,p);`09/*`20write`20the`20struct`20*/
  1247. X`09`09if`20(rc.debug`20`26`20DB_RWGAMES)
  1248. X`09`09`09message("DB-writepeople:`20write`20'%s'`5Cn",p->name);
  1249. X`09`09`7D
  1250. X`09else
  1251. X`09`09nvwrite(fp,nv_pequiv,p);`09/*`20write`20an`20equiv`20record`20*/
  1252. X`09`7D
  1253. Xfclose(fp);
  1254. X`7D
  1255. X
  1256. X
  1257. X
  1258. X/*----------------------------------------------------------------------
  1259. X`20*`09boardstr`20--`20generate`20an`20ascii`20representation`20of`20a`20board
  1260. V
  1261. X`20*
  1262. X`20*`20This`20function`20produces`20a`20visible`20representation`20of`20a`20bo
  1263. Vard.`20`20Each`20point
  1264. X`20*`20on`20the`20board`20takes`20two`20characters;`20the`20quantity`20is`20of
  1265. Vfset`20by`2065,
  1266. X`20*`20putting`20it`20in`20the`20range`20`5BA-P`5D,`20and`20the`20color`20is
  1267. V`20unchanged`20since`20it
  1268. X`20*`20is`20already`20a`20printable`20character.`20`20This`20results`20in`20a
  1269. V`20string`20of
  1270. X`20*`2028`20character`20pairs,`20one`20for`20each`20point`20on`20the`20board.
  1271. V`20`20These`20are
  1272. X`20*`20in`20the`20order:
  1273. X`20*`090:`09BAR`20point`20for`20upbound`20player
  1274. X`20*`091-24:`09board`20points
  1275. X`20*`0925:`09BAR`20point`20for`20downbound`20player
  1276. X`20*`0926:`09OFF`20point`20for`20upbound`20player
  1277. X`20*`0927:`09OFF`20point`20for`20downbound`20player
  1278. X`20*----------------------------------------------------------------------
  1279. X`20*/
  1280. X
  1281. Xchar`20*boardstr(b)
  1282. Xboard`20b;
  1283. X`7B
  1284. Xstatic`20char`20buf`5BBOARDSIZE*2+1`5D;
  1285. Xchar`20*s,`20c;
  1286. Xint`20i;
  1287. X
  1288. Xs`20=`20buf;
  1289. Xfor`20(i`20=`200;`20i`20<`20BOARDSIZE;`20i++)`20`7B
  1290. X`09*s++`20=`20b`5Bi`5D.qty`20+`20'A';`09`09/*`20offset`20qty`20into`20u.c.`20l
  1291. Vetters`20*/
  1292. X`09c`20=`20b`5Bi`5D.color;
  1293. X`09if`20(!`20isalpha(c))
  1294. X`09`09c`20=`20'-';`09`09/*`20use`20printing`20chars`20*/
  1295. X`09*s++`20=`20c;
  1296. X`09`7D
  1297. X*s`20=`20'`5C0';
  1298. Xreturn(buf);
  1299. X`7D
  1300. X
  1301. X
  1302. +-+-+-+-+-+-+-+-  END  OF PART 13 +-+-+-+-+-+-+-+-
  1303.