xref: /original-bsd/games/snake/snscore/snscore.c (revision 6b005e0a)
1 /*
2  * Copyright (c) 1980, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char copyright[] =
10 "@(#) Copyright (c) 1980, 1993\n\
11 	The Regents of the University of California.  All rights reserved.\n";
12 #endif /* not lint */
13 
14 #ifndef lint
15 static char sccsid[] = "@(#)snscore.c	8.1 (Berkeley) 07/19/93";
16 #endif /* not lint */
17 
18 #include <sys/types.h>
19 #include <pwd.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include "pathnames.h"
23 
24 char *recfile = _PATH_RAWSCORES;
25 #define MAXPLAYERS 256
26 
27 struct	player	{
28 	short	uids;
29 	short	scores;
30 	char	*name;
31 } players[MAXPLAYERS], temp;
32 
33 int
34 main()
35 {
36 	short	uid, score;
37 	FILE	*fd;
38 	int	noplayers;
39 	int	i, j, notsorted;
40 	short	whoallbest, allbest;
41 	char	*q;
42 	struct	passwd	*p;
43 
44 	fd = fopen(recfile, "r");
45 	if (fd == NULL) {
46 		perror(recfile);
47 		exit(1);
48 	}
49 	printf("Snake players scores to date\n");
50 	fread(&whoallbest, sizeof(short), 1, fd);
51 	fread(&allbest, sizeof(short), 1, fd);
52 	noplayers = 0;
53 	for (uid = 2; ;uid++) {
54 		if(fread(&score, sizeof(short), 1, fd) == 0)
55 			break;
56 		if (score > 0) {
57 			if (noplayers > MAXPLAYERS) {
58 				printf("too many players\n");
59 				exit(2);
60 			}
61 			players[noplayers].uids = uid;
62 			players[noplayers].scores = score;
63 			p = getpwuid(uid);
64 			if (p == NULL)
65 				continue;
66 			q = p -> pw_name;
67 			players[noplayers].name = malloc(strlen(q) + 1);
68 			strcpy(players[noplayers].name, q);
69 			noplayers++;
70 		}
71 	}
72 
73 	/* bubble sort scores */
74 	for (notsorted = 1; notsorted; ) {
75 		notsorted = 0;
76 		for (i = 0; i < noplayers - 1; i++)
77 			if (players[i].scores < players[i + 1].scores) {
78 				temp = players[i];
79 				players[i] = players[i + 1];
80 				players[i + 1] = temp;
81 				notsorted++;
82 			}
83 	}
84 
85 	j = 1;
86 	for (i = 0; i < noplayers; i++) {
87 		printf("%d:\t$%d\t%s\n", j, players[i].scores, players[i].name);
88 		if (players[i].scores > players[i + 1].scores)
89 			j = i + 2;
90 	}
91 	exit(0);
92 }
93