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