xref: /dragonfly/games/snake/snscore/snscore.c (revision 2cd2d2b5)
1 /*
2  * Copyright (c) 1980, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * @(#) Copyright (c) 1980, 1993 The Regents of the University of California.  All rights reserved.
34  * @(#)snscore.c	8.1 (Berkeley) 7/19/93
35  * $FreeBSD: src/games/snake/snscore/snscore.c,v 1.5 1999/11/30 03:49:42 billf Exp $
36  * $DragonFly: src/games/snake/snscore/snscore.c,v 1.2 2003/06/17 04:25:25 dillon Exp $
37  */
38 
39 #include <sys/types.h>
40 #include <pwd.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include "pathnames.h"
45 
46 char *recfile = _PATH_RAWSCORES;
47 #define MAXPLAYERS 256
48 
49 struct	player	{
50 	short	uids;
51 	short	scores;
52 	char	*name;
53 } players[MAXPLAYERS], temp;
54 
55 int
56 main()
57 {
58 	short	uid, score;
59 	FILE	*fd;
60 	int	noplayers;
61 	int	i, j, notsorted;
62 	short	whoallbest, allbest;
63 	char	*q;
64 	struct	passwd	*p;
65 
66 	fd = fopen(recfile, "r");
67 
68 	if (fd == NULL) {
69 		perror(recfile);
70 		exit(1);
71 	}
72 
73 	printf("Snake players scores to date\n");
74 	fread(&whoallbest, sizeof(short), 1, fd);
75 	fread(&allbest, sizeof(short), 1, fd);
76 	noplayers = 0;
77 	for (uid = 2; ;uid++) {
78 		if(fread(&score, sizeof(short), 1, fd) == 0)
79 			break;
80 		if (score > 0) {
81 			if (noplayers > MAXPLAYERS) {
82 				printf("too many players\n");
83 				exit(2);
84 			}
85 			players[noplayers].uids = uid;
86 			players[noplayers].scores = score;
87 			p = getpwuid(uid);
88 			if (p == NULL)
89 				continue;
90 			q = p -> pw_name;
91 			players[noplayers].name = malloc(strlen(q) + 1);
92 			strcpy(players[noplayers].name, q);
93 			noplayers++;
94 		}
95 	}
96 
97 	/* bubble sort scores */
98 	for (notsorted = 1; notsorted; ) {
99 		notsorted = 0;
100 		for (i = 0; i < noplayers - 1; i++)
101 			if (players[i].scores < players[i + 1].scores) {
102 				temp = players[i];
103 				players[i] = players[i + 1];
104 				players[i + 1] = temp;
105 				notsorted++;
106 			}
107 	}
108 
109 	j = 1;
110 	for (i = 0; i < noplayers; i++) {
111 		printf("%d:\t$%d\t%s\n", j, players[i].scores, players[i].name);
112 		if (players[i].scores > players[i + 1].scores)
113 			j = i + 2;
114 	}
115 	exit(0);
116 }
117