Home | History | Annotate | Line # | Download | only in phantasia
setup.c revision 1.18
      1 /*	$NetBSD: setup.c,v 1.18 2008/01/16 23:23:25 lukem Exp $	*/
      2 
      3 /*
      4  * setup.c - set up all files for Phantasia
      5  * n.b.: this is used at build-time - i.e. during build.sh.
      6  */
      7 #ifdef __NetBSD__
      8 #include <sys/cdefs.h>
      9 #endif
     10 
     11 #include <sys/param.h>
     12 #include <sys/stat.h>
     13 #include <fcntl.h>
     14 #include "include.h"
     15 
     16 #ifndef __dead /* Not NetBSD */
     17 #define __dead
     18 #endif
     19 
     20 int main(int, char *[]);
     21 void Error(const char *, const char *) __dead;
     22 double drandom(void);
     23 
     24 /**/
     26 /************************************************************************
     27 /
     28 / FUNCTION NAME: main()
     29 /
     30 / FUNCTION: setup files for Phantasia 3.3.2
     31 /
     32 / AUTHOR: E. A. Estes, 12/4/85
     33 /
     34 / ARGUMENTS: none
     35 /
     36 / RETURN VALUE: none
     37 /
     38 / MODULES CALLED: time(), exit(), stat(), Error(), creat(), close(), fopen(),
     39 /	fgets(), floor(), srandom(), umask(), drandom(), strcpy(), getuid(),
     40 /	unlink(), fwrite(), fclose(), sscanf(), printf(), strlen(), fprintf()
     41 /
     42 / GLOBAL INPUTS: Curmonster, _iob[], Databuf[], *Monstfp, Enrgyvoid
     43 /
     44 / GLOBAL OUTPUTS: Curmonster, Databuf[], *Monstfp, Enrgyvoid
     45 /
     46 / DESCRIPTION:
     47 /
     48 /	This program tries to verify the parameters specified in
     49 /	the Makefile.
     50 /
     51 /	Create all necessary files.  Note that nothing needs to be
     52 /	put in these files.
     53 /	Also, the monster binary data base is created here.
     54 /
     55 / ************************************************************************/
     56 
     57 static const char *const files[] = {		/* all files to create */
     58 	_PATH_MONST,
     59 	_PATH_PEOPLE,
     60 	_PATH_MESS,
     61 	_PATH_LASTDEAD,
     62 	_PATH_MOTD,
     63 	_PATH_GOLD,
     64 	_PATH_VOID,
     65 	_PATH_SCORE,
     66 	NULL,
     67 };
     68 
     69 const char *monsterfile = "monsters.asc";
     70 
     71 int
     72 main(argc, argv)
     73 	int argc;
     74 	char *argv[];
     75 {
     76 	const char *const *filename; /* for pointing to file names */
     77 	int		fd;		/* file descriptor */
     78 	FILE		*fp;			/* for opening files */
     79 	struct stat	fbuf;		/* for getting files statistics */
     80 	int ch;
     81 	char *path;
     82 
     83 	while ((ch = getopt(argc, argv, "m:")) != -1)
     84 		switch(ch) {
     85 		case 'm':
     86 			monsterfile = optarg;
     87 			break;
     88 		case '?':
     89 		default:
     90 			break;
     91 		}
     92 	argc -= optind;
     93 	argv += optind;
     94 
     95     srandom((unsigned) time(NULL));	/* prime random numbers */
     96 
     97     umask(0117);		/* only owner can read/write created files */
     98 
     99     /* try to create data files */
    100     filename = &files[0];
    101     while (*filename != NULL)
    102 	/* create each file */
    103 	{
    104 	path = strrchr(*filename, '/') + 1;
    105 	if (stat(path, &fbuf) == 0)
    106 	    /* file exists; remove it */
    107 	    {
    108 	    if (unlink(path) < 0)
    109 		Error("Cannot unlink %s.\n", path);
    110 		/*NOTREACHED*/
    111 	    }
    112 
    113 	if ((fd = creat(path, 0660)) < 0)
    114 	    Error("Cannot create %s.\n", path);
    115 	    /*NOTREACHED*/
    116 
    117 	close(fd);			/* close newly created file */
    118 
    119 	++filename;			/* process next file */
    120 	}
    121 
    122     /* Initialize an empty file placeholder for the grail location. */
    123     if ((fp = fopen(path, "w")) == NULL)
    124 	Error("Cannot create %s.\n", path);
    125     fclose(fp);
    126 
    127     /* create binary monster data base */
    128     path = strrchr(_PATH_MONST, '/') + 1;
    129     if ((Monstfp = fopen(path, "w")) == NULL)
    130 	Error("Cannot update %s.\n", path);
    131     else
    132 	{
    133 	if ((fp = fopen(monsterfile, "r")) == NULL)
    134 	    {
    135 	    fclose(Monstfp);
    136 	    Error("cannot open %s to create monster database.\n", "monsters.asc");
    137 	    }
    138 	else
    139 	    {
    140 	    Curmonster.m_o_strength =
    141 	    Curmonster.m_o_speed =
    142 	    Curmonster.m_maxspeed =
    143 	    Curmonster.m_o_energy =
    144 	    Curmonster.m_melee =
    145 	    Curmonster.m_skirmish = 0.0;
    146 
    147 	    while (fgets(Databuf, SZ_DATABUF, fp) != NULL)
    148 		/* read in text file, convert to binary */
    149 		{
    150 		sscanf(&Databuf[24], "%lf%lf%lf%lf%lf%d%d%lf",
    151 		    &Curmonster.m_strength, &Curmonster.m_brains,
    152 		    &Curmonster.m_speed, &Curmonster.m_energy,
    153 		    &Curmonster.m_experience, &Curmonster.m_treasuretype,
    154 		    &Curmonster.m_type, &Curmonster.m_flock);
    155 		Databuf[24] = '\0';
    156 		strcpy(Curmonster.m_name, Databuf);
    157 		fwrite((char *) &Curmonster, SZ_MONSTERSTRUCT, 1, Monstfp);
    158 		}
    159 	    fclose(fp);
    160 	    fflush(Monstfp);
    161 	    if (ferror(Monstfp))
    162 		Error("Writing %s.\n", path);
    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     path = strrchr(_PATH_MOTD, '/') + 1;
    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 void
    234 Error(str, file)
    235 	const 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