xref: /original-bsd/games/snake/snscore/snscore.c (revision 5cc71944)
1 /*
2  * Copyright (c) 1980 Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that the above copyright notice and this paragraph are
7  * duplicated in all such forms and that any documentation,
8  * advertising materials, and other materials related to such
9  * distribution and use acknowledge that the software was developed
10  * by the University of California, Berkeley.  The name of the
11  * University may not be used to endorse or promote products derived
12  * from this software without specific prior written permission.
13  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16  */
17 
18 #ifndef lint
19 char copyright[] =
20 "@(#) Copyright (c) 1980 Regents of the University of California.\n\
21  All rights reserved.\n";
22 #endif /* not lint */
23 
24 #ifndef lint
25 static char sccsid[] = "@(#)snscore.c	5.3 (Berkeley) 06/18/88";
26 #endif /* not lint */
27 
28 #include <stdio.h>
29 #include <pwd.h>
30 char *recfile = "/usr/games/lib/snakerawscores";
31 #define MAXPLAYERS 256
32 
33 struct	passwd	*getpwuid();
34 char	*malloc();
35 
36 struct	player	{
37 	short	uids;
38 	short	scores;
39 	char	*name;
40 } players[MAXPLAYERS], temp;
41 
42 main()
43 {
44 	char	buf[80], cp;
45 	short	uid, score;
46 	FILE	*fd;
47 	int	noplayers;
48 	int	i, j, notsorted;
49 	short	whoallbest, allbest;
50 	char	*q;
51 	struct	passwd	*p;
52 
53 	fd = fopen(recfile, "r");
54 	if (fd == NULL) {
55 		perror(recfile);
56 		exit(1);
57 	}
58 	printf("Snake players scores to date\n");
59 	fread(&whoallbest, sizeof(short), 1, fd);
60 	fread(&allbest, sizeof(short), 1, fd);
61 	for (uid=2;;uid++) {
62 		if(fread(&score, sizeof(short), 1, fd) == 0)
63 			break;
64 		if (score > 0) {
65 			if (noplayers > MAXPLAYERS) {
66 				printf("too many players\n");
67 				exit(2);
68 			}
69 			players[noplayers].uids = uid;
70 			players[noplayers].scores = score;
71 			p = getpwuid(uid);
72 			if (p == NULL)
73 				continue;
74 			q = p -> pw_name;
75 			players[noplayers].name = malloc(strlen(q)+1);
76 			strcpy(players[noplayers].name, q);
77 			noplayers++;
78 		}
79 	}
80 
81 	/* bubble sort scores */
82 	for (notsorted=1; notsorted; ) {
83 		notsorted = 0;
84 		for (i=0; i<noplayers-1; i++)
85 			if (players[i].scores < players[i+1].scores) {
86 				temp = players[i];
87 				players[i] = players[i+1];
88 				players[i+1] = temp;
89 				notsorted++;
90 			}
91 	}
92 
93 	j = 1;
94 	for (i=0; i<noplayers; i++) {
95 		printf("%d:\t$%d\t%s\n", j, players[i].scores, players[i].name);
96 		if (players[i].scores > players[i+1].scores)
97 			j = i+2;
98 	}
99 	exit(0);
100 }
101