NetBSD/games/snake/snscore/snscore.c

127 lines
3.8 KiB
C
Raw Normal View History

1998-09-11 18:25:22 +04:00
/* $NetBSD: snscore.c,v 1.8 1998/09/11 14:25:22 hubertf Exp $ */
1995-04-22 12:34:18 +04:00
1993-03-21 12:45:37 +03:00
/*
1995-04-22 12:34:18 +04:00
* Copyright (c) 1980, 1993
* The Regents of the University of California. All rights reserved.
1993-03-21 12:45:37 +03:00
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
1997-10-12 05:49:24 +04:00
#include <sys/cdefs.h>
1993-03-21 12:45:37 +03:00
#ifndef lint
1997-10-12 05:49:24 +04:00
__COPYRIGHT("@(#) Copyright (c) 1980, 1993\n\
The Regents of the University of California. All rights reserved.\n");
1993-03-21 12:45:37 +03:00
#endif /* not lint */
#ifndef lint
1995-04-22 12:34:18 +04:00
#if 0
static char sccsid[] = "@(#)snscore.c 8.1 (Berkeley) 7/19/93";
#else
1998-09-11 18:25:22 +04:00
__RCSID("$NetBSD: snscore.c,v 1.8 1998/09/11 14:25:22 hubertf Exp $");
1995-04-22 12:34:18 +04:00
#endif
1993-03-21 12:45:37 +03:00
#endif /* not lint */
#include <sys/types.h>
1997-10-12 05:49:24 +04:00
#include <err.h>
1993-03-21 12:45:37 +03:00
#include <pwd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
1993-03-21 12:45:37 +03:00
#include "pathnames.h"
char *recfile = _PATH_RAWSCORES;
#define MAXPLAYERS 256
struct player {
short uids;
short scores;
char *name;
} players[MAXPLAYERS], temp;
1998-09-11 18:25:22 +04:00
int main __P((void));
1997-10-12 05:49:24 +04:00
1995-04-22 12:34:18 +04:00
int
1998-09-11 18:25:22 +04:00
main()
1993-03-21 12:45:37 +03:00
{
short uid, score;
FILE *fd;
int noplayers;
int i, j, notsorted;
short whoallbest, allbest;
1998-07-27 05:12:35 +04:00
const char *q;
1993-03-21 12:45:37 +03:00
struct passwd *p;
fd = fopen(recfile, "r");
1997-10-12 05:49:24 +04:00
if (fd == NULL)
err(1, "opening `%s'", recfile);
1993-03-21 12:45:37 +03:00
printf("Snake players scores to date\n");
fread(&whoallbest, sizeof(short), 1, fd);
fread(&allbest, sizeof(short), 1, fd);
1994-04-05 04:42:49 +04:00
noplayers = 0;
1995-04-22 12:34:18 +04:00
for (uid = 2; ;uid++) {
1993-03-21 12:45:37 +03:00
if(fread(&score, sizeof(short), 1, fd) == 0)
break;
if (score > 0) {
if (noplayers > MAXPLAYERS) {
printf("too many players\n");
exit(2);
}
players[noplayers].uids = uid;
players[noplayers].scores = score;
p = getpwuid(uid);
if (p == NULL)
continue;
q = p -> pw_name;
1995-04-22 12:34:18 +04:00
players[noplayers].name = malloc(strlen(q) + 1);
1993-03-21 12:45:37 +03:00
strcpy(players[noplayers].name, q);
noplayers++;
}
}
/* bubble sort scores */
1995-04-22 12:34:18 +04:00
for (notsorted = 1; notsorted; ) {
1993-03-21 12:45:37 +03:00
notsorted = 0;
1995-04-22 12:34:18 +04:00
for (i = 0; i < noplayers - 1; i++)
if (players[i].scores < players[i + 1].scores) {
1993-03-21 12:45:37 +03:00
temp = players[i];
1995-04-22 12:34:18 +04:00
players[i] = players[i + 1];
players[i + 1] = temp;
1993-03-21 12:45:37 +03:00
notsorted++;
}
}
j = 1;
1995-04-22 12:34:18 +04:00
for (i = 0; i < noplayers; i++) {
1993-03-21 12:45:37 +03:00
printf("%d:\t$%d\t%s\n", j, players[i].scores, players[i].name);
1995-04-22 12:34:18 +04:00
if (players[i].scores > players[i + 1].scores)
j = i + 2;
1993-03-21 12:45:37 +03:00
}
exit(0);
}