scores.c revision 1.17 1 1.17 dholland /* $NetBSD: scores.c,v 1.17 2009/06/01 04:03:26 dholland Exp $ */
2 1.2 cgd
3 1.1 cgd /*-
4 1.1 cgd * Copyright (c) 1992, 1993
5 1.1 cgd * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * This code is derived from software contributed to Berkeley by
8 1.1 cgd * Chris Torek and Darren F. Provine.
9 1.1 cgd *
10 1.1 cgd * Redistribution and use in source and binary forms, with or without
11 1.1 cgd * modification, are permitted provided that the following conditions
12 1.1 cgd * are met:
13 1.1 cgd * 1. Redistributions of source code must retain the above copyright
14 1.1 cgd * notice, this list of conditions and the following disclaimer.
15 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 cgd * notice, this list of conditions and the following disclaimer in the
17 1.1 cgd * documentation and/or other materials provided with the distribution.
18 1.12 agc * 3. Neither the name of the University nor the names of its contributors
19 1.1 cgd * may be used to endorse or promote products derived from this software
20 1.1 cgd * without specific prior written permission.
21 1.1 cgd *
22 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 1.1 cgd * SUCH DAMAGE.
33 1.1 cgd *
34 1.1 cgd * @(#)scores.c 8.1 (Berkeley) 5/31/93
35 1.1 cgd */
36 1.1 cgd
37 1.1 cgd /*
38 1.1 cgd * Score code for Tetris, by Darren Provine (kilroy (at) gboro.glassboro.edu)
39 1.1 cgd * modified 22 January 1992, to limit the number of entries any one
40 1.1 cgd * person has.
41 1.1 cgd *
42 1.1 cgd * Major whacks since then.
43 1.1 cgd */
44 1.7 jsm #include <err.h>
45 1.1 cgd #include <errno.h>
46 1.1 cgd #include <fcntl.h>
47 1.1 cgd #include <pwd.h>
48 1.1 cgd #include <stdio.h>
49 1.1 cgd #include <stdlib.h>
50 1.1 cgd #include <string.h>
51 1.6 jsm #include <sys/stat.h>
52 1.1 cgd #include <time.h>
53 1.4 lukem #include <termcap.h>
54 1.1 cgd #include <unistd.h>
55 1.1 cgd
56 1.1 cgd #include "pathnames.h"
57 1.1 cgd #include "screen.h"
58 1.1 cgd #include "scores.h"
59 1.1 cgd #include "tetris.h"
60 1.1 cgd
61 1.1 cgd /*
62 1.1 cgd * Within this code, we can hang onto one extra "high score", leaving
63 1.1 cgd * room for our current score (whether or not it is high).
64 1.1 cgd *
65 1.1 cgd * We also sometimes keep tabs on the "highest" score on each level.
66 1.1 cgd * As long as the scores are kept sorted, this is simply the first one at
67 1.1 cgd * that level.
68 1.1 cgd */
69 1.1 cgd #define NUMSPOTS (MAXHISCORES + 1)
70 1.1 cgd #define NLEVELS (MAXLEVEL + 1)
71 1.1 cgd
72 1.1 cgd static time_t now;
73 1.1 cgd static int nscores;
74 1.1 cgd static int gotscores;
75 1.1 cgd static struct highscore scores[NUMSPOTS];
76 1.1 cgd
77 1.13 jsm static int checkscores(struct highscore *, int);
78 1.13 jsm static int cmpscores(const void *, const void *);
79 1.16 dholland static void getscores(int *);
80 1.13 jsm static void printem(int, int, struct highscore *, int, const char *);
81 1.13 jsm static char *thisuser(void);
82 1.1 cgd
83 1.16 dholland /* contents chosen to be a highly illegal username */
84 1.16 dholland static const char hsh_magic_val[HSH_MAGIC_SIZE] = "//:\0\0://";
85 1.16 dholland
86 1.16 dholland #define HSH_ENDIAN_NATIVE 0x12345678
87 1.16 dholland #define HSH_ENDIAN_OPP 0x78563412
88 1.16 dholland
89 1.16 dholland /* current file format version */
90 1.16 dholland #define HSH_VERSION 1
91 1.16 dholland
92 1.16 dholland /* codes for scorefile_probe return */
93 1.16 dholland #define SCOREFILE_ERROR (-1)
94 1.16 dholland #define SCOREFILE_CURRENT 0 /* 40-byte */
95 1.16 dholland #define SCOREFILE_CURRENT_OPP 1 /* 40-byte, opposite-endian */
96 1.16 dholland #define SCOREFILE_599 2 /* 36-byte */
97 1.16 dholland #define SCOREFILE_599_OPP 3 /* 36-byte, opposite-endian */
98 1.16 dholland #define SCOREFILE_50 4 /* 32-byte */
99 1.16 dholland #define SCOREFILE_50_OPP 5 /* 32-byte, opposite-endian */
100 1.16 dholland
101 1.16 dholland /*
102 1.16 dholland * Check (or guess) what kind of score file contents we have.
103 1.16 dholland */
104 1.16 dholland static int
105 1.16 dholland scorefile_probe(int sd)
106 1.16 dholland {
107 1.16 dholland struct stat st;
108 1.16 dholland int t1, t2, t3, tx;
109 1.16 dholland ssize_t result;
110 1.16 dholland uint32_t numbers[3], offset56, offset60, offset64;
111 1.16 dholland
112 1.16 dholland if (fstat(sd, &st) < 0) {
113 1.16 dholland warn("Score file %s: fstat", _PATH_SCOREFILE);
114 1.16 dholland return -1;
115 1.16 dholland }
116 1.16 dholland
117 1.16 dholland t1 = st.st_size % sizeof(struct highscore_ondisk) == 0;
118 1.16 dholland t2 = st.st_size % sizeof(struct highscore_ondisk_599) == 0;
119 1.16 dholland t3 = st.st_size % sizeof(struct highscore_ondisk_50) == 0;
120 1.16 dholland tx = t1 + t2 + t3;
121 1.16 dholland if (tx == 1) {
122 1.16 dholland /* Size matches exact number of one kind of records */
123 1.16 dholland if (t1) {
124 1.16 dholland return SCOREFILE_CURRENT;
125 1.16 dholland } else if (t2) {
126 1.16 dholland return SCOREFILE_599;
127 1.16 dholland } else {
128 1.16 dholland return SCOREFILE_50;
129 1.16 dholland }
130 1.16 dholland } else if (tx == 0) {
131 1.16 dholland /* Size matches nothing, pick most likely as default */
132 1.16 dholland goto wildguess;
133 1.16 dholland }
134 1.16 dholland
135 1.16 dholland /*
136 1.16 dholland * File size is multiple of more than one structure size.
137 1.16 dholland * (For example, 288 bytes could be 9*hso50 or 8*hso599.)
138 1.16 dholland * Read the file and see if we can figure out what's going
139 1.16 dholland * on. This is the layout of the first two records:
140 1.16 dholland *
141 1.16 dholland * offset hso / current hso_599 hso_50
142 1.16 dholland * (40-byte) (36-byte) (32-byte)
143 1.16 dholland *
144 1.16 dholland * 0 name #0 name #0 name #0
145 1.16 dholland * 4 : : :
146 1.16 dholland * 8 : : :
147 1.16 dholland * 12 : : :
148 1.16 dholland * 16 : : :
149 1.16 dholland * 20 score #0 score #0 score #0
150 1.16 dholland * 24 level #0 level #0 level #0
151 1.16 dholland * 28 (pad) time #0 time #0
152 1.16 dholland * 32 time #0 name #1
153 1.16 dholland * 36 name #1 :
154 1.16 dholland * 40 name #1 : :
155 1.16 dholland * 44 : : :
156 1.16 dholland * 48 : : :
157 1.16 dholland * 52 : : score #1
158 1.16 dholland * 56 : score #1 level #1
159 1.16 dholland * 60 score #1 level #1 time #1
160 1.16 dholland * 64 level #1 time #1 name #2
161 1.16 dholland * 68 (pad) : :
162 1.16 dholland * 72 time #1 name #2 :
163 1.16 dholland * 76 : : :
164 1.16 dholland * 80 --- end ---
165 1.16 dholland *
166 1.16 dholland * There are a number of things we could check here, but the
167 1.16 dholland * most effective test is based on the following restrictions:
168 1.16 dholland *
169 1.16 dholland * - The level must be between 1 and 9 (inclusive)
170 1.16 dholland * - All times must be after 1985 and are before 2038,
171 1.16 dholland * so the high word must be 0 and the low word may not be
172 1.16 dholland * a small value.
173 1.16 dholland * - Integer values of 0 or 1-9 cannot be the beginning of
174 1.16 dholland * a login name string.
175 1.16 dholland * - Values of 1-9 are probably not a score.
176 1.16 dholland *
177 1.16 dholland * So we read the three words at offsets 56, 60, and 64, and
178 1.16 dholland * poke at the values to try to figure things...
179 1.16 dholland */
180 1.16 dholland
181 1.16 dholland if (lseek(sd, 56, SEEK_SET) < 0) {
182 1.16 dholland warn("Score file %s: lseek", _PATH_SCOREFILE);
183 1.16 dholland return -1;
184 1.16 dholland }
185 1.16 dholland result = read(sd, &numbers, sizeof(numbers));
186 1.16 dholland if (result < 0) {
187 1.16 dholland warn("Score file %s: read", _PATH_SCOREFILE);
188 1.16 dholland return -1;
189 1.16 dholland }
190 1.16 dholland if ((size_t)result != sizeof(numbers)) {
191 1.16 dholland /*
192 1.16 dholland * The smallest file whose size divides by more than
193 1.16 dholland * one of the sizes is substantially larger than 64,
194 1.16 dholland * so this should *never* happen.
195 1.16 dholland */
196 1.16 dholland warnx("Score file %s: Unexpected EOF", _PATH_SCOREFILE);
197 1.16 dholland return -1;
198 1.16 dholland }
199 1.16 dholland
200 1.16 dholland offset56 = numbers[0];
201 1.16 dholland offset60 = numbers[1];
202 1.16 dholland offset64 = numbers[2];
203 1.16 dholland
204 1.16 dholland if (offset64 >= MINLEVEL && offset64 <= MAXLEVEL) {
205 1.16 dholland /* 40-byte structure */
206 1.16 dholland return SCOREFILE_CURRENT;
207 1.16 dholland } else if (offset60 >= MINLEVEL && offset60 <= MAXLEVEL) {
208 1.16 dholland /* 36-byte structure */
209 1.16 dholland return SCOREFILE_599;
210 1.16 dholland } else if (offset56 >= MINLEVEL && offset56 <= MAXLEVEL) {
211 1.16 dholland /* 32-byte structure */
212 1.16 dholland return SCOREFILE_50;
213 1.16 dholland }
214 1.16 dholland
215 1.16 dholland /* None was a valid level; try opposite endian */
216 1.16 dholland offset64 = bswap32(offset64);
217 1.16 dholland offset60 = bswap32(offset60);
218 1.16 dholland offset56 = bswap32(offset56);
219 1.16 dholland
220 1.16 dholland if (offset64 >= MINLEVEL && offset64 <= MAXLEVEL) {
221 1.16 dholland /* 40-byte structure */
222 1.16 dholland return SCOREFILE_CURRENT_OPP;
223 1.16 dholland } else if (offset60 >= MINLEVEL && offset60 <= MAXLEVEL) {
224 1.16 dholland /* 36-byte structure */
225 1.16 dholland return SCOREFILE_599_OPP;
226 1.16 dholland } else if (offset56 >= MINLEVEL && offset56 <= MAXLEVEL) {
227 1.16 dholland /* 32-byte structure */
228 1.16 dholland return SCOREFILE_50_OPP;
229 1.16 dholland }
230 1.16 dholland
231 1.16 dholland /* That didn't work either, dunno what's going on */
232 1.16 dholland wildguess:
233 1.16 dholland warnx("Score file %s is likely corrupt", _PATH_SCOREFILE);
234 1.16 dholland if (sizeof(void *) == 8 && sizeof(time_t) == 8) {
235 1.16 dholland return SCOREFILE_CURRENT;
236 1.16 dholland } else if (sizeof(time_t) == 8) {
237 1.16 dholland return SCOREFILE_599;
238 1.16 dholland } else {
239 1.16 dholland return SCOREFILE_50;
240 1.16 dholland }
241 1.16 dholland }
242 1.16 dholland
243 1.16 dholland /*
244 1.16 dholland * Copy a string safely, making sure it's null-terminated.
245 1.16 dholland */
246 1.16 dholland static void
247 1.16 dholland readname(char *to, size_t maxto, const char *from, size_t maxfrom)
248 1.16 dholland {
249 1.16 dholland size_t amt;
250 1.16 dholland
251 1.16 dholland amt = maxto < maxfrom ? maxto : maxfrom;
252 1.16 dholland memcpy(to, from, amt);
253 1.16 dholland to[maxto-1] = '\0';
254 1.16 dholland }
255 1.16 dholland
256 1.16 dholland /*
257 1.16 dholland * Copy integers, byte-swapping if desired.
258 1.16 dholland */
259 1.16 dholland static int32_t
260 1.16 dholland read32(int32_t val, int doflip)
261 1.16 dholland {
262 1.16 dholland if (doflip) {
263 1.16 dholland val = bswap32(val);
264 1.16 dholland }
265 1.16 dholland return val;
266 1.16 dholland }
267 1.16 dholland
268 1.16 dholland static int64_t
269 1.16 dholland read64(int64_t val, int doflip)
270 1.16 dholland {
271 1.16 dholland if (doflip) {
272 1.16 dholland val = bswap64(val);
273 1.16 dholland }
274 1.16 dholland return val;
275 1.16 dholland }
276 1.16 dholland
277 1.16 dholland /*
278 1.16 dholland * Read up to MAXHISCORES scorefile_ondisk entries.
279 1.16 dholland */
280 1.16 dholland static int
281 1.16 dholland readscores(int sd, int doflip)
282 1.16 dholland {
283 1.16 dholland struct highscore_ondisk buf[MAXHISCORES];
284 1.16 dholland ssize_t result;
285 1.16 dholland int i;
286 1.16 dholland
287 1.16 dholland result = read(sd, buf, sizeof(buf));
288 1.16 dholland if (result < 0) {
289 1.16 dholland warn("Score file %s: read", _PATH_SCOREFILE);
290 1.16 dholland return -1;
291 1.16 dholland }
292 1.16 dholland nscores = result / sizeof(buf[0]);
293 1.16 dholland
294 1.16 dholland for (i=0; i<nscores; i++) {
295 1.16 dholland readname(scores[i].hs_name, sizeof(scores[i].hs_name),
296 1.16 dholland buf[i].hso_name, sizeof(buf[i].hso_name));
297 1.16 dholland scores[i].hs_score = read32(buf[i].hso_score, doflip);
298 1.16 dholland scores[i].hs_level = read32(buf[i].hso_level, doflip);
299 1.16 dholland scores[i].hs_time = read64(buf[i].hso_time, doflip);
300 1.16 dholland }
301 1.16 dholland return 0;
302 1.16 dholland }
303 1.16 dholland
304 1.16 dholland /*
305 1.16 dholland * Read up to MAXHISCORES scorefile_ondisk_599 entries.
306 1.16 dholland */
307 1.16 dholland static int
308 1.16 dholland readscores599(int sd, int doflip)
309 1.16 dholland {
310 1.16 dholland struct highscore_ondisk_599 buf[MAXHISCORES];
311 1.16 dholland ssize_t result;
312 1.16 dholland int i;
313 1.16 dholland
314 1.16 dholland result = read(sd, buf, sizeof(buf));
315 1.16 dholland if (result < 0) {
316 1.16 dholland warn("Score file %s: read", _PATH_SCOREFILE);
317 1.16 dholland return -1;
318 1.16 dholland }
319 1.16 dholland nscores = result / sizeof(buf[0]);
320 1.16 dholland
321 1.16 dholland for (i=0; i<nscores; i++) {
322 1.16 dholland readname(scores[i].hs_name, sizeof(scores[i].hs_name),
323 1.16 dholland buf[i].hso599_name, sizeof(buf[i].hso599_name));
324 1.16 dholland scores[i].hs_score = read32(buf[i].hso599_score, doflip);
325 1.16 dholland scores[i].hs_level = read32(buf[i].hso599_level, doflip);
326 1.16 dholland /*
327 1.16 dholland * Don't bother pasting the time together into a
328 1.16 dholland * 64-bit value; just take whichever half is nonzero.
329 1.16 dholland */
330 1.16 dholland scores[i].hs_time =
331 1.16 dholland read32(buf[i].hso599_time[buf[i].hso599_time[0] == 0],
332 1.16 dholland doflip);
333 1.16 dholland }
334 1.16 dholland return 0;
335 1.16 dholland }
336 1.16 dholland
337 1.16 dholland /*
338 1.16 dholland * Read up to MAXHISCORES scorefile_ondisk_50 entries.
339 1.16 dholland */
340 1.16 dholland static int
341 1.16 dholland readscores50(int sd, int doflip)
342 1.16 dholland {
343 1.16 dholland struct highscore_ondisk_50 buf[MAXHISCORES];
344 1.16 dholland ssize_t result;
345 1.16 dholland int i;
346 1.16 dholland
347 1.16 dholland result = read(sd, buf, sizeof(buf));
348 1.16 dholland if (result < 0) {
349 1.16 dholland warn("Score file %s: read", _PATH_SCOREFILE);
350 1.16 dholland return -1;
351 1.16 dholland }
352 1.16 dholland nscores = result / sizeof(buf[0]);
353 1.16 dholland
354 1.16 dholland for (i=0; i<nscores; i++) {
355 1.16 dholland readname(scores[i].hs_name, sizeof(scores[i].hs_name),
356 1.16 dholland buf[i].hso50_name, sizeof(buf[i].hso50_name));
357 1.16 dholland scores[i].hs_score = read32(buf[i].hso50_score, doflip);
358 1.16 dholland scores[i].hs_level = read32(buf[i].hso50_level, doflip);
359 1.16 dholland scores[i].hs_time = read32(buf[i].hso50_time, doflip);
360 1.16 dholland }
361 1.16 dholland return 0;
362 1.16 dholland }
363 1.16 dholland
364 1.1 cgd /*
365 1.1 cgd * Read the score file. Can be called from savescore (before showscores)
366 1.1 cgd * or showscores (if savescore will not be called). If the given pointer
367 1.16 dholland * is not NULL, sets *fdp to an open file handle that corresponds to a
368 1.1 cgd * read/write score file that is locked with LOCK_EX. Otherwise, the
369 1.1 cgd * file is locked with LOCK_SH for the read and closed before return.
370 1.1 cgd */
371 1.1 cgd static void
372 1.16 dholland getscores(int *fdp)
373 1.1 cgd {
374 1.16 dholland struct highscore_header header;
375 1.1 cgd int sd, mint, lck;
376 1.6 jsm mode_t mask;
377 1.5 jsm const char *mstr, *human;
378 1.16 dholland int doflip;
379 1.16 dholland int serrno;
380 1.16 dholland ssize_t result;
381 1.1 cgd
382 1.16 dholland if (fdp != NULL) {
383 1.1 cgd mint = O_RDWR | O_CREAT;
384 1.1 cgd human = "read/write";
385 1.1 cgd lck = LOCK_EX;
386 1.1 cgd } else {
387 1.1 cgd mint = O_RDONLY;
388 1.1 cgd mstr = "r";
389 1.1 cgd human = "reading";
390 1.1 cgd lck = LOCK_SH;
391 1.1 cgd }
392 1.6 jsm setegid(egid);
393 1.6 jsm mask = umask(S_IWOTH);
394 1.1 cgd sd = open(_PATH_SCOREFILE, mint, 0666);
395 1.16 dholland serrno = errno;
396 1.6 jsm (void)umask(mask);
397 1.16 dholland setegid(gid);
398 1.1 cgd if (sd < 0) {
399 1.16 dholland /*
400 1.16 dholland * If the file simply isn't there because nobody's
401 1.16 dholland * played yet, and we aren't going to be trying to
402 1.16 dholland * update it, don't warn. Even if we are going to be
403 1.16 dholland * trying to write it, don't fail -- we can still show
404 1.16 dholland * the player the score they got.
405 1.16 dholland */
406 1.16 dholland if (fdp != NULL || errno != ENOENT) {
407 1.16 dholland warn("Cannot open %s for %s", _PATH_SCOREFILE, human);
408 1.1 cgd }
409 1.16 dholland goto fail;
410 1.1 cgd }
411 1.1 cgd
412 1.1 cgd /*
413 1.1 cgd * Grab a lock.
414 1.16 dholland * XXX: failure here should probably be more fatal than this.
415 1.1 cgd */
416 1.1 cgd if (flock(sd, lck))
417 1.7 jsm warn("warning: score file %s cannot be locked",
418 1.7 jsm _PATH_SCOREFILE);
419 1.1 cgd
420 1.16 dholland /*
421 1.16 dholland * The current format (since -current of 20090525) is
422 1.16 dholland *
423 1.16 dholland * struct highscore_header
424 1.16 dholland * up to MAXHIGHSCORES x struct highscore_ondisk
425 1.16 dholland *
426 1.16 dholland * Before this, there is no header, and the contents
427 1.16 dholland * might be any of three formats:
428 1.16 dholland *
429 1.16 dholland * highscore_ondisk (64-bit machines with 64-bit time_t)
430 1.16 dholland * highscore_ondisk_599 (32-bit machines with 64-bit time_t)
431 1.16 dholland * highscore_ondisk_50 (32-bit machines with 32-bit time_t)
432 1.16 dholland *
433 1.16 dholland * The first two appear in 5.99 between the time_t change and
434 1.16 dholland * 20090525, depending on whether the compiler inserts
435 1.16 dholland * structure padding before an unaligned 64-bit time_t. The
436 1.16 dholland * last appears in 5.0 and earlier.
437 1.16 dholland *
438 1.16 dholland * Any or all of these might also appear on other OSes where
439 1.16 dholland * this code has been ported.
440 1.16 dholland *
441 1.16 dholland * Since the old file has no header, we will have to guess
442 1.16 dholland * which of these formats it has.
443 1.16 dholland */
444 1.16 dholland
445 1.16 dholland /*
446 1.16 dholland * First, look for a header.
447 1.16 dholland */
448 1.16 dholland result = read(sd, &header, sizeof(header));
449 1.16 dholland if (result < 0) {
450 1.16 dholland warn("Score file %s: read", _PATH_SCOREFILE);
451 1.16 dholland close(sd);
452 1.16 dholland goto fail;
453 1.16 dholland }
454 1.16 dholland if (result != 0 && (size_t)result != sizeof(header)) {
455 1.16 dholland warnx("Score file %s: read: unexpected EOF", _PATH_SCOREFILE);
456 1.16 dholland /*
457 1.16 dholland * File is hopelessly corrupt, might as well truncate it
458 1.16 dholland * and start over with empty scores.
459 1.16 dholland */
460 1.16 dholland if (lseek(sd, 0, SEEK_SET) < 0) {
461 1.16 dholland /* ? */
462 1.16 dholland warn("Score file %s: lseek", _PATH_SCOREFILE);
463 1.16 dholland goto fail;
464 1.16 dholland }
465 1.16 dholland if (ftruncate(sd, 0) == 0) {
466 1.16 dholland result = 0;
467 1.16 dholland } else {
468 1.16 dholland close(sd);
469 1.16 dholland goto fail;
470 1.16 dholland }
471 1.16 dholland }
472 1.16 dholland
473 1.16 dholland if (result == 0) {
474 1.16 dholland /* Empty file; that just means there are no scores. */
475 1.16 dholland nscores = 0;
476 1.16 dholland } else {
477 1.16 dholland /*
478 1.16 dholland * Is what we read a header, or the first 16 bytes of
479 1.16 dholland * a score entry? hsh_magic_val is chosen to be
480 1.16 dholland * something that is extremely unlikely to appear in
481 1.16 dholland * hs_name[].
482 1.16 dholland */
483 1.16 dholland if (!memcmp(header.hsh_magic, hsh_magic_val, HSH_MAGIC_SIZE)) {
484 1.16 dholland /* Yes, we have a header. */
485 1.16 dholland
486 1.16 dholland if (header.hsh_endiantag == HSH_ENDIAN_NATIVE) {
487 1.16 dholland /* native endian */
488 1.16 dholland doflip = 0;
489 1.16 dholland } else if (header.hsh_endiantag == HSH_ENDIAN_OPP) {
490 1.16 dholland doflip = 1;
491 1.16 dholland } else {
492 1.16 dholland warnx("Score file %s: Unknown endian tag %u",
493 1.16 dholland _PATH_SCOREFILE, header.hsh_endiantag);
494 1.16 dholland goto fail;
495 1.16 dholland }
496 1.16 dholland
497 1.16 dholland if (header.hsh_version != HSH_VERSION) {
498 1.16 dholland warnx("Score file %s: Unknown version code %u",
499 1.16 dholland _PATH_SCOREFILE, header.hsh_version);
500 1.16 dholland goto fail;
501 1.16 dholland }
502 1.16 dholland
503 1.16 dholland if (readscores(sd, doflip) < 0) {
504 1.16 dholland goto fail;
505 1.16 dholland }
506 1.16 dholland } else {
507 1.16 dholland /*
508 1.16 dholland * Ok, it wasn't a header. Try to figure out what
509 1.16 dholland * size records we have.
510 1.16 dholland */
511 1.16 dholland result = scorefile_probe(sd);
512 1.16 dholland if (lseek(sd, 0, SEEK_SET) < 0) {
513 1.16 dholland warn("Score file %s: lseek", _PATH_SCOREFILE);
514 1.16 dholland goto fail;
515 1.16 dholland }
516 1.16 dholland switch (result) {
517 1.16 dholland case SCOREFILE_CURRENT:
518 1.16 dholland result = readscores(sd, 0 /* don't flip */);
519 1.16 dholland break;
520 1.16 dholland case SCOREFILE_CURRENT_OPP:
521 1.16 dholland result = readscores(sd, 1 /* do flip */);
522 1.16 dholland break;
523 1.16 dholland case SCOREFILE_599:
524 1.16 dholland result = readscores599(sd, 0 /* don't flip */);
525 1.16 dholland break;
526 1.16 dholland case SCOREFILE_599_OPP:
527 1.16 dholland result = readscores599(sd, 1 /* do flip */);
528 1.16 dholland break;
529 1.16 dholland case SCOREFILE_50:
530 1.16 dholland result = readscores50(sd, 0 /* don't flip */);
531 1.16 dholland break;
532 1.16 dholland case SCOREFILE_50_OPP:
533 1.16 dholland result = readscores50(sd, 1 /* do flip */);
534 1.16 dholland break;
535 1.16 dholland default:
536 1.16 dholland goto fail;
537 1.16 dholland }
538 1.16 dholland if (result < 0) {
539 1.16 dholland goto fail;
540 1.16 dholland }
541 1.16 dholland }
542 1.1 cgd }
543 1.16 dholland
544 1.1 cgd
545 1.16 dholland if (fdp)
546 1.16 dholland *fdp = sd;
547 1.1 cgd else
548 1.16 dholland close(sd);
549 1.16 dholland
550 1.16 dholland return;
551 1.16 dholland
552 1.16 dholland fail:
553 1.16 dholland if (fdp != NULL) {
554 1.16 dholland *fdp = -1;
555 1.16 dholland }
556 1.16 dholland nscores = 0;
557 1.16 dholland }
558 1.16 dholland
559 1.16 dholland /*
560 1.16 dholland * Paranoid write wrapper; unlike fwrite() it preserves errno.
561 1.16 dholland */
562 1.16 dholland static int
563 1.16 dholland dowrite(int sd, const void *vbuf, size_t len)
564 1.16 dholland {
565 1.16 dholland const char *buf = vbuf;
566 1.16 dholland ssize_t result;
567 1.16 dholland size_t done = 0;
568 1.16 dholland
569 1.16 dholland while (done < len) {
570 1.16 dholland result = write(sd, buf+done, len-done);
571 1.16 dholland if (result < 0) {
572 1.16 dholland if (errno == EINTR) {
573 1.16 dholland continue;
574 1.16 dholland }
575 1.16 dholland return -1;
576 1.16 dholland }
577 1.16 dholland done += result;
578 1.16 dholland }
579 1.16 dholland return 0;
580 1.1 cgd }
581 1.1 cgd
582 1.16 dholland /*
583 1.16 dholland * Write the score file out.
584 1.16 dholland */
585 1.16 dholland static void
586 1.16 dholland putscores(int sd)
587 1.16 dholland {
588 1.16 dholland struct highscore_header header;
589 1.17 dholland struct highscore_ondisk buf[MAXHISCORES];
590 1.16 dholland int i;
591 1.16 dholland
592 1.16 dholland if (sd == -1) {
593 1.16 dholland return;
594 1.16 dholland }
595 1.16 dholland
596 1.16 dholland memcpy(header.hsh_magic, hsh_magic_val, HSH_MAGIC_SIZE);
597 1.16 dholland header.hsh_endiantag = HSH_ENDIAN_NATIVE;
598 1.16 dholland header.hsh_version = HSH_VERSION;
599 1.16 dholland
600 1.16 dholland for (i=0; i<nscores; i++) {
601 1.16 dholland strncpy(buf[i].hso_name, scores[i].hs_name,
602 1.16 dholland sizeof(buf[i].hso_name));
603 1.16 dholland buf[i].hso_score = scores[i].hs_score;
604 1.16 dholland buf[i].hso_level = scores[i].hs_level;
605 1.16 dholland buf[i].hso_pad = 0xbaadf00d;
606 1.16 dholland buf[i].hso_time = scores[i].hs_time;
607 1.16 dholland }
608 1.16 dholland
609 1.16 dholland if (lseek(sd, 0, SEEK_SET) < 0) {
610 1.16 dholland warn("Score file %s: lseek", _PATH_SCOREFILE);
611 1.16 dholland goto fail;
612 1.16 dholland }
613 1.16 dholland if (dowrite(sd, &header, sizeof(header)) < 0 ||
614 1.16 dholland dowrite(sd, buf, sizeof(buf[0]) * nscores) < 0) {
615 1.16 dholland warn("Score file %s: write", _PATH_SCOREFILE);
616 1.16 dholland goto fail;
617 1.16 dholland }
618 1.16 dholland return;
619 1.16 dholland fail:
620 1.16 dholland warnx("high scores may be damaged");
621 1.16 dholland }
622 1.16 dholland
623 1.16 dholland /*
624 1.16 dholland * Close the score file.
625 1.16 dholland */
626 1.16 dholland static void
627 1.16 dholland closescores(int sd)
628 1.16 dholland {
629 1.16 dholland flock(sd, LOCK_UN);
630 1.16 dholland close(sd);
631 1.16 dholland }
632 1.16 dholland
633 1.16 dholland /*
634 1.16 dholland * Read and update the scores file with the current reults.
635 1.16 dholland */
636 1.1 cgd void
637 1.15 dholland savescore(int level)
638 1.1 cgd {
639 1.11 wiz struct highscore *sp;
640 1.11 wiz int i;
641 1.1 cgd int change;
642 1.16 dholland int sd;
643 1.1 cgd const char *me;
644 1.1 cgd
645 1.16 dholland getscores(&sd);
646 1.1 cgd gotscores = 1;
647 1.1 cgd (void)time(&now);
648 1.1 cgd
649 1.1 cgd /*
650 1.1 cgd * Allow at most one score per person per level -- see if we
651 1.1 cgd * can replace an existing score, or (easiest) do nothing.
652 1.1 cgd * Otherwise add new score at end (there is always room).
653 1.1 cgd */
654 1.1 cgd change = 0;
655 1.1 cgd me = thisuser();
656 1.1 cgd for (i = 0, sp = &scores[0]; i < nscores; i++, sp++) {
657 1.1 cgd if (sp->hs_level != level || strcmp(sp->hs_name, me) != 0)
658 1.1 cgd continue;
659 1.1 cgd if (score > sp->hs_score) {
660 1.1 cgd (void)printf("%s bettered %s %d score of %d!\n",
661 1.1 cgd "\nYou", "your old level", level,
662 1.1 cgd sp->hs_score * sp->hs_level);
663 1.1 cgd sp->hs_score = score; /* new score */
664 1.1 cgd sp->hs_time = now; /* and time */
665 1.1 cgd change = 1;
666 1.1 cgd } else if (score == sp->hs_score) {
667 1.1 cgd (void)printf("%s tied %s %d high score.\n",
668 1.1 cgd "\nYou", "your old level", level);
669 1.1 cgd sp->hs_time = now; /* renew it */
670 1.1 cgd change = 1; /* gotta rewrite, sigh */
671 1.1 cgd } /* else new score < old score: do nothing */
672 1.1 cgd break;
673 1.1 cgd }
674 1.1 cgd if (i >= nscores) {
675 1.1 cgd strcpy(sp->hs_name, me);
676 1.1 cgd sp->hs_level = level;
677 1.1 cgd sp->hs_score = score;
678 1.1 cgd sp->hs_time = now;
679 1.1 cgd nscores++;
680 1.1 cgd change = 1;
681 1.1 cgd }
682 1.1 cgd
683 1.1 cgd if (change) {
684 1.1 cgd /*
685 1.1 cgd * Sort & clean the scores, then rewrite.
686 1.1 cgd */
687 1.1 cgd nscores = checkscores(scores, nscores);
688 1.16 dholland putscores(sd);
689 1.1 cgd }
690 1.16 dholland closescores(sd);
691 1.1 cgd }
692 1.1 cgd
693 1.1 cgd /*
694 1.1 cgd * Get login name, or if that fails, get something suitable.
695 1.1 cgd * The result is always trimmed to fit in a score.
696 1.1 cgd */
697 1.1 cgd static char *
698 1.15 dholland thisuser(void)
699 1.1 cgd {
700 1.11 wiz const char *p;
701 1.11 wiz struct passwd *pw;
702 1.11 wiz size_t l;
703 1.1 cgd static char u[sizeof(scores[0].hs_name)];
704 1.1 cgd
705 1.1 cgd if (u[0])
706 1.1 cgd return (u);
707 1.1 cgd p = getlogin();
708 1.1 cgd if (p == NULL || *p == '\0') {
709 1.1 cgd pw = getpwuid(getuid());
710 1.1 cgd if (pw != NULL)
711 1.1 cgd p = pw->pw_name;
712 1.1 cgd else
713 1.1 cgd p = " ???";
714 1.1 cgd }
715 1.1 cgd l = strlen(p);
716 1.1 cgd if (l >= sizeof(u))
717 1.1 cgd l = sizeof(u) - 1;
718 1.3 tls memcpy(u, p, l);
719 1.1 cgd u[l] = '\0';
720 1.1 cgd return (u);
721 1.1 cgd }
722 1.1 cgd
723 1.1 cgd /*
724 1.1 cgd * Score comparison function for qsort.
725 1.1 cgd *
726 1.1 cgd * If two scores are equal, the person who had the score first is
727 1.1 cgd * listed first in the highscore file.
728 1.1 cgd */
729 1.1 cgd static int
730 1.15 dholland cmpscores(const void *x, const void *y)
731 1.1 cgd {
732 1.11 wiz const struct highscore *a, *b;
733 1.11 wiz long l;
734 1.1 cgd
735 1.1 cgd a = x;
736 1.1 cgd b = y;
737 1.1 cgd l = (long)b->hs_level * b->hs_score - (long)a->hs_level * a->hs_score;
738 1.1 cgd if (l < 0)
739 1.1 cgd return (-1);
740 1.1 cgd if (l > 0)
741 1.1 cgd return (1);
742 1.1 cgd if (a->hs_time < b->hs_time)
743 1.1 cgd return (-1);
744 1.1 cgd if (a->hs_time > b->hs_time)
745 1.1 cgd return (1);
746 1.1 cgd return (0);
747 1.1 cgd }
748 1.1 cgd
749 1.1 cgd /*
750 1.1 cgd * If we've added a score to the file, we need to check the file and ensure
751 1.1 cgd * that this player has only a few entries. The number of entries is
752 1.1 cgd * controlled by MAXSCORES, and is to ensure that the highscore file is not
753 1.1 cgd * monopolised by just a few people. People who no longer have accounts are
754 1.1 cgd * only allowed the highest score. Scores older than EXPIRATION seconds are
755 1.1 cgd * removed, unless they are someone's personal best.
756 1.1 cgd * Caveat: the highest score on each level is always kept.
757 1.1 cgd */
758 1.1 cgd static int
759 1.15 dholland checkscores(struct highscore *hs, int num)
760 1.1 cgd {
761 1.11 wiz struct highscore *sp;
762 1.11 wiz int i, j, k, numnames;
763 1.1 cgd int levelfound[NLEVELS];
764 1.1 cgd struct peruser {
765 1.1 cgd char *name;
766 1.1 cgd int times;
767 1.1 cgd } count[NUMSPOTS];
768 1.11 wiz struct peruser *pu;
769 1.1 cgd
770 1.1 cgd /*
771 1.1 cgd * Sort so that highest totals come first.
772 1.1 cgd *
773 1.1 cgd * levelfound[i] becomes set when the first high score for that
774 1.1 cgd * level is encountered. By definition this is the highest score.
775 1.1 cgd */
776 1.1 cgd qsort((void *)hs, nscores, sizeof(*hs), cmpscores);
777 1.1 cgd for (i = MINLEVEL; i < NLEVELS; i++)
778 1.1 cgd levelfound[i] = 0;
779 1.1 cgd numnames = 0;
780 1.1 cgd for (i = 0, sp = hs; i < num;) {
781 1.1 cgd /*
782 1.1 cgd * This is O(n^2), but do you think we care?
783 1.1 cgd */
784 1.1 cgd for (j = 0, pu = count; j < numnames; j++, pu++)
785 1.1 cgd if (strcmp(sp->hs_name, pu->name) == 0)
786 1.1 cgd break;
787 1.1 cgd if (j == numnames) {
788 1.1 cgd /*
789 1.1 cgd * Add new user, set per-user count to 1.
790 1.1 cgd */
791 1.1 cgd pu->name = sp->hs_name;
792 1.1 cgd pu->times = 1;
793 1.1 cgd numnames++;
794 1.1 cgd } else {
795 1.1 cgd /*
796 1.1 cgd * Two ways to keep this score:
797 1.1 cgd * - Not too many (per user), still has acct, &
798 1.1 cgd * score not dated; or
799 1.1 cgd * - High score on this level.
800 1.1 cgd */
801 1.1 cgd if ((pu->times < MAXSCORES &&
802 1.1 cgd getpwnam(sp->hs_name) != NULL &&
803 1.1 cgd sp->hs_time + EXPIRATION >= now) ||
804 1.1 cgd levelfound[sp->hs_level] == 0)
805 1.1 cgd pu->times++;
806 1.1 cgd else {
807 1.1 cgd /*
808 1.1 cgd * Delete this score, do not count it,
809 1.1 cgd * do not pass go, do not collect $200.
810 1.1 cgd */
811 1.1 cgd num--;
812 1.1 cgd for (k = i; k < num; k++)
813 1.1 cgd hs[k] = hs[k + 1];
814 1.1 cgd continue;
815 1.1 cgd }
816 1.1 cgd }
817 1.14 drochner if (sp->hs_level < NLEVELS && sp->hs_level >= 0)
818 1.14 drochner levelfound[sp->hs_level] = 1;
819 1.1 cgd i++, sp++;
820 1.1 cgd }
821 1.1 cgd return (num > MAXHISCORES ? MAXHISCORES : num);
822 1.1 cgd }
823 1.1 cgd
824 1.1 cgd /*
825 1.1 cgd * Show current scores. This must be called after savescore, if
826 1.1 cgd * savescore is called at all, for two reasons:
827 1.1 cgd * - Showscores munches the time field.
828 1.1 cgd * - Even if that were not the case, a new score must be recorded
829 1.1 cgd * before it can be shown anyway.
830 1.1 cgd */
831 1.1 cgd void
832 1.15 dholland showscores(int level)
833 1.1 cgd {
834 1.11 wiz struct highscore *sp;
835 1.11 wiz int i, n, c;
836 1.1 cgd const char *me;
837 1.1 cgd int levelfound[NLEVELS];
838 1.1 cgd
839 1.1 cgd if (!gotscores)
840 1.16 dholland getscores(NULL);
841 1.1 cgd (void)printf("\n\t\t\t Tetris High Scores\n");
842 1.1 cgd
843 1.1 cgd /*
844 1.1 cgd * If level == 0, the person has not played a game but just asked for
845 1.1 cgd * the high scores; we do not need to check for printing in highlight
846 1.1 cgd * mode. If SOstr is null, we can't do highlighting anyway.
847 1.1 cgd */
848 1.1 cgd me = level && SOstr ? thisuser() : NULL;
849 1.1 cgd
850 1.1 cgd /*
851 1.1 cgd * Set times to 0 except for high score on each level.
852 1.1 cgd */
853 1.1 cgd for (i = MINLEVEL; i < NLEVELS; i++)
854 1.1 cgd levelfound[i] = 0;
855 1.1 cgd for (i = 0, sp = scores; i < nscores; i++, sp++) {
856 1.14 drochner if (sp->hs_level < NLEVELS && sp->hs_level >= 0) {
857 1.14 drochner if (levelfound[sp->hs_level])
858 1.14 drochner sp->hs_time = 0;
859 1.14 drochner else {
860 1.14 drochner sp->hs_time = 1;
861 1.14 drochner levelfound[sp->hs_level] = 1;
862 1.14 drochner }
863 1.14 drochner }
864 1.1 cgd }
865 1.1 cgd
866 1.1 cgd /*
867 1.1 cgd * Page each screenful of scores.
868 1.1 cgd */
869 1.1 cgd for (i = 0, sp = scores; i < nscores; sp += n) {
870 1.1 cgd n = 40;
871 1.1 cgd if (i + n > nscores)
872 1.1 cgd n = nscores - i;
873 1.1 cgd printem(level, i + 1, sp, n, me);
874 1.1 cgd if ((i += n) < nscores) {
875 1.1 cgd (void)printf("\nHit RETURN to continue.");
876 1.1 cgd (void)fflush(stdout);
877 1.1 cgd while ((c = getchar()) != '\n')
878 1.1 cgd if (c == EOF)
879 1.1 cgd break;
880 1.1 cgd (void)printf("\n");
881 1.1 cgd }
882 1.1 cgd }
883 1.1 cgd }
884 1.1 cgd
885 1.1 cgd static void
886 1.15 dholland printem(int level, int offset, struct highscore *hs, int n, const char *me)
887 1.1 cgd {
888 1.11 wiz struct highscore *sp;
889 1.1 cgd int nrows, row, col, item, i, highlight;
890 1.1 cgd char buf[100];
891 1.1 cgd #define TITLE "Rank Score Name (points/level)"
892 1.1 cgd
893 1.1 cgd /*
894 1.1 cgd * This makes a nice two-column sort with headers, but it's a bit
895 1.1 cgd * convoluted...
896 1.1 cgd */
897 1.1 cgd printf("%s %s\n", TITLE, n > 1 ? TITLE : "");
898 1.1 cgd
899 1.1 cgd highlight = 0;
900 1.1 cgd nrows = (n + 1) / 2;
901 1.1 cgd
902 1.1 cgd for (row = 0; row < nrows; row++) {
903 1.1 cgd for (col = 0; col < 2; col++) {
904 1.1 cgd item = col * nrows + row;
905 1.1 cgd if (item >= n) {
906 1.1 cgd /*
907 1.1 cgd * Can only occur on trailing columns.
908 1.1 cgd */
909 1.1 cgd (void)putchar('\n');
910 1.1 cgd continue;
911 1.1 cgd }
912 1.1 cgd sp = &hs[item];
913 1.14 drochner (void)snprintf(buf, sizeof(buf),
914 1.9 jsm "%3d%c %6d %-11s (%6d on %d)",
915 1.1 cgd item + offset, sp->hs_time ? '*' : ' ',
916 1.1 cgd sp->hs_score * sp->hs_level,
917 1.1 cgd sp->hs_name, sp->hs_score, sp->hs_level);
918 1.1 cgd /*
919 1.1 cgd * Highlight if appropriate. This works because
920 1.1 cgd * we only get one score per level.
921 1.1 cgd */
922 1.1 cgd if (me != NULL &&
923 1.1 cgd sp->hs_level == level &&
924 1.1 cgd sp->hs_score == score &&
925 1.1 cgd strcmp(sp->hs_name, me) == 0) {
926 1.1 cgd putpad(SOstr);
927 1.1 cgd highlight = 1;
928 1.1 cgd }
929 1.1 cgd (void)printf("%s", buf);
930 1.1 cgd if (highlight) {
931 1.1 cgd putpad(SEstr);
932 1.1 cgd highlight = 0;
933 1.1 cgd }
934 1.1 cgd
935 1.1 cgd /* fill in spaces so column 1 lines up */
936 1.1 cgd if (col == 0)
937 1.1 cgd for (i = 40 - strlen(buf); --i >= 0;)
938 1.1 cgd (void)putchar(' ');
939 1.1 cgd else /* col == 1 */
940 1.1 cgd (void)putchar('\n');
941 1.1 cgd }
942 1.1 cgd }
943 1.1 cgd }
944