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