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