186d7f5d3SJohn Marino /*-
286d7f5d3SJohn Marino * Copyright (c) 1980, 1993
386d7f5d3SJohn Marino * The Regents of the University of California. All rights reserved.
486d7f5d3SJohn Marino *
586d7f5d3SJohn Marino * Redistribution and use in source and binary forms, with or without
686d7f5d3SJohn Marino * modification, are permitted provided that the following conditions
786d7f5d3SJohn Marino * are met:
886d7f5d3SJohn Marino * 1. Redistributions of source code must retain the above copyright
986d7f5d3SJohn Marino * notice, this list of conditions and the following disclaimer.
1086d7f5d3SJohn Marino * 2. Redistributions in binary form must reproduce the above copyright
1186d7f5d3SJohn Marino * notice, this list of conditions and the following disclaimer in the
1286d7f5d3SJohn Marino * documentation and/or other materials provided with the distribution.
1386d7f5d3SJohn Marino * 3. Neither the name of the University nor the names of its contributors
1486d7f5d3SJohn Marino * may be used to endorse or promote products derived from this software
1586d7f5d3SJohn Marino * without specific prior written permission.
1686d7f5d3SJohn Marino *
1786d7f5d3SJohn Marino * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
1886d7f5d3SJohn Marino * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1986d7f5d3SJohn Marino * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2086d7f5d3SJohn Marino * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2186d7f5d3SJohn Marino * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2286d7f5d3SJohn Marino * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2386d7f5d3SJohn Marino * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2486d7f5d3SJohn Marino * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2586d7f5d3SJohn Marino * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2686d7f5d3SJohn Marino * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2786d7f5d3SJohn Marino * SUCH DAMAGE.
2886d7f5d3SJohn Marino *
2986d7f5d3SJohn Marino * @(#) Copyright (c) 1980, 1993 The Regents of the University of California. All rights reserved.
3086d7f5d3SJohn Marino * @(#)snscore.c 8.1 (Berkeley) 7/19/93
3186d7f5d3SJohn Marino * $FreeBSD: src/games/snake/snscore/snscore.c,v 1.5 1999/11/30 03:49:42 billf Exp $
3286d7f5d3SJohn Marino * $DragonFly: src/games/snake/snscore/snscore.c,v 1.4 2006/09/03 23:47:56 pavalos Exp $
3386d7f5d3SJohn Marino */
3486d7f5d3SJohn Marino
3586d7f5d3SJohn Marino #include <sys/types.h>
3686d7f5d3SJohn Marino #include <err.h>
3786d7f5d3SJohn Marino #include <pwd.h>
3886d7f5d3SJohn Marino #include <stdio.h>
3986d7f5d3SJohn Marino #include <stdlib.h>
4086d7f5d3SJohn Marino #include <string.h>
4186d7f5d3SJohn Marino #include <unistd.h>
4286d7f5d3SJohn Marino #include "pathnames.h"
4386d7f5d3SJohn Marino
4486d7f5d3SJohn Marino const char *recfile = _PATH_RAWSCORES;
4586d7f5d3SJohn Marino #define MAXPLAYERS 256
4686d7f5d3SJohn Marino
4786d7f5d3SJohn Marino struct player {
4886d7f5d3SJohn Marino short uids;
4986d7f5d3SJohn Marino short scores;
5086d7f5d3SJohn Marino char *name;
5186d7f5d3SJohn Marino };
5286d7f5d3SJohn Marino
5386d7f5d3SJohn Marino struct player players[MAXPLAYERS], temp;
5486d7f5d3SJohn Marino
5586d7f5d3SJohn Marino int
main(int argc __unused,char * argv[]__unused)5686d7f5d3SJohn Marino main(int argc __unused, char *argv[] __unused)
5786d7f5d3SJohn Marino {
5886d7f5d3SJohn Marino short uid, score;
5986d7f5d3SJohn Marino FILE *fd;
6086d7f5d3SJohn Marino int noplayers;
6186d7f5d3SJohn Marino int i, j, notsorted;
6286d7f5d3SJohn Marino short whoallbest, allbest;
6386d7f5d3SJohn Marino const char *q;
6486d7f5d3SJohn Marino struct passwd *p;
6586d7f5d3SJohn Marino
6686d7f5d3SJohn Marino /* Revoke setgid privileges */
6786d7f5d3SJohn Marino setgid(getgid());
6886d7f5d3SJohn Marino
6986d7f5d3SJohn Marino fd = fopen(recfile, "r");
7086d7f5d3SJohn Marino if (fd == NULL)
7186d7f5d3SJohn Marino err(1, "opening `%s'", recfile);
7286d7f5d3SJohn Marino printf("Snake players scores to date\n");
7386d7f5d3SJohn Marino if (fread(&whoallbest, sizeof(short), 1, fd) == 0) {
7486d7f5d3SJohn Marino printf("No scores recorded yet!\n");
7586d7f5d3SJohn Marino exit(0);
7686d7f5d3SJohn Marino }
7786d7f5d3SJohn Marino fread(&allbest, sizeof(short), 1, fd);
7886d7f5d3SJohn Marino noplayers = 0;
7986d7f5d3SJohn Marino for (uid = 2; ;uid++) {
8086d7f5d3SJohn Marino if(fread(&score, sizeof(short), 1, fd) == 0)
8186d7f5d3SJohn Marino break;
8286d7f5d3SJohn Marino if (score > 0) {
8386d7f5d3SJohn Marino if (noplayers >= MAXPLAYERS) {
8486d7f5d3SJohn Marino printf("too many players\n");
8586d7f5d3SJohn Marino exit(2);
8686d7f5d3SJohn Marino }
8786d7f5d3SJohn Marino players[noplayers].uids = uid;
8886d7f5d3SJohn Marino players[noplayers].scores = score;
8986d7f5d3SJohn Marino p = getpwuid(uid);
9086d7f5d3SJohn Marino if (p == NULL)
9186d7f5d3SJohn Marino continue;
9286d7f5d3SJohn Marino q = p -> pw_name;
9386d7f5d3SJohn Marino players[noplayers].name = strdup(q);
9486d7f5d3SJohn Marino if (players[noplayers].name == NULL)
9586d7f5d3SJohn Marino err(1, NULL);
9686d7f5d3SJohn Marino noplayers++;
9786d7f5d3SJohn Marino }
9886d7f5d3SJohn Marino }
9986d7f5d3SJohn Marino
10086d7f5d3SJohn Marino /* bubble sort scores */
10186d7f5d3SJohn Marino for (notsorted = 1; notsorted; ) {
10286d7f5d3SJohn Marino notsorted = 0;
10386d7f5d3SJohn Marino for (i = 0; i < noplayers - 1; i++)
10486d7f5d3SJohn Marino if (players[i].scores < players[i + 1].scores) {
10586d7f5d3SJohn Marino temp = players[i];
10686d7f5d3SJohn Marino players[i] = players[i + 1];
10786d7f5d3SJohn Marino players[i + 1] = temp;
10886d7f5d3SJohn Marino notsorted++;
10986d7f5d3SJohn Marino }
11086d7f5d3SJohn Marino }
11186d7f5d3SJohn Marino
11286d7f5d3SJohn Marino j = 1;
11386d7f5d3SJohn Marino for (i = 0; i < noplayers; i++) {
11486d7f5d3SJohn Marino printf("%d:\t$%d\t%s\n", j, players[i].scores, players[i].name);
11586d7f5d3SJohn Marino if (players[i].scores > players[i + 1].scores)
11686d7f5d3SJohn Marino j = i + 2;
11786d7f5d3SJohn Marino }
11886d7f5d3SJohn Marino exit(0);
11986d7f5d3SJohn Marino }
120