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