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