-/* $NetBSD: scores.c,v 1.10 1997/10/18 20:03:48 christos Exp $ */
+/* $NetBSD: scores.c,v 1.21 2012/06/19 05:30:44 dholland Exp $ */
/*
* scores.c Larn is copyrighted 1986 by Noah Morgan.
*/
#include <sys/cdefs.h>
#ifndef lint
-__RCSID("$NetBSD: scores.c,v 1.10 1997/10/18 20:03:48 christos Exp $");
+__RCSID("$NetBSD: scores.c,v 1.21 2012/06/19 05:30:44 dholland Exp $");
#endif /* not lint */
#include <sys/types.h>
#include <sys/times.h>
struct log_fmt { /* 102 bytes struct for the log file */
long score; /* the players score */
- time_t diedtime; /* time when game was over */
+ int32_t diedtime; /* time when game was over */
short cavelev;/* level in caves */
short diff; /* difficulty player played at */
#ifdef EXTRA
long moves; /* number of moves made by player */
short ac; /* armor class of player */
short hp, hpmax; /* players hitpoints */
- short cputime;/* cpu time needed in seconds */
+ short cputime;/* CPU time needed in seconds */
short killed, spused; /* monsters killed and spells cast */
- short usage; /* usage of the cpu in % */
+ short usage; /* usage of the CPU in % */
short lev; /* player level */
#endif
char who[12];/* player name */
static struct scofmt sco[SCORESIZE]; /* the structure for the scoreboard */
static struct wscofmt winr[SCORESIZE]; /* struct for the winning scoreboard */
static struct log_fmt logg; /* structure for the log file */
-static char *whydead[] = {
+static const char *whydead[] = {
"quit", "suspended", "self - annihilated", "shot by an arrow",
"hit by a dart", "fell into a pit", "fell into a bottomless pit",
"a winner", "trapped in solid rock", "killed by a missing save file",
"died a post mortem death", "wasted by a malloc() failure"
};
+static int readboard(void);
+static int writeboard(void);
+static int winshou(void);
+static int shou(int);
+static int sortboard(void);
+static void newscore(long, char *, int, int);
+static void new1sub(long, int, char *, long);
+static void new2sub(long, int, char *, int);
+static void diedsub(int);
+
/*
* readboard() Function to read in the scoreboard into a static buffer
*
* returns -1 if unable to read in the scoreboard, returns 0 if all is OK
*/
-int
-readboard()
+static int
+readboard(void)
{
int i;
- seteuid(euid);
+ if (gid != egid)
+ setegid(egid);
i = lopen(scorefile);
- seteuid(uid);
+ if (gid != egid)
+ setegid(gid);
if (i < 0) {
lprcat("Can't read scoreboard\n");
lflush();
*
* returns -1 if unable to write the scoreboard, returns 0 if all is OK
*/
-int
-writeboard()
+static int
+writeboard(void)
{
int i;
set_score_output();
- seteuid(euid);
+ if (gid != egid)
+ setegid(egid);
i = lcreat(scorefile);
- seteuid(uid);
+ if (gid != egid)
+ setegid(gid);
if (i < 0) {
lprcat("Can't write scoreboard\n");
lflush();
* returns -1 if unable to write the scoreboard, returns 0 if all is OK
*/
int
-makeboard()
+makeboard(void)
{
int i;
+ set_score_output();
for (i = 0; i < SCORESIZE; i++) {
winr[i].taxes = winr[i].score = sco[i].score = 0;
winr[i].order = sco[i].order = i;
}
if (writeboard())
return (-1);
- seteuid(euid);
+ if (gid != egid)
+ setegid(egid);
chmod(scorefile, 0660);
- seteuid(uid);
+ if (gid != egid)
+ setegid(gid);
return (0);
}
* the winners scoreboard.
*/
int
-hashewon()
+hashewon(void)
{
int i;
c[HARDGAME] = 0;
* Returns amount actually paid.
*/
long
-paytaxes(x)
- long x;
+paytaxes(long x)
{
int i;
long amt;
* (Ughhhhh) */
winr[i].taxes -= amt;
outstanding_taxes -= amt;
+ set_score_output();
if (writeboard() < 0)
return (0);
return (amt);
*
* Returns the number of players on scoreboard that were shown
*/
-int
-winshou()
+static int
+winshou(void)
{
struct wscofmt *p;
int i, j, count;
if (p->order == i) {
if (p->score) {
count++;
- lprintf("%10d %2d %5d Mobuls %s \n",
+ lprintf("%10ld %2ld %5ld Mobuls %s \n",
(long) p->score, (long) p->hardlev, (long) p->timeused, p->who);
}
break;
* Enter with 0 to list the scores, enter with 1 to list inventories too
* Returns the number of players on scoreboard that were shown
*/
-int
-shou(x)
- int x;
+static int
+shou(int x)
{
int i, j, n, k;
int count;
if (sco[j].order == i) {
if (sco[j].score) {
count++;
- lprintf("%10d %2d %s ",
+ lprintf("%10ld %2ld %s ",
(long) sco[j].score, (long) sco[j].hardlev, sco[j].who);
if (sco[j].what < 256)
lprintf("killed by a %s", monster[sco[j].what].name);
*/
static char esb[] = "The scoreboard is empty.\n";
void
-showscores()
+showscores(void)
{
int i, j;
lflush();
* Returns nothing of value
*/
void
-showallscores()
+showallscores(void)
{
int i, j;
lflush();
*
* Returns 0 if no sorting done, else returns 1
*/
-int
-sortboard()
+static int
+sortboard(void)
{
int i, j = 0, pos;
long jdat;
* died() reason # in whyded, and TRUE/FALSE in winner if a winner
* ex. newscore(1000, "player 1", 32, 0);
*/
-void
-newscore(score, whoo, whyded, winner)
- long score;
- int winner, whyded;
- char *whoo;
+static void
+newscore(long score, char *whoo, int whyded, int winner)
{
int i;
long taxes;
* slot in scoreboard in i, and the tax bill in taxes.
* Returns nothing of value
*/
-void
-new1sub(score, i, whoo, taxes)
- long score, taxes;
- int i;
- char *whoo;
+static void
+new1sub(long score, int i, char *whoo, long taxes)
{
struct wscofmt *p;
p = &winr[i];
* died() reason # in whyded, and slot in scoreboard in i.
* Returns nothing of value
*/
-void
-new2sub(score, i, whoo, whyded)
- long score;
- int i, whyded;
- char *whoo;
+static void
+new2sub(long score, int i, char *whoo, int whyded)
{
int j;
struct scofmt *p;
static int scorerror;
void
-died(x)
- int x;
+died(int x)
{
int f, win;
- char ch, *mod;
+ char ch;
+ const char *mod;
time_t zzz;
if (c[LIFEPROT] > 0) { /* if life protection */
switch ((x > 0) ? x : -x) {
set_score_output();
if ((wizard == 0) && (c[GOLD] > 0)) { /* wizards can't score */
#ifndef NOLOG
- seteuid(euid);
+ if (gid != egid)
+ setegid(egid);
if (lappend(logfile) < 0) { /* append to file */
if (lcreat(logfile) < 0) { /* and can't create new
* log file */
lflush();
exit(0);
}
- seteuid(euid);
+ if (gid != egid)
+ setegid(egid);
chmod(logfile, 0660);
- seteuid(uid);
+ if (gid != egid)
+ setegid(gid);
}
- seteuid(uid);
+ if (gid != egid)
+ setegid(gid);
strcpy(logg.who, loginname);
logg.score = c[GOLD];
logg.diff = c[HARDGAME];
mod = "an";
else
mod = "a";
- sprintf(logg.what, "killed by %s %s", mod, monster[x].name);
+ snprintf(logg.what, sizeof(logg.what),
+ "killed by %s %s", mod, monster[x].name);
} else
- sprintf(logg.what, "%s", whydead[x - 256]);
+ snprintf(logg.what, sizeof(logg.what),
+ "%s", whydead[x - 256]);
logg.cavelev = level;
- time(&zzz); /* get cpu time -- write out score info */
+ time(&zzz); /* get CPU time -- write out score info */
logg.diedtime = zzz;
#ifdef EXTRA
- times(&cputime);/* get cpu time -- write out score info */
+ times(&cputime);/* get CPU time -- write out score info */
logg.cputime = i = (cputime.tms_utime + cputime.tms_stime) / 60 + c[CPUTIME];
logg.lev = c[LEVEL];
logg.ac = c[AC];
* game
*/
if (x != 257) {
- if (sortboard())
+ if (sortboard()) {
+ set_score_output();
scorerror = writeboard();
+ }
}
}
if ((x == 256) || (x == 257) || (f != 0))
* diedsub(x) Subroutine to print out the line showing the player when he is killed
* int x;
*/
-void
-diedsub(x)
- int x;
+static void
+diedsub(int x)
{
- char ch, *mod;
- lprintf("Score: %d, Diff: %d, %s ", (long) c[GOLD], (long) c[HARDGAME], logname);
+ char ch;
+ const char *mod;
+
+ lprintf("Score: %ld, Diff: %ld, %s ", (long) c[GOLD], (long) c[HARDGAME], logname);
if (x < 256) {
ch = *monster[x].name;
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')
* diedlog() Subroutine to read a log file and print it out in ascii format
*/
void
-diedlog()
+diedlog(void)
{
int n;
char *p;
+ static char q[] = "?";
struct stat stbuf;
+ time_t t;
+
lcreat((char *) 0);
if (lopen(logfile) < 0) {
lprintf("Can't locate log file <%s>\n", logfile);
return;
}
- if (fstat(fd, &stbuf) < 0) {
+ if (fstat(io_infd, &stbuf) < 0) {
lprintf("Can't stat log file <%s>\n", logfile);
return;
}
for (n = stbuf.st_size / sizeof(struct log_fmt); n > 0; --n) {
lrfill((char *) &logg, sizeof(struct log_fmt));
- p = ctime(&logg.diedtime);
- p[16] = '\n';
- p[17] = 0;
- lprintf("Score: %d, Diff: %d, %s %s on %d at %s", (long) (logg.score), (long) (logg.diff), logg.who, logg.what, (long) (logg.cavelev), p + 4);
+ t = logg.diedtime;
+ if ((p = ctime(&t)) == NULL)
+ p = q;
+ else {
+ p[16] = '\n';
+ p[17] = 0;
+ }
+ lprintf("Score: %ld, Diff: %ld, %s %s on %ld at %s", (long) (logg.score), (long) (logg.diff), logg.who, logg.what, (long) (logg.cavelev), p + 4);
#ifdef EXTRA
if (logg.moves <= 0)
logg.moves = 1;
- lprintf(" Experience Level: %d, AC: %d, HP: %d/%d, Elapsed Time: %d minutes\n", (long) (logg.lev), (long) (logg.ac), (long) (logg.hp), (long) (logg.hpmax), (long) (logg.elapsedtime));
- lprintf(" CPU time used: %d seconds, Machine usage: %d.%02d%%\n", (long) (logg.cputime), (long) (logg.usage / 100), (long) (logg.usage % 100));
- lprintf(" BYTES in: %d, out: %d, moves: %d, deaths: %d, spells cast: %d\n", (long) (logg.bytin), (long) (logg.bytout), (long) (logg.moves), (long) (logg.killed), (long) (logg.spused));
- lprintf(" out bytes per move: %d, time per move: %d ms\n", (long) (logg.bytout / logg.moves), (long) ((logg.cputime * 1000) / logg.moves));
+ lprintf(" Experience Level: %ld, AC: %ld, HP: %ld/%ld, Elapsed Time: %ld minutes\n", (long) (logg.lev), (long) (logg.ac), (long) (logg.hp), (long) (logg.hpmax), (long) (logg.elapsedtime));
+ lprintf(" CPU time used: %ld seconds, Machine usage: %ld.%02ld%%\n", (long) (logg.cputime), (long) (logg.usage / 100), (long) (logg.usage % 100));
+ lprintf(" BYTES in: %ld, out: %ld, moves: %ld, deaths: %ld, spells cast: %ld\n", (long) (logg.bytin), (long) (logg.bytout), (long) (logg.moves), (long) (logg.killed), (long) (logg.spused));
+ lprintf(" out bytes per move: %ld, time per move: %ld ms\n", (long) (logg.bytout / logg.moves), (long) ((logg.cputime * 1000) / logg.moves));
#endif
}
lflush();
if (havepid != -1)
return (havepid); /* already did it */
lflush(); /* flush any pending I/O */
- sprintf(name, "%s\n", nam); /* append a \n to name */
+ snprintf(name, sizeof(name), "%s\n", nam);/* append a \n to name */
if (lopen(playerids) < 0) { /* no file, make it */
- if ((fd7 = creat(playerids, 0666)) < 0)
+ if ((fd7 = creat(playerids, 0664)) < 0)
return (-1); /* can't make it */
close(fd7);
goto addone; /* now append new playerid record to file */
addone:
if (lappend(playerids) < 0)
return (-1); /* can't open file for append */
- lprintf("%d\n%s", (long) ++high, name); /* new id # and name */
+ lprintf("%ld\n%s", (long) ++high, name); /* new id # and name */
lwclose();
lcreat((char *) 0); /* re-open terminal channel */
return (high);