]> git.cameronkatri.com Git - bsdgames-darwin.git/blobdiff - larn/scores.c
Fix '=' for '==' in a test. From NAKAJIMA Yoshihiro in PR 42177.
[bsdgames-darwin.git] / larn / scores.c
index 2fbe635ed77a69245a966bb1d637647c9eb25cb7..a6a47cf64531bb3cfd3fe5f1655cc06ad4212afd 100644 (file)
@@ -1,4 +1,4 @@
-/*     $NetBSD: scores.c,v 1.10 1997/10/18 20:03:48 christos Exp $     */
+/*     $NetBSD: scores.c,v 1.18 2009/08/12 08:04:05 dholland Exp $     */
 
 /*
  * scores.c                     Larn is copyrighted 1986 by Noah Morgan.
@@ -26,7 +26,7 @@
  */
 #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.18 2009/08/12 08:04:05 dholland Exp $");
 #endif                         /* not lint */
 #include <sys/types.h>
 #include <sys/times.h>
@@ -74,9 +74,9 @@ struct log_fmt {              /* 102 bytes struct for the log file                             */
        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                                                                           */
@@ -86,7 +86,7 @@ struct log_fmt {              /* 102 bytes struct for the log file                             */
 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",
@@ -102,19 +102,31 @@ static char    *whydead[] = {
        "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
+static int
 readboard()
 {
        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();
@@ -132,15 +144,17 @@ readboard()
  *
  * returns -1 if unable to write the scoreboard, returns 0 if all is OK
  */
-int
+static int
 writeboard()
 {
        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();
@@ -162,15 +176,18 @@ int
 makeboard()
 {
        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);
 }
 
@@ -226,6 +243,7 @@ paytaxes(x)
                                                         * (Ughhhhh) */
                                winr[i].taxes -= amt;
                                outstanding_taxes -= amt;
+                               set_score_output();
                                if (writeboard() < 0)
                                        return (0);
                                return (amt);
@@ -238,7 +256,7 @@ paytaxes(x)
  *
  * Returns the number of players on scoreboard that were shown
  */
-int
+static int
 winshou()
 {
        struct wscofmt *p;
@@ -260,7 +278,7 @@ winshou()
                                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;
@@ -277,7 +295,7 @@ winshou()
  * 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
+static int
 shou(x)
        int             x;
 {
@@ -297,7 +315,7 @@ shou(x)
                                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);
@@ -382,7 +400,7 @@ showallscores()
  *
  * Returns 0 if no sorting done, else returns 1
  */
-int
+static int
 sortboard()
 {
        int    i, j = 0, pos;
@@ -421,7 +439,7 @@ sortboard()
  *     died() reason # in whyded, and TRUE/FALSE in winner if a winner
  * ex.         newscore(1000, "player 1", 32, 0);
  */
-void
+static void
 newscore(score, whoo, whyded, winner)
        long            score;
        int             winner, whyded;
@@ -490,7 +508,7 @@ newscore(score, whoo, whyded, winner)
  *     slot in scoreboard in i, and the tax bill in taxes.
  * Returns nothing of value
  */
-void
+static void
 new1sub(score, i, whoo, taxes)
        long            score, taxes;
        int             i;
@@ -517,7 +535,7 @@ new1sub(score, i, whoo, taxes)
  *     died() reason # in whyded, and slot in scoreboard in i.
  * Returns nothing of value
  */
-void
+static void
 new2sub(score, i, whoo, whyded)
        long            score;
        int             i, whyded;
@@ -587,7 +605,8 @@ died(x)
        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) {
@@ -643,7 +662,8 @@ invalid:
        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 */
@@ -654,11 +674,14 @@ invalid:
                                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];
@@ -668,14 +691,16 @@ invalid:
                                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];
@@ -698,8 +723,10 @@ invalid:
                 * game
                 */
                if (x != 257) {
-                       if (sortboard())
+                       if (sortboard()) {
+                               set_score_output();
                                scorerror = writeboard();
+                       }
                }
        }
        if ((x == 256) || (x == 257) || (f != 0))
@@ -715,12 +742,13 @@ invalid:
  * 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')
@@ -750,7 +778,7 @@ diedlog()
                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;
        }
@@ -759,14 +787,14 @@ diedlog()
                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);
+               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();
@@ -797,9 +825,9 @@ getplid(nam)
        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 */
@@ -824,7 +852,7 @@ getplid(nam)
 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);