-/* $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
#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).
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;
}
* 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);
}
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);
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;
}
}
} 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 {
/*
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:
result = readscores50(sd, 1 /* do flip */);
break;
default:
- goto fail;
+ goto sdfail;
}
if (result < 0) {
- goto fail;
+ goto sdfail;
}
}
}
return;
+sdfail:
+ close(sd);
fail:
if (fdp != NULL) {
*fdp = -1;
nscores = 0;
}
+#ifdef ALLOW_SCORE_UPDATES
/*
* Paranoid write wrapper; unlike fwrite() it preserves errno.
*/
}
return 0;
}
+#endif /* ALLOW_SCORE_UPDATES */
/*
* Write the score file out.
static void
putscores(int sd)
{
+#ifdef ALLOW_SCORE_UPDATES
struct highscore_header header;
struct highscore_ondisk buf[MAXHISCORES];
int i;
return;
fail:
warnx("high scores may be damaged");
+#else
+ (void)sd;
+#endif /* ALLOW_SCORE_UPDATES */
}
/*
* 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.
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;
}