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>
756081Sbostic #include <stdlib.h>
8*69460Smkm #include <unistd.h>
931409Sbostic /**/
1031409Sbostic /************************************************************************
1131409Sbostic /
1231409Sbostic / FUNCTION NAME: main()
1331409Sbostic /
1431409Sbostic / FUNCTION: setup files for Phantasia 3.3.2
1531409Sbostic /
1631409Sbostic / AUTHOR: E. A. Estes, 12/4/85
1731409Sbostic /
1831409Sbostic / ARGUMENTS: none
1931409Sbostic /
2031409Sbostic / RETURN VALUE: none
2131409Sbostic /
2231409Sbostic / MODULES CALLED: time(), exit(), stat(), Error(), creat(), close(), fopen(),
2331409Sbostic / fgets(), floor(), srandom(), umask(), drandom(), strcpy(), getuid(),
2431409Sbostic / unlink(), fwrite(), fclose(), sscanf(), printf(), strlen(), fprintf()
2531409Sbostic /
2640279Sbostic / GLOBAL INPUTS: Curmonster, _iob[], Databuf[], *Monstfp, 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
4156081Sbostic static char *files[] = { /* all files to create */
4240279Sbostic _PATH_MONST,
4340279Sbostic _PATH_PEOPLE,
4440279Sbostic _PATH_MESS,
4540279Sbostic _PATH_LASTDEAD,
4640279Sbostic _PATH_MOTD,
4740279Sbostic _PATH_GOLD,
4840279Sbostic _PATH_VOID,
4940279Sbostic _PATH_SCORE,
5056081Sbostic NULL,
5156081Sbostic };
5231409Sbostic
5356081Sbostic char *monsterfile="monsters.asc";
5456081Sbostic
5556081Sbostic int
main(argc,argv)5656081Sbostic main(argc, argv)
5756081Sbostic int argc;
5856081Sbostic char *argv[];
5956081Sbostic {
6056081Sbostic register char **filename; /* for pointing to file names */
6156081Sbostic register int fd; /* file descriptor */
6256081Sbostic FILE *fp; /* for opening files */
6356081Sbostic struct stat fbuf; /* for getting files statistics */
6456081Sbostic int ch;
6556081Sbostic
6656081Sbostic while ((ch = getopt(argc, argv, "m:")) != EOF)
6756081Sbostic switch(ch) {
6856081Sbostic case 'm':
6956081Sbostic monsterfile = optarg;
7056081Sbostic break;
7156081Sbostic case '?':
7256081Sbostic default:
7356081Sbostic break;
7456081Sbostic }
7556081Sbostic argc -= optind;
7656081Sbostic argv += optind;
7756081Sbostic
7831409Sbostic srandom((unsigned) time((long *) NULL)); /* prime random numbers */
7931409Sbostic
8031409Sbostic umask(0117); /* only owner can read/write created files */
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 {
9040279Sbostic 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);
11540279Sbostic if ((fp = fopen(_PATH_VOID, "w")) == NULL)
11640279Sbostic 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 */
12440279Sbostic if ((Monstfp = fopen(_PATH_MONST, "w")) == NULL)
12540279Sbostic Error("Cannot update %s.\n", _PATH_MONST);
12631409Sbostic else
12731409Sbostic {
12856081Sbostic if ((fp = fopen(monsterfile, "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';
16440279Sbostic if ((fp = fopen(_PATH_MOTD, "w")) == NULL)
16540279Sbostic 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");
17440279Sbostic printf("Phantasia destination directory: %s\n", _PATH_PHANTDIR);
17541317Sbostic printf("Wizard: root UID: 0\n");
17631409Sbostic
17731409Sbostic #ifdef BSD41
17831409Sbostic printf("Compiled for BSD 4.1\n");
17931409Sbostic #endif
18031409Sbostic
18131409Sbostic #ifdef BSD42
18231409Sbostic printf("Compiled for BSD 4.2\n");
18331409Sbostic #endif
18431409Sbostic
18531409Sbostic #ifdef SYS3
18631409Sbostic printf("Compiled for System III\n");
18731409Sbostic #endif
18831409Sbostic
18931409Sbostic #ifdef SYS5
19031409Sbostic printf("Compiled for System V\n");
19131409Sbostic #endif
19237055Sbostic #endif
19331409Sbostic
19431409Sbostic exit(0);
19531409Sbostic /*NOTREACHED*/
19631409Sbostic }
19731409Sbostic /**/
19831409Sbostic /************************************************************************
19931409Sbostic /
20031409Sbostic / FUNCTION NAME: Error()
20131409Sbostic /
20231409Sbostic / FUNCTION: print an error message, and exit
20331409Sbostic /
20431409Sbostic / AUTHOR: E. A. Estes, 12/4/85
20531409Sbostic /
20631409Sbostic / ARGUMENTS:
20731409Sbostic / char *str - format string for printf()
20831409Sbostic / char *file - file which caused error
20931409Sbostic /
21031409Sbostic / RETURN VALUE: none
21131409Sbostic /
21231409Sbostic / MODULES CALLED: exit(), perror(), fprintf()
21331409Sbostic /
21431409Sbostic / GLOBAL INPUTS: _iob[]
21531409Sbostic /
21631409Sbostic / GLOBAL OUTPUTS: none
21731409Sbostic /
21831409Sbostic / DESCRIPTION:
21931409Sbostic / Print an error message, then exit.
22031409Sbostic /
22131409Sbostic /************************************************************************/
22231409Sbostic
Error(str,file)22331409Sbostic Error(str, file)
22431409Sbostic char *str, *file;
22531409Sbostic {
22631409Sbostic fprintf(stderr, "Error: ");
22731409Sbostic fprintf(stderr, str, file);
22831409Sbostic perror(file);
22931409Sbostic exit(1);
23031409Sbostic /*NOTREACHED*/
23131409Sbostic }
23231409Sbostic /**/
23331409Sbostic /************************************************************************
23431409Sbostic /
23531409Sbostic / FUNCTION NAME: drandom()
23631409Sbostic /
23731409Sbostic / FUNCTION: return a random number
23831409Sbostic /
23931409Sbostic / AUTHOR: E. A. Estes, 2/7/86
24031409Sbostic /
24131409Sbostic / ARGUMENTS: none
24231409Sbostic /
24331409Sbostic / RETURN VALUE: none
24431409Sbostic /
24531409Sbostic / MODULES CALLED: random()
24631409Sbostic /
24731409Sbostic / GLOBAL INPUTS: none
24831409Sbostic /
24931409Sbostic / GLOBAL OUTPUTS: none
25031409Sbostic /
25131409Sbostic / DESCRIPTION:
25231409Sbostic /
25331409Sbostic /************************************************************************/
25431409Sbostic
25531409Sbostic double
drandom()25631409Sbostic drandom()
25731409Sbostic {
25831409Sbostic if (sizeof(int) != 2)
25931409Sbostic return((double) (random() & 0x7fff) / 32768.0);
26031409Sbostic else
26131409Sbostic return((double) random() / 32768.0);
26231409Sbostic }
263