Home | History | Annotate | Line # | Download | only in phantasia
setup.c revision 1.19
      1 /*	$NetBSD: setup.c,v 1.19 2009/05/25 23:08:45 dholland 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(int argc, char *argv[])
     73 {
     74 	const char *const *filename; /* for pointing to file names */
     75 	int		fd;		/* file descriptor */
     76 	FILE		*fp;			/* for opening files */
     77 	struct stat	fbuf;		/* for getting files statistics */
     78 	int ch;
     79 	char *path;
     80 
     81 	while ((ch = getopt(argc, argv, "m:")) != -1)
     82 		switch(ch) {
     83 		case 'm':
     84 			monsterfile = optarg;
     85 			break;
     86 		case '?':
     87 		default:
     88 			break;
     89 		}
     90 	argc -= optind;
     91 	argv += optind;
     92 
     93     srandom((unsigned) time(NULL));	/* prime random numbers */
     94 
     95     umask(0117);		/* only owner can read/write created files */
     96 
     97     /* try to create data files */
     98     filename = &files[0];
     99     while (*filename != NULL)
    100 	/* create each file */
    101 	{
    102 	path = strrchr(*filename, '/') + 1;
    103 	if (stat(path, &fbuf) == 0)
    104 	    /* file exists; remove it */
    105 	    {
    106 	    if (unlink(path) < 0)
    107 		Error("Cannot unlink %s.\n", path);
    108 		/*NOTREACHED*/
    109 	    }
    110 
    111 	if ((fd = creat(path, 0660)) < 0)
    112 	    Error("Cannot create %s.\n", path);
    113 	    /*NOTREACHED*/
    114 
    115 	close(fd);			/* close newly created file */
    116 
    117 	++filename;			/* process next file */
    118 	}
    119 
    120     /* Initialize an empty file placeholder for the grail location. */
    121     if ((fp = fopen(path, "w")) == NULL)
    122 	Error("Cannot create %s.\n", path);
    123     fclose(fp);
    124 
    125     /* create binary monster data base */
    126     path = strrchr(_PATH_MONST, '/') + 1;
    127     if ((Monstfp = fopen(path, "w")) == NULL)
    128 	Error("Cannot update %s.\n", path);
    129     else
    130 	{
    131 	if ((fp = fopen(monsterfile, "r")) == NULL)
    132 	    {
    133 	    fclose(Monstfp);
    134 	    Error("cannot open %s to create monster database.\n", "monsters.asc");
    135 	    }
    136 	else
    137 	    {
    138 	    Curmonster.m_o_strength =
    139 	    Curmonster.m_o_speed =
    140 	    Curmonster.m_maxspeed =
    141 	    Curmonster.m_o_energy =
    142 	    Curmonster.m_melee =
    143 	    Curmonster.m_skirmish = 0.0;
    144 
    145 	    while (fgets(Databuf, SZ_DATABUF, fp) != NULL)
    146 		/* read in text file, convert to binary */
    147 		{
    148 		sscanf(&Databuf[24], "%lf%lf%lf%lf%lf%d%d%lf",
    149 		    &Curmonster.m_strength, &Curmonster.m_brains,
    150 		    &Curmonster.m_speed, &Curmonster.m_energy,
    151 		    &Curmonster.m_experience, &Curmonster.m_treasuretype,
    152 		    &Curmonster.m_type, &Curmonster.m_flock);
    153 		Databuf[24] = '\0';
    154 		strcpy(Curmonster.m_name, Databuf);
    155 		fwrite((char *) &Curmonster, SZ_MONSTERSTRUCT, 1, Monstfp);
    156 		}
    157 	    fclose(fp);
    158 	    fflush(Monstfp);
    159 	    if (ferror(Monstfp))
    160 		Error("Writing %s.\n", path);
    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     path = strrchr(_PATH_MOTD, '/') + 1;
    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 void
    232 Error(const char *str, const char *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(void)
    266 {
    267     if (sizeof(int) != 2)
    268 	return((double) (random() & 0x7fff) / 32768.0);
    269     else
    270 	return((double) random() / 32768.0);
    271 }
    272