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