xref: /csrg-svn/games/snake/snscore/snscore.c (revision 63915)
121259Sdist /*
2*63915Sbostic  * Copyright (c) 1980, 1993
3*63915Sbostic  *	The Regents of the University of California.  All rights reserved.
433692Sbostic  *
542601Sbostic  * %sccs.include.redist.c%
621259Sdist  */
721259Sdist 
813778Ssam #ifndef lint
9*63915Sbostic static char copyright[] =
10*63915Sbostic "@(#) Copyright (c) 1980, 1993\n\
11*63915Sbostic 	The Regents of the University of California.  All rights reserved.\n";
1233692Sbostic #endif /* not lint */
1313778Ssam 
1433692Sbostic #ifndef lint
15*63915Sbostic static char sccsid[] = "@(#)snscore.c	8.1 (Berkeley) 07/19/93";
1633692Sbostic #endif /* not lint */
1733692Sbostic 
1837018Sbostic #include <sys/types.h>
1913778Ssam #include <pwd.h>
2046764Sbostic #include <stdio.h>
2146764Sbostic #include <stdlib.h>
2237018Sbostic #include "pathnames.h"
2337018Sbostic 
2437018Sbostic char *recfile = _PATH_RAWSCORES;
2513778Ssam #define MAXPLAYERS 256
2613778Ssam 
2713778Ssam struct	player	{
2813778Ssam 	short	uids;
2913778Ssam 	short	scores;
3013778Ssam 	char	*name;
3113778Ssam } players[MAXPLAYERS], temp;
3213778Ssam 
3363914Sbostic int
main()3413778Ssam main()
3513778Ssam {
3613778Ssam 	short	uid, score;
3713778Ssam 	FILE	*fd;
3813778Ssam 	int	noplayers;
3913778Ssam 	int	i, j, notsorted;
4013778Ssam 	short	whoallbest, allbest;
4113778Ssam 	char	*q;
4213778Ssam 	struct	passwd	*p;
4313778Ssam 
4413778Ssam 	fd = fopen(recfile, "r");
4513778Ssam 	if (fd == NULL) {
4613778Ssam 		perror(recfile);
4713778Ssam 		exit(1);
4813778Ssam 	}
4913778Ssam 	printf("Snake players scores to date\n");
5013778Ssam 	fread(&whoallbest, sizeof(short), 1, fd);
5113778Ssam 	fread(&allbest, sizeof(short), 1, fd);
5263914Sbostic 	noplayers = 0;
5363914Sbostic 	for (uid = 2; ;uid++) {
5413778Ssam 		if(fread(&score, sizeof(short), 1, fd) == 0)
5513778Ssam 			break;
5613778Ssam 		if (score > 0) {
5713778Ssam 			if (noplayers > MAXPLAYERS) {
5813778Ssam 				printf("too many players\n");
5913778Ssam 				exit(2);
6013778Ssam 			}
6113778Ssam 			players[noplayers].uids = uid;
6213778Ssam 			players[noplayers].scores = score;
6313778Ssam 			p = getpwuid(uid);
6413778Ssam 			if (p == NULL)
6513778Ssam 				continue;
6613778Ssam 			q = p -> pw_name;
6763914Sbostic 			players[noplayers].name = malloc(strlen(q) + 1);
6813778Ssam 			strcpy(players[noplayers].name, q);
6913778Ssam 			noplayers++;
7013778Ssam 		}
7113778Ssam 	}
7213778Ssam 
7313778Ssam 	/* bubble sort scores */
7463914Sbostic 	for (notsorted = 1; notsorted; ) {
7513778Ssam 		notsorted = 0;
7663914Sbostic 		for (i = 0; i < noplayers - 1; i++)
7763914Sbostic 			if (players[i].scores < players[i + 1].scores) {
7813778Ssam 				temp = players[i];
7963914Sbostic 				players[i] = players[i + 1];
8063914Sbostic 				players[i + 1] = temp;
8113778Ssam 				notsorted++;
8213778Ssam 			}
8313778Ssam 	}
8413778Ssam 
8513778Ssam 	j = 1;
8663914Sbostic 	for (i = 0; i < noplayers; i++) {
8713778Ssam 		printf("%d:\t$%d\t%s\n", j, players[i].scores, players[i].name);
8863914Sbostic 		if (players[i].scores > players[i + 1].scores)
8963914Sbostic 			j = i + 2;
9013778Ssam 	}
9113778Ssam 	exit(0);
9213778Ssam }
93