Home | History | Annotate | Line # | Download | only in phantasia
setup.c revision 1.3
      1 /*	$NetBSD: setup.c,v 1.3 1995/03/24 04:00:51 cgd Exp $	*/
      2 
      3 /*
      4  * setup.c - set up all files for Phantasia
      5  */
      6 #include "include.h"
      7 #include <sys/param.h>
      8 #include <sys/stat.h>
      9 #include <stdlib.h>
     10 /**/
     12 /************************************************************************
     13 /
     14 / FUNCTION NAME: main()
     15 /
     16 / FUNCTION: setup files for Phantasia 3.3.2
     17 /
     18 / AUTHOR: E. A. Estes, 12/4/85
     19 /
     20 / ARGUMENTS: none
     21 /
     22 / RETURN VALUE: none
     23 /
     24 / MODULES CALLED: time(), exit(), stat(), Error(), creat(), close(), fopen(),
     25 /	fgets(), floor(), srandom(), umask(), drandom(), strcpy(), getuid(),
     26 /	unlink(), fwrite(), fclose(), sscanf(), printf(), strlen(), fprintf()
     27 /
     28 / GLOBAL INPUTS: Curmonster, _iob[], Databuf[], *Monstfp, Enrgyvoid
     29 /
     30 / GLOBAL OUTPUTS: Curmonster, Databuf[], *Monstfp, Enrgyvoid
     31 /
     32 / DESCRIPTION:
     33 /
     34 /	This program tries to verify the parameters specified in
     35 /	the Makefile.
     36 /
     37 /	Create all necessary files.  Note that nothing needs to be
     38 /	put in these files.
     39 /	Also, the monster binary data base is created here.
     40 /
     41 /************************************************************************/
     42 
     43 static char *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 
     55 char *monsterfile="monsters.asc";
     56 
     57 int
     58 main(argc, argv)
     59 	int argc;
     60 	char *argv[];
     61 {
     62 	register char	**filename;	/* for pointing to file names */
     63 	register int	fd;		/* file descriptor */
     64 	FILE	*fp;			/* for opening files */
     65 	struct stat	fbuf;		/* for getting files statistics */
     66 	int ch;
     67 	char path[MAXPATHLEN], *prefix;
     68 
     69 	while ((ch = getopt(argc, argv, "m:")) != EOF)
     70 		switch(ch) {
     71 		case 'm':
     72 			monsterfile = optarg;
     73 			break;
     74 		case '?':
     75 		default:
     76 			break;
     77 		}
     78 	argc -= optind;
     79 	argv += optind;
     80 
     81     srandom((unsigned) time((long *) NULL));	/* prime random numbers */
     82 
     83     umask(0117);		/* only owner can read/write created files */
     84 
     85     prefix = getenv("DESTDIR");
     86 
     87     /* try to create data files */
     88     filename = &files[0];
     89     while (*filename != NULL)
     90 	/* create each file */
     91 	{
     92 	snprintf(path, sizeof(path), "%s%s", prefix?prefix:"", *filename);
     93 	if (stat(path, &fbuf) == 0)
     94 	    /* file exists; remove it */
     95 	    {
     96 	    if (!strcmp(*filename, _PATH_PEOPLE))
     97 		/* do not reset character file if it already exists */
     98 		{
     99 		++filename;
    100 		continue;
    101 		}
    102 
    103 	    if (unlink(path) < 0)
    104 		Error("Cannot unlink %s.\n", path);
    105 		/*NOTREACHED*/
    106 	    }
    107 
    108 	if ((fd = creat(path, 0660)) < 0)
    109 	    Error("Cannot create %s.\n", path);
    110 	    /*NOTREACHED*/
    111 
    112 	close(fd);			/* close newly created file */
    113 
    114 	++filename;			/* process next file */
    115 	}
    116 
    117     /* put holy grail info into energy void file */
    118     Enrgyvoid.ev_active = TRUE;
    119     Enrgyvoid.ev_x = ROLL(-1.0e6, 2.0e6);
    120     Enrgyvoid.ev_y = ROLL(-1.0e6, 2.0e6);
    121     snprintf(path, sizeof(path), "%s%s", prefix?prefix:"", _PATH_VOID);
    122     if ((fp = fopen(path, "w")) == NULL)
    123 	Error("Cannot update %s.\n", path);
    124     else
    125 	{
    126 	fwrite(&Enrgyvoid, SZ_VOIDSTRUCT, 1, fp);
    127 	fclose(fp);
    128 	}
    129 
    130     /* create binary monster data base */
    131     snprintf(path, sizeof(path), "%s%s", prefix?prefix:"", _PATH_MONST);
    132     if ((Monstfp = fopen(path, "w")) == NULL)
    133 	Error("Cannot update %s.\n", path);
    134     else
    135 	{
    136 	if ((fp = fopen(monsterfile, "r")) == NULL)
    137 	    {
    138 	    fclose(Monstfp);
    139 	    Error("cannot open %s to create monster database.\n", "monsters.asc");
    140 	    }
    141 	else
    142 	    {
    143 	    Curmonster.m_o_strength =
    144 	    Curmonster.m_o_speed =
    145 	    Curmonster.m_maxspeed =
    146 	    Curmonster.m_o_energy =
    147 	    Curmonster.m_melee =
    148 	    Curmonster.m_skirmish = 0.0;
    149 
    150 	    while (fgets(Databuf, SZ_DATABUF, fp) != NULL)
    151 		/* read in text file, convert to binary */
    152 		{
    153 		sscanf(&Databuf[24], "%lf%lf%lf%lf%lf%d%d%lf",
    154 		    &Curmonster.m_strength, &Curmonster.m_brains,
    155 		    &Curmonster.m_speed, &Curmonster.m_energy,
    156 		    &Curmonster.m_experience, &Curmonster.m_treasuretype,
    157 		    &Curmonster.m_type, &Curmonster.m_flock);
    158 		Databuf[24] = '\0';
    159 		strcpy(Curmonster.m_name, Databuf);
    160 		fwrite((char *) &Curmonster, SZ_MONSTERSTRUCT, 1, Monstfp);
    161 		}
    162 	    fclose(fp);
    163 	    fclose(Monstfp);
    164 	    }
    165 	}
    166 
    167 #ifdef MAKE_INSTALLS_THIS_AND_DOESNT_WANT_TO_HEAR_ABOUT_IT
    168     /* write to motd file */
    169     printf("One line 'motd' ? ");
    170     if (fgets(Databuf, SZ_DATABUF, stdin) == NULL)
    171 	Databuf[0] = '\0';
    172     snprintf(path, sizeof(path), "%s%s", prefix?prefix:"", _PATH_MOTD);
    173     if ((fp = fopen(path, "w")) == NULL)
    174 	Error("Cannot update %s.\n", path);
    175     else
    176 	{
    177 	fwrite(Databuf, sizeof(char), strlen(Databuf), fp);
    178 	fclose(fp);
    179 	}
    180 
    181     /* report compile-time options */
    182     printf("Compiled options:\n\n");
    183     printf("Phantasia destination directory:  %s\n", _PATH_PHANTDIR);
    184     printf("Wizard: root UID: 0\n");
    185 
    186 #ifdef BSD41
    187     printf("Compiled for BSD 4.1\n");
    188 #endif
    189 
    190 #ifdef BSD42
    191     printf("Compiled for BSD 4.2\n");
    192 #endif
    193 
    194 #ifdef SYS3
    195     printf("Compiled for System III\n");
    196 #endif
    197 
    198 #ifdef SYS5
    199     printf("Compiled for System V\n");
    200 #endif
    201 #endif
    202 
    203     exit(0);
    204     /*NOTREACHED*/
    205 }
    206 /**/
    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 Error(str, file)
    234 char	*str, *file;
    235 {
    236     fprintf(stderr, "Error: ");
    237     fprintf(stderr, str, file);
    238     perror(file);
    239     exit(1);
    240     /*NOTREACHED*/
    241 }
    242 /**/
    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