1: #include <stdio.h>
2: #include <pwd.h>
3: char *recfile = "/usr/games/lib/snakerawscores";
4: #define MAXPLAYERS 256
5:
6: struct passwd *getpwuid();
7: char *malloc();
8:
9: struct player {
10: short uids;
11: short scores;
12: char *name;
13: } players[MAXPLAYERS], temp;
14:
15: main()
16: {
17: char buf[80], cp;
18: short uid, score;
19: FILE *fd;
20: int noplayers;
21: int i, j, notsorted;
22: short whoallbest, allbest;
23: char *q;
24: struct passwd *p;
25:
26: fd = fopen(recfile, "r");
27: if (fd == NULL) {
28: perror(recfile);
29: exit(1);
30: }
31: printf("Snake players scores to date\n");
32: fread(&whoallbest, sizeof(short), 1, fd);
33: fread(&allbest, sizeof(short), 1, fd);
34: for (uid=2;;uid++) {
35: if(fread(&score, sizeof(short), 1, fd) == 0)
36: break;
37: if (score > 0) {
38: if (noplayers > MAXPLAYERS) {
39: printf("too many players\n");
40: exit(2);
41: }
42: players[noplayers].uids = uid;
43: players[noplayers].scores = score;
44: /* This is faster if passwd is sorted by uid. */
45: p = getpwuid(uid);
46: if (p == NULL)
47: continue;
48: q = p -> pw_name;
49: players[noplayers].name = malloc(strlen(q)+1);
50: strcpy(players[noplayers].name, q);
51: noplayers++;
52: }
53: }
54:
55: /* bubble sort scores */
56: for (notsorted=1; notsorted; ) {
57: notsorted = 0;
58: for (i=0; i<noplayers-1; i++)
59: if (players[i].scores < players[i+1].scores) {
60: temp = players[i];
61: players[i] = players[i+1];
62: players[i+1] = temp;
63: notsorted++;
64: }
65: }
66:
67: j = 1;
68: for (i=0; i<noplayers; i++) {
69: printf("%d:\t$%d\t%s\n", j, players[i].scores, players[i].name);
70: if (players[i].scores > players[i+1].scores)
71: j = i+2;
72: }
73: exit(0);
74: }
75:
76: struct passwd *
77: getpwuid(uid)
78: register uid;
79: {
80: register struct passwd *p;
81: struct passwd *getpwent();
82:
83: while( (p = getpwent()) && p->pw_uid != uid );
84: if (p->pw_uid == uid)
85: return(p);
86: setpwent();
87: while( (p = getpwent()) && p->pw_uid != uid );
88: endpwent();
89: return(p);
90: }
Defined functions
main
defined in line
15;
never used
Defined variables
temp
defined in line
13; used 2 times
Defined struct's
player
defined in line
9;
never used
Defined macros