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