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