xref: /csrg-svn/games/phantasia/setup.c (revision 37055)
131409Sbostic /*
231409Sbostic  * setup.c - set up all files for Phantasia
331409Sbostic  */
431409Sbostic #include "include.h"
531409Sbostic #include <sys/types.h>
631409Sbostic #include <sys/stat.h>
731409Sbostic /**/
831409Sbostic /************************************************************************
931409Sbostic /
1031409Sbostic / FUNCTION NAME: main()
1131409Sbostic /
1231409Sbostic / FUNCTION: setup files for Phantasia 3.3.2
1331409Sbostic /
1431409Sbostic / AUTHOR: E. A. Estes, 12/4/85
1531409Sbostic /
1631409Sbostic / ARGUMENTS: none
1731409Sbostic /
1831409Sbostic / RETURN VALUE: none
1931409Sbostic /
2031409Sbostic / MODULES CALLED: time(), exit(), stat(), Error(), creat(), close(), fopen(),
2131409Sbostic /	fgets(), floor(), srandom(), umask(), drandom(), strcpy(), getuid(),
2231409Sbostic /	unlink(), fwrite(), fclose(), sscanf(), printf(), strlen(), fprintf()
2331409Sbostic /
2431409Sbostic / GLOBAL INPUTS: Peoplefile[], Curmonster, _iob[], Databuf[], *Monstfp,
2531409Sbostic /	Lastdead[], Goldfile[], Voidfile[], Motdfile[], Messfile[], Scorefile[],
2631409Sbostic /	Enemyfile[], Monstfile[], Enrgyvoid
2731409Sbostic /
2831409Sbostic / GLOBAL OUTPUTS: Curmonster, Databuf[], *Monstfp, Enrgyvoid
2931409Sbostic /
3031409Sbostic / DESCRIPTION:
3131409Sbostic /
3231409Sbostic /	This program tries to verify the parameters specified in
3331409Sbostic /	the Makefile.
3431409Sbostic /
3531409Sbostic /	Create all necessary files.  Note that nothing needs to be
3631409Sbostic /	put in these files.
3731409Sbostic /	Also, the monster binary data base is created here.
3831409Sbostic /
3931409Sbostic /************************************************************************/
4031409Sbostic 
4131409Sbostic main()
4231409Sbostic {
4331409Sbostic FILE	*fp;			/* for opening files */
4431409Sbostic struct stat	fbuf;		/* for getting files statistics */
4531409Sbostic register char	**filename;	/* for pointing to file names */
4631409Sbostic register int	fd;		/* file descriptor */
4731409Sbostic static char *files[] =		/* all files to create */
4831409Sbostic 	{
4931409Sbostic 	Monstfile,
5031409Sbostic 	Peoplefile,
5131409Sbostic 	Messfile,
5231409Sbostic 	Lastdead,
5331409Sbostic 	Motdfile,
5431409Sbostic 	Goldfile,
5531409Sbostic 	Voidfile,
5631409Sbostic 	Scorefile,
5731409Sbostic #ifdef ENEMY
5831409Sbostic 	Enemyfile,
5931409Sbostic #endif
6031409Sbostic 	(char *) NULL
6131409Sbostic 	};
6231409Sbostic 
6331409Sbostic     srandom((unsigned) time((long *) NULL));	/* prime random numbers */
6431409Sbostic 
6531409Sbostic     umask(0117);		/* only owner can read/write created files */
6631409Sbostic 
6731409Sbostic     if (getuid() != UID)
6831409Sbostic 	fprintf(stderr, "Warning: UID (%d) is not equal to current uid.\n", UID);
6931409Sbostic 
7031409Sbostic     /* check Phantasia destination directory */
7131409Sbostic     if (stat(DEST", &fbuf) < 0)
7231409Sbostic 	/* not found */
7331409Sbostic 	{
7431409Sbostic 	Error("Cannot stat %s.\n", DEST");
7531409Sbostic 	exit(1);
7631409Sbostic 	/*NOTREACHED*/
7731409Sbostic 	}
7831409Sbostic 
7931409Sbostic     if ((fbuf.st_mode & S_IFDIR) == 0)
8031409Sbostic 	/* not a directory */
8131409Sbostic 	Error("%s is not a directory.\n", DEST");
8231409Sbostic 	/*NOTREACHED*/
8331409Sbostic 
8431409Sbostic     /* try to create data files */
8531409Sbostic     filename = &files[0];
8631409Sbostic     while (*filename != NULL)
8731409Sbostic 	/* create each file */
8831409Sbostic 	{
8931409Sbostic 	if (stat(*filename, &fbuf) == 0)
9031409Sbostic 	    /* file exists; remove it */
9131409Sbostic 	    {
9231409Sbostic 	    if (*filename == Peoplefile)
9331409Sbostic 		/* do not reset character file if it already exists */
9431409Sbostic 		{
9531409Sbostic 		++filename;
9631409Sbostic 		continue;
9731409Sbostic 		}
9831409Sbostic 
9931409Sbostic 	    if (unlink(*filename) < 0)
10031409Sbostic 		Error("Cannot unlink %s.\n", *filename);
10131409Sbostic 		/*NOTREACHED*/
10231409Sbostic 	    }
10331409Sbostic 
10431409Sbostic 	if ((fd = creat(*filename, 0660)) < 0)
10531409Sbostic 	    Error("Cannot create %s.\n", *filename);
10631409Sbostic 	    /*NOTREACHED*/
10731409Sbostic 
10831409Sbostic 	close(fd);			/* close newly created file */
10931409Sbostic 
11031409Sbostic 	++filename;			/* process next file */
11131409Sbostic 	}
11231409Sbostic 
11331409Sbostic     /* put holy grail info into energy void file */
11431409Sbostic     Enrgyvoid.ev_active = TRUE;
11531409Sbostic     Enrgyvoid.ev_x = ROLL(-1.0e6, 2.0e6);
11631409Sbostic     Enrgyvoid.ev_y = ROLL(-1.0e6, 2.0e6);
11731409Sbostic     if ((fp = fopen(Voidfile, "w")) == NULL)
11831409Sbostic 	Error("Cannot update %s.\n", Voidfile);
11931409Sbostic     else
12031409Sbostic 	{
12131409Sbostic 	fwrite(&Enrgyvoid, SZ_VOIDSTRUCT, 1, fp);
12231409Sbostic 	fclose(fp);
12331409Sbostic 	}
12431409Sbostic 
12531409Sbostic     /* create binary monster data base */
12631409Sbostic     if ((Monstfp = fopen(Monstfile, "w")) == NULL)
12731409Sbostic 	Error("Cannot update %s.\n", Monstfile);
12831409Sbostic     else
12931409Sbostic 	{
13031409Sbostic 	if ((fp = fopen("monsters.asc", "r")) == NULL)
13131409Sbostic 	    {
13231409Sbostic 	    fclose(Monstfp);
13331409Sbostic 	    Error("cannot open %s to create monster database.\n", "monsters.asc");
13431409Sbostic 	    }
13531409Sbostic 	else
13631409Sbostic 	    {
13731409Sbostic 	    Curmonster.m_o_strength =
13831409Sbostic 	    Curmonster.m_o_speed =
13931409Sbostic 	    Curmonster.m_maxspeed =
14031409Sbostic 	    Curmonster.m_o_energy =
14131409Sbostic 	    Curmonster.m_melee =
14231409Sbostic 	    Curmonster.m_skirmish = 0.0;
14331409Sbostic 
14431409Sbostic 	    while (fgets(Databuf, SZ_DATABUF, fp) != NULL)
14531409Sbostic 		/* read in text file, convert to binary */
14631409Sbostic 		{
14734613Sbostic 		sscanf(&Databuf[24], "%lf%lf%lf%lf%lf%d%d%lf",
14831409Sbostic 		    &Curmonster.m_strength, &Curmonster.m_brains,
14931409Sbostic 		    &Curmonster.m_speed, &Curmonster.m_energy,
15031409Sbostic 		    &Curmonster.m_experience, &Curmonster.m_treasuretype,
15131409Sbostic 		    &Curmonster.m_type, &Curmonster.m_flock);
15231409Sbostic 		Databuf[24] = '\0';
15331409Sbostic 		strcpy(Curmonster.m_name, Databuf);
15431409Sbostic 		fwrite((char *) &Curmonster, SZ_MONSTERSTRUCT, 1, Monstfp);
15531409Sbostic 		}
15631409Sbostic 	    fclose(fp);
15731409Sbostic 	    fclose(Monstfp);
15831409Sbostic 	    }
15931409Sbostic 	}
16031409Sbostic 
161*37055Sbostic #ifdef MAKE_INSTALLS_THIS_AND_DOESNT_WANT_TO_HEAR_ABOUT_IT
16231409Sbostic     /* write to motd file */
16331409Sbostic     printf("One line 'motd' ? ");
16431409Sbostic     if (fgets(Databuf, SZ_DATABUF, stdin) == NULL)
16531409Sbostic 	Databuf[0] = '\0';
16631409Sbostic     if ((fp = fopen(Motdfile, "w")) == NULL)
16731409Sbostic 	Error("Cannot update %s.\n", Motdfile);
16831409Sbostic     else
16931409Sbostic 	{
17031409Sbostic 	fwrite(Databuf, sizeof(char), strlen(Databuf), fp);
17131409Sbostic 	fclose(fp);
17231409Sbostic 	}
17331409Sbostic 
17431409Sbostic     /* report compile-time options */
17531409Sbostic     printf("Compiled options:\n\n");
17631409Sbostic     printf("Phantasia destination directory:  %s\n", DEST");
17731409Sbostic     printf("Wizard:  %s   UID:  %d\n", WIZARD, UID);
17831409Sbostic 
17931409Sbostic #ifdef ENEMY
18031409Sbostic     printf("Enemy list enabled.\n");
18131409Sbostic #else
18231409Sbostic     printf("Enemy list disabled.\n");
18331409Sbostic #endif
18431409Sbostic 
18531409Sbostic #ifdef SHELL
18631409Sbostic     printf("Shell escapes enabled.  Default shell:  %s\n", SHELL);
18731409Sbostic #else
18831409Sbostic     printf("Shell escapes disabled.\n");
18931409Sbostic #endif
19031409Sbostic 
19131409Sbostic #ifdef BSD41
19231409Sbostic     printf("Compiled for BSD 4.1\n");
19331409Sbostic #endif
19431409Sbostic 
19531409Sbostic #ifdef BSD42
19631409Sbostic     printf("Compiled for BSD 4.2\n");
19731409Sbostic #endif
19831409Sbostic 
19931409Sbostic #ifdef SYS3
20031409Sbostic     printf("Compiled for System III\n");
20131409Sbostic #endif
20231409Sbostic 
20331409Sbostic #ifdef SYS5
20431409Sbostic     printf("Compiled for System V\n");
20531409Sbostic #endif
206*37055Sbostic #endif
20731409Sbostic 
20831409Sbostic     exit(0);
20931409Sbostic     /*NOTREACHED*/
21031409Sbostic }
21131409Sbostic /**/
21231409Sbostic /************************************************************************
21331409Sbostic /
21431409Sbostic / FUNCTION NAME: Error()
21531409Sbostic /
21631409Sbostic / FUNCTION: print an error message, and exit
21731409Sbostic /
21831409Sbostic / AUTHOR: E. A. Estes, 12/4/85
21931409Sbostic /
22031409Sbostic / ARGUMENTS:
22131409Sbostic /	char *str - format string for printf()
22231409Sbostic /	char *file - file which caused error
22331409Sbostic /
22431409Sbostic / RETURN VALUE: none
22531409Sbostic /
22631409Sbostic / MODULES CALLED: exit(), perror(), fprintf()
22731409Sbostic /
22831409Sbostic / GLOBAL INPUTS: _iob[]
22931409Sbostic /
23031409Sbostic / GLOBAL OUTPUTS: none
23131409Sbostic /
23231409Sbostic / DESCRIPTION:
23331409Sbostic /	Print an error message, then exit.
23431409Sbostic /
23531409Sbostic /************************************************************************/
23631409Sbostic 
23731409Sbostic Error(str, file)
23831409Sbostic char	*str, *file;
23931409Sbostic {
24031409Sbostic     fprintf(stderr, "Error: ");
24131409Sbostic     fprintf(stderr, str, file);
24231409Sbostic     perror(file);
24331409Sbostic     exit(1);
24431409Sbostic     /*NOTREACHED*/
24531409Sbostic }
24631409Sbostic /**/
24731409Sbostic /************************************************************************
24831409Sbostic /
24931409Sbostic / FUNCTION NAME: drandom()
25031409Sbostic /
25131409Sbostic / FUNCTION: return a random number
25231409Sbostic /
25331409Sbostic / AUTHOR: E. A. Estes, 2/7/86
25431409Sbostic /
25531409Sbostic / ARGUMENTS: none
25631409Sbostic /
25731409Sbostic / RETURN VALUE: none
25831409Sbostic /
25931409Sbostic / MODULES CALLED: random()
26031409Sbostic /
26131409Sbostic / GLOBAL INPUTS: none
26231409Sbostic /
26331409Sbostic / GLOBAL OUTPUTS: none
26431409Sbostic /
26531409Sbostic / DESCRIPTION:
26631409Sbostic /
26731409Sbostic /************************************************************************/
26831409Sbostic 
26931409Sbostic double
27031409Sbostic drandom()
27131409Sbostic {
27231409Sbostic     if (sizeof(int) != 2)
27331409Sbostic 	return((double) (random() & 0x7fff) / 32768.0);
27431409Sbostic     else
27531409Sbostic 	return((double) random() / 32768.0);
27631409Sbostic }
277