#include <sys/cdefs.h>
#ifndef lint
__COPYRIGHT("@(#) Copyright (c) 1980, 1993\
The Regents of the University of California. All rights reserved.");
#endif
#ifndef lint
#if 0
static char sccsid[] = "@(#)snscore.c 8.1 (Berkeley) 7/19/93";
#else
__RCSID("$NetBSD: snscore.c,v 1.20 2021/05/12 15:26:44 kre Exp $");
#endif
#endif
#include <sys/types.h>
#include <err.h>
#include <pwd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "pathnames.h"
static const char *recfile = SNAKE_PATH_RAWSCORES;
#define MAXPLAYERS 256
struct player {
short uids;
short scores;
char *name;
};
static struct player players[MAXPLAYERS], temp;
int main(void);
int
main(void)
{
short uid, score;
FILE *fd;
int noplayers;
int i, j, notsorted;
short whoallbest, allbest;
const char *q;
struct passwd *p;
setgid(getgid());
fd = fopen(recfile, "r");
if (fd == NULL)
err(1, "opening `%s'", recfile);
printf("Snake players scores to date\n");
if (fread(&whoallbest, sizeof(short), 1, fd) == 0) {
printf("No scores recorded yet!\n");
exit(0);
}
fread(&allbest, sizeof(short), 1, fd);
noplayers = 0;
for (uid = 2; ;uid++) {
if(fread(&score, sizeof(short), 1, fd) == 0)
break;
if (score > 0) {
if (noplayers >= MAXPLAYERS) {
printf("too many players\n");
exit(2);
}
players[noplayers].uids = uid;
players[noplayers].scores = score;
p = getpwuid(uid);
if (p == NULL)
continue;
q = p -> pw_name;
players[noplayers].name = strdup(q);
if (players[noplayers].name == NULL)
err(1, NULL);
noplayers++;
}
}
for (notsorted = 1; notsorted; ) {
notsorted = 0;
for (i = 0; i < noplayers - 1; i++)
if (players[i].scores < players[i + 1].scores) {
temp = players[i];
players[i] = players[i + 1];
players[i + 1] = temp;
notsorted++;
}
}
j = 1;
for (i = 0; i < noplayers; i++) {
printf("%d:\t$%d\t%s\n", j, players[i].scores, players[i].name);
if (players[i].scores > players[i + 1].scores)
j = i + 2;
}
exit(0);
}