xref: /original-bsd/games/snake/snscore/snscore.c (revision cba8738a)
1 /*
2  * Copyright (c) 1980 Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 char copyright[] =
10 "@(#) Copyright (c) 1980 Regents of the University of California.\n\
11  All rights reserved.\n";
12 #endif /* not lint */
13 
14 #ifndef lint
15 static char sccsid[] = "@(#)snscore.c	5.7 (Berkeley) 02/28/91";
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 main()
34 {
35 	char	buf[80], cp;
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 	for (uid=2;;uid++) {
53 		if(fread(&score, sizeof(short), 1, fd) == 0)
54 			break;
55 		if (score > 0) {
56 			if (noplayers > MAXPLAYERS) {
57 				printf("too many players\n");
58 				exit(2);
59 			}
60 			players[noplayers].uids = uid;
61 			players[noplayers].scores = score;
62 			p = getpwuid(uid);
63 			if (p == NULL)
64 				continue;
65 			q = p -> pw_name;
66 			players[noplayers].name = malloc(strlen(q)+1);
67 			strcpy(players[noplayers].name, q);
68 			noplayers++;
69 		}
70 	}
71 
72 	/* bubble sort scores */
73 	for (notsorted=1; notsorted; ) {
74 		notsorted = 0;
75 		for (i=0; i<noplayers-1; i++)
76 			if (players[i].scores < players[i+1].scores) {
77 				temp = players[i];
78 				players[i] = players[i+1];
79 				players[i+1] = temp;
80 				notsorted++;
81 			}
82 	}
83 
84 	j = 1;
85 	for (i=0; i<noplayers; i++) {
86 		printf("%d:\t$%d\t%s\n", j, players[i].scores, players[i].name);
87 		if (players[i].scores > players[i+1].scores)
88 			j = i+2;
89 	}
90 	exit(0);
91 }
92