Home | History | Annotate | Line # | Download | only in phantasia
setup.c revision 1.14
      1 /*	$NetBSD: setup.c,v 1.14 2004/12/09 05:15:59 jmc 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     /* Initialize an empty file placeholder for the grail location. */
    114     if ((fp = fopen(path, "w")) == NULL)
    115 	Error("Cannot create %s.\n", path);
    116     fclose(fp);
    117 
    118     /* create binary monster data base */
    119     path = strrchr(_PATH_MONST, '/') + 1;
    120     if ((Monstfp = fopen(path, "w")) == NULL)
    121 	Error("Cannot update %s.\n", path);
    122     else
    123 	{
    124 	if ((fp = fopen(monsterfile, "r")) == NULL)
    125 	    {
    126 	    fclose(Monstfp);
    127 	    Error("cannot open %s to create monster database.\n", "monsters.asc");
    128 	    }
    129 	else
    130 	    {
    131 	    Curmonster.m_o_strength =
    132 	    Curmonster.m_o_speed =
    133 	    Curmonster.m_maxspeed =
    134 	    Curmonster.m_o_energy =
    135 	    Curmonster.m_melee =
    136 	    Curmonster.m_skirmish = 0.0;
    137 
    138 	    while (fgets(Databuf, SZ_DATABUF, fp) != NULL)
    139 		/* read in text file, convert to binary */
    140 		{
    141 		sscanf(&Databuf[24], "%lf%lf%lf%lf%lf%d%d%lf",
    142 		    &Curmonster.m_strength, &Curmonster.m_brains,
    143 		    &Curmonster.m_speed, &Curmonster.m_energy,
    144 		    &Curmonster.m_experience, &Curmonster.m_treasuretype,
    145 		    &Curmonster.m_type, &Curmonster.m_flock);
    146 		Databuf[24] = '\0';
    147 		strcpy(Curmonster.m_name, Databuf);
    148 		fwrite((char *) &Curmonster, SZ_MONSTERSTRUCT, 1, Monstfp);
    149 		}
    150 	    fclose(fp);
    151 	    fflush(Monstfp);
    152 	    if (ferror(Monstfp))
    153 		Error("Writing %s.\n", path);
    154 	    fclose(Monstfp);
    155 	    }
    156 	}
    157 
    158 #ifdef MAKE_INSTALLS_THIS_AND_DOESNT_WANT_TO_HEAR_ABOUT_IT
    159     /* write to motd file */
    160     printf("One line 'motd' ? ");
    161     if (fgets(Databuf, SZ_DATABUF, stdin) == NULL)
    162 	Databuf[0] = '\0';
    163     path = strrchr(_PATH_MOTD, '/') + 1;
    164     if ((fp = fopen(path, "w")) == NULL)
    165 	Error("Cannot update %s.\n", path);
    166     else
    167 	{
    168 	fwrite(Databuf, sizeof(char), strlen(Databuf), fp);
    169 	fclose(fp);
    170 	}
    171 
    172     /* report compile-time options */
    173     printf("Compiled options:\n\n");
    174     printf("Phantasia destination directory:  %s\n", _PATH_PHANTDIR);
    175     printf("Wizard: root UID: 0\n");
    176 
    177 #ifdef BSD41
    178     printf("Compiled for BSD 4.1\n");
    179 #endif
    180 
    181 #ifdef BSD42
    182     printf("Compiled for BSD 4.2\n");
    183 #endif
    184 
    185 #ifdef SYS3
    186     printf("Compiled for System III\n");
    187 #endif
    188 
    189 #ifdef SYS5
    190     printf("Compiled for System V\n");
    191 #endif
    192 #endif
    193 
    194     exit(0);
    195     /*NOTREACHED*/
    196 }
    197 /**/
    199 /************************************************************************
    200 /
    201 / FUNCTION NAME: Error()
    202 /
    203 / FUNCTION: print an error message, and exit
    204 /
    205 / AUTHOR: E. A. Estes, 12/4/85
    206 /
    207 / ARGUMENTS:
    208 /	char *str - format string for printf()
    209 /	char *file - file which caused error
    210 /
    211 / RETURN VALUE: none
    212 /
    213 / MODULES CALLED: exit(), perror(), fprintf()
    214 /
    215 / GLOBAL INPUTS: _iob[]
    216 /
    217 / GLOBAL OUTPUTS: none
    218 /
    219 / DESCRIPTION:
    220 /	Print an error message, then exit.
    221 /
    222 / ************************************************************************/
    223 
    224 void
    225 Error(str, file)
    226 	const char	*str, *file;
    227 {
    228     fprintf(stderr, "Error: ");
    229     fprintf(stderr, str, file);
    230     perror(file);
    231     exit(1);
    232     /*NOTREACHED*/
    233 }
    234 /**/
    236 /************************************************************************
    237 /
    238 / FUNCTION NAME: drandom()
    239 /
    240 / FUNCTION: return a random number
    241 /
    242 / AUTHOR: E. A. Estes, 2/7/86
    243 /
    244 / ARGUMENTS: none
    245 /
    246 / RETURN VALUE: none
    247 /
    248 / MODULES CALLED: random()
    249 /
    250 / GLOBAL INPUTS: none
    251 /
    252 / GLOBAL OUTPUTS: none
    253 /
    254 / DESCRIPTION:
    255 /
    256 / ************************************************************************/
    257 
    258 double
    259 drandom()
    260 {
    261     if (sizeof(int) != 2)
    262 	return((double) (random() & 0x7fff) / 32768.0);
    263     else
    264 	return((double) random() / 32768.0);
    265 }
    266