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