Home | History | Annotate | Line # | Download | only in phantasia
setup.c revision 1.8
      1 /*	$NetBSD: setup.c,v 1.8 1997/11/24 01:47:26 mrg 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 "include.h"
      9 /**/
     11 /************************************************************************
     12 /
     13 / FUNCTION NAME: main()
     14 /
     15 / FUNCTION: setup files for Phantasia 3.3.2
     16 /
     17 / AUTHOR: E. A. Estes, 12/4/85
     18 /
     19 / ARGUMENTS: none
     20 /
     21 / RETURN VALUE: none
     22 /
     23 / MODULES CALLED: time(), exit(), stat(), Error(), creat(), close(), fopen(),
     24 /	fgets(), floor(), srandom(), umask(), drandom(), strcpy(), getuid(),
     25 /	unlink(), fwrite(), fclose(), sscanf(), printf(), strlen(), fprintf()
     26 /
     27 / GLOBAL INPUTS: Curmonster, _iob[], Databuf[], *Monstfp, Enrgyvoid
     28 /
     29 / GLOBAL OUTPUTS: Curmonster, Databuf[], *Monstfp, Enrgyvoid
     30 /
     31 / DESCRIPTION:
     32 /
     33 /	This program tries to verify the parameters specified in
     34 /	the Makefile.
     35 /
     36 /	Create all necessary files.  Note that nothing needs to be
     37 /	put in these files.
     38 /	Also, the monster binary data base is created here.
     39 /
     40 /************************************************************************/
     41 
     42 static char *files[] = {		/* all files to create */
     43 	_PATH_MONST,
     44 	_PATH_PEOPLE,
     45 	_PATH_MESS,
     46 	_PATH_LASTDEAD,
     47 	_PATH_MOTD,
     48 	_PATH_GOLD,
     49 	_PATH_VOID,
     50 	_PATH_SCORE,
     51 	NULL,
     52 };
     53 
     54 char *monsterfile="monsters.asc";
     55 
     56 int
     57 main(argc, argv)
     58 	int argc;
     59 	char *argv[];
     60 {
     61 	register char	**filename;	/* for pointing to file names */
     62 	register int	fd;		/* file descriptor */
     63 	FILE	*fp;			/* for opening files */
     64 	struct stat	fbuf;		/* for getting files statistics */
     65 	int ch;
     66 	char path[MAXPATHLEN], *prefix;
     67 
     68 	while ((ch = getopt(argc, argv, "m:")) != -1)
     69 		switch(ch) {
     70 		case 'm':
     71 			monsterfile = optarg;
     72 			break;
     73 		case '?':
     74 		default:
     75 			break;
     76 		}
     77 	argc -= optind;
     78 	argv += optind;
     79 
     80     srandom((unsigned) time(NULL));	/* prime random numbers */
     81 
     82     umask(0117);		/* only owner can read/write created files */
     83 
     84     prefix = getenv("DESTDIR");
     85 
     86     /* try to create data files */
     87     filename = &files[0];
     88     while (*filename != NULL)
     89 	/* create each file */
     90 	{
     91 	snprintf(path, sizeof(path), "%s%s", prefix?prefix:"", *filename);
     92 	if (stat(path, &fbuf) == 0)
     93 	    /* file exists; remove it */
     94 	    {
     95 	    if (!strcmp(*filename, _PATH_PEOPLE))
     96 		/* do not reset character file if it already exists */
     97 		{
     98 		++filename;
     99 		continue;
    100 		}
    101 
    102 	    if (!strcmp(*filename, _PATH_SCORE))
    103 		/* do not reset score file if it already exists */
    104 		{
    105 		++filename;
    106 		continue;
    107 		}
    108 
    109 	    if (unlink(path) < 0)
    110 		Error("Cannot unlink %s.\n", path);
    111 		/*NOTREACHED*/
    112 	    }
    113 
    114 	if ((fd = creat(path, 0660)) < 0)
    115 	    Error("Cannot create %s.\n", path);
    116 	    /*NOTREACHED*/
    117 
    118 	close(fd);			/* close newly created file */
    119 
    120 	++filename;			/* process next file */
    121 	}
    122 
    123     /* put holy grail info into energy void file */
    124     Enrgyvoid.ev_active = TRUE;
    125     Enrgyvoid.ev_x = ROLL(-1.0e6, 2.0e6);
    126     Enrgyvoid.ev_y = ROLL(-1.0e6, 2.0e6);
    127     snprintf(path, sizeof(path), "%s%s", prefix?prefix:"", _PATH_VOID);
    128     if ((fp = fopen(path, "w")) == NULL)
    129 	Error("Cannot update %s.\n", path);
    130     else
    131 	{
    132 	fwrite(&Enrgyvoid, SZ_VOIDSTRUCT, 1, fp);
    133 	fclose(fp);
    134 	}
    135 
    136     /* create binary monster data base */
    137     snprintf(path, sizeof(path), "%s%s", prefix?prefix:"", _PATH_MONST);
    138     if ((Monstfp = fopen(path, "w")) == NULL)
    139 	Error("Cannot update %s.\n", path);
    140     else
    141 	{
    142 	if ((fp = fopen(monsterfile, "r")) == NULL)
    143 	    {
    144 	    fclose(Monstfp);
    145 	    Error("cannot open %s to create monster database.\n", "monsters.asc");
    146 	    }
    147 	else
    148 	    {
    149 	    Curmonster.m_o_strength =
    150 	    Curmonster.m_o_speed =
    151 	    Curmonster.m_maxspeed =
    152 	    Curmonster.m_o_energy =
    153 	    Curmonster.m_melee =
    154 	    Curmonster.m_skirmish = 0.0;
    155 
    156 	    while (fgets(Databuf, SZ_DATABUF, fp) != NULL)
    157 		/* read in text file, convert to binary */
    158 		{
    159 		sscanf(&Databuf[24], "%lf%lf%lf%lf%lf%d%d%lf",
    160 		    &Curmonster.m_strength, &Curmonster.m_brains,
    161 		    &Curmonster.m_speed, &Curmonster.m_energy,
    162 		    &Curmonster.m_experience, &Curmonster.m_treasuretype,
    163 		    &Curmonster.m_type, &Curmonster.m_flock);
    164 		Databuf[24] = '\0';
    165 		strcpy(Curmonster.m_name, Databuf);
    166 		fwrite((char *) &Curmonster, SZ_MONSTERSTRUCT, 1, Monstfp);
    167 		}
    168 	    fclose(fp);
    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     snprintf(path, sizeof(path), "%s%s", prefix?prefix:"", _PATH_MOTD);
    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 /**/
    214 /************************************************************************
    215 /
    216 / FUNCTION NAME: Error()
    217 /
    218 / FUNCTION: print an error message, and exit
    219 /
    220 / AUTHOR: E. A. Estes, 12/4/85
    221 /
    222 / ARGUMENTS:
    223 /	char *str - format string for printf()
    224 /	char *file - file which caused error
    225 /
    226 / RETURN VALUE: none
    227 /
    228 / MODULES CALLED: exit(), perror(), fprintf()
    229 /
    230 / GLOBAL INPUTS: _iob[]
    231 /
    232 / GLOBAL OUTPUTS: none
    233 /
    234 / DESCRIPTION:
    235 /	Print an error message, then exit.
    236 /
    237 /************************************************************************/
    238 
    239 Error(str, file)
    240 char	*str, *file;
    241 {
    242     fprintf(stderr, "Error: ");
    243     fprintf(stderr, str, file);
    244     perror(file);
    245     exit(1);
    246     /*NOTREACHED*/
    247 }
    248 /**/
    250 /************************************************************************
    251 /
    252 / FUNCTION NAME: drandom()
    253 /
    254 / FUNCTION: return a random number
    255 /
    256 / AUTHOR: E. A. Estes, 2/7/86
    257 /
    258 / ARGUMENTS: none
    259 /
    260 / RETURN VALUE: none
    261 /
    262 / MODULES CALLED: random()
    263 /
    264 / GLOBAL INPUTS: none
    265 /
    266 / GLOBAL OUTPUTS: none
    267 /
    268 / DESCRIPTION:
    269 /
    270 /************************************************************************/
    271 
    272 double
    273 drandom()
    274 {
    275     if (sizeof(int) != 2)
    276 	return((double) (random() & 0x7fff) / 32768.0);
    277     else
    278 	return((double) random() / 32768.0);
    279 }
    280