-/* $NetBSD: main.c,v 1.4 1997/10/13 02:18:27 lukem Exp $ */
+/* $NetBSD: main.c,v 1.24 2019/02/03 03:19:25 mrg Exp $ */
/*
* Phantasia 3.3.2 -- Interterminal fantasy game
*/
#include <sys/types.h>
+#include <sys/stat.h>
+#include <err.h>
+#include <math.h>
#include <pwd.h>
+#include <setjmp.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include "macros.h"
+#include "phantdefs.h"
+#include "phantstruct.h"
+#include "phantglobs.h"
+#include "pathnames.h"
+
+#undef bool
+#include <curses.h>
/*
* The program allocates as much file space as it needs to store characters,
* main.c Main routines for Phantasia
*/
-#include "include.h"
+static void genchar(int);
+static void initialstate(void);
+static void neatstuff(void);
+static void playinit(void);
+static void procmain(void);
+static long recallplayer(void);
+static long rollnewplayer(void);
+static void titlelist(void);
-int main __P((int, char **));
+int main(int, char **);
int
-main(argc, argv)
- int argc;
- char **argv;
+main(int argc, char **argv)
{
bool noheader = FALSE; /* set if don't want header */
bool headeronly = FALSE; /* set if only want header */
case 'a': /* all users */
activelist();
cleanup(TRUE);
+ __unreachable();
/* NOTREACHED */
case 'p': /* purge old players */
purgeoldplayers();
cleanup(TRUE);
+ __unreachable();
/* NOTREACHED */
case 'S': /* set 'Wizard' */
case 'm': /* monsters */
monstlist();
cleanup(TRUE);
+ __unreachable();
/* NOTREACHED */
case 'b': /* scoreboard */
scorelist();
cleanup(TRUE);
+ __unreachable();
/* NOTREACHED */
}
if (examine) {
changestats(FALSE);
cleanup(TRUE);
+ __unreachable();
/* NOTREACHED */
}
if (!noheader) {
titlelist();
purgeoldplayers(); /* clean up old characters */
}
- if (headeronly)
+ if (headeronly) {
cleanup(TRUE);
- /* NOTREACHED */
+ __unreachable();
+ /* NOTREACHED */
+ }
do
/* get the player structure filled */
case 'Q':
cleanup(TRUE);
+ __unreachable();
/* NOTREACHED */
default:
Timeout = TRUE;
/* update some important player statistics */
- strcpy(Player.p_login, Login);
+ strlcpy(Player.p_login, Login, sizeof(Player.p_login));
time(&seconds);
Player.p_lastused = localtime(&seconds)->tm_yday;
Player.p_status = S_PLAYING;
checkbattle(); /* check for player to player battle */
neatstuff(); /* gurus, medics, etc. */
- if (Player.p_status == S_CLOAKED)
+ if (Player.p_status == S_CLOAKED) {
/* costs 3 mana per turn to be cloaked */
if (Player.p_mana > 3.0)
Player.p_mana -= 3.0;
Player.p_status = S_PLAYING;
Changed = TRUE;
}
+ }
if (Player.p_status != S_PLAYING && Player.p_status != S_CLOAKED)
/* change status back to S_PLAYING */
}
}
-void
-initialstate()
+static void
+initialstate(void)
{
+ struct stat sb;
+ struct passwd *pw;
+
Beyond = FALSE;
Marsh = FALSE;
Throne = FALSE;
Echo = TRUE;
/* setup login name */
- if ((Login = getlogin()) == NULL)
- Login = getpwuid(getuid())->pw_name;
+ if ((Login = getlogin()) == NULL) {
+ pw = getpwuid(getuid());
+ if (pw == NULL) {
+ errx(1, "Who are you?");
+ }
+ Login = pw->pw_name;
+ }
/* open some files */
if ((Playersfp = fopen(_PATH_PEOPLE, "r+")) == NULL)
error(_PATH_PEOPLE);
/* NOTREACHED */
+ if (fileno(Playersfp) < 3)
+ exit(1);
if ((Monstfp = fopen(_PATH_MONST, "r+")) == NULL)
error(_PATH_MONST);
if ((Energyvoidfp = fopen(_PATH_VOID, "r+")) == NULL)
error(_PATH_VOID);
+ if (fstat(fileno(Energyvoidfp), &sb) == -1)
+ error("stat");
+ if (sb.st_size == 0) {
+ /* initialize grail to new location */
+ Enrgyvoid.ev_active = TRUE;
+ Enrgyvoid.ev_x = ROLL(-1.0e6, 2.0e6);
+ Enrgyvoid.ev_y = ROLL(-1.0e6, 2.0e6);
+ writevoid(&Enrgyvoid, 0L);
+ }
+
/* NOTREACHED */
srandom((unsigned) time(NULL)); /* prime random numbers */
}
-long
-rollnewplayer()
+static long
+rollnewplayer(void)
{
int chartype; /* character type */
int ch; /* input */
do {
mvaddstr(20, 0, "Give your character a password [up to 8 characters] ? ");
getstring(Player.p_password, SZ_PASSWORD);
- mvaddstr(21, 0, "One more time to verify ? ");
+ mvaddstr(21, 0, "Enter again to verify: ");
getstring(Databuf, SZ_PASSWORD);
}
while (strcmp(Player.p_password, Databuf) != 0);
return (allocrecord());
}
-void
-procmain()
+static void
+procmain(void)
{
int ch; /* input */
double x; /* desired new x coordinate */
case '5': /* good-bye */
leavegame();
+ __unreachable();
/* NOTREACHED */
case '6': /* cloak */
}
}
-void
-titlelist()
+static void
+titlelist(void)
{
FILE *fp; /* used for opening various files */
bool councilfound = FALSE; /* set if we find a member of the
fclose(fp);
}
/* search for king */
- fseek(Playersfp, 0L, 0);
+ fseek(Playersfp, 0L, SEEK_SET);
while (fread((char *) &Other, SZ_PLAYERSTRUCT, 1, Playersfp) == 1)
if (Other.p_specialtype == SC_KING &&
Other.p_status != S_NOTUSED)
/* found the king */
{
- sprintf(Databuf, "The present ruler is %s Level:%.0f",
+ snprintf(Databuf, SZ_DATABUF,
+ "The present ruler is %s Level:%.0f",
Other.p_name, Other.p_level);
mvaddstr(4, 40 - strlen(Databuf) / 2, Databuf);
kingfound = TRUE;
mvaddstr(4, 24, "There is no ruler at this time.");
/* search for valar */
- fseek(Playersfp, 0L, 0);
+ fseek(Playersfp, 0L, SEEK_SET);
while (fread((char *) &Other, SZ_PLAYERSTRUCT, 1, Playersfp) == 1)
if (Other.p_specialtype == SC_VALAR && Other.p_status != S_NOTUSED)
/* found the valar */
{
- sprintf(Databuf, "The Valar is %s Login: %s", Other.p_name, Other.p_login);
+ snprintf(Databuf, SZ_DATABUF,
+ "The Valar is %s Login: %s",
+ Other.p_name, Other.p_login);
mvaddstr(6, 40 - strlen(Databuf) / 2, Databuf);
break;
}
/* search for council of the wise */
- fseek(Playersfp, 0L, 0);
+ fseek(Playersfp, 0L, SEEK_SET);
Lines = 10;
while (fread((char *) &Other, SZ_PLAYERSTRUCT, 1, Playersfp) == 1)
if (Other.p_specialtype == SC_COUNCIL && Other.p_status != S_NOTUSED)
councilfound = TRUE;
}
/* This assumes a finite (<=5) number of C.O.W.: */
- sprintf(Databuf, "%s Login: %s", Other.p_name, Other.p_login);
+ snprintf(Databuf, SZ_DATABUF,
+ "%s Login: %s", Other.p_name, Other.p_login);
mvaddstr(Lines++, 40 - strlen(Databuf) / 2, Databuf);
}
/* search for the two highest players */
hiexp = 0.0;
nxtlvl = hilvl = 0;
- fseek(Playersfp, 0L, 0);
+ fseek(Playersfp, 0L, SEEK_SET);
while (fread((char *) &Other, SZ_PLAYERSTRUCT, 1, Playersfp) == 1)
if (Other.p_experience > hiexp && Other.p_specialtype <= SC_KING && Other.p_status != S_NOTUSED)
/* highest found so far */
strcpy(nxtname, Other.p_name);
}
mvaddstr(15, 28, "Highest characters are:");
- sprintf(Databuf, "%s Level:%.0f and %s Level:%.0f",
+ snprintf(Databuf, SZ_DATABUF,
+ "%s Level:%.0f and %s Level:%.0f",
hiname, hilvl, nxtname, nxtlvl);
mvaddstr(17, 40 - strlen(Databuf) / 2, Databuf);
&& fgets(Databuf, SZ_DATABUF, fp) != NULL) {
mvaddstr(19, 25, "The last character to die was:");
mvaddstr(20, 40 - strlen(Databuf) / 2, Databuf);
- fclose(fp);
}
+ if (fp)
+ fclose(fp);
refresh();
}
-long
-recallplayer()
+static long
+recallplayer(void)
{
long loc = 0L; /* location in player file */
int loop; /* loop counter */
return (-1L);
}
-void
-neatstuff()
+static void
+neatstuff(void)
{
double temp; /* for temporary calculations */
int ch; /* input */
}
}
-void
-genchar(type)
- int type;
+static void
+genchar(int type)
{
int subscript; /* used for subscripting into Stattable */
- struct charstats *statptr; /* for pointing into Stattable */
+ const struct charstats *statptr; /* for pointing into Stattable */
subscript = type - '1';
Player.p_experience = ROLL(600.0, 200.0);
}
-void
-playinit()
+static void
+playinit(void)
{
/* catch/ingnore signals */
signal(SIGPIPE, ill_sig);
#endif
- initscr(); /* turn on curses */
+ if (!initscr()) { /* turn on curses */
+ fprintf(stderr, "couldn't initialize screen\n");
+ exit (0);
+ }
noecho(); /* do not echo input */
- crmode(); /* do not process erase, kill */
+ cbreak(); /* do not process erase, kill */
clear();
refresh();
Windows = TRUE; /* mark the state */
}
void
-cleanup(doexit)
- int doexit;
+cleanup(int doexit)
{
if (Windows) {
move(LINES - 2, 0);
refresh();
- nocrmode();
+ nocbreak();
endwin();
}
- fclose(Playersfp);
- fclose(Monstfp);
- fclose(Messagefp);
- fclose(Energyvoidfp);
+ if (Playersfp)
+ fclose(Playersfp);
+ if (Monstfp)
+ fclose(Monstfp);
+ if (Messagefp)
+ fclose(Messagefp);
+ if (Energyvoidfp)
+ fclose(Energyvoidfp);
if (doexit)
exit(0);