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