]> git.cameronkatri.com Git - bsdgames-darwin.git/blobdiff - tetris/scores.c
Center game board on screen. OK from christos@.
[bsdgames-darwin.git] / tetris / scores.c
index 83b2c36ad3267764bcdd2e2baa037bec7a0e7dfd..2157e8bdf36ef43c1ac040111b6a24b18a664855 100644 (file)
@@ -1,4 +1,4 @@
-/*     $NetBSD: scores.c,v 1.17 2009/06/01 04:03:26 dholland Exp $     */
+/*     $NetBSD: scores.c,v 1.22 2014/03/22 19:05:30 dholland Exp $     */
 
 /*-
  * Copyright (c) 1992, 1993
@@ -50,7 +50,7 @@
 #include <string.h>
 #include <sys/stat.h>
 #include <time.h>
-#include <termcap.h>
+#include <term.h>
 #include <unistd.h>
 
 #include "pathnames.h"
 #include "scores.h"
 #include "tetris.h"
 
+/*
+ * Allow updating the high scores unless we're built as part of /rescue.
+ */
+#ifndef RESCUEDIR
+#define ALLOW_SCORE_UPDATES
+#endif
+
 /*
  * Within this code, we can hang onto one extra "high score", leaving
  * room for our current score (whether or not it is high).
@@ -374,18 +381,20 @@ getscores(int *fdp)
        struct highscore_header header;
        int sd, mint, lck;
        mode_t mask;
-       const char *mstr, *human;
+       const char *human;
        int doflip;
        int serrno;
        ssize_t result;
 
+#ifdef ALLOW_SCORE_UPDATES
        if (fdp != NULL) {
                mint = O_RDWR | O_CREAT;
                human = "read/write";
                lck = LOCK_EX;
-       } else {
+       } else
+#endif
+       {
                mint = O_RDONLY;
-               mstr = "r";
                human = "reading";
                lck = LOCK_SH;
        }
@@ -403,6 +412,7 @@ getscores(int *fdp)
                 * trying to write it, don't fail -- we can still show
                 * the player the score they got.
                 */
+               errno = serrno;
                if (fdp != NULL || errno != ENOENT) {
                        warn("Cannot open %s for %s", _PATH_SCOREFILE, human);
                }
@@ -448,8 +458,7 @@ getscores(int *fdp)
        result = read(sd, &header, sizeof(header));
        if (result < 0) {
                warn("Score file %s: read", _PATH_SCOREFILE);
-               close(sd);
-               goto fail;
+               goto sdfail;
        }
        if (result != 0 && (size_t)result != sizeof(header)) {
                warnx("Score file %s: read: unexpected EOF", _PATH_SCOREFILE);
@@ -460,13 +469,12 @@ getscores(int *fdp)
                if (lseek(sd, 0, SEEK_SET) < 0) {
                        /* ? */
                        warn("Score file %s: lseek", _PATH_SCOREFILE);
-                       goto fail;
+                       goto sdfail;
                }
                if (ftruncate(sd, 0) == 0) {
                        result = 0;
                } else {
-                       close(sd);
-                       goto fail;
+                       goto sdfail;
                }
        }
 
@@ -491,17 +499,17 @@ getscores(int *fdp)
                        } else {
                                warnx("Score file %s: Unknown endian tag %u",
                                        _PATH_SCOREFILE, header.hsh_endiantag);
-                               goto fail;
+                               goto sdfail;
                        }
 
                        if (header.hsh_version != HSH_VERSION) {
                                warnx("Score file %s: Unknown version code %u",
                                        _PATH_SCOREFILE, header.hsh_version);
-                               goto fail;
+                               goto sdfail;
                        }
 
                        if (readscores(sd, doflip) < 0) {
-                               goto fail;
+                               goto sdfail;
                        }
                } else {
                        /*
@@ -511,7 +519,7 @@ getscores(int *fdp)
                        result = scorefile_probe(sd);
                        if (lseek(sd, 0, SEEK_SET) < 0) {
                                warn("Score file %s: lseek", _PATH_SCOREFILE);
-                               goto fail;
+                               goto sdfail;
                        }
                        switch (result) {
                        case SCOREFILE_CURRENT:
@@ -533,10 +541,10 @@ getscores(int *fdp)
                                result = readscores50(sd, 1 /* do flip */);
                                break;
                        default:
-                               goto fail;
+                               goto sdfail;
                        }
                        if (result < 0) {
-                               goto fail;
+                               goto sdfail;
                        }
                }
        }
@@ -549,6 +557,8 @@ getscores(int *fdp)
 
        return;
 
+sdfail:
+       close(sd);
  fail:
        if (fdp != NULL) {
                *fdp = -1;
@@ -556,6 +566,7 @@ getscores(int *fdp)
        nscores = 0;
 }
 
+#ifdef ALLOW_SCORE_UPDATES
 /*
  * Paranoid write wrapper; unlike fwrite() it preserves errno.
  */
@@ -578,6 +589,7 @@ dowrite(int sd, const void *vbuf, size_t len)
        }
        return 0;
 }
+#endif /* ALLOW_SCORE_UPDATES */
 
 /*
  * Write the score file out.
@@ -585,6 +597,7 @@ dowrite(int sd, const void *vbuf, size_t len)
 static void
 putscores(int sd)
 {
+#ifdef ALLOW_SCORE_UPDATES
        struct highscore_header header;
        struct highscore_ondisk buf[MAXHISCORES];
        int i;
@@ -618,6 +631,9 @@ putscores(int sd)
        return;
  fail:
        warnx("high scores may be damaged");
+#else
+       (void)sd;
+#endif /* ALLOW_SCORE_UPDATES */
 }
 
 /*
@@ -845,7 +861,7 @@ showscores(int level)
         * the high scores; we do not need to check for printing in highlight
         * mode.  If SOstr is null, we can't do highlighting anyway.
         */
-       me = level && SOstr ? thisuser() : NULL;
+       me = level && enter_standout_mode ? thisuser() : NULL;
 
        /*
         * Set times to 0 except for high score on each level.
@@ -923,12 +939,12 @@ printem(int level, int offset, struct highscore *hs, int n, const char *me)
                            sp->hs_level == level &&
                            sp->hs_score == score &&
                            strcmp(sp->hs_name, me) == 0) {
-                               putpad(SOstr);
+                               putpad(enter_standout_mode);
                                highlight = 1;
                        }
                        (void)printf("%s", buf);
                        if (highlight) {
-                               putpad(SEstr);
+                               putpad(exit_standout_mode);
                                highlight = 0;
                        }