setup.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /* $NetBSD: setup.c,v 1.14 2004/12/09 05:15:59 jmc Exp $ */
  2. /*
  3. * setup.c - set up all files for Phantasia
  4. */
  5. #include <sys/param.h>
  6. #include <sys/stat.h>
  7. #include <fcntl.h>
  8. #include "include.h"
  9. int main(int, char *[]);
  10. void Error(const char *, const char *) __attribute__((__noreturn__));
  11. double drandom(void);
  12. /* */
  13. /************************************************************************
  14. /
  15. / FUNCTION NAME: main()
  16. /
  17. / FUNCTION: setup files for Phantasia 3.3.2
  18. /
  19. / AUTHOR: E. A. Estes, 12/4/85
  20. /
  21. / ARGUMENTS: none
  22. /
  23. / RETURN VALUE: none
  24. /
  25. / MODULES CALLED: time(), exit(), stat(), Error(), creat(), close(), fopen(),
  26. / fgets(), floor(), srandom(), umask(), drandom(), strcpy(), getuid(),
  27. / unlink(), fwrite(), fclose(), sscanf(), printf(), strlen(), fprintf()
  28. /
  29. / GLOBAL INPUTS: Curmonster, _iob[], Databuf[], *Monstfp, Enrgyvoid
  30. /
  31. / GLOBAL OUTPUTS: Curmonster, Databuf[], *Monstfp, Enrgyvoid
  32. /
  33. / DESCRIPTION:
  34. /
  35. / This program tries to verify the parameters specified in
  36. / the Makefile.
  37. /
  38. / Create all necessary files. Note that nothing needs to be
  39. / put in these files.
  40. / Also, the monster binary data base is created here.
  41. /
  42. / ************************************************************************/
  43. static const char *const files[] = { /* all files to create */
  44. _PATH_MONST,
  45. _PATH_PEOPLE,
  46. _PATH_MESS,
  47. _PATH_LASTDEAD,
  48. _PATH_MOTD,
  49. _PATH_GOLD,
  50. _PATH_VOID,
  51. _PATH_SCORE,
  52. NULL,
  53. };
  54. const char *monsterfile = "monsters.asc";
  55. int
  56. main(argc, argv)
  57. int argc;
  58. char *argv[];
  59. {
  60. const char *const *filename; /* for pointing to file names */
  61. int fd; /* file descriptor */
  62. FILE *fp; /* for opening files */
  63. struct stat fbuf; /* for getting files statistics */
  64. int ch;
  65. char *path;
  66. while ((ch = getopt(argc, argv, "m:")) != -1)
  67. switch(ch) {
  68. case 'm':
  69. monsterfile = optarg;
  70. break;
  71. case '?':
  72. default:
  73. break;
  74. }
  75. argc -= optind;
  76. argv += optind;
  77. srandom((unsigned) time(NULL)); /* prime random numbers */
  78. umask(0117); /* only owner can read/write created files */
  79. /* try to create data files */
  80. filename = &files[0];
  81. while (*filename != NULL)
  82. /* create each file */
  83. {
  84. path = strrchr(*filename, '/') + 1;
  85. if (stat(path, &fbuf) == 0)
  86. /* file exists; remove it */
  87. {
  88. if (unlink(path) < 0)
  89. Error("Cannot unlink %s.\n", path);
  90. /*NOTREACHED*/
  91. }
  92. if ((fd = creat(path, 0660)) < 0)
  93. Error("Cannot create %s.\n", path);
  94. /*NOTREACHED*/
  95. close(fd); /* close newly created file */
  96. ++filename; /* process next file */
  97. }
  98. /* Initialize an empty file placeholder for the grail location. */
  99. if ((fp = fopen(path, "w")) == NULL)
  100. Error("Cannot create %s.\n", path);
  101. fclose(fp);
  102. /* create binary monster data base */
  103. path = strrchr(_PATH_MONST, '/') + 1;
  104. if ((Monstfp = fopen(path, "w")) == NULL)
  105. Error("Cannot update %s.\n", path);
  106. else
  107. {
  108. if ((fp = fopen(monsterfile, "r")) == NULL)
  109. {
  110. fclose(Monstfp);
  111. Error("cannot open %s to create monster database.\n", "monsters.asc");
  112. }
  113. else
  114. {
  115. Curmonster.m_o_strength =
  116. Curmonster.m_o_speed =
  117. Curmonster.m_maxspeed =
  118. Curmonster.m_o_energy =
  119. Curmonster.m_melee =
  120. Curmonster.m_skirmish = 0.0;
  121. while (fgets(Databuf, SZ_DATABUF, fp) != NULL)
  122. /* read in text file, convert to binary */
  123. {
  124. sscanf(&Databuf[24], "%lf%lf%lf%lf%lf%d%d%lf",
  125. &Curmonster.m_strength, &Curmonster.m_brains,
  126. &Curmonster.m_speed, &Curmonster.m_energy,
  127. &Curmonster.m_experience, &Curmonster.m_treasuretype,
  128. &Curmonster.m_type, &Curmonster.m_flock);
  129. Databuf[24] = '\0';
  130. strcpy(Curmonster.m_name, Databuf);
  131. fwrite((char *) &Curmonster, SZ_MONSTERSTRUCT, 1, Monstfp);
  132. }
  133. fclose(fp);
  134. fflush(Monstfp);
  135. if (ferror(Monstfp))
  136. Error("Writing %s.\n", path);
  137. fclose(Monstfp);
  138. }
  139. }
  140. #ifdef MAKE_INSTALLS_THIS_AND_DOESNT_WANT_TO_HEAR_ABOUT_IT
  141. /* write to motd file */
  142. printf("One line 'motd' ? ");
  143. if (fgets(Databuf, SZ_DATABUF, stdin) == NULL)
  144. Databuf[0] = '\0';
  145. path = strrchr(_PATH_MOTD, '/') + 1;
  146. if ((fp = fopen(path, "w")) == NULL)
  147. Error("Cannot update %s.\n", path);
  148. else
  149. {
  150. fwrite(Databuf, sizeof(char), strlen(Databuf), fp);
  151. fclose(fp);
  152. }
  153. /* report compile-time options */
  154. printf("Compiled options:\n\n");
  155. printf("Phantasia destination directory: %s\n", _PATH_PHANTDIR);
  156. printf("Wizard: root UID: 0\n");
  157. #ifdef BSD41
  158. printf("Compiled for BSD 4.1\n");
  159. #endif
  160. #ifdef BSD42
  161. printf("Compiled for BSD 4.2\n");
  162. #endif
  163. #ifdef SYS3
  164. printf("Compiled for System III\n");
  165. #endif
  166. #ifdef SYS5
  167. printf("Compiled for System V\n");
  168. #endif
  169. #endif
  170. exit(0);
  171. /*NOTREACHED*/
  172. }
  173. /* */
  174. /************************************************************************
  175. /
  176. / FUNCTION NAME: Error()
  177. /
  178. / FUNCTION: print an error message, and exit
  179. /
  180. / AUTHOR: E. A. Estes, 12/4/85
  181. /
  182. / ARGUMENTS:
  183. / char *str - format string for printf()
  184. / char *file - file which caused error
  185. /
  186. / RETURN VALUE: none
  187. /
  188. / MODULES CALLED: exit(), perror(), fprintf()
  189. /
  190. / GLOBAL INPUTS: _iob[]
  191. /
  192. / GLOBAL OUTPUTS: none
  193. /
  194. / DESCRIPTION:
  195. / Print an error message, then exit.
  196. /
  197. / ************************************************************************/
  198. void
  199. Error(str, file)
  200. const char *str, *file;
  201. {
  202. fprintf(stderr, "Error: ");
  203. fprintf(stderr, str, file);
  204. perror(file);
  205. exit(1);
  206. /*NOTREACHED*/
  207. }
  208. /* */
  209. /************************************************************************
  210. /
  211. / FUNCTION NAME: drandom()
  212. /
  213. / FUNCTION: return a random number
  214. /
  215. / AUTHOR: E. A. Estes, 2/7/86
  216. /
  217. / ARGUMENTS: none
  218. /
  219. / RETURN VALUE: none
  220. /
  221. / MODULES CALLED: random()
  222. /
  223. / GLOBAL INPUTS: none
  224. /
  225. / GLOBAL OUTPUTS: none
  226. /
  227. / DESCRIPTION:
  228. /
  229. / ************************************************************************/
  230. double
  231. drandom()
  232. {
  233. if (sizeof(int) != 2)
  234. return((double) (random() & 0x7fff) / 32768.0);
  235. else
  236. return((double) random() / 32768.0);
  237. }